DataHandler updated
[staff/due1/sed-hs15-srs-purple.git] / src / main / java / ch / bfh / ti / srs / datahandler / DataHandlerForTests.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.datahandler;
9
10 import java.util.ArrayList;
11 import java.util.HashSet;
12 import java.util.List;
13 import java.util.Set;
14 import java.util.UUID;
15
16 import ch.bfh.ti.daterange.DateRange;
17 import ch.bfh.ti.srs.dataschema.Reservation;
18 import ch.bfh.ti.srs.dataschema.Resource;
19 import ch.bfh.ti.srs.dataschema.User;
20
21 public class DataHandlerForTests implements DataHandler {
22 private Set<User> userList = new HashSet<User>();
23 private Set<Resource> resourceList = new HashSet<Resource>();
24 private Set<Reservation> reservationList = new HashSet<Reservation>();
25
26 public DataHandlerForTests() {
27
28 }
29
30 /**
31 * {@inheritDoc}
32 */
33 @Override
34 public User findUser(String userName) {
35 for (User user : userList) {
36 if (user.getUserName().equals(userName))
37 return user;
38 }
39 return null;
40 }
41
42 /**
43 * {@inheritDoc}
44 */
45 @Override
46 public boolean addUser(User user) throws Exception {
47 if (user == null) {
48 throw new IllegalArgumentException("User object must not be null!");
49 } else if (userList.contains(user)) {
50 return false;
51 } else {
52 userList.add(user);
53 return true;
54 }
55 }
56
57 /**
58 * {@inheritDoc}
59 */
60 @Override
61 public Resource findResource(String resourceName) {
62 for (Resource resource : resourceList) {
63 if (resource.getName().equals(resourceName))
64 return resource;
65 }
66 return null;
67 }
68
69 /**
70 * {@inheritDoc}
71 */
72 @Override
73 public boolean addResource(Resource resource) throws Exception {
74 if (resource == null) {
75 throw new IllegalArgumentException("Resource object must not be null!");
76 } else if (resourceList.contains(resource)) {
77 return false;
78 } else {
79 for (Resource res : resourceList) {
80 if (res.getId() == resource.getId()) {
81 return false;
82 }
83 }
84 resourceList.add(resource);
85 return true;
86 }
87 }
88
89 /**
90 * {@inheritDoc}
91 */
92 @Override
93 public Reservation findReservation(UUID id) {
94 for (Reservation reservation : reservationList) {
95 if (reservation.getId().equals(id))
96 return reservation;
97 }
98 return null;
99 }
100
101 /**
102 * {@inheritDoc}
103 */
104 @Override
105 public List<Reservation> findGroupReservations(UUID groupId) {
106 if (groupId == null)
107 throw new IllegalArgumentException("DateRange object must not be null!");
108 List<Reservation> groupReservations = new ArrayList<Reservation>();
109 for (Reservation reservation : reservationList) {
110 if (reservation.getGroupId().equals(groupId))
111 groupReservations.add(reservation);
112 }
113 return groupReservations;
114 }
115
116 /**
117 * {@inheritDoc}
118 */
119 @Override
120 public List<Reservation> userReservations(User user) {
121 if (user == null)
122 throw new IllegalArgumentException("User object must not be null!");
123 List<Reservation> allUserReservations = new ArrayList<Reservation>();
124 for (Reservation reservation : reservationList) {
125 if (reservation.getUser().equals(user))
126 allUserReservations.add(reservation);
127 }
128 return allUserReservations;
129 }
130
131 /**
132 * {@inheritDoc}
133 */
134 @Override
135 public List<Reservation> overlaps(DateRange range) {
136 if (range == null)
137 throw new IllegalArgumentException("DateRange object must not be null!");
138 List<Reservation> overlappingReservations = new ArrayList<Reservation>();
139 for (Reservation reservation : reservationList) {
140 if (reservation.getDateRange().overlaps(range))
141 overlappingReservations.add(reservation);
142 }
143 return overlappingReservations;
144 }
145
146 /**
147 * {@inheritDoc}
148 */
149 @Override
150 public List<Reservation> findAllReservations() {
151 List<Reservation> allReservations = new ArrayList<Reservation>();
152 for (Reservation reservation : reservationList) {
153 allReservations.add(reservation);
154 }
155 return allReservations;
156 }
157
158 /**
159 * {@inheritDoc}
160 */
161 @Override
162 public boolean addReservation(Reservation reservation) throws Exception {
163 if (reservation == null) {
164 throw new IllegalArgumentException("Reservation object must not be null!");
165 } else if (reservationList.contains(reservation)) {
166 return false;
167 } else {
168 for (Reservation res : reservationList) {
169 if (res.getId() == reservation.getId()) {
170 return false;
171 } else if (res.getResource() == reservation.getResource()
172 && res.getDateRange().overlaps(reservation.getDateRange())) {
173 return false;
174 }
175 }
176 reservationList.add(reservation);
177 return true;
178 }
179 }
180
181 /**
182 * {@inheritDoc}
183 */
184 @Override
185 public void saveChanges() {
186 }
187
188 /**
189 * {@inheritDoc}
190 * @throws Exception
191 * no implementation for <code>true</code> possible
192 */
193 @Override
194 public void refresh(boolean discardChanges) throws Exception {
195 if (discardChanges == true)
196 throw new Exception("No implementation on DataHandlerForTests possible");
197 }
198
199 /**
200 * {@inheritDoc}
201 */
202 @Override
203 public boolean editReservation(Reservation reservation) {
204 if (reservation == null) {
205 throw new IllegalArgumentException("Reservation object must not be null!");
206 }
207 if (findReservation(reservation.getId()) != null) {
208 reservationList.remove(findReservation(reservation.getId()));
209 reservationList.add(reservation);
210 return true;
211 }
212 return false;
213 }
214
215 /**
216 * {@inheritDoc}
217 */
218 @Override
219 public boolean deleteReservation(Reservation reservation) {
220 if (reservation == null) {
221 throw new IllegalArgumentException("Reservation object must not be null!");
222 } else if (reservationList.contains(reservation)) {
223 reservationList.remove(reservation);
224 return true;
225 } else {
226 return false;
227 }
228 }
229
230 /**
231 * {@inheritDoc}
232 */
233 @Override
234 public Set<Resource> getAllResources() {
235
236 return resourceList;
237 }
238
239 /**
240 * {@inheritDoc}
241 */
242 @Override
243 public boolean editResource(Resource resource) {
244 if (resource == null) {
245 throw new IllegalArgumentException("Resource object must not be null!");
246 }
247 if (findResource(resource.getName()) != null) {
248 resourceList.remove(findResource(resource.getName()));
249 resourceList.add(resource);
250 return true;
251 }
252 return false;
253 }
254
255 /**
256 * {@inheritDoc}
257 */
258 @Override
259 public boolean deleteResource(Resource resource) {
260 if (resource == null) {
261 throw new IllegalArgumentException("Resource object must not be null!");
262 } else if (resourceList.contains(resource)) {
263 resourceList.remove(resource);
264 return true;
265 } else {
266 return false;
267 }
268 }
269
270 @Override
271 public boolean editUser(User user) {
272 if (user == null) {
273 throw new IllegalArgumentException("User object must not be null!");
274 }
275 if (findUser(user.getName()) != null) {
276 userList.remove(findUser(user.getName()));
277 userList.add(user);
278 return true;
279 }
280 return false;
281 }
282 }