View Javadoc
1   package ejava.examples.txhotel.blimpl;
2   
3   import java.util.ArrayList;
4   import java.util.Date;
5   import java.util.List;
6   
7   import org.apache.commons.logging.Log;
8   import org.apache.commons.logging.LogFactory;
9   
10  import ejava.examples.txhotel.bl.HotelReservationException;
11  import ejava.examples.txhotel.bl.HotelReservationSession;
12  import ejava.examples.txhotel.bl.HotelReservationist;
13  import ejava.examples.txhotel.bo.Person;
14  import ejava.examples.txhotel.bo.Reservation;
15  
16  /**
17   * This class represents some stateful business logic that caches desired
18   * reservations until the caller calls commit. At that time they will all
19   * be added to the hotel using the stateless reservationist.
20   */
21  public class HotelReservationSessionImpl implements HotelReservationSession {
22      private static final Log log = 
23          LogFactory.getLog(HotelReservationSessionImpl.class);
24      private List<Reservation> pending = new ArrayList<Reservation>();
25      private HotelReservationist reservationist;
26  
27      public void createReservation(Person person, Date startDate, Date endDate)
28          throws HotelReservationException {
29          pending.add(new Reservation(0,0,null,person, startDate, endDate));        
30      	log.debug("added pending reservation, size=" + pending.size());
31      }
32      
33      public void cancelReservations() throws HotelReservationException {
34      }
35  
36      public List<Reservation> commit() throws HotelReservationException {
37          log.info("************ creating " + pending.size() + " reservations ***");
38          List<Reservation> commited = new ArrayList<Reservation>();
39          for(Reservation p: pending) {
40              Reservation c = reservationist.createReservation(
41                      p.getPerson(), p.getStartDate(), p.getEndDate());
42              log.debug("created reservation:" + c);
43              commited.add(c);
44          }
45          log.debug("returning " + commited.size() + " reservations");
46          return commited;
47      }
48  
49      public void setReservationist(HotelReservationist reservationist) {
50          this.reservationist = reservationist;
51      }
52      
53      @Override
54      public void close() {
55      }
56  }