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