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.slf4j.Logger;
15 import org.slf4j.LoggerFactory;
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 public class HotelRegistrationEJB implements HotelRegistrationRemote,
32 HotelRegistrationLocal {
33 private static final Logger log = LoggerFactory.getLogger(HotelRegistrationEJB.class);
34
35 @PersistenceContext(unitName="txhotel")
36 private EntityManager em;
37
38 @Resource
39 private SessionContext ctx;
40
41
42 private HotelReservationist reservationist;
43
44
45
46
47
48
49
50 @PostConstruct
51 public void init() {
52 log.info("*** HotelRegistrationEJB initializing ***");
53 log.debug("em=" + em);
54 log.debug("ctx=" + ctx);
55
56 ReservationDAO dao = new JPAReservationDAO();
57 ((JPAReservationDAO)dao).setEntityManager(em);
58 reservationist = new HotelReservationImpl();
59 ((HotelReservationImpl)reservationist).setDao(dao);
60 }
61
62
63
64
65
66
67 @PreDestroy
68 public void close() {
69 reservationist=null;
70 }
71
72
73
74
75
76 public void cancelReservation(Reservation reservation)
77 throws HotelReservationException {
78 try {
79 reservationist.cancelReservation(reservation);
80 }
81 catch (HotelReservationException ex) {
82 log.info("error canceling reservation, rolling back transaction:"
83 + ex.getMessage());
84 ctx.setRollbackOnly();
85 throw ex;
86 }
87 }
88
89
90
91
92 public Reservation commitReservation(Reservation reservation)
93 throws HotelReservationException {
94 return reservationist.commitReservation(reservation);
95 }
96
97
98
99
100
101 public Reservation createReservation(Person person, Date startDate,
102 Date endDate) throws HotelReservationException {
103 try {
104 return reservationist.createReservation(person, startDate, endDate);
105 }
106 catch (HotelReservationException ex) {
107 log.info("error creating reservation, rolling back transaction:"
108 + ex.getMessage());
109 ctx.setRollbackOnly();
110 throw ex;
111 }
112 }
113
114 public List<Reservation> getReservations(int index, int count)
115 throws HotelReservationException {
116 return reservationist.getReservations(index, count);
117 }
118
119 public List<Reservation> getReservationsForPerson(Person person, int index,
120 int count) throws HotelReservationException {
121 return reservationist.getReservationsForPerson(person, index, count);
122 }
123
124 public Reservation getReservationByConfirmation(String confirmation)
125 throws HotelReservationException {
126 return reservationist.getReservationByConfirmation(confirmation);
127 }
128
129 public void cleanupReservation(String confirmation)
130 throws HotelReservationException {
131 reservationist.cleanupReservation(confirmation);
132 }
133 }