View Javadoc
1   package ejava.examples.asyncmarket.jpa;
2   
3   import java.util.ArrayList;
4   import java.util.List;
5   import java.util.Map;
6   
7   import javax.persistence.EntityManager;
8   import javax.persistence.Query;
9   
10  import ejava.examples.asyncmarket.bo.AuctionItem;
11  import ejava.examples.asyncmarket.bo.Bid;
12  import ejava.examples.asyncmarket.dao.AuctionItemDAO;
13  
14  public class JPAAuctionItemDAO implements AuctionItemDAO {
15      private EntityManager em;
16      
17      public void setEntityManager(EntityManager em) {
18          this.em = em;
19      }
20  
21      public AuctionItem getItem(long itemId) {
22          return em.find(AuctionItem.class, itemId);
23      }
24  
25      public AuctionItem createItem(AuctionItem item) {
26          em.persist(item);
27          return item;
28      }
29      
30      public AuctionItem updateItem(AuctionItem item) {
31          return em.merge(item);
32      }
33  
34      @SuppressWarnings("unchecked")
35      public List<AuctionItem> getItems(int index, int count) {
36          Query query = em.createNamedQuery("AsyncMarket_getAuctionItems")
37                          .setFirstResult(index)
38                          .setMaxResults(count);
39          return query.getResultList();
40      }
41      
42      @SuppressWarnings("unchecked")
43      public List<AuctionItem> getAvailableItems(int index, int count) {
44          Query query = em.createNamedQuery("AsyncMarket_getAvailableAuctionItems")
45                          .setFirstResult(index)
46                          .setMaxResults(count);
47          return query.getResultList();
48      }
49      
50      @SuppressWarnings("unchecked")
51      public List<AuctionItem> getItems(
52              String queryString, Map<String, Object> params, 
53              int index, int count) {
54          Query query = em.createNamedQuery(queryString)
55                          .setFirstResult(index)
56                          .setMaxResults(count);
57          if (params != null && params.size() != 0) {
58              for (String name: params.keySet()) {
59                  query.setParameter(name, params.get(name));
60              }
61          }
62          return query.getResultList();
63      }
64  
65      public void removeItem(long id) {
66          AuctionItem item = getItem(id);
67          //create a copy to prevent iterating over changing collection
68          List<Bid> bids = new ArrayList<Bid>(item.getBids());
69          for (Bid bid : bids) {
70              bid.getBidder().getBids().remove(bid);
71              item.getBids().remove(bid);
72              em.remove(bid);
73          }
74          item.getOwner().getItems().remove(item);
75          em.createQuery("delete from Order o " +
76                  "where o.item.id = :itemId")
77            .setParameter("itemId", id)
78            .executeUpdate();
79          em.remove(item);
80      }
81  
82      public Bid getBid(long bidId) {
83          return em.find(Bid.class, bidId);
84      }
85  
86      public void removeBid(Bid bid) {
87          bid.getBidder().getBids().remove(bid);
88          bid.getItem().getBids().remove(bid);
89          em.remove(bid);
90      }
91  }