fc4766978e6bfcaacb74724eccc57328e8b32725
[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.List;
12 import java.util.UUID;
13
14 import ch.bfh.ti.daterange.DateRange;
15 import ch.bfh.ti.srs.datahandler.DataHandler;
16 import ch.bfh.ti.srs.dataschema.Reservation;
17 import ch.bfh.ti.srs.dataschema.User;
18
19 /**
20 * @author Michael Grossen
21 */
22 public class UserController {
23 private User currentUser;
24 private DataHandler dh;
25
26 public UserController(){
27 }
28
29
30 /**
31 * Sets the DataHandler implementation for this Controller, must be set before using all other methods!
32 *
33 * @param dh DataHandler implementation (i.e. DataHandlerForExcel)
34 */
35 public void setDataHandler(DataHandler dh){
36 this.dh = dh;
37 }
38
39 /**
40 * Tries to logIn a user with the given credentials
41 *
42 * @param userName username
43 * @param password the stored password for the given username
44 * @return <code>true</code> if the user could log in successfully;
45 * <code>false</code> eighter the username or the password was not correct.
46 * @throws Exception no datahandler set or thrown by datahandler
47 * @throws AlreadyBoundException a user is already logged in
48 */
49 public boolean logIn(String userName, String password) throws Exception{
50 if(dh == null) throw new Exception("No DataHandler set");
51 if(currentUser != null) throw new AlreadyBoundException("A user is already logged in");
52 User tempUser = dh.findUser(userName);
53 if(tempUser == null) return false;
54 if(tempUser.checkPassword(password)){
55 currentUser = tempUser;
56 return true;
57 }
58 else return false;
59 }
60
61 /**
62 * Logs out the current user, throws exception if no user is logged in
63 *
64 * @throws Exception
65 */
66 public void logOut() throws Exception{
67 if(dh == null) throw new Exception("No DataHandler set");
68 if(currentUser == null) throw new Exception("No user is logged in");
69 currentUser = null;
70 }
71
72 /**
73 * Registers a new user to the current DataHandler
74 *
75 * @param userName Username
76 * @param password Plaintext password
77 * @param email E-Mail address
78 * @param name Name
79 * @param lastname Lastname
80 * @throws Exception
81 */
82 public void addUser(String userName, String password, String email, String name, String lastname) throws Exception{
83 if(dh == null) throw new Exception("No DataHandler set");
84 User newUser = new User(userName,password);
85 newUser.setMail(email);
86 newUser.setName(name);
87 newUser.setLastname(lastname);
88 dh.addUser(newUser);
89 }
90
91
92 /**
93 * Gets the username of the current user
94 *
95 * @return Username
96 * @throws Exception
97 */
98 public String getLoggedUserName() throws Exception{
99 if(currentUser == null) throw new Exception("No user is logged in");
100 return currentUser.getUserName();
101 }
102
103
104 /**
105 * Gets the mail address of the current user
106 *
107 * @return Mail address
108 * @throws Exception
109 */
110 public String getLoggedMail() throws Exception{
111 if(currentUser == null) throw new Exception("No user is logged in");
112 return currentUser.getMail();
113 }
114
115 /**
116 * Gets the first name of the current user
117 *
118 * @return First name
119 * @throws Exception
120 */
121 public String getLoggedName() throws Exception {
122 if(currentUser == null) throw new Exception("No user is logged in");
123 return currentUser.getName();
124 }
125
126 /**
127 * Gets the last name of the current user
128 *
129 * @return Last name
130 * @throws Exception
131 */
132 public String getLoggedLastname() throws Exception {
133 if(currentUser == null) throw new Exception("No user is logged in");
134 return currentUser.getLastname();
135 }
136
137 /**
138 * Returns all reservations overlapping the given dateRange in a List
139 *
140 * @param dateRange
141 * @return
142 * @throws Exception
143 */
144 public List<Reservation> getReservationsInDateRange(DateRange dateRange) throws Exception{
145 if(dh == null) throw new Exception("No DataHandler set");
146 if(dateRange == null) throw new Exception("dateRange must be defined");
147 return dh.overlaps(dateRange);
148 }
149
150 /**
151 * Returns the reservation or null if no reservation was found
152 *
153 * @param id
154 * @return
155 * @throws Exception
156 */
157 public Reservation getReservation(UUID id) throws Exception{
158 if(dh == null) throw new Exception("No DataHandler set");
159 if(id == null) throw new Exception("uuid must be defined");
160 return dh.findReservation(id);
161 }
162 }