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
5555b609
M
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
e3535133 8package ch.bfh.ti.srs.controller;\r
5555b609 9\r
1ec0e01f 10import java.rmi.AlreadyBoundException;\r
b5b8ffc1 11import java.util.ArrayList;\r
4574ef68
M
12import java.util.HashMap;\r
13import java.util.HashSet;\r
13313998 14import java.util.List;\r
4574ef68
M
15import java.util.Map;\r
16import java.util.Set;\r
13313998 17import java.util.UUID;\r
1ec0e01f 18\r
13313998 19import ch.bfh.ti.daterange.DateRange;\r
e3535133 20import ch.bfh.ti.srs.datahandler.DataHandler;\r
13313998 21import ch.bfh.ti.srs.dataschema.Reservation;\r
1702d8b9 22import ch.bfh.ti.srs.dataschema.Resource;\r
5c0ee08b 23import ch.bfh.ti.srs.dataschema.User;\r
e3535133 24\r
1ec0e01f
M
25/**\r
26 * @author Michael Grossen\r
27 */\r
da277fcf 28public class UserController extends Controller{\r
66cc653d 29\r
da277fcf
M
30 public UserController(DataHandler dh) {\r
31 super(dh);\r
1f7b5004
M
32 }\r
33\r
1f7b5004 34 /**\r
757804bb 35 * Gets the user name of the current user\r
66cc653d
D
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
1f7b5004 40 */\r
66cc653d
D
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
1f7b5004
M
45 return currentUser.getUserName();\r
46 }\r
47\r
1f7b5004
M
48 /**\r
49 * Gets the mail address of the current user\r
757804bb 50 * @deprecated use <code>getLoggedUser().getMail()</code>\r
66cc653d
D
51 * @return Mail address\r
52 * @throws Exception\r
53 * no user is logged in\r
1f7b5004 54 */\r
1702d8b9 55 @Deprecated\r
66cc653d
D
56 public String getLoggedMail() throws Exception {\r
57 if (currentUser == null)\r
58 throw new Exception("No user is logged in");\r
1f7b5004
M
59 return currentUser.getMail();\r
60 }\r
61\r
62 /**\r
63 * Gets the first name of the current user\r
757804bb 64 * @deprecated use <code>getLoggedUser().getName()</code>\r
66cc653d
D
65 * @return First name\r
66 * @throws Exception\r
67 * no user is logged in\r
1f7b5004 68 */\r
1702d8b9 69 @Deprecated\r
66cc653d
D
70 public String getLoggedName() throws Exception {\r
71 if (currentUser == null)\r
72 throw new Exception("No user is logged in");\r
1f7b5004
M
73 return currentUser.getName();\r
74 }\r
75\r
76 /**\r
77 * Gets the last name of the current user\r
757804bb 78 * @deprecated use <code>getLoggedUser().getLastName()</code>\r
66cc653d
D
79 * @return Last name\r
80 * @throws Exception\r
81 * no user is logged in\r
1f7b5004 82 */\r
1702d8b9 83 @Deprecated\r
66cc653d
D
84 public String getLoggedLastname() throws Exception {\r
85 if (currentUser == null)\r
86 throw new Exception("No user is logged in");\r
1f7b5004 87 return currentUser.getLastname();\r
1ec0e01f 88 }\r
66cc653d 89\r
757804bb
M
90 /**\r
91 * Gets the current logged <code>User</code>\r
66cc653d
D
92 * @return current <code>User</code>, or <code>null</code> if no\r
93 * <code>User</code> is logged in\r
757804bb 94 */\r
66cc653d
D
95 public User getLoggedUser() {\r
96 if (currentUser == null)\r
97 return null;\r
4574ef68
M
98 return currentUser;\r
99 }\r
66cc653d 100\r
757804bb
M
101 /**\r
102 * Searches the set DataHandler for a specific <code>User</code>\r
66cc653d
D
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
757804bb 108 */\r
66cc653d
D
109 public User getUser(String userName) throws Exception {\r
110 if (dh == null)\r
111 throw new Exception("No DataHandler set");\r
4574ef68 112 return dh.findUser(userName).clone();\r
a08b0437 113 }\r
66cc653d 114\r
13313998 115 /**\r
757804bb 116 * Returns the reservations of the given user\r
66cc653d
D
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
b5b8ffc1 124 */\r
66cc653d 125 public Map<Reservation, Boolean> getUserReservations(User user) throws Exception {\r
66cc653d
D
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
b5b8ffc1 131 }\r
da277fcf 132 return super.getUserReservations_Super(user);\r
4574ef68 133 }\r
66cc653d 134\r
757804bb 135 /**\r
66cc653d
D
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
757804bb 147 */\r
66cc653d 148 public boolean changeReservation(Reservation reservation) throws Exception {\r
66cc653d
D
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
da277fcf 153 if (!reservation.getUser().equals(super.currentUser))\r
66cc653d 154 throw new Exception("You are not authorized to make this change");\r
da277fcf 155 return super.changeReservation_Super(reservation);\r
4574ef68 156 }\r
66cc653d 157\r
757804bb 158 /**\r
66cc653d
D
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
757804bb 170 */\r
66cc653d 171 public boolean deleteReservation(Reservation reservation) throws Exception {\r
66cc653d
D
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
da277fcf 178 return super.deleteReservation_Super(reservation);\r
4574ef68 179 }\r
66cc653d 180\r
5555b609 181}\r