aeceff9b36451feacaae15955beb611091efc266
[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 DataHandler 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 logIn a user with the given credentials
46 *
47 * @param userName username
48 * @param password the stored password for the given username
49 * @return <code>true</code> if the user could log in successfully;
50 * <code>false</code> eighter the username or the password was not correct.
51 * @throws Exception no datahandler set or thrown by datahandler
52 * @throws AlreadyBoundException a user 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 user, throws exception if no user is logged in
67 *
68 * @throws Exception eighter no datahandler 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 Username
80 * @param password Plaintext password
81 * @param email E-Mail address
82 * @param name Name
83 * @param lastname Lastname
84 * @throws Exception eighter no datahandler is set or user could not be added to the userlist
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 * @deprecated use getLoggedUser
97 * Gets the username of the current user, use getLoggedUser
98 *
99 * @return Username unique username
100 * @throws Exception no datahandler 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 * @deprecated use getLoggedUser
110 * Gets the mail address of the current user
111 *
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 * @deprecated use getLoggedUser
123 * Gets the first name of the current user
124 *
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 * @deprecated use getLoggedUser
136 * Gets the last name of the current user
137 *
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 public User getLoggedUser(){
148 if(currentUser == null) return null;
149 return currentUser;
150 }
151
152 public User getUser(String userName) throws Exception{
153 if(dh == null) throw new Exception("No DataHandler set");
154 return dh.findUser(userName).clone();
155 }
156
157 /**
158 * Returns all reservations overlapping the given dateRange in a List
159 *
160 * @param dateRange
161 * @return <code>Map</code> if list is empty, no overlapping reservation could be found
162 * @throws Exception no user is logged in
163 */
164 public Map<Reservation, Boolean> getReservationsInDateRange(DateRange dateRange) throws Exception{
165 if(dh == null) throw new Exception("No DataHandler set");
166 if(dateRange == null) throw new Exception("dateRange must be defined");
167
168 // Could be swapped out to private method for better performance (just check currentUser once)
169 // and easier code maintenance
170 Map<Reservation, Boolean> returnSet = new HashMap<Reservation, Boolean>();
171 for (Reservation reservation : dh.overlaps(dateRange)) {
172 returnSet.put(reservation, reservation.getUser().equals(currentUser));
173 }
174 return returnSet;
175 }
176
177 /**
178 * Returns the reservation or null if no reservation was found
179 *
180 * @param id universal unique idendifier
181 * @return <code>Reservation</code> copy of reservation with given id
182 * <code>null</code> no reservation with this id found
183 * @throws Exception
184 */
185 public Reservation getReservation(UUID id) throws Exception{
186 if(dh == null) throw new Exception("No DataHandler set");
187 if(id == null) throw new Exception("uuid must be defined");
188 if(dh.findReservation(id) == null) return null;
189 else return dh.findReservation(id).clone();
190 }
191
192 /**
193 * Returns the reservations of the given user, if null returns the reservations of the current logged in user
194 *
195 * @param user user you want the reservations of
196 * @return List of reservations copies of a specific user
197 * @throws Exception eighter no datahandler is set or no user is logged in while user param = null
198 */
199 public Map<Reservation, Boolean> getUserReservations(User user) throws Exception{
200 if(dh == null) throw new Exception("No DataHandler set");
201 if(user == null){
202 if(currentUser == null) throw new Exception("user must be defined, if no user is logged in");
203 else user = currentUser;
204 }
205
206 // Could be swapped out to private method for better performance (just check currentUser once)
207 // and easier code maintenance
208 Map<Reservation, Boolean> returnSet = new HashMap<Reservation, Boolean>();
209 for (Reservation reservation : dh.userReservations(user)) {
210 returnSet.put(reservation, reservation.getUser().equals(currentUser));
211 }
212 return returnSet;
213 }
214
215 /**
216 * @param groupId groupId of the searched reservations group, throws Exception if groupId is null
217 * @return <code>List</code> reservation copies of a group
218 * @throws Exception eighter no datahandler is set or groupId is null
219 */
220 public Map<Reservation, Boolean> getReservationGroup(UUID groupId) throws Exception{
221 if(dh == null) throw new Exception("No DataHandler set");
222 if(groupId == null) throw new Exception("groupId has to be defined");
223
224 // Could be swapped out to private method for better performance (just check currentUser once)
225 // and easier code maintenance
226 Map<Reservation, Boolean> returnSet = new HashMap<Reservation, Boolean>();
227 for (Reservation reservation : dh.findGroupReservations(groupId)) {
228 returnSet.put(reservation, reservation.getUser().equals(currentUser));
229 }
230 return returnSet;
231 }
232
233
234 /**
235 * @param user
236 * @param resource
237 * @param dateRangeList
238 * @param remark
239 * @return
240 * @throws Exception
241 */
242 public Set<Reservation> makeReservation(User user, Resource resource, List<DateRange> dateRangeList, String remark) throws Exception
243 {
244 if(user == null) user = currentUser;
245 if(resource == null) throw new Exception("Resource must be definded");
246 if(dateRangeList == null) throw new Exception("dateRangeList can not be null");
247 if(dateRangeList.isEmpty()) throw new Exception("dateRangeList empty");
248
249 Set<Reservation> invalidReservations = new HashSet<Reservation>();
250 UUID groupId = UUID.randomUUID();
251 for (DateRange dateRange : dateRangeList) {
252 Reservation reservation = new Reservation(groupId, user, resource, dateRange, remark);
253 if(!dh.addReservation(reservation)) invalidReservations.add(reservation);
254 }
255 if(invalidReservations.isEmpty())
256 {
257 dh.saveChanges();
258 return null;
259 }
260 else return invalidReservations;
261 }
262
263 public boolean changeReservation(Reservation reservation) throws Exception
264 {
265 if(dh == null) throw new Exception("No DataHandler set");
266 if(currentUser == null) throw new Exception("No user is logged in");
267 if(reservation == null) throw new IllegalArgumentException("Reservation must be defined");
268 if(!reservation.getUser().equals(currentUser)) throw new Exception("You are not authorized to make this change");
269 return dh.editReservation(reservation);
270 }
271
272 public boolean deleteReservation(Reservation reservation) throws Exception
273 {
274 if(dh == null) throw new Exception("No DataHandler set");
275 if(currentUser == null) throw new Exception("No user is logged in");
276 if(reservation == null) throw new IllegalArgumentException("Reservation must be defined");
277 if(!reservation.getUser().equals(currentUser)) throw new Exception("You are not authorized to make this change");
278 return dh.deleteReservation(reservation);
279 }
280
281 public List<Resource> getResources() throws Exception{
282 if(dh == null) throw new Exception("No DataHandler set");
283 List<Resource> resourceList = new ArrayList<Resource>();
284 for (Resource resource : dh.getAllResources()) {
285 resourceList.add(resource.clone());
286 }
287 return resourceList;
288 }
289 }