/*
 * 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 sinem
 */
@Stateless
//@TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
public class NewsEntityFacade {
    @PersistenceContext(unitName = "NewsApp-ejbPU")
    private EntityManager em;

    public void create(NewsEntity newsEntity) {
        em.persist(newsEntity);
    }

    public void edit(NewsEntity newsEntity) {
        em.merge(newsEntity);
    }

    public void remove(NewsEntity newsEntity) {
        em.remove(em.merge(newsEntity));
    }

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

    public List<NewsEntity> findByUser(String user) {
        Query query = em.createNamedQuery("findByUser");
        query.setParameter("userName", user);
        return query.getResultList();
    }

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

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

        public List getIdByTopic(String topic) {
        if(topic==null)
            return null;
        List resultList = em.createNativeQuery("SELECT ID FROM NEWSENTITY WHERE TOPIC = '"+ topic + "'").getResultList();
        if(resultList.size() == 0)
            return null;

        return resultList;
    }


      public Long getMaxId(String userName){

           List resultList = em.createNativeQuery("SELECT ID FROM NEWSENTITY WHERE SENTBY='"+userName+"'").getResultList();
           Long maxi=0L;
           if(resultList==null)
               return null;
           for(int i=0;i<resultList.size();i++)
               if(((Long)resultList.get(i))>maxi)
                   maxi=((Long)resultList.get(i));
           return find(maxi).getId();
        }


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

    public boolean twExist() {
        List resultList = em.createNativeQuery("SELECT * FROM NEWSENTITY WHERE TITLE LIKE 'Twt:%' ").getResultList();
        if(resultList.size()>0)
             return true;
        else
             return false;
    }

    public long getMaxTwitId() {

           List resultList = em.createNativeQuery("SELECT TWITTERMSGID FROM NEWSENTITY WHERE TITLE LIKE 'Twt:%' ").getResultList();
           Long maxi=0L;
           for(int i=0;i<resultList.size();i++)
               if(((Long)resultList.get(i))>maxi)
                   maxi=((Long)resultList.get(i));

           return maxi;
    }

}
