View Javadoc
1   package info.ejava.examples.ejb.cdisales.dao;
2   
3   import java.util.Collections;
4   import java.util.List;
5   
6   import info.ejava.examples.ejb.cdisales.bl.ProductCatalog;
7   import info.ejava.examples.ejb.cdisales.bo.Member;
8   import info.ejava.examples.ejb.cdisales.bo.Product;
9   
10  import javax.enterprise.inject.Alternative;
11  import javax.inject.Inject;
12  import javax.inject.Named;
13  import javax.persistence.EntityManager;
14  import javax.persistence.TypedQuery;
15  import javax.persistence.criteria.CriteriaBuilder;
16  import javax.persistence.criteria.CriteriaQuery;
17  import javax.persistence.criteria.Expression;
18  import javax.persistence.criteria.Predicate;
19  import javax.persistence.criteria.Root;
20  
21  @Named
22  public class ProductCatalogDAO implements ProductCatalog {
23      @Inject
24      private EntityManager em;
25  
26      @Override
27      public Product addProduct(Product product) {
28          if (product==null) { return null; }
29          product=em.merge(product);
30          return product;
31      }
32  
33      @Override
34      public List<Product> getProductsForSale(Product template, int offset, int limit) {
35          CriteriaBuilder cb = em.getCriteriaBuilder();
36          CriteriaQuery<Product> qdef = cb.createQuery(Product.class);
37          Root<Product> p = qdef.from(Product.class);
38          qdef.select(p).orderBy(cb.asc(p.get("name")));
39          
40          Predicate pred = cb.conjunction();
41          if (template != null) {
42              List<Expression<Boolean>> expr = pred.getExpressions();
43              if (template.getCategory()!=null) {
44                  expr.add(cb.equal(p.get("category"), template.getCategory()));
45              }
46              if (template.getName()!=null) {
47                  String name = template.getName().replace("*", "%").trim();
48                  expr.add(cb.like(p.<String>get("name"), name));
49              }
50          }
51          qdef.where(pred);
52          
53          TypedQuery<Product> query = em.createQuery(qdef);
54          if (offset > 0) {
55              query.setFirstResult(offset);
56          }
57          if (limit > 0) {
58              query.setMaxResults(limit);
59          }
60          return query.getResultList();
61      }
62  
63      @Override
64      public List<Product> getBuyerProducts(Member buyer, int offset, int limit) {
65          TypedQuery<Product> query = null;
66          if (buyer!=null) {
67              if (buyer.getId()!=0) {
68                  query = em.createNamedQuery("CDIProduct.getProductsByBuyer", Product.class).setParameter("buyer", buyer);
69              } else if (buyer.getLogin()!=null) {
70                  query = em.createNamedQuery("CDIProduct.getProductsByBuyerLogin", Product.class).setParameter("login", buyer.getLogin());
71              }
72          }
73          if (query!=null) {
74              if (offset > 0) {
75                  query.setFirstResult(offset);
76              }
77              if (limit > 0) {
78                  query.setMaxResults(limit);
79              }
80          }
81          
82          return query==null ? Collections.<Product>emptyList() : query.getResultList();
83      }
84  
85      @Override
86      public List<Product> getSellerProducts(Member seller, int offset, int limit) {
87          TypedQuery<Product> query = null;
88          if (seller!=null) {
89              if (seller.getId()!=0) {
90                  query = em.createNamedQuery("CDIProduct.getProductsBySeller", Product.class).setParameter("seller", seller);
91              } else if (seller.getLogin()!=null) {
92                  query = em.createNamedQuery("CDIProduct.getProductsBySellerLogin", Product.class).setParameter("login", seller.getLogin());
93              }
94          }
95          if (query!=null) {
96              if (offset > 0) {
97                  query.setFirstResult(offset);
98              }
99              if (limit > 0) {
100                 query.setMaxResults(limit);
101             }
102         }
103         
104         return query==null ? Collections.<Product>emptyList() : query.getResultList();
105     }
106     
107     @Override
108     public int remove(Product product) {
109         if (product==null || product.getId()==0) { return 0; }
110         product = em.find(Product.class, product.getId());
111         if (product!=null) {
112             em.remove(product);
113             return 1;
114         }
115         return 0;
116     }
117 }