DataHandlerForTestsTest added,
[staff/due1/sed-hs15-srs-purple.git] / src / main / java / ch / bfh / ti / srs / core / 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
8package ch.bfh.ti.srs.core;\r
9\r
1ec0e01f
M
10import java.rmi.AlreadyBoundException;\r
11import java.util.concurrent.ExecutionException;\r
12\r
13/**\r
14 * @author Michael Grossen\r
15 */\r
5555b609 16public class UserController {\r
1ec0e01f
M
17 final String filePath = "test.xls";\r
18 User currentUser;\r
1f7b5004 19 DataHandler dh;\r
5555b609 20 \r
1f7b5004
M
21 public UserController(){\r
22 }\r
23 \r
24 \r
25 /**\r
26 * Sets the DataHandler implementation for this Controller, must be set before using all other methods!\r
27 * \r
28 * @param dh DataHandler implementation (i.e. DataHandlerForExcel)\r
29 */\r
30 public void setDataHandler(DataHandler dh){\r
31 this.dh = dh;\r
5555b609
M
32 }\r
33 \r
1ec0e01f
M
34 /**\r
35 * Tries to logIn a user with the given credentials\r
36 * \r
37 * @param userName username\r
38 * @param password the stored password for the given username\r
39 * @return <code>true</code> if the user could log in successfully;\r
40 * <code>false</code> eighter the username or the password was not correct.\r
41 * @throws Exception\r
42 */\r
5555b609 43 public boolean logIn(String userName, String password) throws Exception{\r
1f7b5004 44 if(dh == null) throw new Exception("No DataHandler set");\r
1ec0e01f 45 if(currentUser != null) throw new AlreadyBoundException("A user is already logged in");\r
1f7b5004 46 User tempUser = dh.findUser(userName);\r
5555b609
M
47 if(tempUser == null) return false;\r
48 if(tempUser.checkPassword(password)){\r
1ec0e01f 49 currentUser = tempUser;\r
5555b609
M
50 return true;\r
51 }\r
52 else return false;\r
53 }\r
54 \r
1ec0e01f
M
55 /**\r
56 * Logs out the current user, throws exception if no user is logged in\r
57 * \r
58 * @throws Exception \r
59 */\r
60 public void logOut() throws Exception{\r
1f7b5004 61 if(dh == null) throw new Exception("No DataHandler set");\r
1ec0e01f
M
62 if(currentUser == null) throw new Exception("No user is logged in");\r
63 currentUser = null;\r
64 }\r
5555b609 65 \r
1f7b5004
M
66 /**\r
67 * Registers a new user to the current DataHandler\r
68 * \r
69 * @param userName Username\r
70 * @param password Plaintext password\r
71 * @param email E-Mail address\r
72 * @param name Name\r
73 * @param lastname Lastname\r
74 * @throws Exception\r
75 */\r
1ec0e01f 76 public void addUser(String userName, String password, String email, String name, String lastname) throws Exception{\r
1f7b5004 77 if(dh == null) throw new Exception("No DataHandler set");\r
1ec0e01f
M
78 User newUser = new User(userName,password);\r
79 newUser.setMail(email);\r
80 newUser.setName(name);\r
81 newUser.setLastname(lastname);\r
1f7b5004
M
82 dh.addUser(newUser);\r
83 }\r
84\r
85\r
86 /**\r
87 * Gets the username of the current user\r
88 * \r
89 * @return Username\r
90 * @throws Exception\r
91 */\r
92 public String getLoggedUserName() throws Exception{\r
93 if(currentUser == null) throw new Exception("No user is logged in");\r
94 return currentUser.getUserName();\r
95 }\r
96\r
97\r
98 /**\r
99 * Gets the mail address of the current user\r
100 * \r
101 * @return Mail address\r
102 * @throws Exception\r
103 */\r
104 public String getLoggedMail() throws Exception{\r
105 if(currentUser == null) throw new Exception("No user is logged in");\r
106 return currentUser.getMail();\r
107 }\r
108\r
109 /**\r
110 * Gets the first name of the current user\r
111 * \r
112 * @return First name\r
113 * @throws Exception\r
114 */\r
115 public String getLoggedName() throws Exception {\r
116 if(currentUser == null) throw new Exception("No user is logged in");\r
117 return currentUser.getName();\r
118 }\r
119\r
120 /**\r
121 * Gets the last name of the current user\r
122 * \r
123 * @return Last name\r
124 * @throws Exception\r
125 */\r
126 public String getLoggedLastname() throws Exception {\r
127 if(currentUser == null) throw new Exception("No user is logged in");\r
128 return currentUser.getLastname();\r
1ec0e01f 129 }\r
5555b609 130}\r