adapted version
[staff/due1/sed-hs15-srs-purple.git] / src / main / java / ch / bfh / ti / srs / controller / UserController.java
CommitLineData
5555b609
M
1/*\r
2 * Copyright (c) 2015 Berner Fachhochschule, Switzerland.\r
3 *\r
4 * Project Smart Reservation System.\r
5 *\r
6 * Distributable under GPL license. See terms of license at gnu.org.\r
7 */\r
e3535133 8package ch.bfh.ti.srs.controller;\r
5555b609 9\r
1ec0e01f 10import java.rmi.AlreadyBoundException;\r
b5b8ffc1 11import java.util.ArrayList;\r
4574ef68
M
12import java.util.HashMap;\r
13import java.util.HashSet;\r
13313998 14import java.util.List;\r
4574ef68
M
15import java.util.Map;\r
16import java.util.Set;\r
13313998 17import java.util.UUID;\r
1ec0e01f 18\r
13313998 19import ch.bfh.ti.daterange.DateRange;\r
e3535133 20import ch.bfh.ti.srs.datahandler.DataHandler;\r
13313998 21import ch.bfh.ti.srs.dataschema.Reservation;\r
1702d8b9 22import ch.bfh.ti.srs.dataschema.Resource;\r
5c0ee08b 23import ch.bfh.ti.srs.dataschema.User;\r
e3535133 24\r
1ec0e01f
M
25/**\r
26 * @author Michael Grossen\r
27 */\r
5555b609 28public class UserController {\r
1e4d6813
M
29 private User currentUser;\r
30 private DataHandler dh;\r
66cc653d
D
31\r
32 public UserController() {\r
1f7b5004 33 }\r
66cc653d 34\r
1f7b5004 35 /**\r
66cc653d
D
36 * Sets the <code>DataHandler</code> implementation for this Controller,\r
37 * must be set before using all other methods!\r
38 * @param dh\r
39 * DataHandler implementation (i.e. DataHandlerForExcel)\r
1f7b5004 40 */\r
66cc653d 41 public void setDataHandler(DataHandler dh) {\r
1f7b5004 42 this.dh = dh;\r
5555b609 43 }\r
66cc653d 44\r
1ec0e01f 45 /**\r
757804bb 46 * Tries to log in a <code>User</code> with the given credentials\r
66cc653d
D
47 * @param userName\r
48 * user name\r
49 * @param password\r
50 * the stored password for the given user name\r
51 * @return <code>True</code> if the user could log in successfully,\r
52 * <code>False</code> either the username or the password was not\r
53 * correct.\r
54 * @throws Exception\r
55 * no <code>DataHandler</code> set\r
56 * @throws AlreadyBoundException\r
57 * a <code>User</code> is already logged in\r
1ec0e01f 58 */\r
66cc653d
D
59 public boolean logIn(String userName, String password) throws Exception {\r
60 if (dh == null)\r
61 throw new Exception("No DataHandler set");\r
62 if (currentUser != null)\r
63 throw new AlreadyBoundException("A user is already logged in");\r
1f7b5004 64 User tempUser = dh.findUser(userName);\r
66cc653d
D
65 if (tempUser == null || !tempUser.checkPassword(password))\r
66 return false;\r
67 else {\r
1ec0e01f 68 currentUser = tempUser;\r
5555b609
M
69 return true;\r
70 }\r
5555b609 71 }\r
66cc653d 72\r
1ec0e01f 73 /**\r
757804bb 74 * Logs out the current logged user\r
66cc653d
D
75 * @throws Exception\r
76 * either no <code>DataHandler</code> is set or no user is\r
77 * logged in\r
1ec0e01f 78 */\r
66cc653d
D
79 public void logOut() throws Exception {\r
80 if (dh == null)\r
81 throw new Exception("No DataHandler set");\r
82 if (currentUser == null)\r
83 throw new Exception("No user is logged in");\r
1ec0e01f
M
84 currentUser = null;\r
85 }\r
66cc653d 86\r
1f7b5004
M
87 /**\r
88 * Registers a new user to the current DataHandler\r
66cc653d
D
89 * @param userName\r
90 * unique user name\r
91 * @param password\r
92 * plain-text password\r
93 * @param email\r
94 * e-mail address\r
95 * @param name\r
96 * name\r
97 * @param lastname\r
98 * last name\r
99 * @throws Exception\r
100 * no <code>DataHandler</code> set\r
1f7b5004 101 */\r
66cc653d
D
102 public void addUser(String userName, String password, String email, String name, String lastname) throws Exception {\r
103 if (dh == null)\r
104 throw new Exception("No DataHandler set");\r
105 User newUser = new User(userName, password);\r
1ec0e01f
M
106 newUser.setMail(email);\r
107 newUser.setName(name);\r
108 newUser.setLastname(lastname);\r
1f7b5004
M
109 dh.addUser(newUser);\r
110 }\r
111\r
1f7b5004 112 /**\r
757804bb 113 * Gets the user name of the current user\r
66cc653d
D
114 * @deprecated use <code>getLoggedUser().getUserName()</code>\r
115 * @return user name\r
116 * @throws Exception\r
117 * no <code>DataHandler</code> is set\r
1f7b5004 118 */\r
66cc653d
D
119 @Deprecated\r
120 public String getLoggedUserName() throws Exception {\r
121 if (currentUser == null)\r
122 throw new Exception("No user is logged in");\r
1f7b5004
M
123 return currentUser.getUserName();\r
124 }\r
125\r
1f7b5004
M
126 /**\r
127 * Gets the mail address of the current user\r
757804bb 128 * @deprecated use <code>getLoggedUser().getMail()</code>\r
66cc653d
D
129 * @return Mail address\r
130 * @throws Exception\r
131 * no user is logged in\r
1f7b5004 132 */\r
1702d8b9 133 @Deprecated\r
66cc653d
D
134 public String getLoggedMail() throws Exception {\r
135 if (currentUser == null)\r
136 throw new Exception("No user is logged in");\r
1f7b5004
M
137 return currentUser.getMail();\r
138 }\r
139\r
140 /**\r
141 * Gets the first name of the current user\r
757804bb 142 * @deprecated use <code>getLoggedUser().getName()</code>\r
66cc653d
D
143 * @return First name\r
144 * @throws Exception\r
145 * no user is logged in\r
1f7b5004 146 */\r
1702d8b9 147 @Deprecated\r
66cc653d
D
148 public String getLoggedName() throws Exception {\r
149 if (currentUser == null)\r
150 throw new Exception("No user is logged in");\r
1f7b5004
M
151 return currentUser.getName();\r
152 }\r
153\r
154 /**\r
155 * Gets the last name of the current user\r
757804bb 156 * @deprecated use <code>getLoggedUser().getLastName()</code>\r
66cc653d
D
157 * @return Last name\r
158 * @throws Exception\r
159 * no user is logged in\r
1f7b5004 160 */\r
1702d8b9 161 @Deprecated\r
66cc653d
D
162 public String getLoggedLastname() throws Exception {\r
163 if (currentUser == null)\r
164 throw new Exception("No user is logged in");\r
1f7b5004 165 return currentUser.getLastname();\r
1ec0e01f 166 }\r
66cc653d 167\r
757804bb
M
168 /**\r
169 * Gets the current logged <code>User</code>\r
66cc653d
D
170 * @return current <code>User</code>, or <code>null</code> if no\r
171 * <code>User</code> is logged in\r
757804bb 172 */\r
66cc653d
D
173 public User getLoggedUser() {\r
174 if (currentUser == null)\r
175 return null;\r
4574ef68
M
176 return currentUser;\r
177 }\r
66cc653d 178\r
757804bb
M
179 /**\r
180 * Searches the set DataHandler for a specific <code>User</code>\r
66cc653d
D
181 * @param userName\r
182 * <code>User</code>\r
183 * @return clone of <code>User</code>\r
184 * @throws Exception\r
185 * No <code>DataHandler</code> is set\r
757804bb 186 */\r
66cc653d
D
187 public User getUser(String userName) throws Exception {\r
188 if (dh == null)\r
189 throw new Exception("No DataHandler set");\r
4574ef68 190 return dh.findUser(userName).clone();\r
a08b0437 191 }\r
66cc653d 192\r
13313998 193 /**\r
66cc653d
D
194 * Returns all reservations overlapping the given dateRange in a\r
195 * <code>Map</code>\r
196 * @param dateRange\r
197 * time span\r
198 * @return <code>Key</code> reservation, <code>Value</code> is editable\r
199 * @throws Exception\r
200 * no <code>User</code> is logged in or no\r
201 * <code>DataHandler</code> is set\r
13313998 202 */\r
66cc653d
D
203 public Map<Reservation, Boolean> getReservationsInDateRange(DateRange dateRange) throws Exception {\r
204 if (dh == null)\r
205 throw new Exception("No DataHandler set");\r
206 if (dateRange == null)\r
207 throw new Exception("dateRange must be defined");\r
208\r
209 // Could be swapped out to private method for better performance (just\r
210 // check currentUser once)\r
4574ef68
M
211 // and easier code maintenance\r
212 Map<Reservation, Boolean> returnSet = new HashMap<Reservation, Boolean>();\r
213 for (Reservation reservation : dh.overlaps(dateRange)) {\r
5a196189 214 returnSet.put(reservation, reservation.getUser().getUserName().equals(currentUser.getUserName()));\r
4574ef68
M
215 }\r
216 return returnSet;\r
13313998 217 }\r
66cc653d 218\r
13313998
M
219 /**\r
220 * Returns the reservation or null if no reservation was found\r
66cc653d
D
221 * @param id\r
222 * reservation identifier\r
223 * @return <code>Reservation</code> copy of reservation with given id,\r
224 * <code>null</code> no reservation with this id found\r
225 * @throws Exception\r
226 * No DataHandler set or id null\r
13313998 227 */\r
66cc653d
D
228 public Reservation getReservation(UUID id) throws Exception {\r
229 if (dh == null)\r
230 throw new Exception("No DataHandler set");\r
231 if (id == null)\r
232 throw new Exception("uuid must be defined");\r
233 if (dh.findReservation(id) == null)\r
234 return null;\r
235 else\r
236 return dh.findReservation(id).clone();\r
13313998 237 }\r
66cc653d 238\r
b5b8ffc1 239 /**\r
757804bb 240 * Returns the reservations of the given user\r
66cc653d
D
241 * @param user\r
242 * Owner, if <code>null</code> returns the reservations of the\r
243 * current logged in user\r
244 * @return <code>List</code> of reservation copies of the given owner\r
245 * @throws Exception\r
246 * either no <code>DataHandler</code> is set or no user is\r
247 * logged in while user param = null\r
b5b8ffc1 248 */\r
66cc653d
D
249 public Map<Reservation, Boolean> getUserReservations(User user) throws Exception {\r
250 if (dh == null)\r
251 throw new Exception("No DataHandler set");\r
252 if (user == null) {\r
253 if (currentUser == null)\r
254 throw new Exception("user must be defined, if no user is logged in");\r
255 else\r
256 user = currentUser;\r
b5b8ffc1 257 }\r
66cc653d
D
258\r
259 // Could be swapped out to private method for better performance (just\r
260 // check currentUser once)\r
4574ef68
M
261 // and easier code maintenance\r
262 Map<Reservation, Boolean> returnSet = new HashMap<Reservation, Boolean>();\r
263 for (Reservation reservation : dh.userReservations(user)) {\r
5a196189 264 returnSet.put(reservation, reservation.getUser().getUserName().equals(currentUser.getUserName()));\r
4574ef68
M
265 }\r
266 return returnSet;\r
b5b8ffc1 267 }\r
66cc653d 268\r
b5b8ffc1 269 /**\r
757804bb 270 * Returns <code>Map</code> of reservation group (recurring reservations)\r
66cc653d
D
271 * @param groupId\r
272 * group identifier\r
273 * @return <code>Key</code> reservation, <code>Value</code> is editable\r
274 * @throws IllegalArgumentException\r
275 * groupId is null\r
276 * @throws Exception\r
277 * no <code>DataHandler</code> set\r
b5b8ffc1 278 */\r
66cc653d
D
279 public Map<Reservation, Boolean> getReservationGroup(UUID groupId) throws Exception {\r
280 if (dh == null)\r
281 throw new Exception("No DataHandler set");\r
282 if (groupId == null)\r
283 throw new IllegalArgumentException("groupId has to be defined");\r
284\r
285 // Could be swapped out to private method for better performance (just\r
286 // check currentUser once)\r
4574ef68
M
287 // and easier code maintenance\r
288 Map<Reservation, Boolean> returnSet = new HashMap<Reservation, Boolean>();\r
289 for (Reservation reservation : dh.findGroupReservations(groupId)) {\r
5a196189 290 returnSet.put(reservation, reservation.getUser().getUserName().equals(currentUser.getUserName()));\r
4574ef68
M
291 }\r
292 return returnSet;\r
b5b8ffc1 293 }\r
4574ef68 294\r
b5b8ffc1 295 /**\r
757804bb 296 * Makes a reservation in the current set DataHandler\r
66cc653d
D
297 * @param user\r
298 * user owning the reservation, if null currentUser is set\r
299 * @param resource\r
300 * resource to reserve\r
301 * @param dateRangeList\r
302 * <code>List</code> of DateRanges defining when the resource is\r
303 * occupied\r
304 * @param remark\r
305 * public remark for the reservation\r
306 * @return <code>Set</code> of Reservations that overlap an already stored\r
307 * reservation (conflict), if not empty no reservation was saved\r
308 * @throws IllegalArgumentException\r
309 * <code>resource</code> or <code>dateRangeList</code> is null,\r
310 * or <code>dateRangeList</code> is empty\r
311 * @throws Exception\r
312 * thrown by <code>DataHandler</code>\r
b5b8ffc1 313 */\r
66cc653d
D
314 public Set<Reservation> makeReservation(User user, Resource resource, List<DateRange> dateRangeList, String remark)\r
315 throws Exception {\r
316 if (user == null) {\r
317 if (currentUser == null)\r
318 throw new IllegalArgumentException("User must be defined if no user is logged in");\r
5a196189
M
319 user = currentUser;\r
320 }\r
66cc653d
D
321 if (resource == null)\r
322 throw new IllegalArgumentException("Resource must be definded");\r
323 if (dateRangeList == null)\r
324 throw new IllegalArgumentException("dateRangeList can not be null");\r
325 if (dateRangeList.isEmpty())\r
326 throw new IllegalArgumentException("dateRangeList empty");\r
327\r
4574ef68
M
328 Set<Reservation> invalidReservations = new HashSet<Reservation>();\r
329 UUID groupId = UUID.randomUUID();\r
1702d8b9 330 for (DateRange dateRange : dateRangeList) {\r
4574ef68 331 Reservation reservation = new Reservation(groupId, user, resource, dateRange, remark);\r
66cc653d
D
332 if (!dh.addReservation(reservation))\r
333 invalidReservations.add(reservation);\r
4574ef68 334 }\r
66cc653d
D
335 if (invalidReservations.isEmpty()) {\r
336 dh.saveChanges();\r
337 return null;\r
338 } else\r
339 return invalidReservations;\r
4574ef68 340 }\r
66cc653d 341\r
757804bb 342 /**\r
66cc653d
D
343 * Replaces the given reservation in the set DataHandler referring to the\r
344 * reservationid\r
345 * @param reservation\r
346 * Copy of the reservation to edit\r
347 * @return <code>True</code> Changes saved, <code>False</code> Changes could\r
348 * not be saved\r
349 * @throws IllegalArgumentException\r
350 * if reservation param is null\r
351 * @throws Exception\r
352 * if no <code>DataHandler</code> is set or the logged user is\r
353 * not authorized\r
757804bb 354 */\r
66cc653d
D
355 public boolean changeReservation(Reservation reservation) throws Exception {\r
356 if (dh == null)\r
357 throw new Exception("No DataHandler set");\r
358 if (currentUser == null)\r
359 throw new Exception("No user is logged in");\r
360 if (reservation == null)\r
361 throw new IllegalArgumentException("Reservation must be defined");\r
362 if (!reservation.getUser().equals(currentUser))\r
363 throw new Exception("You are not authorized to make this change");\r
4574ef68
M
364 return dh.editReservation(reservation);\r
365 }\r
66cc653d 366\r
757804bb 367 /**\r
66cc653d
D
368 * Deletes the given reservation in the set <code>DataHandler</code>\r
369 * referring to the reservation id\r
370 * @param reservation\r
371 * Copy of the reservation to edit\r
372 * @return <code>True</code> Changes saved <code>False</code> Changes could\r
373 * not be saved\r
374 * @throws IllegalArgumentException\r
375 * if reservation is null\r
376 * @throws Exception\r
377 * if no <code>DataHandler</code> is set, no <code>User</code>\r
378 * is logged in or logged <code>User</code> is not authorized\r
757804bb 379 */\r
66cc653d
D
380 public boolean deleteReservation(Reservation reservation) throws Exception {\r
381 if (dh == null)\r
382 throw new Exception("No DataHandler set");\r
383 if (currentUser == null)\r
384 throw new Exception("No user is logged in");\r
385 if (reservation == null)\r
386 throw new IllegalArgumentException("Reservation must be defined");\r
387 if (!reservation.getUser().equals(currentUser))\r
388 throw new Exception("You are not authorized to make this change");\r
4574ef68
M
389 return dh.deleteReservation(reservation);\r
390 }\r
66cc653d 391\r
757804bb
M
392 /**\r
393 * Returns all stored resources of the set DataHandler\r
66cc653d
D
394 * @return <code>List</code> of resources\r
395 * @throws Exception\r
396 * no <code>DataHandler</code> is set\r
757804bb 397 */\r
66cc653d
D
398 public List<Resource> getResources() throws Exception {\r
399 if (dh == null)\r
400 throw new Exception("No DataHandler set");\r
4574ef68
M
401 List<Resource> resourceList = new ArrayList<Resource>();\r
402 for (Resource resource : dh.getAllResources()) {\r
403 resourceList.add(resource.clone());\r
1702d8b9 404 }\r
4574ef68 405 return resourceList;\r
1702d8b9 406 }\r
5555b609 407}\r