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