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