Merge remote-tracking branch 'origin/development'
[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.util.List;\r
11import java.util.Map;\r
12import java.util.Set;\r
13\r
14import ch.bfh.ti.daterange.DateRange;\r
15import ch.bfh.ti.srs.datahandler.DataHandler;\r
16import ch.bfh.ti.srs.dataschema.Reservation;\r
17import ch.bfh.ti.srs.dataschema.Resource;\r
18import ch.bfh.ti.srs.dataschema.User;\r
19\r
20/**\r
21 * @author Michael Grossen\r
22 */\r
23public class UserController extends Controller {\r
24\r
25 public UserController(DataHandler dh, SessionController sc) {\r
26 super(dh, sc);\r
27 }\r
28\r
29 /**\r
30 * Searches the set DataHandler for a specific <code>User</code>\r
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
36 */\r
37 public User getUser(String userName) throws Exception {\r
38 if (dh == null)\r
39 throw new Exception("No DataHandler set");\r
40 return dh.findUser(userName).clone();\r
41 }\r
42\r
43 /**\r
44 * Returns the reservations of the given user\r
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
52 */\r
53 public Map<Reservation, Boolean> getUserReservations(User user) throws Exception {\r
54 if (user == null) {\r
55 if (sc.getLoggedUser() == null)\r
56 throw new Exception("user must be defined, if no user is logged in");\r
57 else\r
58 user = sc.getLoggedUser();\r
59 }\r
60 return super.getUserReservations_Super(user);\r
61 }\r
62\r
63 /**\r
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
75 */\r
76 public boolean changeReservation(Reservation reservation) throws Exception {\r
77 if (sc.getLoggedUser() == null)\r
78 throw new Exception("No user is logged in");\r
79 if (reservation == null)\r
80 throw new IllegalArgumentException("Reservation must be defined");\r
81 if (!reservation.getUser().getUserName().equals(super.sc.getLoggedUser().getUserName())) {\r
82 throw new Exception("You are not authorized to make this change");\r
83 }\r
84\r
85 return super.changeReservation_Super(reservation);\r
86 }\r
87\r
88 /**\r
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
100 */\r
101 public boolean deleteReservation(Reservation reservation) throws Exception {\r
102 if (sc.getLoggedUser() == null)\r
103 throw new Exception("No user is logged in");\r
104 if (reservation == null)\r
105 throw new IllegalArgumentException("Reservation must be defined");\r
106 if (!reservation.getUser().equals(sc.getLoggedUser()))\r
107 throw new Exception("You are not authorized to make this change");\r
108 return super.deleteReservation_Super(reservation);\r
109 }\r
110\r
111 /**\r
112 * {@inheritDoc}\r
113 */\r
114 public Set<Reservation> makeReservation(User user, Resource resource, List<DateRange> dateRangeList, String remark)\r
115 throws Exception {\r
116 if (user == null) {\r
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
121 if (!user.equals(sc.getLoggedUser()))\r
122 throw new Exception("You are not authorized to make this reservation");\r
123 return super.makeReservation(user, resource, dateRangeList, remark);\r
124 }\r
125}\r