UserControllerTest updated for better test coverage on abstract class
[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
b07a6f5c
M
110// Not needed at the moment since this check is already done in UserController\r
111// if (dh == null)\r
112// throw new Exception("No DataHandler set");\r
da277fcf
M
113 if (dateRange == null)\r
114 throw new Exception("dateRange must be defined");\r
115\r
116 // Could be swapped out to private method for better performance (just\r
f0b6a43e 117 // check sc.getLoggedUser() once)\r
da277fcf
M
118 // and easier code maintenance\r
119 Map<Reservation, Boolean> returnSet = new HashMap<Reservation, Boolean>();\r
120 for (Reservation reservation : dh.overlaps(dateRange)) {\r
f0b6a43e 121 returnSet.put(reservation, reservation.getUser().getUserName().equals(sc.getLoggedUser().getUserName()));\r
da277fcf
M
122 }\r
123 return returnSet;\r
124 }\r
125\r
126 /**\r
127 * Returns the reservations of the given user\r
128 * @param user\r
129 * Owner, if <code>null</code> returns the reservations of the\r
130 * current logged in user\r
131 * @return <code>List</code> of reservation copies of the given owner\r
132 * @throws Exception\r
133 * either no <code>DataHandler</code> is set or no user is\r
134 * logged in while user param = null\r
135 */\r
136 public Map<Reservation, Boolean> getUserReservations_Super(User user) throws Exception {\r
b07a6f5c
M
137// Not needed at the moment since this check is already done in UserController\r
138// if (dh == null)\r
139// throw new Exception("No DataHandler set");\r
140// if (user == null) {\r
141// throw new Exception("user must be defined");\r
142// }\r
da277fcf
M
143\r
144 // Could be swapped out to private method for better performance (just\r
f0b6a43e 145 // check sc.getLoggedUser() once)\r
da277fcf
M
146 // and easier code maintenance\r
147 Map<Reservation, Boolean> returnSet = new HashMap<Reservation, Boolean>();\r
148 for (Reservation reservation : dh.userReservations(user)) {\r
f0b6a43e 149 returnSet.put(reservation, reservation.getUser().getUserName().equals(sc.getLoggedUser().getUserName()));\r
da277fcf
M
150 }\r
151 return returnSet;\r
152 }\r
153\r
154 /**\r
155 * Returns the reservation or null if no reservation was found\r
156 * @param id\r
157 * reservation identifier\r
158 * @return <code>Reservation</code> copy of reservation with given id,\r
159 * <code>null</code> no reservation with this id found\r
160 * @throws Exception\r
161 * No DataHandler set or id null\r
162 */\r
163 public Reservation getReservation(UUID id) throws Exception {\r
b07a6f5c
M
164// Not needed at the moment since this check is already done in 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
b07a6f5c
M
222// Not needed at the moment since this check is already done in UserController\r
223// if (user == null) {\r
224// if (sc.getLoggedUser() == null)\r
225// throw new IllegalArgumentException("User must be defined if no user is logged in");\r
226// user = sc.getLoggedUser();\r
227// }\r
da277fcf
M
228 if (resource == null)\r
229 throw new IllegalArgumentException("Resource must be definded");\r
230 if (dateRangeList == null)\r
231 throw new IllegalArgumentException("dateRangeList can not be null");\r
232 if (dateRangeList.isEmpty())\r
233 throw new IllegalArgumentException("dateRangeList empty");\r
234\r
235 Set<Reservation> invalidReservations = new HashSet<Reservation>();\r
236 UUID groupId = UUID.randomUUID();\r
237 for (DateRange dateRange : dateRangeList) {\r
238 Reservation reservation = new Reservation(groupId, user, resource, dateRange, remark);\r
239 if (!dh.addReservation(reservation))\r
240 invalidReservations.add(reservation);\r
241 }\r
242 if (invalidReservations.isEmpty()) {\r
243 dh.saveChanges();\r
244 return null;\r
245 } else\r
246 return invalidReservations;\r
247 }\r
248\r
249 /**\r
250 * Replaces the given reservation in the set DataHandler referring to the\r
251 * reservationid\r
252 * @param reservation\r
253 * Copy of the reservation to edit\r
254 * @return <code>True</code> Changes saved, <code>False</code> Changes could\r
255 * not be saved\r
256 * @throws IllegalArgumentException\r
257 * if reservation param is null\r
258 * @throws Exception\r
259 * if no <code>DataHandler</code> is set or the logged user is\r
260 * not authorized\r
261 */\r
262 public boolean changeReservation_Super(Reservation reservation) throws Exception {\r
b07a6f5c
M
263// Not needed at the moment since this check is already done in UserController\r
264// if (dh == null)\r
265// throw new Exception("No DataHandler set");\r
266// if (reservation == null)\r
267// throw new IllegalArgumentException("Reservation must be defined");\r
da277fcf
M
268 return dh.editReservation(reservation);\r
269 }\r
270 \r
271 /**\r
272 * Deletes the given reservation in the set <code>DataHandler</code>\r
273 * referring to the reservation id\r
274 * @param reservation\r
275 * Copy of the reservation to edit\r
276 * @return <code>True</code> Changes saved <code>False</code> Changes could\r
277 * not be saved\r
278 * @throws IllegalArgumentException\r
279 * if reservation is null\r
280 * @throws Exception\r
281 * if no <code>DataHandler</code> is set, no <code>User</code>\r
282 * is logged in or logged <code>User</code> is not authorized\r
283 */\r
284 public boolean deleteReservation_Super(Reservation reservation) throws Exception {\r
b07a6f5c
M
285// Not needed at the moment since this check is already done in UserController\r
286// if (dh == null)\r
287// throw new Exception("No DataHandler set");\r
288// if (reservation == null)\r
289// throw new IllegalArgumentException("Reservation must be defined");\r
da277fcf
M
290 return dh.deleteReservation(reservation);\r
291 }\r
292}\r