DataHandler updated
[staff/due1/sed-hs15-srs-purple.git] / src / main / java / ch / bfh / ti / srs / controller / Controller.java
1 /*
2 * Copyright (c) 2015 Berner Fachhochschule, Switzerland.
3 *
4 * Project Smart Reservation System.
5 *
6 * Distributable under GPL license. See terms of license at gnu.org.
7 */
8 /**
9 *
10 */
11 package ch.bfh.ti.srs.controller;
12
13 import java.rmi.AlreadyBoundException;
14 import java.util.ArrayList;
15 import java.util.HashMap;
16 import java.util.HashSet;
17 import java.util.List;
18 import java.util.Map;
19 import java.util.Set;
20 import java.util.UUID;
21
22 import ch.bfh.ti.daterange.DateRange;
23 import ch.bfh.ti.srs.datahandler.DataHandler;
24 import ch.bfh.ti.srs.dataschema.Reservation;
25 import ch.bfh.ti.srs.dataschema.Resource;
26 import ch.bfh.ti.srs.dataschema.User;
27
28 /**
29 * @author Michael
30 *
31 */
32 public abstract class Controller {
33 protected DataHandler dh;
34 protected SessionController sc;
35
36
37 /**
38 * Sets the <code>DataHandler</code> implementation for this Controller,
39 * must be set before using all other methods!
40 * @param dh
41 * DataHandler implementation (i.e. DataHandlerForExcel)
42 */
43 public Controller(DataHandler dh, SessionController sc) {
44 this.dh = dh;
45 }
46
47 /**
48 * Sets the <code>DataHandler</code> implementation for this Controller,
49 * must be set before using all other methods!
50 * @param dh
51 * DataHandler implementation (i.e. DataHandlerForExcel)
52 */
53 public void setDataHandler(DataHandler dh) {
54 this.dh = dh;
55 }
56
57 /**
58 * Registers a new user to the current DataHandler
59 * @param userName
60 * unique user name
61 * @param password
62 * plain-text password
63 * @param email
64 * e-mail address
65 * @param name
66 * name
67 * @param lastname
68 * last name
69 * @throws Exception
70 * no <code>DataHandler</code> set
71 */
72 public void addUser(String userName, String password, String email, String name, String lastname) throws Exception {
73 if (dh == null)
74 throw new Exception("No DataHandler set");
75 User newUser = new User(userName, password);
76 newUser.setMail(email);
77 newUser.setName(name);
78 newUser.setLastname(lastname);
79 dh.addUser(newUser);
80 }
81
82 /**
83 * Returns all stored resources of the set DataHandler
84 * @return <code>List</code> of resources
85 * @throws Exception
86 * no <code>DataHandler</code> is set
87 */
88 public List<Resource> getResources() throws Exception {
89 if (dh == null)
90 throw new Exception("No DataHandler set");
91 List<Resource> resourceList = new ArrayList<Resource>();
92 for (Resource resource : dh.getAllResources()) {
93 resourceList.add(resource.clone());
94 }
95 return resourceList;
96 }
97
98 /**
99 * Returns all reservations overlapping the given dateRange in a
100 * <code>Map</code>
101 * @param dateRange
102 * time span
103 * @return <code>Key</code> reservation, <code>Value</code> is editable
104 * @throws Exception
105 * no <code>User</code> is logged in or no
106 * <code>DataHandler</code> is set
107 */
108 public Map<Reservation, Boolean> getReservationsInDateRange(DateRange dateRange) throws Exception {
109 if (dh == null)
110 throw new Exception("No DataHandler set");
111 if (dateRange == null)
112 throw new Exception("dateRange must be defined");
113
114 // Could be swapped out to private method for better performance (just
115 // check sc.getLoggedUser() once)
116 // and easier code maintenance
117 Map<Reservation, Boolean> returnSet = new HashMap<Reservation, Boolean>();
118 for (Reservation reservation : dh.overlaps(dateRange)) {
119 returnSet.put(reservation, reservation.getUser().getUserName().equals(sc.getLoggedUser().getUserName()));
120 }
121 return returnSet;
122 }
123
124 /**
125 * Returns the reservations of the given user
126 * @param user
127 * Owner, if <code>null</code> returns the reservations of the
128 * current logged in user
129 * @return <code>List</code> of reservation copies of the given owner
130 * @throws Exception
131 * either no <code>DataHandler</code> is set or no user is
132 * logged in while user param = null
133 */
134 public Map<Reservation, Boolean> getUserReservations_Super(User user) throws Exception {
135 if (dh == null)
136 throw new Exception("No DataHandler set");
137 if (user == null) {
138 throw new Exception("user must be defined");
139 }
140
141 // Could be swapped out to private method for better performance (just
142 // check sc.getLoggedUser() once)
143 // and easier code maintenance
144 Map<Reservation, Boolean> returnSet = new HashMap<Reservation, Boolean>();
145 for (Reservation reservation : dh.userReservations(user)) {
146 returnSet.put(reservation, reservation.getUser().getUserName().equals(sc.getLoggedUser().getUserName()));
147 }
148 return returnSet;
149 }
150
151 /**
152 * Returns the reservation or null if no reservation was found
153 * @param id
154 * reservation identifier
155 * @return <code>Reservation</code> copy of reservation with given id,
156 * <code>null</code> no reservation with this id found
157 * @throws Exception
158 * No DataHandler set or id null
159 */
160 public Reservation getReservation(UUID id) throws Exception {
161 if (dh == null)
162 throw new Exception("No DataHandler set");
163 if (id == null)
164 throw new Exception("uuid must be defined");
165 if (dh.findReservation(id) == null)
166 return null;
167 else
168 return dh.findReservation(id).clone();
169 }
170
171 /**
172 * Returns <code>Map</code> of reservation group (recurring reservations)
173 * @param groupId
174 * group identifier
175 * @return <code>Key</code> reservation, <code>Value</code> is editable
176 * @throws IllegalArgumentException
177 * groupId is null
178 * @throws Exception
179 * no <code>DataHandler</code> set
180 */
181 public Map<Reservation, Boolean> getReservationGroup(UUID groupId) throws Exception {
182 if (dh == null)
183 throw new Exception("No DataHandler set");
184 if (groupId == null)
185 throw new IllegalArgumentException("groupId has to be defined");
186
187 // Could be swapped out to private method for better performance (just
188 // check sc.getLoggedUser() once)
189 // and easier code maintenance
190 Map<Reservation, Boolean> returnSet = new HashMap<Reservation, Boolean>();
191 for (Reservation reservation : dh.findGroupReservations(groupId)) {
192 returnSet.put(reservation, reservation.getUser().getUserName().equals(sc.getLoggedUser().getUserName()));
193 }
194 return returnSet;
195 }
196
197 /**
198 * Makes a reservation in the current set DataHandler
199 * @param user
200 * user owning the reservation, if null sc.getLoggedUser() is set
201 * @param resource
202 * resource to reserve
203 * @param dateRangeList
204 * <code>List</code> of DateRanges defining when the resource is
205 * occupied
206 * @param remark
207 * public remark for the reservation
208 * @return <code>Set</code> of Reservations that overlap an already stored
209 * reservation (conflict), if not empty no reservation was saved
210 * @throws IllegalArgumentException
211 * <code>resource</code> or <code>dateRangeList</code> is null,
212 * or <code>dateRangeList</code> is empty
213 * @throws Exception
214 * thrown by <code>DataHandler</code>
215 */
216 public Set<Reservation> makeReservation(User user, Resource resource, List<DateRange> dateRangeList, String remark)
217 throws Exception {
218 if (user == null) {
219 if (sc.getLoggedUser() == null)
220 throw new IllegalArgumentException("User must be defined if no user is logged in");
221 user = sc.getLoggedUser();
222 }
223 if (resource == null)
224 throw new IllegalArgumentException("Resource must be definded");
225 if (dateRangeList == null)
226 throw new IllegalArgumentException("dateRangeList can not be null");
227 if (dateRangeList.isEmpty())
228 throw new IllegalArgumentException("dateRangeList empty");
229
230 Set<Reservation> invalidReservations = new HashSet<Reservation>();
231 UUID groupId = UUID.randomUUID();
232 for (DateRange dateRange : dateRangeList) {
233 Reservation reservation = new Reservation(groupId, user, resource, dateRange, remark);
234 if (!dh.addReservation(reservation))
235 invalidReservations.add(reservation);
236 }
237 if (invalidReservations.isEmpty()) {
238 dh.saveChanges();
239 return null;
240 } else
241 return invalidReservations;
242 }
243
244 /**
245 * Replaces the given reservation in the set DataHandler referring to the
246 * reservationid
247 * @param reservation
248 * Copy of the reservation to edit
249 * @return <code>True</code> Changes saved, <code>False</code> Changes could
250 * not be saved
251 * @throws IllegalArgumentException
252 * if reservation param is null
253 * @throws Exception
254 * if no <code>DataHandler</code> is set or the logged user is
255 * not authorized
256 */
257 public boolean changeReservation_Super(Reservation reservation) throws Exception {
258 if (dh == null)
259 throw new Exception("No DataHandler set");
260 if (reservation == null)
261 throw new IllegalArgumentException("Reservation must be defined");
262 return dh.editReservation(reservation);
263 }
264
265 /**
266 * Deletes the given reservation in the set <code>DataHandler</code>
267 * referring to the reservation id
268 * @param reservation
269 * Copy of the reservation to edit
270 * @return <code>True</code> Changes saved <code>False</code> Changes could
271 * not be saved
272 * @throws IllegalArgumentException
273 * if reservation is null
274 * @throws Exception
275 * if no <code>DataHandler</code> is set, no <code>User</code>
276 * is logged in or logged <code>User</code> is not authorized
277 */
278 public boolean deleteReservation_Super(Reservation reservation) throws Exception {
279 if (dh == null)
280 throw new Exception("No DataHandler set");
281 if (reservation == null)
282 throw new IllegalArgumentException("Reservation must be defined");
283 return dh.deleteReservation(reservation);
284 }
285 }