1 package ejava.examples.txhotel.ejb;
2
3 import java.util.Date;
4 import java.util.List;
5
6 import javax.annotation.PostConstruct;
7 import javax.annotation.PreDestroy;
8 import javax.annotation.Resource;
9 import javax.ejb.SessionContext;
10
11 import javax.persistence.EntityManager;
12 import javax.persistence.PersistenceContext;
13
14 import org.apache.commons.logging.Log;
15 import org.apache.commons.logging.LogFactory;
16
17 import ejava.examples.txhotel.bl.HotelReservationException;
18 import ejava.examples.txhotel.bl.HotelReservationist;
19 import ejava.examples.txhotel.blimpl.HotelReservationImpl;
20 import ejava.examples.txhotel.bo.Person;
21 import ejava.examples.txhotel.bo.Reservation;
22 import ejava.examples.txhotel.dao.ReservationDAO;
23 import ejava.examples.txhotel.jpa.JPAReservationDAO;
24
25
26
27
28
29
30
31
32 public class HotelRegistrationEJB implements HotelRegistrationRemote,
33 HotelRegistrationLocal {
34 private Log log = LogFactory.getLog(HotelRegistrationEJB.class);
35
36 @PersistenceContext(unitName="txhotel")
37 private EntityManager em;
38
39 @Resource
40 private SessionContext ctx;
41
42
43 private HotelReservationist reservationist;
44
45
46
47
48
49
50
51 @PostConstruct
52 public void init() {
53 log.info("*** HotelRegistrationEJB initializing ***");
54 log.debug("em=" + em);
55 log.debug("ctx=" + ctx);
56
57 ReservationDAO dao = new JPAReservationDAO();
58 ((JPAReservationDAO)dao).setEntityManager(em);
59 reservationist = new HotelReservationImpl();
60 ((HotelReservationImpl)reservationist).setDao(dao);
61 }
62
63
64
65
66
67
68 @PreDestroy
69 public void close() {
70 reservationist=null;
71 }
72
73
74
75
76
77 public void cancelReservation(Reservation reservation)
78 throws HotelReservationException {
79 try {
80 reservationist.cancelReservation(reservation);
81 }
82 catch (HotelReservationException ex) {
83 log.info("error canceling reservation, rolling back transaction:"
84 + ex.getMessage());
85 ctx.setRollbackOnly();
86 throw ex;
87 }
88 }
89
90
91
92
93 public Reservation commitReservation(Reservation reservation)
94 throws HotelReservationException {
95 return reservationist.commitReservation(reservation);
96 }
97
98
99
100
101
102 public Reservation createReservation(Person person, Date startDate,
103 Date endDate) throws HotelReservationException {
104 try {
105 return reservationist.createReservation(person, startDate, endDate);
106 }
107 catch (HotelReservationException ex) {
108 log.info("error creating reservation, rolling back transaction:"
109 + ex.getMessage());
110 ctx.setRollbackOnly();
111 throw ex;
112 }
113 }
114
115 public List<Reservation> getReservations(int index, int count)
116 throws HotelReservationException {
117 return reservationist.getReservations(index, count);
118 }
119
120 public List<Reservation> getReservationsForPerson(Person person, int index,
121 int count) throws HotelReservationException {
122 return reservationist.getReservationsForPerson(person, index, count);
123 }
124
125 public Reservation getReservationByConfirmation(String confirmation)
126 throws HotelReservationException {
127 return reservationist.getReservationByConfirmation(confirmation);
128 }
129
130 public void cleanupReservation(String confirmation)
131 throws HotelReservationException {
132 reservationist.cleanupReservation(confirmation);
133 }
134 }