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