DataHandler update
[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
5555b609 31 \r
1f7b5004
M
32 public UserController(){\r
33 }\r
34 \r
1f7b5004 35 /**\r
757804bb 36 * Sets the <code>DataHandler</code> implementation for this Controller, must be set before using all other methods!\r
1f7b5004
M
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
5555b609
M
42 }\r
43 \r
1ec0e01f 44 /**\r
757804bb 45 * Tries to log in a <code>User</code> with the given credentials\r
1ec0e01f 46 * \r
757804bb
M
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
1ec0e01f 53 */\r
5555b609 54 public boolean logIn(String userName, String password) throws Exception{\r
1f7b5004 55 if(dh == null) throw new Exception("No DataHandler set");\r
1ec0e01f 56 if(currentUser != null) throw new AlreadyBoundException("A user is already logged in");\r
1f7b5004 57 User tempUser = dh.findUser(userName);\r
4574ef68
M
58 if(tempUser == null || !tempUser.checkPassword(password)) return false;\r
59 else{\r
1ec0e01f 60 currentUser = tempUser;\r
5555b609
M
61 return true;\r
62 }\r
5555b609
M
63 }\r
64 \r
1ec0e01f 65 /**\r
757804bb 66 * Logs out the current logged user\r
1ec0e01f 67 * \r
757804bb 68 * @throws Exception either no <code>DataHandler</code> is set or no user is logged in\r
1ec0e01f
M
69 */\r
70 public void logOut() throws Exception{\r
1f7b5004 71 if(dh == null) throw new Exception("No DataHandler set");\r
1ec0e01f
M
72 if(currentUser == null) throw new Exception("No user is logged in");\r
73 currentUser = null;\r
74 }\r
5555b609 75 \r
1f7b5004
M
76 /**\r
77 * Registers a new user to the current DataHandler\r
78 * \r
757804bb
M
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
1f7b5004 85 */\r
1ec0e01f 86 public void addUser(String userName, String password, String email, String name, String lastname) throws Exception{\r
1f7b5004 87 if(dh == null) throw new Exception("No DataHandler set");\r
1ec0e01f
M
88 User newUser = new User(userName,password);\r
89 newUser.setMail(email);\r
90 newUser.setName(name);\r
91 newUser.setLastname(lastname);\r
1f7b5004
M
92 dh.addUser(newUser);\r
93 }\r
94\r
1f7b5004 95 /**\r
757804bb 96 * Gets the user name of the current user\r
1f7b5004 97 * \r
757804bb
M
98 * @deprecated use <code>getLoggedUser().getUserName()</code>\r
99 * @return user name\r
100 * @throws Exception no <code>DataHandler</code> is set\r
1f7b5004 101 */\r
1702d8b9 102 @Deprecated \r
1f7b5004
M
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
1f7b5004
M
108 /**\r
109 * Gets the mail address of the current user\r
110 * \r
757804bb 111 * @deprecated use <code>getLoggedUser().getMail()</code>\r
1f7b5004 112 * @return Mail address\r
b5b8ffc1 113 * @throws Exception no user is logged in\r
1f7b5004 114 */\r
1702d8b9 115 @Deprecated\r
1f7b5004
M
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
757804bb 124 * @deprecated use <code>getLoggedUser().getName()</code>\r
1f7b5004 125 * @return First name\r
b5b8ffc1 126 * @throws Exception no user is logged in\r
1f7b5004 127 */\r
1702d8b9
M
128 @Deprecated\r
129 public String getLoggedName() throws Exception{\r
1f7b5004
M
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
757804bb 137 * @deprecated use <code>getLoggedUser().getLastName()</code>\r
1f7b5004 138 * @return Last name\r
b5b8ffc1 139 * @throws Exception no user is logged in\r
1f7b5004 140 */\r
1702d8b9
M
141 @Deprecated\r
142 public String getLoggedLastname() throws Exception{\r
1f7b5004
M
143 if(currentUser == null) throw new Exception("No user is logged in");\r
144 return currentUser.getLastname();\r
1ec0e01f 145 }\r
13313998 146 \r
757804bb
M
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
4574ef68 152 public User getLoggedUser(){\r
a08b0437 153 if(currentUser == null) return null;\r
4574ef68
M
154 return currentUser;\r
155 }\r
156 \r
757804bb
M
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
4574ef68
M
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
a08b0437
M
167 }\r
168 \r
13313998 169 /**\r
757804bb 170 * Returns all reservations overlapping the given dateRange in a <code>Map</code>\r
13313998 171 * \r
757804bb
M
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
13313998 176 */\r
4574ef68 177 public Map<Reservation, Boolean> getReservationsInDateRange(DateRange dateRange) throws Exception{\r
13313998
M
178 if(dh == null) throw new Exception("No DataHandler set");\r
179 if(dateRange == null) throw new Exception("dateRange must be defined");\r
4574ef68
M
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
13313998
M
188 }\r
189 \r
190 /**\r
191 * Returns the reservation or null if no reservation was found\r
192 * \r
757804bb
M
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
13313998
M
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
4574ef68
M
201 if(dh.findReservation(id) == null) return null;\r
202 else return dh.findReservation(id).clone();\r
13313998 203 }\r
b5b8ffc1
M
204 \r
205 /**\r
757804bb 206 * Returns the reservations of the given user\r
b5b8ffc1 207 * \r
757804bb
M
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
b5b8ffc1 211 */\r
4574ef68 212 public Map<Reservation, Boolean> getUserReservations(User user) throws Exception{\r
b5b8ffc1
M
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
4574ef68
M
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
b5b8ffc1
M
226 }\r
227 \r
228 /**\r
757804bb
M
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
b5b8ffc1 236 */\r
4574ef68 237 public Map<Reservation, Boolean> getReservationGroup(UUID groupId) throws Exception{\r
b5b8ffc1 238 if(dh == null) throw new Exception("No DataHandler set");\r
757804bb 239 if(groupId == null) throw new IllegalArgumentException("groupId has to be defined");\r
4574ef68
M
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
b5b8ffc1 248 }\r
4574ef68 249\r
b5b8ffc1 250 /**\r
757804bb
M
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
b5b8ffc1 260 */\r
4574ef68 261 public Set<Reservation> makeReservation(User user, Resource resource, List<DateRange> dateRangeList, String remark) throws Exception\r
1702d8b9
M
262 {\r
263 if(user == null) user = currentUser;\r
757804bb
M
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
1702d8b9 267 \r
4574ef68
M
268 Set<Reservation> invalidReservations = new HashSet<Reservation>();\r
269 UUID groupId = UUID.randomUUID();\r
1702d8b9 270 for (DateRange dateRange : dateRangeList) {\r
4574ef68
M
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
757804bb
M
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
4574ef68
M
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
757804bb
M
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
4574ef68
M
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
757804bb
M
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
4574ef68
M
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
1702d8b9 330 }\r
4574ef68 331 return resourceList;\r
1702d8b9 332 }\r
5555b609 333}\r