Create ResourceManagerController, create Modify interface in dataschema,
[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 @Override
200 public boolean editReservation(Reservation reservation) {
201 if (reservation == null) {
202 throw new IllegalArgumentException("Reservation object must not be null!");
203 }
204 if (findReservation(reservation.getId()) != null) {
205 reservationList.remove(findReservation(reservation.getId()));
206 reservationList.add(reservation);
207 return true;
208 }
209 return false;
210 }
211
212 @Override
213 public boolean deleteReservation(Reservation reservation) {
214 if (reservation == null) {
215 throw new IllegalArgumentException("Reservation object must not be null!");
216 } else if (reservationList.contains(reservation)) {
217 reservationList.remove(reservation);
218 return true;
219 } else {
220 return false;
221 }
222 }
223
224 @Override
225 public Set<Resource> getAllResources() {
226
227 return resourceList;
228 }
229
230 @Override
231 public boolean editResource(Resource resource) {
232 // TODO Auto-generated method stub
233 return false;
234 }
235
236 @Override
237 public boolean deleteResource(Resource resource) {
238 // TODO Auto-generated method stub
239 return false;
240 }
241
242 }