View Javadoc
1   package ejava.examples.txagent.blimpl;
2   
3   import java.util.List;
4   
5   import org.apache.commons.logging.Log;
6   import org.apache.commons.logging.LogFactory;
7   
8   import ejava.examples.txagent.bl.AgentReservationException;
9   import ejava.examples.txagent.bl.BookingAgent;
10  import ejava.examples.txagent.bo.Booking;
11  import ejava.examples.txagent.dao.BookingDAO;
12  import ejava.examples.txhotel.bl.HotelReservationException;
13  import ejava.examples.txhotel.bl.HotelReservationist;
14  import ejava.examples.txhotel.bo.Reservation;
15  
16  public class AgentImpl implements BookingAgent {
17      private static Log log = LogFactory.getLog(AgentImpl.class);
18      private BookingDAO bookingDAO;
19      private HotelReservationist reservationist;
20  
21      /**
22       * Get reservations for stored IDs
23       */
24      protected void populateBooking(Booking booking) 
25          throws AgentReservationException {
26          for(String c: booking.getHotelConfirmations()) {
27              try {
28              	Reservation r = reservationist.getReservationByConfirmation(c);
29                  booking.addHotelReservation(r);
30              } catch (HotelReservationException ex) {
31                  throw new AgentReservationException(
32                          "error getting reservation by conf# " + c, ex);
33              }
34          }
35      }
36      
37      public Booking getBookingByConfirmation(String confirmation) 
38          throws AgentReservationException {
39          Booking booking = bookingDAO.getBookingByConfirmation(confirmation);
40          if (booking != null) {
41              populateBooking(booking);
42          }
43          return booking;
44      }
45  
46      public List<Booking> getBookings(int index, int count) 
47          throws AgentReservationException {
48          List<Booking> bookings = bookingDAO.getBookings(index, count);
49          return bookings;
50      }
51      public void cleanupBooking(String confirmation) 
52          throws AgentReservationException {
53          log.debug("cleanup booking# " + confirmation);
54          Booking booking = bookingDAO.getBookingByConfirmation(confirmation);
55          if (booking != null) {
56              bookingDAO.removeBooking(booking);
57              for(String c: booking.getHotelConfirmations()) {
58                  try {
59                      log.debug("cleaning up reservation # " + c);
60                      reservationist.cleanupReservation(c);
61                  } catch (HotelReservationException ex) {
62                      throw new AgentReservationException(
63                              "unable to cancel reservation:" + ex);
64                  }
65              }
66          }
67      }
68  
69      public void setBookingDAO(BookingDAO bookingDAO) {
70          this.bookingDAO = bookingDAO;
71      }
72      public void setReservationist(HotelReservationist reservationist) {
73          this.reservationist = reservationist;
74      }
75  
76  }