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