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