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