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