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