Roles added
[staff/due1/sed-hs15-srs-purple.git] / src / main / java / ch / bfh / ti / srs / controller / UserController.java
1 /*
2 * Copyright (c) 2015 Berner Fachhochschule, Switzerland.
3 *
4 * Project Smart Reservation System.
5 *
6 * Distributable under GPL license. See terms of license at gnu.org.
7 */
8 package ch.bfh.ti.srs.controller;
9
10 import java.util.List;
11 import java.util.Map;
12 import java.util.Set;
13
14 import ch.bfh.ti.daterange.DateRange;
15 import ch.bfh.ti.srs.datahandler.DataHandler;
16 import ch.bfh.ti.srs.dataschema.Reservation;
17 import ch.bfh.ti.srs.dataschema.Resource;
18 import ch.bfh.ti.srs.dataschema.User;
19
20 /**
21 * @author Michael Grossen
22 */
23 public class UserController extends Controller{
24
25 public UserController(DataHandler dh, SessionController sc) {
26 super(dh, sc);
27 }
28
29 /**
30 * Searches the set DataHandler for a specific <code>User</code>
31 * @param userName
32 * <code>User</code>
33 * @return clone of <code>User</code>
34 * @throws Exception
35 * No <code>DataHandler</code> is set
36 */
37 public User getUser(String userName) throws Exception {
38 if (dh == null)
39 throw new Exception("No DataHandler set");
40 return dh.findUser(userName).clone();
41 }
42
43 /**
44 * Returns the reservations of the given user
45 * @param user
46 * Owner, if <code>null</code> returns the reservations of the
47 * current logged in user
48 * @return <code>List</code> of reservation copies of the given owner
49 * @throws Exception
50 * either no <code>DataHandler</code> is set or no user is
51 * logged in while user param = null
52 */
53 public Map<Reservation, Boolean> getUserReservations(User user) throws Exception {
54 if (user == null) {
55 if (sc.getLoggedUser().getUserName() == null)
56 throw new Exception("user must be defined, if no user is logged in");
57 else
58 user = sc.getLoggedUser();
59 }
60 return super.getUserReservations_Super(user);
61 }
62
63 /**
64 * Replaces the given reservation in the set DataHandler referring to the
65 * reservationid
66 * @param reservation
67 * Copy of the reservation to edit
68 * @return <code>True</code> Changes saved, <code>False</code> Changes could
69 * not be saved
70 * @throws IllegalArgumentException
71 * if reservation param is null
72 * @throws Exception
73 * if no <code>DataHandler</code> is set or the logged user is
74 * not authorized
75 */
76 public boolean changeReservation(Reservation reservation) throws Exception {
77 if (sc.getLoggedUser().getUserName() == null)
78 throw new Exception("No user is logged in");
79 if (reservation == null)
80 throw new IllegalArgumentException("Reservation must be defined");
81 if (!reservation.getUser().equals(super.sc.getLoggedUser().getUserName()))
82 throw new Exception("You are not authorized to make this change");
83 return super.changeReservation_Super(reservation);
84 }
85
86 /**
87 * Deletes the given reservation in the set <code>DataHandler</code>
88 * referring to the reservation id
89 * @param reservation
90 * Copy of the reservation to edit
91 * @return <code>True</code> Changes saved <code>False</code> Changes could
92 * not be saved
93 * @throws IllegalArgumentException
94 * if reservation is null
95 * @throws Exception
96 * if no <code>DataHandler</code> is set, no <code>User</code>
97 * is logged in or logged <code>User</code> is not authorized
98 */
99 public boolean deleteReservation(Reservation reservation) throws Exception {
100 if (sc.getLoggedUser().getUserName() == null)
101 throw new Exception("No user is logged in");
102 if (reservation == null)
103 throw new IllegalArgumentException("Reservation must be defined");
104 if (!reservation.getUser().equals(sc.getLoggedUser()))
105 throw new Exception("You are not authorized to make this change");
106 return super.deleteReservation_Super(reservation);
107 }
108
109 /**
110 * {@inheritDoc}
111 */
112 public Set<Reservation> makeReservation(User user, Resource resource, List<DateRange> dateRangeList, String remark) throws Exception {
113 if (user == null) user = sc.getLoggedUser();
114 if (!user.equals(sc.getLoggedUser())) throw new Exception("You are not authorized to make this reservation");
115 return super.makeReservation(user, resource, dateRangeList, remark);
116 }
117 }