/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ejb;

import java.sql.Date;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;

/**
 *
 * @author zwergnase
 */
@Stateless
public class UsersFacade {
    @PersistenceContext(unitName = "NewsApp-ejbPU")
    private EntityManager em;

    public void create(Users users) {
        em.persist(users);
    }

    public void edit(Users users) {
        em.merge(users);
    }

    public void remove(Users users) {
        em.remove(em.merge(users));
    }

    public Users find(Object id) {
        return em.find(Users.class, id);
    }

    public List<Users> findAll() {
        CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
        cq.select(cq.from(Users.class));
        return em.createQuery(cq).getResultList();
    }

    public List<Users> findRange(int[] range) {
        CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
        cq.select(cq.from(Users.class));
        Query q = em.createQuery(cq);
        q.setMaxResults(range[1] - range[0]);
        q.setFirstResult(range[0]);
        return q.getResultList();
    }

    public int count() {
        CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
        Root<Users> rt = cq.from(Users.class);
        cq.select(em.getCriteriaBuilder().count(rt));
        Query q = em.createQuery(cq);
        return ((Long) q.getSingleResult()).intValue();
    }


        public boolean validateUser(String user,String password) {

        if(user == null || password == null)
            return false;

        List resultList = em.createNativeQuery("SELECT PASSWORD FROM USERS WHERE USERNAME = '"+user+"'").getResultList();
        String passwd = null;
        if(resultList.size()>0) {
            passwd = (String) resultList.get(0);
        }
            if(password.equals(passwd))
            {
                return true;
            }
        return false;
    }

  public List<Long> getMachinists(){
        List<Long> resultList = em.createNativeQuery("SELECT ID FROM USERS WHERE TYPE = 1").getResultList();
        return resultList;
    }


     public boolean userExists(String user){
        if(user == null)
            return false;
        List resultList = em.createNativeQuery("SELECT * FROM USERS WHERE USERNAME = '"+user+"'").getResultList();
        if(resultList.size()>0)
             return true;
        else
             return false;
    }

    public int getTypeByUser(String user) {
        if(user==null)
            return 3;
        List resultList = em.createNativeQuery("SELECT TYPE FROM USERS WHERE USERNAME = '"+ user + "'").getResultList();

           int type = 3;    //superuser = 0...guest=3
        if(resultList.size()>0) {
            type = Integer.parseInt(resultList.get(0).toString());
        }

        return type;
    }
        public Long getIdByUser(String user) {
        if(user==null)
            return -1L;
        List resultList = em.createNativeQuery("SELECT ID FROM USERS WHERE USERNAME = '"+ user + "'").getResultList();

        if(resultList.size()>0) {
            return Long.parseLong(resultList.get(0).toString());
        }
        else {
            return -1L;
        }
    }

    public List<Long> getMachinstWithLocation(String Location,Date scheduleDate)
    {
        List <Long> resultList = em.createNativeQuery("SELECT USERS.ID FROM USERS WHERE TYPE = 1 AND LOCATION='"+Location+"'" +
                "AND NOT EXISTS(SELECT * FROM SCHEDULE WHERE USERS.ID=MACHINSTID AND SCHEDULEDATE='"+
                scheduleDate+"')").getResultList();
        if(resultList.size()==0)
             return null;
        return resultList;

    }

 public List<Long> getMachinstWithLocationWithSchedule(String Location,Date scheduleDate)
    {
         List <Long> resultList = em.createNativeQuery("SELECT USERS.ID FROM USERS WHERE TYPE = 1 AND LOCATION='"+Location+"'" +
                "AND EXISTS(SELECT * FROM SCHEDULE WHERE USERS.ID=MACHINSTID AND SCHEDULEDATE='"+
                scheduleDate+"')").getResultList();
         if(resultList.size()==0)
             return null;
         return resultList;
    }


        public boolean updateActive(Long id) {
            try {
                Users u = find(id);
                u.setActive(true);
                edit(u);
                return true;
            }
            catch(Exception e) {
                return false;
            }
        }



}
