662253d73f6ff495b659588068d4d5525c24029c
[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 class Controller {
33 protected DataHandler dh;
34 protected User currentUser;
35
36 /**
37 * Sets the <code>DataHandler</code> implementation for this Controller,
38 * must be set before using all other methods!
39 * @param dh
40 * DataHandler implementation (i.e. DataHandlerForExcel)
41 */
42 public Controller(DataHandler dh) {
43 this.dh = dh;
44 }
45
46 /**
47 * Sets the <code>DataHandler</code> implementation for this Controller,
48 * must be set before using all other methods!
49 * @param dh
50 * DataHandler implementation (i.e. DataHandlerForExcel)
51 */
52 public void setDataHandler(DataHandler dh) {
53 this.dh = dh;
54 }
55
56 /**
57 * Tries to log in a <code>User</code> with the given credentials
58 * @param userName
59 * user name
60 * @param password
61 * the stored password for the given user name
62 * @return <code>True</code> if the user could log in successfully,
63 * <code>False</code> either the username or the password was not
64 * correct.
65 * @throws Exception
66 * no <code>DataHandler</code> set
67 * @throws AlreadyBoundException
68 * a <code>User</code> is already logged in
69 */
70 public boolean logIn(String userName, String password) throws Exception {
71 if (dh == null)
72 throw new Exception("No DataHandler set");
73 if (currentUser != null)
74 throw new AlreadyBoundException("A user is already logged in");
75 User tempUser = dh.findUser(userName);
76 if (tempUser == null || !tempUser.checkPassword(password))
77 return false;
78 else {
79 currentUser = tempUser;
80 return true;
81 }
82 }
83
84 /**
85 * Logs out the current logged user
86 * @throws Exception
87 * either no <code>DataHandler</code> is set or no user is
88 * logged in
89 */
90 public void logOut() throws Exception {
91 if (dh == null)
92 throw new Exception("No DataHandler set");
93 if (currentUser == null)
94 throw new Exception("No user is logged in");
95 currentUser = null;
96 }
97
98 /**
99 * Registers a new user to the current DataHandler
100 * @param userName
101 * unique user name
102 * @param password
103 * plain-text password
104 * @param email
105 * e-mail address
106 * @param name
107 * name
108 * @param lastname
109 * last name
110 * @throws Exception
111 * no <code>DataHandler</code> set
112 */
113 public void addUser(String userName, String password, String email, String name, String lastname) throws Exception {
114 if (dh == null)
115 throw new Exception("No DataHandler set");
116 User newUser = new User(userName, password);
117 newUser.setMail(email);
118 newUser.setName(name);
119 newUser.setLastname(lastname);
120 dh.addUser(newUser);
121 }
122
123 /**
124 * Returns all stored resources of the set DataHandler
125 * @return <code>List</code> of resources
126 * @throws Exception
127 * no <code>DataHandler</code> is set
128 */
129 public List<Resource> getResources() throws Exception {
130 if (dh == null)
131 throw new Exception("No DataHandler set");
132 List<Resource> resourceList = new ArrayList<Resource>();
133 for (Resource resource : dh.getAllResources()) {
134 resourceList.add(resource.clone());
135 }
136 return resourceList;
137 }
138
139 /**
140 * Returns all reservations overlapping the given dateRange in a
141 * <code>Map</code>
142 * @param dateRange
143 * time span
144 * @return <code>Key</code> reservation, <code>Value</code> is editable
145 * @throws Exception
146 * no <code>User</code> is logged in or no
147 * <code>DataHandler</code> is set
148 */
149 public Map<Reservation, Boolean> getReservationsInDateRange(DateRange dateRange) throws Exception {
150 if (dh == null)
151 throw new Exception("No DataHandler set");
152 if (dateRange == null)
153 throw new Exception("dateRange must be defined");
154
155 // Could be swapped out to private method for better performance (just
156 // check currentUser once)
157 // and easier code maintenance
158 Map<Reservation, Boolean> returnSet = new HashMap<Reservation, Boolean>();
159 for (Reservation reservation : dh.overlaps(dateRange)) {
160 returnSet.put(reservation, reservation.getUser().getUserName().equals(currentUser.getUserName()));
161 }
162 return returnSet;
163 }
164
165 /**
166 * Returns the reservations of the given user
167 * @param user
168 * Owner, if <code>null</code> returns the reservations of the
169 * current logged in user
170 * @return <code>List</code> of reservation copies of the given owner
171 * @throws Exception
172 * either no <code>DataHandler</code> is set or no user is
173 * logged in while user param = null
174 */
175 public Map<Reservation, Boolean> getUserReservations_Super(User user) throws Exception {
176 if (dh == null)
177 throw new Exception("No DataHandler set");
178 if (user == null) {
179 throw new Exception("user must be defined");
180 }
181
182 // Could be swapped out to private method for better performance (just
183 // check currentUser once)
184 // and easier code maintenance
185 Map<Reservation, Boolean> returnSet = new HashMap<Reservation, Boolean>();
186 for (Reservation reservation : dh.userReservations(user)) {
187 returnSet.put(reservation, reservation.getUser().getUserName().equals(currentUser.getUserName()));
188 }
189 return returnSet;
190 }
191
192 /**
193 * Returns the reservation or null if no reservation was found
194 * @param id
195 * reservation identifier
196 * @return <code>Reservation</code> copy of reservation with given id,
197 * <code>null</code> no reservation with this id found
198 * @throws Exception
199 * No DataHandler set or id null
200 */
201 public Reservation getReservation(UUID id) throws Exception {
202 if (dh == null)
203 throw new Exception("No DataHandler set");
204 if (id == null)
205 throw new Exception("uuid must be defined");
206 if (dh.findReservation(id) == null)
207 return null;
208 else
209 return dh.findReservation(id).clone();
210 }
211
212 /**
213 * Returns <code>Map</code> of reservation group (recurring reservations)
214 * @param groupId
215 * group identifier
216 * @return <code>Key</code> reservation, <code>Value</code> is editable
217 * @throws IllegalArgumentException
218 * groupId is null
219 * @throws Exception
220 * no <code>DataHandler</code> set
221 */
222 public Map<Reservation, Boolean> getReservationGroup(UUID groupId) throws Exception {
223 if (dh == null)
224 throw new Exception("No DataHandler set");
225 if (groupId == null)
226 throw new IllegalArgumentException("groupId has to be defined");
227
228 // Could be swapped out to private method for better performance (just
229 // check currentUser once)
230 // and easier code maintenance
231 Map<Reservation, Boolean> returnSet = new HashMap<Reservation, Boolean>();
232 for (Reservation reservation : dh.findGroupReservations(groupId)) {
233 returnSet.put(reservation, reservation.getUser().getUserName().equals(currentUser.getUserName()));
234 }
235 return returnSet;
236 }
237
238 /**
239 * Makes a reservation in the current set DataHandler
240 * @param user
241 * user owning the reservation, if null currentUser is set
242 * @param resource
243 * resource to reserve
244 * @param dateRangeList
245 * <code>List</code> of DateRanges defining when the resource is
246 * occupied
247 * @param remark
248 * public remark for the reservation
249 * @return <code>Set</code> of Reservations that overlap an already stored
250 * reservation (conflict), if not empty no reservation was saved
251 * @throws IllegalArgumentException
252 * <code>resource</code> or <code>dateRangeList</code> is null,
253 * or <code>dateRangeList</code> is empty
254 * @throws Exception
255 * thrown by <code>DataHandler</code>
256 */
257 public Set<Reservation> makeReservation(User user, Resource resource, List<DateRange> dateRangeList, String remark)
258 throws Exception {
259 if (user == null) {
260 if (currentUser == null)
261 throw new IllegalArgumentException("User must be defined if no user is logged in");
262 user = currentUser;
263 }
264 if (resource == null)
265 throw new IllegalArgumentException("Resource must be definded");
266 if (dateRangeList == null)
267 throw new IllegalArgumentException("dateRangeList can not be null");
268 if (dateRangeList.isEmpty())
269 throw new IllegalArgumentException("dateRangeList empty");
270
271 Set<Reservation> invalidReservations = new HashSet<Reservation>();
272 UUID groupId = UUID.randomUUID();
273 for (DateRange dateRange : dateRangeList) {
274 Reservation reservation = new Reservation(groupId, user, resource, dateRange, remark);
275 if (!dh.addReservation(reservation))
276 invalidReservations.add(reservation);
277 }
278 if (invalidReservations.isEmpty()) {
279 dh.saveChanges();
280 return null;
281 } else
282 return invalidReservations;
283 }
284
285 /**
286 * Replaces the given reservation in the set DataHandler referring to the
287 * reservationid
288 * @param reservation
289 * Copy of the reservation to edit
290 * @return <code>True</code> Changes saved, <code>False</code> Changes could
291 * not be saved
292 * @throws IllegalArgumentException
293 * if reservation param is null
294 * @throws Exception
295 * if no <code>DataHandler</code> is set or the logged user is
296 * not authorized
297 */
298 public boolean changeReservation_Super(Reservation reservation) throws Exception {
299 if (dh == null)
300 throw new Exception("No DataHandler set");
301 if (reservation == null)
302 throw new IllegalArgumentException("Reservation must be defined");
303 return dh.editReservation(reservation);
304 }
305
306 /**
307 * Deletes the given reservation in the set <code>DataHandler</code>
308 * referring to the reservation id
309 * @param reservation
310 * Copy of the reservation to edit
311 * @return <code>True</code> Changes saved <code>False</code> Changes could
312 * not be saved
313 * @throws IllegalArgumentException
314 * if reservation is null
315 * @throws Exception
316 * if no <code>DataHandler</code> is set, no <code>User</code>
317 * is logged in or logged <code>User</code> is not authorized
318 */
319 public boolean deleteReservation_Super(Reservation reservation) throws Exception {
320 if (dh == null)
321 throw new Exception("No DataHandler set");
322 if (reservation == null)
323 throw new IllegalArgumentException("Reservation must be defined");
324 return dh.deleteReservation(reservation);
325 }
326 }