SessionController added/updated
[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 if (dh == null)
111 throw new Exception("No DataHandler set");
112 if (dateRange == null)
113 throw new Exception("dateRange must be defined");
114
115 // Could be swapped out to private method for better performance (just
116 // check sc.getLoggedUser() once)
117 // and easier code maintenance
118 Map<Reservation, Boolean> returnSet = new HashMap<Reservation, Boolean>();
119 for (Reservation reservation : dh.overlaps(dateRange)) {
120 returnSet.put(reservation, reservation.getUser().getUserName().equals(sc.getLoggedUser().getUserName()));
121 }
122 return returnSet;
123 }
124
125 /**
126 * Returns the reservations of the given user
127 * @param user
128 * Owner, if <code>null</code> returns the reservations of the
129 * current logged in user
130 * @return <code>List</code> of reservation copies of the given owner
131 * @throws Exception
132 * either no <code>DataHandler</code> is set or no user is
133 * logged in while user param = null
134 */
135 public Map<Reservation, Boolean> getUserReservations_Super(User user) throws Exception {
136 if (dh == null)
137 throw new Exception("No DataHandler set");
138 if (user == null) {
139 throw new Exception("user must be defined");
140 }
141
142 // Could be swapped out to private method for better performance (just
143 // check sc.getLoggedUser() once)
144 // and easier code maintenance
145 Map<Reservation, Boolean> returnSet = new HashMap<Reservation, Boolean>();
146 for (Reservation reservation : dh.userReservations(user)) {
147 returnSet.put(reservation, reservation.getUser().getUserName().equals(sc.getLoggedUser().getUserName()));
148 }
149 return returnSet;
150 }
151
152 /**
153 * Returns the reservation or null if no reservation was found
154 * @param id
155 * reservation identifier
156 * @return <code>Reservation</code> copy of reservation with given id,
157 * <code>null</code> no reservation with this id found
158 * @throws Exception
159 * No DataHandler set or id null
160 */
161 public Reservation getReservation(UUID id) throws Exception {
162 if (dh == null)
163 throw new Exception("No DataHandler set");
164 if (id == null)
165 throw new Exception("uuid must be defined");
166 if (dh.findReservation(id) == null)
167 return null;
168 else
169 return dh.findReservation(id).clone();
170 }
171
172 /**
173 * Returns <code>Map</code> of reservation group (recurring reservations)
174 * @param groupId
175 * group identifier
176 * @return <code>Key</code> reservation, <code>Value</code> is editable
177 * @throws IllegalArgumentException
178 * groupId is null
179 * @throws Exception
180 * no <code>DataHandler</code> set
181 */
182 public Map<Reservation, Boolean> getReservationGroup(UUID groupId) throws Exception {
183 if (dh == null)
184 throw new Exception("No DataHandler set");
185 if (groupId == null)
186 throw new IllegalArgumentException("groupId has to be defined");
187
188 // Could be swapped out to private method for better performance (just
189 // check sc.getLoggedUser() once)
190 // and easier code maintenance
191 Map<Reservation, Boolean> returnSet = new HashMap<Reservation, Boolean>();
192 for (Reservation reservation : dh.findGroupReservations(groupId)) {
193 returnSet.put(reservation, reservation.getUser().getUserName().equals(sc.getLoggedUser().getUserName()));
194 }
195 return returnSet;
196 }
197
198 /**
199 * Makes a reservation in the current set DataHandler
200 * @param user
201 * user owning the reservation, if null sc.getLoggedUser() is set
202 * @param resource
203 * resource to reserve
204 * @param dateRangeList
205 * <code>List</code> of DateRanges defining when the resource is
206 * occupied
207 * @param remark
208 * public remark for the reservation
209 * @return <code>Set</code> of Reservations that overlap an already stored
210 * reservation (conflict), if not empty no reservation was saved
211 * @throws IllegalArgumentException
212 * <code>resource</code> or <code>dateRangeList</code> is null,
213 * or <code>dateRangeList</code> is empty
214 * @throws Exception
215 * thrown by <code>DataHandler</code>
216 */
217 public Set<Reservation> makeReservation(User user, Resource resource, List<DateRange> dateRangeList, String remark)
218 throws Exception {
219 if (user == null) {
220 if (sc.getLoggedUser() == null)
221 throw new IllegalArgumentException("User must be defined if no user is logged in");
222 user = sc.getLoggedUser();
223 }
224 if (resource == null)
225 throw new IllegalArgumentException("Resource must be definded");
226 if (dateRangeList == null)
227 throw new IllegalArgumentException("dateRangeList can not be null");
228 if (dateRangeList.isEmpty())
229 throw new IllegalArgumentException("dateRangeList empty");
230
231 Set<Reservation> invalidReservations = new HashSet<Reservation>();
232 UUID groupId = UUID.randomUUID();
233 for (DateRange dateRange : dateRangeList) {
234 Reservation reservation = new Reservation(groupId, user, resource, dateRange, remark);
235 if (!dh.addReservation(reservation))
236 invalidReservations.add(reservation);
237 }
238 if (invalidReservations.isEmpty()) {
239 dh.saveChanges();
240 return null;
241 } else
242 return invalidReservations;
243 }
244
245 /**
246 * Replaces the given reservation in the set DataHandler referring to the
247 * reservationid
248 * @param reservation
249 * Copy of the reservation to edit
250 * @return <code>True</code> Changes saved, <code>False</code> Changes could
251 * not be saved
252 * @throws IllegalArgumentException
253 * if reservation param is null
254 * @throws Exception
255 * if no <code>DataHandler</code> is set or the logged user is
256 * not authorized
257 */
258 public boolean changeReservation_Super(Reservation reservation) throws Exception {
259 if (dh == null)
260 throw new Exception("No DataHandler set");
261 if (reservation == null)
262 throw new IllegalArgumentException("Reservation must be defined");
263 return dh.editReservation(reservation);
264 }
265
266 /**
267 * Deletes the given reservation in the set <code>DataHandler</code>
268 * referring to the reservation id
269 * @param reservation
270 * Copy of the reservation to edit
271 * @return <code>True</code> Changes saved <code>False</code> Changes could
272 * not be saved
273 * @throws IllegalArgumentException
274 * if reservation is null
275 * @throws Exception
276 * if no <code>DataHandler</code> is set, no <code>User</code>
277 * is logged in or logged <code>User</code> is not authorized
278 */
279 public boolean deleteReservation_Super(Reservation reservation) throws Exception {
280 if (dh == null)
281 throw new Exception("No DataHandler set");
282 if (reservation == null)
283 throw new IllegalArgumentException("Reservation must be defined");
284 return dh.deleteReservation(reservation);
285 }
286 }