DataHandler updated
[staff/due1/sed-hs15-srs-purple.git] / src / main / java / ch / bfh / ti / srs / controller / SessionController.java
CommitLineData
f0b6a43e
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.controller;\r
9\r
10import java.rmi.AlreadyBoundException;\r
11import java.util.HashSet;\r
12import java.util.Set;\r
13\r
14import ch.bfh.ti.srs.datahandler.DataHandler;\r
15import ch.bfh.ti.srs.datahandler.DataHandlerForTests;\r
16import ch.bfh.ti.srs.dataschema.User;\r
17\r
18public class SessionController {\r
19 private Set<Controller> controllers;\r
20 private DataHandler dh;\r
21 private User currentUser; \r
22\r
23 public SessionController(){\r
24 dh = new DataHandlerForTests();\r
25 controllers = new HashSet<Controller>();\r
26 }\r
27 \r
28 /**\r
29 * Tries to log in a <code>User</code> with the given credentials\r
30 * @param userName\r
31 * user name\r
32 * @param password\r
33 * the stored password for the given user name\r
34 * @return <code>True</code> if the user could log in successfully,\r
35 * <code>False</code> either the username or the password was not\r
36 * correct.\r
37 * @throws Exception\r
38 * no <code>DataHandler</code> set\r
39 * @throws AlreadyBoundException\r
40 * a <code>User</code> is already logged in\r
41 */\r
42 public Set<Controller> logIn(String userName, String password) throws Exception {\r
43 if (dh == null)\r
44 throw new Exception("No DataHandler set");\r
45 if (currentUser != null)\r
46 throw new Exception("A user is already logged in");\r
47 User tempUser = dh.findUser(userName);\r
48 if (tempUser != null || tempUser.checkPassword(password)){\r
49 currentUser = tempUser;\r
50 //TODO: Check for userRoles, add new controller object for each\r
51 }\r
52 return controllers;\r
53 }\r
54\r
55 /**\r
56 * Logs out the current logged user\r
57 * @throws Exception\r
58 * either no <code>DataHandler</code> is set or no user is\r
59 * logged in\r
60 */\r
61 public void logOut() throws Exception {\r
62 if (dh == null)\r
63 throw new Exception("No DataHandler set");\r
64 if (currentUser == null)\r
65 throw new Exception("No user is logged in");\r
66 currentUser = null;\r
67 controllers.clear();\r
68 }\r
69\r
70 /**\r
71 * Gets the user name of the current user\r
72 * @deprecated use <code>getLoggedUser().getUserName()</code>\r
73 * @return user name\r
74 * @throws Exception\r
75 * no <code>DataHandler</code> is set\r
76 */\r
77 @Deprecated\r
78 public String getLoggedUserName() throws Exception {\r
79 if (currentUser == null)\r
80 throw new Exception("No user is logged in");\r
81 return currentUser.getUserName();\r
82 }\r
83\r
84 /**\r
85 * Gets the mail address of the current user\r
86 * @deprecated use <code>getLoggedUser().getMail()</code>\r
87 * @return Mail address\r
88 * @throws Exception\r
89 * no user is logged in\r
90 */\r
91 @Deprecated\r
92 public String getLoggedMail() throws Exception {\r
93 if (currentUser == null)\r
94 throw new Exception("No user is logged in");\r
95 return currentUser.getMail();\r
96 }\r
97\r
98 /**\r
99 * Gets the first name of the current user\r
100 * @deprecated use <code>getLoggedUser().getName()</code>\r
101 * @return First name\r
102 * @throws Exception\r
103 * no user is logged in\r
104 */\r
105 @Deprecated\r
106 public String getLoggedName() throws Exception {\r
107 if (currentUser == null)\r
108 throw new Exception("No user is logged in");\r
109 return currentUser.getName();\r
110 }\r
111\r
112 /**\r
113 * Gets the last name of the current user\r
114 * @deprecated use <code>getLoggedUser().getLastName()</code>\r
115 * @return Last name\r
116 * @throws Exception\r
117 * no user is logged in\r
118 */\r
119 @Deprecated\r
120 public String getLoggedLastname() throws Exception {\r
121 if (currentUser == null)\r
122 throw new Exception("No user is logged in");\r
123 return currentUser.getLastname();\r
124 }\r
125\r
126 /**\r
127 * Gets the current logged <code>User</code>\r
128 * @return current <code>User</code>, or <code>null</code> if no\r
129 * <code>User</code> is logged in\r
130 */\r
131 public User getLoggedUser() {\r
132 if (currentUser == null)\r
133 return null;\r
134 return currentUser;\r
135 }\r
136\r
137}\r