Create ResourceManagerController, create Modify interface in dataschema,
[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
a77189a6
M
30 /**
31 * {@inheritDoc}
7903e50e
M
32 */
33 @Override
34 public User findUser(String userName) {
35 for (User user : userList) {
25d43fa9
M
36 if (user.getUserName().equals(userName))
37 return user;
7903e50e
M
38 }
39 return null;
40 }
41
a77189a6
M
42 /**
43 * {@inheritDoc}
7903e50e
M
44 */
45 @Override
5ff98db0 46 public boolean addUser(User user) throws Exception {
66cc653d 47 if (user == null) {
25d43fa9 48 throw new IllegalArgumentException("User object must not be null!");
66cc653d 49 } else if (userList.contains(user)) {
5ff98db0 50 return false;
66cc653d 51 } else {
5ff98db0
M
52 userList.add(user);
53 return true;
54 }
7903e50e
M
55 }
56
a77189a6
M
57 /**
58 * {@inheritDoc}
7903e50e
M
59 */
60 @Override
61 public Resource findResource(String resourceName) {
62 for (Resource resource : resourceList) {
25d43fa9
M
63 if (resource.getName().equals(resourceName))
64 return resource;
7903e50e
M
65 }
66 return null;
67 }
25d43fa9 68
a77189a6
M
69 /**
70 * {@inheritDoc}
7903e50e
M
71 */
72 @Override
5ff98db0 73 public boolean addResource(Resource resource) throws Exception {
66cc653d 74 if (resource == null) {
25d43fa9 75 throw new IllegalArgumentException("Resource object must not be null!");
66cc653d 76 } else if (resourceList.contains(resource)) {
5ff98db0 77 return false;
66cc653d 78 } else {
076d9c07 79 for (Resource res : resourceList) {
66cc653d 80 if (res.getId() == resource.getId()) {
e86af888
M
81 return false;
82 }
83 }
5ff98db0
M
84 resourceList.add(resource);
85 return true;
86 }
7903e50e
M
87 }
88
a77189a6
M
89 /**
90 * {@inheritDoc}
7903e50e
M
91 */
92 @Override
be594ada 93 public Reservation findReservation(UUID id) {
7903e50e 94 for (Reservation reservation : reservationList) {
25d43fa9
M
95 if (reservation.getId().equals(id))
96 return reservation;
7903e50e
M
97 }
98 return null;
99 }
25d43fa9 100
a77189a6
M
101 /**
102 * {@inheritDoc}
25d43fa9 103 */
be594ada 104 @Override
a77189a6
M
105 public List<Reservation> findGroupReservations(UUID groupId) {
106 if (groupId == null)
107 throw new IllegalArgumentException("DateRange object must not be null!");
be594ada
M
108 List<Reservation> groupReservations = new ArrayList<Reservation>();
109 for (Reservation reservation : reservationList) {
a77189a6 110 if (reservation.getGroupId().equals(groupId))
be594ada
M
111 groupReservations.add(reservation);
112 }
113 return groupReservations;
114 }
115
a77189a6
M
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}
25d43fa9 133 */
be594ada
M
134 @Override
135 public List<Reservation> overlaps(DateRange range) {
a77189a6
M
136 if (range == null)
137 throw new IllegalArgumentException("DateRange object must not be null!");
25d43fa9
M
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;
be594ada 144 }
25d43fa9 145
a77189a6
M
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}
7903e50e
M
160 */
161 @Override
5ff98db0 162 public boolean addReservation(Reservation reservation) throws Exception {
66cc653d 163 if (reservation == null) {
25d43fa9 164 throw new IllegalArgumentException("Reservation object must not be null!");
66cc653d 165 } else if (reservationList.contains(reservation)) {
5ff98db0 166 return false;
66cc653d 167 } else {
e86af888 168 for (Reservation res : reservationList) {
66cc653d 169 if (res.getId() == reservation.getId()) {
e86af888 170 return false;
66cc653d
D
171 } else if (res.getResource() == reservation.getResource()
172 && res.getDateRange().overlaps(reservation.getDateRange())) {
aa252043
M
173 return false;
174 }
e86af888 175 }
5ff98db0
M
176 reservationList.add(reservation);
177 return true;
178 }
7903e50e 179 }
be594ada 180
003130bd
M
181 /**
182 * {@inheritDoc}
183 */
184 @Override
185 public void saveChanges() {
186 }
187
188 /**
189 * {@inheritDoc}
66cc653d
D
190 * @throws Exception
191 * no implementation for <code>true</code> possible
003130bd
M
192 */
193 @Override
194 public void refresh(boolean discardChanges) throws Exception {
66cc653d
D
195 if (discardChanges == true)
196 throw new Exception("No implementation on DataHandlerForTests possible");
003130bd
M
197 }
198
e16f8b0c
M
199 @Override
200 public boolean editReservation(Reservation reservation) {
66cc653d 201 if (reservation == null) {
e86af888
M
202 throw new IllegalArgumentException("Reservation object must not be null!");
203 }
66cc653d 204 if (findReservation(reservation.getId()) != null) {
076d9c07
M
205 reservationList.remove(findReservation(reservation.getId()));
206 reservationList.add(reservation);
207 return true;
e86af888 208 }
e16f8b0c
M
209 return false;
210 }
211
212 @Override
213 public boolean deleteReservation(Reservation reservation) {
66cc653d 214 if (reservation == null) {
e86af888 215 throw new IllegalArgumentException("Reservation object must not be null!");
66cc653d 216 } else if (reservationList.contains(reservation)) {
076d9c07 217 reservationList.remove(reservation);
e86af888 218 return true;
66cc653d 219 } else {
e86af888
M
220 return false;
221 }
e16f8b0c
M
222 }
223
224 @Override
e86af888
M
225 public Set<Resource> getAllResources() {
226
227 return resourceList;
e16f8b0c
M
228 }
229
5fca88b4
MT
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
7903e50e 242}