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