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