1 package ejava.examples.txhotel.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.txhotel.bo.Person;
14 import ejava.examples.txhotel.bo.Reservation;
15 import ejava.examples.txhotel.dao.DAOException;
16 import ejava.examples.txhotel.dao.ReservationDAO;
17
18 public class JPAReservationDAO implements ReservationDAO {
19 private static final Logger log = LoggerFactory.getLogger(JPAReservationDAO.class);
20
21 private EntityManager em;
22
23 public void setEntityManager(EntityManager em) {
24 this.em = em;
25 }
26
27 public Reservation createReservation(Reservation reservation)
28 throws DAOException {
29 try {
30 log.debug("persisting reservation:" + reservation);
31
32
33 Person person = reservation.getPerson();
34 if (person != null && person.getId() != 0) {
35 person = em.find(Person.class, person.getId());
36 reservation.setPerson(person);
37 log.debug("found existing person:" + person);
38 }
39
40 em.persist(reservation);
41 log.debug("reservation persisted:" + reservation);
42 return reservation;
43 }
44 catch (RuntimeException ex) {
45 log.debug("error persisting reservation:" + reservation, ex);
46 throw new DAOException("error persisting reservation:" +
47 reservation, ex);
48 }
49 }
50
51 public Reservation getReservation(long id) throws DAOException {
52 try {
53 log.debug("getting reservation:" + id);
54 Reservation reservation = em.find(Reservation.class, id);
55 log.debug("found reservation:" + reservation);
56 return reservation;
57 }
58 catch (RuntimeException ex) {
59 log.debug("error getting reservation:" + id, ex);
60 throw new DAOException("error getting reservation:" + id, ex);
61 }
62 }
63
64 public List<Reservation> getReservations(int index, int count)
65 throws DAOException {
66 return getReservations("getAllReservations", null, index, count);
67 }
68
69 @SuppressWarnings("unchecked")
70 public List<Reservation> getReservations(
71 String queryName, Map<String, Object> params,
72 int index, int count)
73 throws DAOException {
74 try {
75 Query query = em.createNamedQuery(queryName)
76 .setFirstResult(index)
77 .setMaxResults(count);
78 if (params != null && !params.keySet().isEmpty()) {
79 for(String key: params.keySet()) {
80 query.setParameter(key, params.get(key));
81 }
82 }
83 List<Reservation> reservations = query.getResultList();
84 log.debug(reservations.size() + " reservations found");
85 return reservations;
86 }
87 catch (RuntimeException ex) {
88 log.debug("error getting reservations:" + queryName +
89 ", params=" + params, ex);
90 throw new DAOException("error getting reservations:" + queryName +
91 ", params=" + params, ex);
92 }
93 }
94
95 public Reservation removeReservation(Reservation reservation)
96 throws DAOException {
97 try {
98 log.debug("removing reservation:" + reservation);
99 reservation = em.find(Reservation.class,
100 reservation.getId());
101 em.remove(reservation);
102 log.debug("removed reservation:" + reservation);
103 return reservation;
104 }
105 catch (RuntimeException ex) {
106 log.debug("error removing reservation:" + reservation, ex);
107 throw new DAOException("error removing reservation:" +
108 reservation, ex);
109 }
110 }
111
112 public Reservation updateReservation(Reservation reservation) throws DAOException {
113 try {
114 log.debug("merging reservation:" + reservation);
115 Reservation updated = em.merge(reservation);
116 log.debug("merged reservation:" + updated);
117 return updated;
118 }
119 catch (RuntimeException ex) {
120 log.debug("error merging reservation:" + reservation, ex);
121 throw new DAOException("error merging reservation:" +
122 reservation, ex);
123 }
124 }
125
126 public Reservation getReservationByConfirmation(String confirmation)
127 throws DAOException {
128
129 Map<String, Object> params = new HashMap<String, Object>();
130 params.put("confirmation", confirmation);
131 return getReservations(
132 "getReservationsByConfirmation", params, 0, 1).get(0);
133 }
134 }