Updated Tests
[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() == 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() == 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().getUserName().equals(super.sc.getLoggedUser().getUserName())){
82 throw new Exception("You are not authorized to make this change");
83 }
84
85 return super.changeReservation_Super(reservation);
86 }
87
88 /**
89 * Deletes the given reservation in the set <code>DataHandler</code>
90 * referring to the reservation id
91 * @param reservation
92 * Copy of the reservation to edit
93 * @return <code>True</code> Changes saved <code>False</code> Changes could
94 * not be saved
95 * @throws IllegalArgumentException
96 * if reservation is null
97 * @throws Exception
98 * if no <code>DataHandler</code> is set, no <code>User</code>
99 * is logged in or logged <code>User</code> is not authorized
100 */
101 public boolean deleteReservation(Reservation reservation) throws Exception {
102 if (sc.getLoggedUser() == null)
103 throw new Exception("No user is logged in");
104 if (reservation == null)
105 throw new IllegalArgumentException("Reservation must be defined");
106 if (!reservation.getUser().equals(sc.getLoggedUser()))
107 throw new Exception("You are not authorized to make this change");
108 return super.deleteReservation_Super(reservation);
109 }
110
111 /**
112 * {@inheritDoc}
113 */
114 public Set<Reservation> makeReservation(User user, Resource resource, List<DateRange> dateRangeList, String remark) throws Exception {
115 if (user == null){
116 if (sc.getLoggedUser() == null)
117 throw new IllegalArgumentException("User must be defined if no user is logged in");
118 user = sc.getLoggedUser();
119 }
120 if (!user.equals(sc.getLoggedUser())) throw new Exception("You are not authorized to make this reservation");
121 return super.makeReservation(user, resource, dateRangeList, remark);
122 }
123 }