Merge remote-tracking branch 'origin/development'
[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 package ch.bfh.ti.srs.controller;
9
10 import java.rmi.AlreadyBoundException;
11 import java.util.ArrayList;
12 import java.util.HashMap;
13 import java.util.HashSet;
14 import java.util.List;
15 import java.util.Map;
16 import java.util.Set;
17 import java.util.UUID;
18
19 import ch.bfh.ti.daterange.DateRange;
20 import ch.bfh.ti.srs.datahandler.DataHandler;
21 import ch.bfh.ti.srs.dataschema.Reservation;
22 import ch.bfh.ti.srs.dataschema.Resource;
23 import ch.bfh.ti.srs.dataschema.User;
24
25 /**
26 * @author Michael
27 */
28 public abstract class Controller {
29 protected DataHandler dh;
30 protected SessionController sc;
31
32 /**
33 * Sets the <code>DataHandler</code> implementation for this Controller,
34 * must be set before using all other methods!
35 * @param dh
36 * DataHandler implementation (i.e. DataHandlerForExcel)
37 * @param sc
38 * SessionController which is responsable for the login
39 */
40 public Controller(DataHandler dh, SessionController sc) {
41 this.dh = dh;
42 this.sc = sc;
43 }
44
45 /**
46 * Sets the <code>DataHandler</code> implementation for this Controller,
47 * must be set before using all other methods!
48 * @param dh
49 * DataHandler implementation (i.e. DataHandlerForExcel)
50 */
51 public void setDataHandler(DataHandler dh) {
52 this.dh = dh;
53 }
54
55 /**
56 * Registers a new user to the current DataHandler
57 * @param userName
58 * unique user name
59 * @param password
60 * plain-text password
61 * @param email
62 * e-mail address
63 * @param name
64 * name
65 * @param lastname
66 * last name
67 * @throws Exception
68 * no <code>DataHandler</code> set
69 */
70 public void addUser(String userName, String password, String email, String name, String lastname) throws Exception {
71 if (dh == null)
72 throw new Exception("No DataHandler set");
73 User newUser = new User(userName, password);
74 newUser.setMail(email);
75 newUser.setName(name);
76 newUser.setLastname(lastname);
77 dh.addUser(newUser);
78 }
79
80 /**
81 * Returns all stored resources of the set DataHandler
82 * @return <code>List</code> of resources
83 * @throws Exception
84 * no <code>DataHandler</code> is set
85 */
86 public List<Resource> getResources() throws Exception {
87 if (dh == null)
88 throw new Exception("No DataHandler set");
89 List<Resource> resourceList = new ArrayList<Resource>();
90 for (Resource resource : dh.getAllResources()) {
91 resourceList.add(resource.clone());
92 }
93 return resourceList;
94 }
95
96 /**
97 * Returns all reservations overlapping the given dateRange in a
98 * <code>Map</code>
99 * @param dateRange
100 * time span
101 * @return <code>Key</code> reservation, <code>Value</code> is editable
102 * @throws Exception
103 * no <code>User</code> is logged in or no
104 * <code>DataHandler</code> is set
105 */
106 public Map<Reservation, Boolean> getReservationsInDateRange(DateRange dateRange) throws Exception {
107 // Not needed at the moment since this check is already done in
108 // UserController
109 // if (dh == null)
110 // throw new Exception("No DataHandler set");
111 if (dateRange == null)
112 throw new Exception("dateRange must be defined");
113
114 // Could be swapped out to private method for better performance (just
115 // check sc.getLoggedUser() once)
116 // and easier code maintenance
117 Map<Reservation, Boolean> returnSet = new HashMap<Reservation, Boolean>();
118 for (Reservation reservation : dh.overlaps(dateRange)) {
119 returnSet.put(reservation, reservation.getUser().getUserName().equals(sc.getLoggedUser().getUserName()));
120 }
121 return returnSet;
122 }
123
124 /**
125 * Returns the reservations of the given user
126 * @param user
127 * Owner, if <code>null</code> returns the reservations of the
128 * current logged in user
129 * @return <code>List</code> of reservation copies of the given owner
130 * @throws Exception
131 * either no <code>DataHandler</code> is set or no user is
132 * logged in while user param = null
133 */
134 public Map<Reservation, Boolean> getUserReservations_Super(User user) throws Exception {
135 // Not needed at the moment since this check is already done in
136 // UserController
137 // if (dh == null)
138 // throw new Exception("No DataHandler set");
139 // if (user == null) {
140 // throw new Exception("user must be defined");
141 // }
142
143 // Could be swapped out to private method for better performance (just
144 // check sc.getLoggedUser() once)
145 // and easier code maintenance
146 Map<Reservation, Boolean> returnSet = new HashMap<Reservation, Boolean>();
147 for (Reservation reservation : dh.userReservations(user)) {
148 returnSet.put(reservation, reservation.getUser().getUserName().equals(sc.getLoggedUser().getUserName()));
149 }
150 return returnSet;
151 }
152
153 /**
154 * Returns the reservation or null if no reservation was found
155 * @param id
156 * reservation identifier
157 * @return <code>Reservation</code> copy of reservation with given id,
158 * <code>null</code> no reservation with this id found
159 * @throws Exception
160 * No DataHandler set or id null
161 */
162 public Reservation getReservation(UUID id) throws Exception {
163 // Not needed at the moment since this check is already done in
164 // 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
223 // UserController
224 // if (user == null) {
225 // if (sc.getLoggedUser() == null)
226 // throw new IllegalArgumentException("User must be defined if no user
227 // is logged in");
228 // user = sc.getLoggedUser();
229 // }
230 if (resource == null)
231 throw new IllegalArgumentException("Resource must be definded");
232 if (dateRangeList == null)
233 throw new IllegalArgumentException("dateRangeList can not be null");
234 if (dateRangeList.isEmpty())
235 throw new IllegalArgumentException("dateRangeList empty");
236
237 Set<Reservation> invalidReservations = new HashSet<Reservation>();
238 UUID groupId = UUID.randomUUID();
239 for (DateRange dateRange : dateRangeList) {
240 Reservation reservation = new Reservation(groupId, user, resource, dateRange, remark);
241 if (!dh.addReservation(reservation))
242 invalidReservations.add(reservation);
243 }
244 if (invalidReservations.isEmpty()) {
245 dh.saveChanges();
246 return null;
247 } else
248 return invalidReservations;
249 }
250
251 /**
252 * Replaces the given reservation in the set DataHandler referring to the
253 * reservationid
254 * @param reservation
255 * Copy of the reservation to edit
256 * @return <code>True</code> Changes saved, <code>False</code> Changes could
257 * not be saved
258 * @throws IllegalArgumentException
259 * if reservation param is null
260 * @throws Exception
261 * if no <code>DataHandler</code> is set or the logged user is
262 * not authorized
263 */
264 public boolean changeReservation_Super(Reservation reservation) throws Exception {
265 // Not needed at the moment since this check is already done in
266 // UserController
267 // if (dh == null)
268 // throw new Exception("No DataHandler set");
269 // if (reservation == null)
270 // throw new IllegalArgumentException("Reservation must be defined");
271 return dh.editReservation(reservation);
272 }
273
274 /**
275 * Deletes the given reservation in the set <code>DataHandler</code>
276 * referring to the reservation id
277 * @param reservation
278 * Copy of the reservation to edit
279 * @return <code>True</code> Changes saved <code>False</code> Changes could
280 * not be saved
281 * @throws IllegalArgumentException
282 * if reservation is null
283 * @throws Exception
284 * if no <code>DataHandler</code> is set, no <code>User</code>
285 * is logged in or logged <code>User</code> is not authorized
286 */
287 public boolean deleteReservation_Super(Reservation reservation) throws Exception {
288 // Not needed at the moment since this check is already done in
289 // UserController
290 // if (dh == null)
291 // throw new Exception("No DataHandler set");
292 // if (reservation == null)
293 // throw new IllegalArgumentException("Reservation must be defined");
294 return dh.deleteReservation(reservation);
295 }
296 }