201141c3ff98dc0289d18ea91af95e3c9862299b
[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 import java.util.ArrayList;
12 import java.util.HashMap;
13 import java.util.HashSet;
14 import java.util.List;
15 import java.util.Map;
16 import java.util.Set;
17 import java.util.UUID;
18
19 import ch.bfh.ti.daterange.DateRange;
20 import ch.bfh.ti.srs.datahandler.DataHandler;
21 import ch.bfh.ti.srs.dataschema.Reservation;
22 import ch.bfh.ti.srs.dataschema.Resource;
23 import ch.bfh.ti.srs.dataschema.User;
24
25 /**
26 * @author Michael Grossen
27 */
28 public class UserController extends Controller{
29
30 public UserController(DataHandler dh) {
31 super(dh);
32 }
33
34 /**
35 * Gets the user name of the current user
36 * @deprecated use <code>getLoggedUser().getUserName()</code>
37 * @return user name
38 * @throws Exception
39 * no <code>DataHandler</code> is set
40 */
41 @Deprecated
42 public String getLoggedUserName() throws Exception {
43 if (currentUser == null)
44 throw new Exception("No user is logged in");
45 return currentUser.getUserName();
46 }
47
48 /**
49 * Gets the mail address of the current user
50 * @deprecated use <code>getLoggedUser().getMail()</code>
51 * @return Mail address
52 * @throws Exception
53 * no user is logged in
54 */
55 @Deprecated
56 public String getLoggedMail() throws Exception {
57 if (currentUser == null)
58 throw new Exception("No user is logged in");
59 return currentUser.getMail();
60 }
61
62 /**
63 * Gets the first name of the current user
64 * @deprecated use <code>getLoggedUser().getName()</code>
65 * @return First name
66 * @throws Exception
67 * no user is logged in
68 */
69 @Deprecated
70 public String getLoggedName() throws Exception {
71 if (currentUser == null)
72 throw new Exception("No user is logged in");
73 return currentUser.getName();
74 }
75
76 /**
77 * Gets the last name of the current user
78 * @deprecated use <code>getLoggedUser().getLastName()</code>
79 * @return Last name
80 * @throws Exception
81 * no user is logged in
82 */
83 @Deprecated
84 public String getLoggedLastname() throws Exception {
85 if (currentUser == null)
86 throw new Exception("No user is logged in");
87 return currentUser.getLastname();
88 }
89
90 /**
91 * Gets the current logged <code>User</code>
92 * @return current <code>User</code>, or <code>null</code> if no
93 * <code>User</code> is logged in
94 */
95 public User getLoggedUser() {
96 if (currentUser == null)
97 return null;
98 return currentUser;
99 }
100
101 /**
102 * Searches the set DataHandler for a specific <code>User</code>
103 * @param userName
104 * <code>User</code>
105 * @return clone of <code>User</code>
106 * @throws Exception
107 * No <code>DataHandler</code> is set
108 */
109 public User getUser(String userName) throws Exception {
110 if (dh == null)
111 throw new Exception("No DataHandler set");
112 return dh.findUser(userName).clone();
113 }
114
115 /**
116 * Returns the reservations of the given user
117 * @param user
118 * Owner, if <code>null</code> returns the reservations of the
119 * current logged in user
120 * @return <code>List</code> of reservation copies of the given owner
121 * @throws Exception
122 * either no <code>DataHandler</code> is set or no user is
123 * logged in while user param = null
124 */
125 public Map<Reservation, Boolean> getUserReservations(User user) throws Exception {
126 if (user == null) {
127 if (currentUser == null)
128 throw new Exception("user must be defined, if no user is logged in");
129 else
130 user = currentUser;
131 }
132 return super.getUserReservations_Super(user);
133 }
134
135 /**
136 * Replaces the given reservation in the set DataHandler referring to the
137 * reservationid
138 * @param reservation
139 * Copy of the reservation to edit
140 * @return <code>True</code> Changes saved, <code>False</code> Changes could
141 * not be saved
142 * @throws IllegalArgumentException
143 * if reservation param is null
144 * @throws Exception
145 * if no <code>DataHandler</code> is set or the logged user is
146 * not authorized
147 */
148 public boolean changeReservation(Reservation reservation) throws Exception {
149 if (currentUser == null)
150 throw new Exception("No user is logged in");
151 if (reservation == null)
152 throw new IllegalArgumentException("Reservation must be defined");
153 if (!reservation.getUser().equals(super.currentUser))
154 throw new Exception("You are not authorized to make this change");
155 return super.changeReservation_Super(reservation);
156 }
157
158 /**
159 * Deletes the given reservation in the set <code>DataHandler</code>
160 * referring to the reservation id
161 * @param reservation
162 * Copy of the reservation to edit
163 * @return <code>True</code> Changes saved <code>False</code> Changes could
164 * not be saved
165 * @throws IllegalArgumentException
166 * if reservation is null
167 * @throws Exception
168 * if no <code>DataHandler</code> is set, no <code>User</code>
169 * is logged in or logged <code>User</code> is not authorized
170 */
171 public boolean deleteReservation(Reservation reservation) throws Exception {
172 if (currentUser == null)
173 throw new Exception("No user is logged in");
174 if (reservation == null)
175 throw new IllegalArgumentException("Reservation must be defined");
176 if (!reservation.getUser().equals(currentUser))
177 throw new Exception("You are not authorized to make this change");
178 return super.deleteReservation_Super(reservation);
179 }
180
181 }