UserController updated
[staff/due1/sed-hs15-srs-purple.git] / src / main / java / ch / bfh / ti / srs / datahandler / DataHandlerForTests.java
CommitLineData
7903e50e
M
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 */
8package ch.bfh.ti.srs.datahandler;
9
be594ada 10import java.util.ArrayList;
7903e50e 11import java.util.HashSet;
be594ada 12import java.util.List;
7903e50e 13import java.util.Set;
be594ada 14import java.util.UUID;
7903e50e
M
15
16import ch.bfh.ti.daterange.DateRange;
17import ch.bfh.ti.srs.dataschema.Reservation;
18import ch.bfh.ti.srs.dataschema.Resource;
19import ch.bfh.ti.srs.dataschema.User;
20
25d43fa9 21public class DataHandlerForTests implements DataHandler {
1e4d6813
M
22 private Set<User> userList = new HashSet<User>();
23 private Set<Resource> resourceList = new HashSet<Resource>();
24 private Set<Reservation> reservationList = new HashSet<Reservation>();
25d43fa9
M
25
26 public DataHandlerForTests() {
27
7903e50e
M
28 }
29
25d43fa9
M
30 /*
31 * (non-Javadoc)
7903e50e
M
32 * @see ch.bfh.ti.srs.dataschema.DataHandler#findUser(java.lang.String)
33 */
34 @Override
35 public User findUser(String userName) {
36 for (User user : userList) {
25d43fa9
M
37 if (user.getUserName().equals(userName))
38 return user;
7903e50e
M
39 }
40 return null;
41 }
42
25d43fa9
M
43 /*
44 * (non-Javadoc)
45 * @see
46 * ch.bfh.ti.srs.dataschema.DataHandler#addUser(ch.bfh.ti.srs.dataschema.
47 * User)
7903e50e
M
48 */
49 @Override
50 public void addUser(User user) throws Exception {
25d43fa9
M
51 if (user == null)
52 throw new IllegalArgumentException("User object must not be null!");
53 if (userList.contains(user))
54 throw new IllegalArgumentException("User already exists!");
7903e50e
M
55 userList.add(user);
56 }
57
25d43fa9
M
58 /*
59 * (non-Javadoc)
7903e50e
M
60 * @see ch.bfh.ti.srs.dataschema.DataHandler#findResource(java.lang.String)
61 */
62 @Override
63 public Resource findResource(String resourceName) {
64 for (Resource resource : resourceList) {
25d43fa9
M
65 if (resource.getName().equals(resourceName))
66 return resource;
7903e50e
M
67 }
68 return null;
69 }
25d43fa9
M
70
71 /*
72 * (non-Javadoc)
73 * @see
74 * ch.bfh.ti.srs.dataschema.DataHandler#addResource(ch.bfh.ti.srs.dataschema
75 * .Resource)
7903e50e
M
76 */
77 @Override
78 public void addResource(Resource resource) throws Exception {
25d43fa9
M
79 if (resource == null)
80 throw new IllegalArgumentException("Resource object must not be null!");
81 if (resourceList.contains(resource))
82 throw new IllegalArgumentException("Resource already exists!");
7903e50e
M
83 resourceList.add(resource);
84 }
85
25d43fa9
M
86 /*
87 * (non-Javadoc)
88 * @see
89 * ch.bfh.ti.srs.dataschema.DataHandler#findReservation(java.lang.String)
7903e50e
M
90 */
91 @Override
be594ada 92 public Reservation findReservation(UUID id) {
7903e50e 93 for (Reservation reservation : reservationList) {
25d43fa9
M
94 if (reservation.getId().equals(id))
95 return reservation;
7903e50e
M
96 }
97 return null;
98 }
25d43fa9
M
99
100 /*
101 * (non-Javadoc)
102 * @see
103 * ch.bfh.ti.srs.dataschema.DataHandler#findGroupReservations(ch.bfh.ti.srs.
104 * dataschema.Reservation)
105 */
be594ada
M
106 @Override
107 public List<Reservation> findGroupReservations(UUID groupID) {
108 List<Reservation> groupReservations = new ArrayList<Reservation>();
109 for (Reservation reservation : reservationList) {
25d43fa9 110 if (reservation.getGroupId().equals(groupID))
be594ada
M
111 groupReservations.add(reservation);
112 }
113 return groupReservations;
114 }
115
25d43fa9
M
116 /*
117 * (non-Javadoc)
118 * @see
119 * ch.bfh.ti.srs.dataschema.DataHandler#overlaps(ch.bfh.ti.srs.dataschema.
120 * Reservation)
121 */
be594ada
M
122 @Override
123 public List<Reservation> overlaps(DateRange range) {
25d43fa9
M
124 List<Reservation> overlappingReservations = new ArrayList<Reservation>();
125 for (Reservation reservation : reservationList) {
126 if (reservation.getDateRange().overlaps(range))
127 overlappingReservations.add(reservation);
128 }
129 return overlappingReservations;
be594ada 130 }
25d43fa9
M
131
132 /*
133 * (non-Javadoc)
134 * @see ch.bfh.ti.srs.dataschema.DataHandler#addReservation(ch.bfh.ti.srs.
135 * dataschema.Reservation)
7903e50e
M
136 */
137 @Override
138 public void addReservation(Reservation reservation) throws Exception {
25d43fa9
M
139 if (reservation == null)
140 throw new IllegalArgumentException("Reservation object must not be null!");
141 if (resourceList.contains(reservation))
142 throw new IllegalArgumentException("Reservation already exists!");
143 reservationList.add(reservation);
7903e50e 144 }
be594ada 145
7903e50e 146}