View Javadoc
1   package ejava.examples.txagent.blimpl;
2   
3   import java.util.Date;
4   import java.util.List;
5   
6   import org.apache.commons.logging.Log;
7   import org.apache.commons.logging.LogFactory;
8   
9   import ejava.examples.txagent.bl.AgentReservationException;
10  import ejava.examples.txagent.bl.AgentReservationSession;
11  import ejava.examples.txagent.bo.Booking;
12  import ejava.examples.txagent.dao.BookingDAO;
13  import ejava.examples.txhotel.bl.HotelReservationException;
14  import ejava.examples.txhotel.bl.HotelReservationSession;
15  import ejava.examples.txhotel.bl.InvalidParameterException;
16  import ejava.examples.txhotel.bo.Person;
17  import ejava.examples.txhotel.bo.Reservation;
18  
19  public class AgentSessionImpl implements AgentReservationSession {
20      private static final Log log = 
21          LogFactory.getLog(AgentSessionImpl.class);
22      private Booking booking = new Booking();
23      private BookingDAO bookingDAO;
24      private HotelReservationSession reservationist;
25      private long counter = 0L;
26      
27      public void createBooking() throws AgentReservationException {
28      }
29  
30      public void addReservation(Person person, Date startDate, Date endDate) 
31          throws AgentReservationException {
32          try {
33          	log.debug("creating reservation in stateful session bean");
34              reservationist.createReservation(person, startDate, endDate);
35          } 
36          catch (InvalidParameterException ex) {
37              throw new ejava.examples.txagent.bl.InvalidParameterException(
38                      "hotel reported invalid parameters", ex);
39          }
40          catch (HotelReservationException ex2) {
41              throw new AgentReservationException(
42                      "error adding hotel reservation", ex2);
43          }
44      }
45  
46      public void cancelBooking() throws AgentReservationException {
47          try {
48              reservationist.cancelReservations();
49          } 
50          catch (HotelReservationException ex2) {
51              throw new AgentReservationException(
52                      "error cancelling hotel reservation", ex2);
53          }
54      }
55  
56      public Booking commit() throws AgentReservationException {
57          try {
58              log.debug("committing booking: " + booking);
59              String confirmation = 
60                  new Long(System.currentTimeMillis()).toString() + "-" + 
61                  ++counter;
62              booking.setConfirmation(confirmation);
63              bookingDAO.createBooking(booking);
64              log.debug("committing hotels for booking");
65              List<Reservation> reservations = reservationist.commit();
66              log.debug(String.format("commit complete, adding %d hotel reservations to booking",
67              		reservations.size()));
68              for (Reservation r: reservations) {
69                  booking.addHotelReservation(r);
70              }
71              log.debug("completed booking:" + booking);
72              return booking;
73          } 
74          catch (HotelReservationException ex) {
75              log.info("error commiting booking, hotel reported issue:" + ex);
76              throw new AgentReservationException(
77                      "error committing booking", ex);
78          }
79          finally {
80              //reservationist.close();        	
81          }
82      }
83      @Override
84      public void close() {
85      }
86      public void setBookingDAO(BookingDAO bookingDAO) {
87          this.bookingDAO = bookingDAO;
88      }
89      public void setReservationist(HotelReservationSession reservationist) {
90          this.reservationist = reservationist;
91      }
92  }