View Javadoc
1   package ejava.examples.ejbwar.inventory.dao;
2   
3   import java.util.List;
4   
5   import javax.inject.Inject;
6   import javax.persistence.EntityManager;
7   import javax.persistence.TypedQuery;
8   
9   import ejava.examples.ejbwar.inventory.bo.Category;
10  import ejava.examples.ejbwar.inventory.bo.Product;
11  import ejava.examples.ejbwar.inventory.cdi.Inventory;
12  
13  /**
14   * This DAO implementation uses JPA and an injected entity manager to 
15   * perform CRUD operations on inventory data.
16   */
17  public class InventoryDAOImpl implements InventoryDAO {
18  	@Inject @Inventory
19  	private EntityManager em;
20  	
21  	public void setEntityManager(EntityManager em) {
22  		this.em = em;
23  	}
24  
25  	@Override
26  	public void createCategory(Category category) {
27  		em.persist(category);
28  	}
29  	
30  	@Override
31  	public Category getCategory(int id) {
32  		return em.find(Category.class, id);
33  	}
34  	
35  	@Override
36  	public List<Category> findCategoryByName(String criteria, int offset, int limit) {
37  		TypedQuery<Category> query = em.createNamedQuery(Category.FIND_BY_NAME, Category.class)
38  				.setParameter("criteria", criteria==null ? "%" : criteria);
39  		applyBounds(query, offset, limit);
40  		return query.getResultList();
41  	}
42  	
43  	@Override
44  	public void detachCategory(Category category) {
45  		em.detach(category);
46  	}
47  	
48  	@Override
49  	public void deleteCategory(Category category) {
50  		em.remove(category);
51  	}
52  	
53  	@Override
54  	public void addProduct(Product p) {
55  		em.persist(p);
56  	}
57  	
58  	@Override
59  	public Product getProduct(int id) {
60  		return em.find(Product.class, id);
61  	}
62  	
63  	@Override
64  	public Product updateProduct(Product product) {
65  		return em.merge(product);
66  	}
67  	@Override
68  	public List<Product> findProductsByName(String criteria, int offset, int limit) {
69  		TypedQuery<Product> query = em.createNamedQuery(Product.FIND_BY_NAME, Product.class)
70  				.setParameter("criteria", criteria==null ? "%" : criteria);
71  		applyBounds(query, offset, limit);
72  		return query.getResultList();
73  	}
74  	
75  	@Override
76  	public void deleteProduct(Product product) {
77  		//make sure we are dealing with a managed instance
78  		if (!em.contains(product)) {
79  			product = getProduct(product.getId());
80  		}
81  		if (product!=null) {
82  			for (Category category :em.createNamedQuery(Category.FIND_BY_PRODUCT, Category.class)
83  					.setParameter("product", product)
84  					.getResultList()) {
85  				//since product and catagory are currently managed a simple
86  				//object match should work here
87  				category.getProducts().remove(product);
88  			}
89  		em.remove(product);
90  		}
91  	}
92  	
93  	private void applyBounds(TypedQuery<?> query, int offset, int limit) {
94  		if (offset > 0) {
95  			query.setFirstResult(offset);
96  		}
97  		if (limit > 0) {
98  			query.setMaxResults(limit);
99  		}
100 	}
101 	
102 }