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

package ejb;

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 SeyirDefteriFacade {
    @PersistenceContext(unitName = "NewsApp-ejbPU")
    private EntityManager em;

    public void create(SeyirDefteri seyirDefteri) {
        em.persist(seyirDefteri);
    }

    public void edit(SeyirDefteri seyirDefteri) {
        em.merge(seyirDefteri);
    }

    public void remove(SeyirDefteri seyirDefteri) {
        em.remove(em.merge(seyirDefteri));
    }

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

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

    public List<SeyirDefteri> findRange(int[] range) {
        CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
        cq.select(cq.from(SeyirDefteri.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<SeyirDefteri> rt = cq.from(SeyirDefteri.class);
        cq.select(em.getCriteriaBuilder().count(rt));
        Query q = em.createQuery(cq);
        return ((Long) q.getSingleResult()).intValue();
    }

    public List<Long> getSeferByUserId(Long userId)
    {
       List resultList = em.createNativeQuery("SELECT ID FROM SEYIRDEFTERI WHERE USERID = "+userId+" AND " +
               "ISTICKET=0").getResultList();
       return resultList;
    }

    public List<Long> getBiletByUserId(Long userId) {
        List resultList = em.createNativeQuery("SELECT ID FROM SEYIRDEFTERI WHERE USERID = "+userId+" AND " +
                "ISTICKET=1").getResultList();
       return resultList;
    }





    /*
    public void makeItTicket(List<Long> ids)
    {
        int size=ids.size();
        for(int i=0;i<size;i++)
        {
            Query queryRenameCity = em.createQuery(
                            "UPDATE SEYIRDEFTERI SET ISTICKET = 1 WHERE ID = "+ids.get(i));
            queryRenameCity.executeUpdate();
        }
    }
     * 
     */


}
