View Javadoc
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  //import javax.ejb.Stateless;
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   * This class provides a stateless EJB wrapper around stateless logic 
27   * that performs operations on the hotel.
28   *
29   */
30  //@Stateless - will be supplied in ejb-jar.xml due to multi deploy
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      //initialized by init() method
42      private HotelReservationist reservationist;
43      
44  
45      /** 
46       * This method is called when the bean is created and is responsble for
47       * instantiating the business logic implementation, a DAO, and assigning
48       * the EntityManager for the DAO(s) to use.
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       * This method is called whenever the EJB is evicted from the container;
64       * which is not too often for stateless session beans under normal 
65       * circumstances.
66       */
67      @PreDestroy
68      public void close() {
69          reservationist=null;
70      }    
71  
72      /**
73       * This method will cause the current transaction to be rolled back
74       * if the business logic reports an exception cancelling a reservation.
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       * This method is just a pass-thru to the business logic.
91       */
92      public Reservation commitReservation(Reservation reservation)
93              throws HotelReservationException {
94          return reservationist.commitReservation(reservation);
95      }
96  
97      /**
98       * This method will rollack the current transaction if the business
99       * logic throws an exception while trying to create a reservation.
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 }