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

package inventario.facade;

import inventario.facade.remote.ItemFacadeRemote;
import inventario.entity.Item;
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 ItemFacade implements  ItemFacadeRemote {
    @PersistenceContext
    private EntityManager em;

    public void create(Item item) {
        em.persist(item);
    }

    public void edit(Item item) {
        em.merge(item);
    }

    public void remove(Item item) {
        em.remove(em.merge(item));
    }

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

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

    public List<Item> findByCategory(Long cat){
        Query q = em.createQuery("SELECT o FROM Item o WHERE o.category.id =:cat ");
        q.setParameter("cat", cat);
        return q.getResultList();
    }

    public List<Item> findByName(String cat){
        Query q = em.createQuery("SELECT o FROM Item o WHERE o.category.item_name =:cat ");
        q.setParameter("cat", cat);
        return q.getResultList();
    }
}
