View Javadoc
1   package ejava.examples.txagent.jpa;
2   
3   import java.util.HashMap;
4   import java.util.List;
5   import java.util.Map;
6   
7   import javax.persistence.EntityManager;
8   import javax.persistence.Query;
9   
10  import org.slf4j.Logger;
11  import org.slf4j.LoggerFactory;
12  
13  import ejava.examples.txagent.bo.Booking;
14  import ejava.examples.txagent.dao.DAOException;
15  import ejava.examples.txagent.dao.BookingDAO;
16  
17  public class JPABookingDAO implements BookingDAO {
18      Logger log = LoggerFactory.getLogger(JPABookingDAO.class);
19      
20      private EntityManager em;
21      
22      public void setEntityManager(EntityManager em) {
23          this.em = em;
24      }
25  
26      public Booking createBooking(Booking booking)
27              throws DAOException {      
28          try {
29              log.debug("persisting booking:" + booking);
30              
31              em.persist(booking);            
32  
33              log.debug("booking persisted:" + booking);
34              return booking;
35          }
36          catch (RuntimeException ex) {
37              log.debug("error persisting booking:" + booking, ex);
38              throw new DAOException("error persisting booking:" + 
39                      booking, ex);
40          }
41      }
42  
43      public Booking getBooking(long id) throws DAOException {
44          try {
45              log.debug("getting booking:" + id);
46              Booking booking = em.find(Booking.class, id);            
47              log.debug("found booking:" + booking);
48              return booking;
49          }
50          catch (RuntimeException ex) {
51              log.debug("error getting booking:" + id, ex);
52              throw new DAOException("error getting booking:" + id, ex);
53          }
54      }
55  
56      public List<Booking> getBookings(int index, int count) 
57          throws DAOException {
58          return getBookings("getAllBookings", null, index, count); 
59      }
60  
61      @SuppressWarnings("unchecked")
62      public List<Booking> getBookings(
63              String queryName, Map<String, Object> params, 
64              int index, int count) 
65              throws DAOException {
66          try {
67              Query query = em.createNamedQuery(queryName)
68                              .setFirstResult(index)
69                              .setMaxResults(count);
70              if (params != null && !params.keySet().isEmpty()) {
71                  for(String key: params.keySet()) {
72                      query.setParameter(key, params.get(key));
73                  }                
74              }
75              List<Booking> bookings = query.getResultList();
76              log.debug(bookings.size() + " bookings found");
77              return bookings;            
78          }
79          catch (RuntimeException ex) {
80              log.debug("error getting bookings:" + queryName + 
81                      ", params=" + params, ex);
82              throw new DAOException("error getting bookings:" + queryName + 
83                      ", params=" + params, ex);
84          }
85      }
86  
87      public Booking removeBooking(Booking booking) 
88          throws DAOException {
89          try {
90              log.debug("removing booking:" + booking);
91              booking = em.find(Booking.class, 
92                      booking.getId());
93              em.remove(booking);
94              log.debug("removed booking:" + booking);
95              return booking;
96          }
97          catch (RuntimeException ex) {
98              log.debug("error removing booking:" + booking, ex);
99              throw new DAOException("error removing booking:" + 
100                     booking, ex);
101         }
102     }
103 
104     public Booking updateBooking(Booking booking) throws DAOException {
105         try {
106             log.debug("merging booking:" + booking);
107             Booking updated = em.merge(booking); 
108             log.debug("merged booking:" + updated);
109             return updated;
110         }
111         catch (RuntimeException ex) {
112             log.debug("error merging booking:" + booking, ex);
113             throw new DAOException("error merging booking:" + 
114                     booking, ex);
115         }
116     }
117 
118     public Booking getBookingByConfirmation(String confirmation) 
119         throws DAOException {
120         
121         Map<String, Object> params = new HashMap<String, Object>();
122         params.put("confirmation", confirmation);
123         return getBookings(
124             "getBookingByConfirmation", params, 0, 1).get(0);        
125     }
126 
127 }