UserController extended and new methods of DataHander in
[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
5c0ee08b 18import ch.bfh.ti.srs.dataschema.User;\r
e3535133 19\r
1ec0e01f
M
20/**\r
21 * @author Michael Grossen\r
22 */\r
5555b609 23public class UserController {\r
1e4d6813
M
24 private User currentUser;\r
25 private DataHandler dh;\r
5555b609 26 \r
1f7b5004
M
27 public UserController(){\r
28 }\r
29 \r
30 \r
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
92\r
93 /**\r
94 * Gets the username of the current user\r
95 * \r
b5b8ffc1
M
96 * @return Username unique username\r
97 * @throws Exception no datahandler is set\r
1f7b5004
M
98 */\r
99 public String getLoggedUserName() throws Exception{\r
100 if(currentUser == null) throw new Exception("No user is logged in");\r
101 return currentUser.getUserName();\r
102 }\r
103\r
104\r
105 /**\r
106 * Gets the mail address of the current user\r
107 * \r
108 * @return Mail address\r
b5b8ffc1 109 * @throws Exception no user is logged in\r
1f7b5004
M
110 */\r
111 public String getLoggedMail() throws Exception{\r
112 if(currentUser == null) throw new Exception("No user is logged in");\r
113 return currentUser.getMail();\r
114 }\r
115\r
116 /**\r
117 * Gets the first name of the current user\r
118 * \r
119 * @return First name\r
b5b8ffc1 120 * @throws Exception no user is logged in\r
1f7b5004
M
121 */\r
122 public String getLoggedName() throws Exception {\r
123 if(currentUser == null) throw new Exception("No user is logged in");\r
124 return currentUser.getName();\r
125 }\r
126\r
127 /**\r
128 * Gets the last name of the current user\r
129 * \r
130 * @return Last name\r
b5b8ffc1 131 * @throws Exception no user is logged in\r
1f7b5004
M
132 */\r
133 public String getLoggedLastname() throws Exception {\r
134 if(currentUser == null) throw new Exception("No user is logged in");\r
135 return currentUser.getLastname();\r
1ec0e01f 136 }\r
13313998
M
137 \r
138 /**\r
139 * Returns all reservations overlapping the given dateRange in a List\r
140 * \r
141 * @param dateRange\r
b5b8ffc1
M
142 * @return <code>List</code> if list is empty, no overlapping reservation could be found\r
143 * @throws Exception no user is logged in\r
13313998
M
144 */\r
145 public List<Reservation> getReservationsInDateRange(DateRange dateRange) throws Exception{\r
146 if(dh == null) throw new Exception("No DataHandler set");\r
147 if(dateRange == null) throw new Exception("dateRange must be defined");\r
148 return dh.overlaps(dateRange);\r
149 }\r
150 \r
151 /**\r
152 * Returns the reservation or null if no reservation was found\r
153 * \r
b5b8ffc1
M
154 * @param id universal unique idendifier\r
155 * @return <code>reservation</code> reservation with given id\r
156 * <code>null</code> no reservation with this id found\r
13313998
M
157 * @throws Exception\r
158 */\r
159 public Reservation getReservation(UUID id) throws Exception{\r
160 if(dh == null) throw new Exception("No DataHandler set");\r
161 if(id == null) throw new Exception("uuid must be defined");\r
162 return dh.findReservation(id);\r
163 }\r
b5b8ffc1
M
164 \r
165 /**\r
166 * Returns the reservations of the given user, if null returns the reservations of the current logged in user\r
167 * \r
168 * @param user user you want the reservations of\r
169 * @return List of reservations of a specific user\r
170 * @throws Exception eighter no datahandler is set or no user is logged in while user param = null\r
171 */\r
172 public List<Reservation> getUserReservations(User user) throws Exception{\r
173 if(dh == null) throw new Exception("No DataHandler set");\r
174 if(user == null){\r
175 if(currentUser == null) throw new Exception("user must be defined, if no user is logged in");\r
176 else user = currentUser;\r
177 }\r
178 return dh.userReservations(user);\r
179 }\r
180 \r
181 /**\r
182 * @param groupId groupId of the searched reservations group, throws Exception if groupId is null\r
183 * @return <code>List</code> reservation group\r
184 * @throws Exception eighter no datahandler is set or groupId is null\r
185 */\r
186 public List<Reservation> getReservationGroup(UUID groupId) throws Exception{\r
187 if(dh == null) throw new Exception("No DataHandler set");\r
188 if(groupId == null) throw new Exception("groupId has to be defined");\r
189 return dh.findGroupReservations(groupId);\r
190 }\r
191 \r
192 /**\r
193 * @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
194 * @return <code>List</code> reservation group\r
195 * @throws Exception no datahandler is set\r
196 */\r
197 public List<Reservation> getReservationGroup(Reservation reservation) throws Exception{\r
198 if(dh == null) throw new Exception("No DataHandler set");\r
199 if(reservation.getGroupId() == null){\r
200 ArrayList<Reservation> grpList = new ArrayList<>();\r
201 grpList.add(reservation);\r
202 return grpList;\r
203 }\r
204 return dh.findGroupReservations(reservation.getGroupId());\r
205 }\r
5555b609 206}\r