getLoggedUserName, getLoggedMail, getLoggedName, getLoggedLastname set
[staff/due1/sed-hs15-srs-purple.git] / src / main / java / ch / bfh / ti / srs / controller / UserController.java
CommitLineData
5555b609
M
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
e3535133 8package ch.bfh.ti.srs.controller;\r
5555b609 9\r
1ec0e01f 10import java.rmi.AlreadyBoundException;\r
b5b8ffc1 11import java.util.ArrayList;\r
13313998
M
12import java.util.List;\r
13import java.util.UUID;\r
1ec0e01f 14\r
13313998 15import ch.bfh.ti.daterange.DateRange;\r
e3535133 16import ch.bfh.ti.srs.datahandler.DataHandler;\r
13313998 17import ch.bfh.ti.srs.dataschema.Reservation;\r
1702d8b9 18import ch.bfh.ti.srs.dataschema.Resource;\r
5c0ee08b 19import ch.bfh.ti.srs.dataschema.User;\r
e3535133 20\r
1ec0e01f
M
21/**\r
22 * @author Michael Grossen\r
23 */\r
5555b609 24public class UserController {\r
1e4d6813
M
25 private User currentUser;\r
26 private DataHandler dh;\r
5555b609 27 \r
1f7b5004
M
28 public UserController(){\r
29 }\r
30 \r
1f7b5004
M
31 /**\r
32 * Sets the DataHandler implementation for this Controller, must be set before using all other methods!\r
33 * \r
34 * @param dh DataHandler implementation (i.e. DataHandlerForExcel)\r
35 */\r
36 public void setDataHandler(DataHandler dh){\r
37 this.dh = dh;\r
5555b609
M
38 }\r
39 \r
1ec0e01f
M
40 /**\r
41 * Tries to logIn a user with the given credentials\r
42 * \r
13313998
M
43 * @param userName username\r
44 * @param password the stored password for the given username\r
45 * @return <code>true</code> if the user could log in successfully;\r
46 * <code>false</code> eighter the username or the password was not correct.\r
47 * @throws Exception no datahandler set or thrown by datahandler\r
48 * @throws AlreadyBoundException a user is already logged in\r
1ec0e01f 49 */\r
5555b609 50 public boolean logIn(String userName, String password) throws Exception{\r
1f7b5004 51 if(dh == null) throw new Exception("No DataHandler set");\r
1ec0e01f 52 if(currentUser != null) throw new AlreadyBoundException("A user is already logged in");\r
1f7b5004 53 User tempUser = dh.findUser(userName);\r
5555b609
M
54 if(tempUser == null) return false;\r
55 if(tempUser.checkPassword(password)){\r
1ec0e01f 56 currentUser = tempUser;\r
5555b609
M
57 return true;\r
58 }\r
59 else return false;\r
60 }\r
61 \r
1ec0e01f
M
62 /**\r
63 * Logs out the current user, throws exception if no user is logged in\r
64 * \r
b5b8ffc1 65 * @throws Exception eighter no datahandler is set or no user is logged in\r
1ec0e01f
M
66 */\r
67 public void logOut() throws Exception{\r
1f7b5004 68 if(dh == null) throw new Exception("No DataHandler set");\r
1ec0e01f
M
69 if(currentUser == null) throw new Exception("No user is logged in");\r
70 currentUser = null;\r
71 }\r
5555b609 72 \r
1f7b5004
M
73 /**\r
74 * Registers a new user to the current DataHandler\r
75 * \r
76 * @param userName Username\r
77 * @param password Plaintext password\r
78 * @param email E-Mail address\r
79 * @param name Name\r
80 * @param lastname Lastname\r
b5b8ffc1 81 * @throws Exception eighter no datahandler is set or user could not be added to the userlist\r
1f7b5004 82 */\r
1ec0e01f 83 public void addUser(String userName, String password, String email, String name, String lastname) throws Exception{\r
1f7b5004 84 if(dh == null) throw new Exception("No DataHandler set");\r
1ec0e01f
M
85 User newUser = new User(userName,password);\r
86 newUser.setMail(email);\r
87 newUser.setName(name);\r
88 newUser.setLastname(lastname);\r
1f7b5004
M
89 dh.addUser(newUser);\r
90 }\r
91\r
1f7b5004 92 /**\r
1702d8b9
M
93 * @deprecated use getLoggedUser\r
94 * Gets the username of the current user, use getLoggedUser\r
1f7b5004 95 * \r
b5b8ffc1
M
96 * @return Username unique username\r
97 * @throws Exception no datahandler is set\r
1f7b5004 98 */\r
1702d8b9 99 @Deprecated \r
1f7b5004
M
100 public String getLoggedUserName() throws Exception{\r
101 if(currentUser == null) throw new Exception("No user is logged in");\r
102 return currentUser.getUserName();\r
103 }\r
104\r
1f7b5004 105 /**\r
1702d8b9 106 * @deprecated use getLoggedUser\r
1f7b5004
M
107 * Gets the mail address of the current user\r
108 * \r
109 * @return Mail address\r
b5b8ffc1 110 * @throws Exception no user is logged in\r
1f7b5004 111 */\r
1702d8b9 112 @Deprecated\r
1f7b5004
M
113 public String getLoggedMail() throws Exception{\r
114 if(currentUser == null) throw new Exception("No user is logged in");\r
115 return currentUser.getMail();\r
116 }\r
117\r
118 /**\r
1702d8b9 119 * @deprecated use getLoggedUser\r
1f7b5004
M
120 * Gets the first name of the current user\r
121 * \r
122 * @return First name\r
b5b8ffc1 123 * @throws Exception no user is logged in\r
1f7b5004 124 */\r
1702d8b9
M
125 @Deprecated\r
126 public String getLoggedName() throws Exception{\r
1f7b5004
M
127 if(currentUser == null) throw new Exception("No user is logged in");\r
128 return currentUser.getName();\r
129 }\r
130\r
131 /**\r
1702d8b9 132 * @deprecated use getLoggedUser\r
1f7b5004
M
133 * Gets the last name of the current user\r
134 * \r
135 * @return Last name\r
b5b8ffc1 136 * @throws Exception no user is logged in\r
1f7b5004 137 */\r
1702d8b9
M
138 @Deprecated\r
139 public String getLoggedLastname() throws Exception{\r
1f7b5004
M
140 if(currentUser == null) throw new Exception("No user is logged in");\r
141 return currentUser.getLastname();\r
1ec0e01f 142 }\r
13313998
M
143 \r
144 /**\r
145 * Returns all reservations overlapping the given dateRange in a List\r
146 * \r
147 * @param dateRange\r
b5b8ffc1
M
148 * @return <code>List</code> if list is empty, no overlapping reservation could be found\r
149 * @throws Exception no user is logged in\r
13313998
M
150 */\r
151 public List<Reservation> getReservationsInDateRange(DateRange dateRange) throws Exception{\r
152 if(dh == null) throw new Exception("No DataHandler set");\r
153 if(dateRange == null) throw new Exception("dateRange must be defined");\r
154 return dh.overlaps(dateRange);\r
155 }\r
156 \r
157 /**\r
158 * Returns the reservation or null if no reservation was found\r
159 * \r
b5b8ffc1
M
160 * @param id universal unique idendifier\r
161 * @return <code>reservation</code> reservation with given id\r
162 * <code>null</code> no reservation with this id found\r
13313998
M
163 * @throws Exception\r
164 */\r
165 public Reservation getReservation(UUID id) throws Exception{\r
166 if(dh == null) throw new Exception("No DataHandler set");\r
167 if(id == null) throw new Exception("uuid must be defined");\r
168 return dh.findReservation(id);\r
169 }\r
b5b8ffc1
M
170 \r
171 /**\r
172 * Returns the reservations of the given user, if null returns the reservations of the current logged in user\r
173 * \r
174 * @param user user you want the reservations of\r
175 * @return List of reservations of a specific user\r
176 * @throws Exception eighter no datahandler is set or no user is logged in while user param = null\r
177 */\r
178 public List<Reservation> getUserReservations(User user) throws Exception{\r
179 if(dh == null) throw new Exception("No DataHandler set");\r
180 if(user == null){\r
181 if(currentUser == null) throw new Exception("user must be defined, if no user is logged in");\r
182 else user = currentUser;\r
183 }\r
184 return dh.userReservations(user);\r
185 }\r
186 \r
187 /**\r
188 * @param groupId groupId of the searched reservations group, throws Exception if groupId is null\r
189 * @return <code>List</code> reservation group\r
190 * @throws Exception eighter no datahandler is set or groupId is null\r
191 */\r
192 public List<Reservation> getReservationGroup(UUID groupId) throws Exception{\r
193 if(dh == null) throw new Exception("No DataHandler set");\r
194 if(groupId == null) throw new Exception("groupId has to be defined");\r
195 return dh.findGroupReservations(groupId);\r
196 }\r
197 \r
198 /**\r
199 * @param reservation one of the reservations of the group you are looking for, returns a List with just the given Reservation if no groupId is set\r
200 * @return <code>List</code> reservation group\r
201 * @throws Exception no datahandler is set\r
202 */\r
203 public List<Reservation> getReservationGroup(Reservation reservation) throws Exception{\r
204 if(dh == null) throw new Exception("No DataHandler set");\r
205 if(reservation.getGroupId() == null){\r
206 ArrayList<Reservation> grpList = new ArrayList<>();\r
207 grpList.add(reservation);\r
208 return grpList;\r
209 }\r
210 return dh.findGroupReservations(reservation.getGroupId());\r
211 }\r
1702d8b9
M
212 \r
213 public void makeReservation(User user, Resource resource, List<DateRange> dateRangeList, String remark) throws Exception\r
214 {\r
215 if(user == null) user = currentUser;\r
216 if(resource == null) throw new Exception("Resource must be definded");\r
217 if(dateRangeList == null) throw new Exception("dateRangeList can not be null");\r
218 if(dateRangeList.isEmpty()) throw new Exception("dateRangeList empty");\r
219 \r
220 UUID groupId = null;\r
221 if(dateRangeList.size() > 1) groupId = UUID.randomUUID();\r
222 for (DateRange dateRange : dateRangeList) {\r
223 dh.addReservation(new Reservation(groupId, user, resource, dateRange, remark));\r
224 }\r
225 }\r
5555b609 226}\r