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