Controller Struktur angepasst
[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
4574ef68 10import java.util.Map;\r
1ec0e01f 11\r
e3535133 12import ch.bfh.ti.srs.datahandler.DataHandler;\r
13313998 13import ch.bfh.ti.srs.dataschema.Reservation;\r
5c0ee08b 14import ch.bfh.ti.srs.dataschema.User;\r
e3535133 15\r
1ec0e01f
M
16/**\r
17 * @author Michael Grossen\r
18 */\r
da277fcf 19public class UserController extends Controller{\r
f0b6a43e
M
20 \r
21 public UserController(DataHandler dh, SessionController sc) {\r
22 super(dh, sc);\r
4574ef68 23 }\r
66cc653d 24\r
757804bb
M
25 /**\r
26 * Searches the set DataHandler for a specific <code>User</code>\r
66cc653d
D
27 * @param userName\r
28 * <code>User</code>\r
29 * @return clone of <code>User</code>\r
30 * @throws Exception\r
31 * No <code>DataHandler</code> is set\r
757804bb 32 */\r
66cc653d
D
33 public User getUser(String userName) throws Exception {\r
34 if (dh == null)\r
35 throw new Exception("No DataHandler set");\r
4574ef68 36 return dh.findUser(userName).clone();\r
a08b0437 37 }\r
66cc653d 38\r
13313998 39 /**\r
757804bb 40 * Returns the reservations of the given user\r
66cc653d
D
41 * @param user\r
42 * Owner, if <code>null</code> returns the reservations of the\r
43 * current logged in user\r
44 * @return <code>List</code> of reservation copies of the given owner\r
45 * @throws Exception\r
46 * either no <code>DataHandler</code> is set or no user is\r
47 * logged in while user param = null\r
b5b8ffc1 48 */\r
66cc653d 49 public Map<Reservation, Boolean> getUserReservations(User user) throws Exception {\r
66cc653d 50 if (user == null) {\r
f0b6a43e 51 if (sc.getLoggedUser().getUserName() == null)\r
66cc653d
D
52 throw new Exception("user must be defined, if no user is logged in");\r
53 else\r
f0b6a43e 54 user = sc.getLoggedUser();\r
b5b8ffc1 55 }\r
da277fcf 56 return super.getUserReservations_Super(user);\r
4574ef68 57 }\r
66cc653d 58\r
757804bb 59 /**\r
66cc653d
D
60 * Replaces the given reservation in the set DataHandler referring to the\r
61 * reservationid\r
62 * @param reservation\r
63 * Copy of the reservation to edit\r
64 * @return <code>True</code> Changes saved, <code>False</code> Changes could\r
65 * not be saved\r
66 * @throws IllegalArgumentException\r
67 * if reservation param is null\r
68 * @throws Exception\r
69 * if no <code>DataHandler</code> is set or the logged user is\r
70 * not authorized\r
757804bb 71 */\r
66cc653d 72 public boolean changeReservation(Reservation reservation) throws Exception {\r
f0b6a43e 73 if (sc.getLoggedUser().getUserName() == null)\r
66cc653d
D
74 throw new Exception("No user is logged in");\r
75 if (reservation == null)\r
76 throw new IllegalArgumentException("Reservation must be defined");\r
f0b6a43e 77 if (!reservation.getUser().equals(super.sc.getLoggedUser().getUserName()))\r
66cc653d 78 throw new Exception("You are not authorized to make this change");\r
da277fcf 79 return super.changeReservation_Super(reservation);\r
4574ef68 80 }\r
66cc653d 81\r
757804bb 82 /**\r
66cc653d
D
83 * Deletes the given reservation in the set <code>DataHandler</code>\r
84 * referring to the reservation id\r
85 * @param reservation\r
86 * Copy of the reservation to edit\r
87 * @return <code>True</code> Changes saved <code>False</code> Changes could\r
88 * not be saved\r
89 * @throws IllegalArgumentException\r
90 * if reservation is null\r
91 * @throws Exception\r
92 * if no <code>DataHandler</code> is set, no <code>User</code>\r
93 * is logged in or logged <code>User</code> is not authorized\r
757804bb 94 */\r
66cc653d 95 public boolean deleteReservation(Reservation reservation) throws Exception {\r
f0b6a43e 96 if (sc.getLoggedUser().getUserName() == null)\r
66cc653d
D
97 throw new Exception("No user is logged in");\r
98 if (reservation == null)\r
99 throw new IllegalArgumentException("Reservation must be defined");\r
f0b6a43e 100 if (!reservation.getUser().equals(sc.getLoggedUser()))\r
66cc653d 101 throw new Exception("You are not authorized to make this change");\r
da277fcf 102 return super.deleteReservation_Super(reservation);\r
4574ef68 103 }\r
66cc653d 104\r
5555b609 105}\r