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