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