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