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