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