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

package inventario.facade;


import inventario.facade.remote.PersonFacadeRemote;
import inventario.entity.Person;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author Daniel
 */
@Stateless
public class PersonFacade implements PersonFacadeRemote {
    @PersistenceContext
    private EntityManager em;

    public void create(Person person) {
        em.persist(person);
    }

    public void edit(Person person) {
        em.merge(person);
    }

    public void remove(Person person) {
        em.remove(em.merge(person));
    }

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

    public List<Person> findAll() {
        return em.createQuery("select object(o) from Person as o").getResultList();
    }

    public List<Person> findByLogin(String login,String pass){
        Query q = em.createQuery("SELECT o FROM Person o WHERE o.login =:login AND o.password =:pass");
        q.setParameter("login", login);
        q.setParameter("pass", pass);
        return q.getResultList();
    }
}
