DataHandler update
[staff/due1/sed-hs15-srs-purple.git] / src / main / java / ch / bfh / ti / srs / controller / UserController.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 Grossen
27 */
28 public class UserController {
29 private User currentUser;
30 private DataHandler dh;
31
32 public UserController(){
33 }
34
35 /**
36 * Sets the <code>DataHandler</code> implementation for this Controller, must be set before using all other methods!
37 *
38 * @param dh DataHandler implementation (i.e. DataHandlerForExcel)
39 */
40 public void setDataHandler(DataHandler dh){
41 this.dh = dh;
42 }
43
44 /**
45 * Tries to log in a <code>User</code> with the given credentials
46 *
47 * @param userName user name
48 * @param password the stored password for the given user name
49 * @return <code>True</code> if the user could log in successfully,
50 * <code>False</code> either the username or the password was not correct.
51 * @throws Exception no <code>DataHandler</code> set
52 * @throws AlreadyBoundException a <code>User</code> is already logged in
53 */
54 public boolean logIn(String userName, String password) throws Exception{
55 if(dh == null) throw new Exception("No DataHandler set");
56 if(currentUser != null) throw new AlreadyBoundException("A user is already logged in");
57 User tempUser = dh.findUser(userName);
58 if(tempUser == null || !tempUser.checkPassword(password)) return false;
59 else{
60 currentUser = tempUser;
61 return true;
62 }
63 }
64
65 /**
66 * Logs out the current logged user
67 *
68 * @throws Exception either no <code>DataHandler</code> is set or no user is logged in
69 */
70 public void logOut() throws Exception{
71 if(dh == null) throw new Exception("No DataHandler set");
72 if(currentUser == null) throw new Exception("No user is logged in");
73 currentUser = null;
74 }
75
76 /**
77 * Registers a new user to the current DataHandler
78 *
79 * @param userName unique user name
80 * @param password plain-text password
81 * @param email e-mail address
82 * @param name name
83 * @param lastname last name
84 * @throws Exception no <code>DataHandler</code> set
85 */
86 public void addUser(String userName, String password, String email, String name, String lastname) throws Exception{
87 if(dh == null) throw new Exception("No DataHandler set");
88 User newUser = new User(userName,password);
89 newUser.setMail(email);
90 newUser.setName(name);
91 newUser.setLastname(lastname);
92 dh.addUser(newUser);
93 }
94
95 /**
96 * Gets the user name of the current user
97 *
98 * @deprecated use <code>getLoggedUser().getUserName()</code>
99 * @return user name
100 * @throws Exception no <code>DataHandler</code> is set
101 */
102 @Deprecated
103 public String getLoggedUserName() throws Exception{
104 if(currentUser == null) throw new Exception("No user is logged in");
105 return currentUser.getUserName();
106 }
107
108 /**
109 * Gets the mail address of the current user
110 *
111 * @deprecated use <code>getLoggedUser().getMail()</code>
112 * @return Mail address
113 * @throws Exception no user is logged in
114 */
115 @Deprecated
116 public String getLoggedMail() throws Exception{
117 if(currentUser == null) throw new Exception("No user is logged in");
118 return currentUser.getMail();
119 }
120
121 /**
122 * Gets the first name of the current user
123 *
124 * @deprecated use <code>getLoggedUser().getName()</code>
125 * @return First name
126 * @throws Exception no user is logged in
127 */
128 @Deprecated
129 public String getLoggedName() throws Exception{
130 if(currentUser == null) throw new Exception("No user is logged in");
131 return currentUser.getName();
132 }
133
134 /**
135 * Gets the last name of the current user
136 *
137 * @deprecated use <code>getLoggedUser().getLastName()</code>
138 * @return Last name
139 * @throws Exception no user is logged in
140 */
141 @Deprecated
142 public String getLoggedLastname() throws Exception{
143 if(currentUser == null) throw new Exception("No user is logged in");
144 return currentUser.getLastname();
145 }
146
147 /**
148 * Gets the current logged <code>User</code>
149 *
150 * @return current <code>User</code>, or <code>null</code> if no <code>User</code> is logged in
151 */
152 public User getLoggedUser(){
153 if(currentUser == null) return null;
154 return currentUser;
155 }
156
157 /**
158 * Searches the set DataHandler for a specific <code>User</code>
159 *
160 * @param userName <code>User</code>
161 * @return clone of <code>User</code>
162 * @throws Exception No <code>DataHandler</code> is set
163 */
164 public User getUser(String userName) throws Exception{
165 if(dh == null) throw new Exception("No DataHandler set");
166 return dh.findUser(userName).clone();
167 }
168
169 /**
170 * Returns all reservations overlapping the given dateRange in a <code>Map</code>
171 *
172 * @param dateRange time span
173 * @return <code>Key</code> reservation,
174 * <code>Value</code> is editable
175 * @throws Exception no <code>User</code> is logged in or no <code>DataHandler</code> is set
176 */
177 public Map<Reservation, Boolean> getReservationsInDateRange(DateRange dateRange) throws Exception{
178 if(dh == null) throw new Exception("No DataHandler set");
179 if(dateRange == null) throw new Exception("dateRange must be defined");
180
181 // Could be swapped out to private method for better performance (just check currentUser once)
182 // and easier code maintenance
183 Map<Reservation, Boolean> returnSet = new HashMap<Reservation, Boolean>();
184 for (Reservation reservation : dh.overlaps(dateRange)) {
185 returnSet.put(reservation, reservation.getUser().equals(currentUser));
186 }
187 return returnSet;
188 }
189
190 /**
191 * Returns the reservation or null if no reservation was found
192 *
193 * @param id reservation identifier
194 * @return <code>Reservation</code> copy of reservation with given id,
195 * <code>null</code> no reservation with this id found
196 * @throws Exception No DataHandler set or id null
197 */
198 public Reservation getReservation(UUID id) throws Exception{
199 if(dh == null) throw new Exception("No DataHandler set");
200 if(id == null) throw new Exception("uuid must be defined");
201 if(dh.findReservation(id) == null) return null;
202 else return dh.findReservation(id).clone();
203 }
204
205 /**
206 * Returns the reservations of the given user
207 *
208 * @param user Owner, if <code>null</code> returns the reservations of the current logged in user
209 * @return <code>List</code> of reservation copies of the given owner
210 * @throws Exception either no <code>DataHandler</code> is set or no user is logged in while user param = null
211 */
212 public Map<Reservation, Boolean> getUserReservations(User user) throws Exception{
213 if(dh == null) throw new Exception("No DataHandler set");
214 if(user == null){
215 if(currentUser == null) throw new Exception("user must be defined, if no user is logged in");
216 else user = currentUser;
217 }
218
219 // Could be swapped out to private method for better performance (just check currentUser once)
220 // and easier code maintenance
221 Map<Reservation, Boolean> returnSet = new HashMap<Reservation, Boolean>();
222 for (Reservation reservation : dh.userReservations(user)) {
223 returnSet.put(reservation, reservation.getUser().equals(currentUser));
224 }
225 return returnSet;
226 }
227
228 /**
229 * Returns <code>Map</code> of reservation group (recurring reservations)
230 *
231 * @param groupId group identifier
232 * @return <code>Key</code> reservation,
233 * <code>Value</code> is editable
234 * @throws IllegalArgumentException groupId is null
235 * @throws Exception no <code>DataHandler</code> set
236 */
237 public Map<Reservation, Boolean> getReservationGroup(UUID groupId) throws Exception{
238 if(dh == null) throw new Exception("No DataHandler set");
239 if(groupId == null) throw new IllegalArgumentException("groupId has to be defined");
240
241 // Could be swapped out to private method for better performance (just check currentUser once)
242 // and easier code maintenance
243 Map<Reservation, Boolean> returnSet = new HashMap<Reservation, Boolean>();
244 for (Reservation reservation : dh.findGroupReservations(groupId)) {
245 returnSet.put(reservation, reservation.getUser().equals(currentUser));
246 }
247 return returnSet;
248 }
249
250 /**
251 * Makes a reservation in the current set DataHandler
252 *
253 * @param user user owning the reservation
254 * @param resource resource to reserve
255 * @param dateRangeList <code>List</code> of DateRanges defining when the resource is occupied
256 * @param remark public remark for the reservation
257 * @return <code>Set</code> of Reservations that overlap an already stored reservation (conflict), if not empty no reservation was saved
258 * @throws IllegalArgumentException <code>resource</code> or <code>dateRangeList</code> is null, or <code>dateRangeList</code> is empty
259 * @throws Exception thrown by <code>DataHandler</code>
260 */
261 public Set<Reservation> makeReservation(User user, Resource resource, List<DateRange> dateRangeList, String remark) throws Exception
262 {
263 if(user == null) user = currentUser;
264 if(resource == null) throw new IllegalArgumentException("Resource must be definded");
265 if(dateRangeList == null) throw new IllegalArgumentException("dateRangeList can not be null");
266 if(dateRangeList.isEmpty()) throw new IllegalArgumentException("dateRangeList empty");
267
268 Set<Reservation> invalidReservations = new HashSet<Reservation>();
269 UUID groupId = UUID.randomUUID();
270 for (DateRange dateRange : dateRangeList) {
271 Reservation reservation = new Reservation(groupId, user, resource, dateRange, remark);
272 if(!dh.addReservation(reservation)) invalidReservations.add(reservation);
273 }
274 if(invalidReservations.isEmpty())
275 {
276 dh.saveChanges();
277 return null;
278 }
279 else return invalidReservations;
280 }
281
282 /**
283 * Replaces the given reservation in the set DataHandler referring to the reservationid
284 *
285 * @param reservation Copy of the reservation to edit
286 * @return <code>True</code> Changes saved,
287 * <code>False</code> Changes could not be saved
288 * @throws IllegalArgumentException if reservation param is null
289 * @throws Exception if no <code>DataHandler</code> is set or the logged user is not authorized
290 */
291 public boolean changeReservation(Reservation reservation) throws Exception
292 {
293 if(dh == null) throw new Exception("No DataHandler set");
294 if(currentUser == null) throw new Exception("No user is logged in");
295 if(reservation == null) throw new IllegalArgumentException("Reservation must be defined");
296 if(!reservation.getUser().equals(currentUser)) throw new Exception("You are not authorized to make this change");
297 return dh.editReservation(reservation);
298 }
299
300 /**
301 * Deletes the given reservation in the set <code>DataHandler</code> referring to the reservation id
302 *
303 * @param reservation Copy of the reservation to edit
304 * @return <code>True</code> Changes saved
305 * <code>False</code> Changes could not be saved
306 * @throws IllegalArgumentException if reservation is null
307 * @throws Exception if no <code>DataHandler</code> is set, no <code>User</code> is logged in or logged <code>User</code> is not authorized
308 */
309 public boolean deleteReservation(Reservation reservation) throws Exception
310 {
311 if(dh == null) throw new Exception("No DataHandler set");
312 if(currentUser == null) throw new Exception("No user is logged in");
313 if(reservation == null) throw new IllegalArgumentException("Reservation must be defined");
314 if(!reservation.getUser().equals(currentUser)) throw new Exception("You are not authorized to make this change");
315 return dh.deleteReservation(reservation);
316 }
317
318
319 /**
320 * Returns all stored resources of the set DataHandler
321 *
322 * @return <code>List</code> of resources
323 * @throws Exception no <code>DataHandler</code> is set
324 */
325 public List<Resource> getResources() throws Exception{
326 if(dh == null) throw new Exception("No DataHandler set");
327 List<Resource> resourceList = new ArrayList<Resource>();
328 for (Resource resource : dh.getAllResources()) {
329 resourceList.add(resource.clone());
330 }
331 return resourceList;
332 }
333 }