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