1 package ejava.examples.txhotel.blimpl;
2
3 import java.util.Calendar;
4 import java.util.Date;
5 import java.util.GregorianCalendar;
6 import java.util.HashMap;
7 import java.util.List;
8 import java.util.Map;
9
10 import org.slf4j.Logger;
11 import org.slf4j.LoggerFactory;
12
13 import ejava.examples.txhotel.bl.HotelReservationException;
14 import ejava.examples.txhotel.bl.HotelReservationist;
15 import ejava.examples.txhotel.bl.InvalidParameterException;
16 import ejava.examples.txhotel.bl.InvalidReservationChangeException;
17 import ejava.examples.txhotel.bl.ReservationNotFoundException;
18 import ejava.examples.txhotel.bo.Person;
19 import ejava.examples.txhotel.bo.Reservation;
20 import ejava.examples.txhotel.dao.ReservationDAO;
21
22 public class HotelReservationImpl implements HotelReservationist {
23 private static final Logger log = LoggerFactory.getLogger(HotelReservationImpl.class);
24 protected ReservationDAO dao;
25 private static long counter=0;
26
27 protected Reservation getReservation(String confirmation)
28 throws HotelReservationException {
29
30 Reservation reservation =
31 dao.getReservationByConfirmation(confirmation);
32 if (reservation == null) {
33 throw new ReservationNotFoundException(
34 "unable to locate reservation for conf#" +
35 confirmation);
36 }
37 return reservation;
38 }
39
40 public void cancelReservation(Reservation reservation)
41 throws HotelReservationException {
42
43 Reservation officialCopy =
44 getReservation(reservation.getConfirmation());
45
46 Calendar cutoff = new GregorianCalendar();
47 cutoff.setTime(officialCopy.getStartDate());
48 cutoff.add(Calendar.DAY_OF_YEAR, -2);
49
50 Calendar now = new GregorianCalendar();
51
52 log.debug("comparing now=" + now.getTime() +
53 ", cutoff=" + cutoff.getTime() +
54 "=" + now.compareTo(cutoff));
55
56 if (now.compareTo(cutoff) > 0) {
57 throw new InvalidReservationChangeException(
58 "unable to cancel reservation, " +
59 "start date to close or has passed:" +
60 officialCopy.getStartDate());
61 }
62
63 dao.removeReservation(officialCopy);
64 }
65
66 public Reservation commitReservation(Reservation reservation)
67 throws HotelReservationException {
68
69 log.info("nothing implemented yet");
70 return dao.getReservation(reservation.getId());
71 }
72
73 public Reservation createReservation(
74 Person person, Date startDate, Date endDate)
75 throws HotelReservationException {
76
77 log.debug("checking dates; start=" + startDate + ", end=" + endDate);
78 if (startDate == null) {
79 throw new InvalidParameterException("no start date supplied");
80 }
81 else if (endDate == null) {
82 throw new InvalidParameterException("no end date supplied");
83 }
84 else if (startDate.getTime() > endDate.getTime()) {
85 throw new InvalidParameterException("start date after end date");
86 }
87
88 String confirmation = new Long(
89 System.currentTimeMillis()).toString() + "-" + ++counter;
90 Reservation reservation = new Reservation(
91 0, 0, confirmation, person, startDate, endDate);
92 return dao.createReservation(reservation);
93 }
94
95 public List<Reservation> getReservations(int index, int count)
96 throws HotelReservationException {
97 return dao.getReservations(index, count);
98 }
99
100 public List<Reservation> getReservationsForPerson(
101 Person person, int index, int count)
102 throws HotelReservationException {
103 Map<String, Object> params = new HashMap<String, Object>();
104 params.put("firstName", person.getFirstName());
105 params.put("lastName", person.getLastName());
106 return dao.getReservations("getReservationsByName",
107 params, index, count);
108 }
109
110 public void cleanupReservation(String confirmation)
111 throws HotelReservationException {
112 log.debug("cleanup reservation# " + confirmation);
113 Reservation reservation = getReservation(confirmation);
114 if (reservation != null) {
115 dao.removeReservation(reservation);
116 }
117 }
118
119
120 public void setDao(ReservationDAO dao) {
121 this.dao = dao;
122 }
123
124 public Reservation getReservationByConfirmation(String confirmation)
125 throws HotelReservationException {
126 return getReservation(confirmation);
127 }
128 }