/*
 * 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.NmailUserLogin;
import java.util.Date;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author attila
 */
@Stateless
public class NmailUserLoginManagerBean implements NmailUserLoginManagerRemote, NmailUserLoginManagerLocal {
    @PersistenceContext(unitName="nmailPU")
    private EntityManager em;

    public Long getUserId(String userName, String password) {
        Date now = new Date();
        Query query = em.createNamedQuery("NmailUserLogin.findUserLogin");
        query.setParameter("userName", userName);
        query.setParameter("password", password);
        query.setParameter("now", now);
        Object queryResult = query.getSingleResult();
        
        //if userLogin doesn't exsist
        if (queryResult == null) {
            return null;
        }
        
        NmailUserLogin nmailUserLogin = (NmailUserLogin) queryResult;
        
        return nmailUserLogin.getId();
    }

    public void persist(NmailUserLogin nmailUserLogin) {
        em.persist(nmailUserLogin);
    }
    
    public void remove(NmailUserLogin nmailUserLogin) {
        em.remove(nmailUserLogin);
    }
    
    public void update(NmailUserLogin nmailUserLogin) {
        em.merge(nmailUserLogin);
    }
}
