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

package hu.nir.nmail.ejb.stateless;

import hu.nir.nmail.ejb.entity.EmailSendingProces;
import hu.nir.nmail.ejb.entity.NmailUser;
import hu.nir.nmail.ejb.entity.UserProces;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author attila
 */
@Stateless
public class UserProcesManagerBean implements UserProcesManagerRemote, UserProcesManagerLocal {
    @PersistenceContext(unitName="nmailPU")
    private EntityManager em;

    public void persist(UserProces userProces) {
        em.persist(userProces);
    }
    
    public UserProces merge(UserProces userProces) {
        return em.merge(userProces);
    }
    
    public EmailSendingProces getNotFinishedEmailSendingProceses(NmailUser nmailUser) {
        Query query = em.createNamedQuery("UserProces.findNotFinishedEmailSendingProceses");
        query.setParameter("userId", nmailUser);
        try {
            Object queryResult = query.getSingleResult();
            return (EmailSendingProces) queryResult;
        } catch(NoResultException ex) {
            return null;
        }
    }
    
    public EmailSendingProces getLastEmailSendingProces(NmailUser nmailUser) {
        Query query = em.createNamedQuery("UserProces.findLastEmailSendingProces");
        query.setParameter("userId", nmailUser);
        try {
            Object queryResult = query.getSingleResult();
            return (EmailSendingProces) queryResult;
        } catch(NoResultException ex) {
            return null;
        }
    }
    
    public List<EmailSendingProces> getAllEmailSendingProces(NmailUser nmailUser) {
        Query query = em.createNamedQuery("UserProces.findAllEmailSendingProceses");
        query.setParameter("userId", nmailUser);
        try {
            return query.getResultList();
        } catch (NoResultException ex) {
            return new ArrayList<EmailSendingProces>();
        }
    }
}
