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