renamed DataController -> DataHandler
[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 dc;
20
21 public UserController() throws Exception {
22 dc = new DataHandlerForExcel(filePath);
23 }
24
25 /**
26 * Tries to logIn a user with the given credentials
27 *
28 * @param userName username
29 * @param password the stored password for the given username
30 * @return <code>true</code> if the user could log in successfully;
31 * <code>false</code> eighter the username or the password was not correct.
32 * @throws Exception
33 */
34 public boolean logIn(String userName, String password) throws Exception{
35 if(currentUser != null) throw new AlreadyBoundException("A user is already logged in");
36 User tempUser = dc.findUser(userName);
37 if(tempUser == null) return false;
38 if(tempUser.checkPassword(password)){
39 currentUser = tempUser;
40 return true;
41 }
42 else return false;
43 }
44
45 /**
46 * Logs out the current user, throws exception if no user is logged in
47 *
48 * @throws Exception
49 */
50 public void logOut() throws Exception{
51 if(currentUser == null) throw new Exception("No user is logged in");
52 currentUser = null;
53 }
54
55 public void addUser(String userName, String password, String email, String name, String lastname) throws Exception{
56 User newUser = new User(userName,password);
57 newUser.setMail(email);
58 newUser.setName(name);
59 newUser.setLastname(lastname);
60 dc.addUser(newUser);
61 }
62 }