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