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