DataHandler 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
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
M
46 public boolean addUser(User user) throws Exception {
47 if (user == null){
25d43fa9 48 throw new IllegalArgumentException("User object must not be null!");
5ff98db0
M
49 }
50 else if (userList.contains(user)){
51 return false;
52 }
53 else{
54 userList.add(user);
55 return true;
56 }
7903e50e
M
57 }
58
a77189a6
M
59 /**
60 * {@inheritDoc}
7903e50e
M
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 70
a77189a6
M
71 /**
72 * {@inheritDoc}
7903e50e
M
73 */
74 @Override
5ff98db0
M
75 public boolean addResource(Resource resource) throws Exception {
76 if (resource == null){
25d43fa9 77 throw new IllegalArgumentException("Resource object must not be null!");
5ff98db0
M
78 }
79 else if (resourceList.contains(resource)){
80 return false;
81 }
82 else{
e86af888
M
83 for (Reservation res : reservationList) {
84 if (res.getId() == resource.getId()){
85 return false;
86 }
87 }
5ff98db0
M
88 resourceList.add(resource);
89 return true;
90 }
7903e50e
M
91 }
92
a77189a6
M
93 /**
94 * {@inheritDoc}
7903e50e
M
95 */
96 @Override
be594ada 97 public Reservation findReservation(UUID id) {
7903e50e 98 for (Reservation reservation : reservationList) {
25d43fa9
M
99 if (reservation.getId().equals(id))
100 return reservation;
7903e50e
M
101 }
102 return null;
103 }
25d43fa9 104
a77189a6
M
105 /**
106 * {@inheritDoc}
25d43fa9 107 */
be594ada 108 @Override
a77189a6
M
109 public List<Reservation> findGroupReservations(UUID groupId) {
110 if (groupId == null)
111 throw new IllegalArgumentException("DateRange object must not be null!");
be594ada
M
112 List<Reservation> groupReservations = new ArrayList<Reservation>();
113 for (Reservation reservation : reservationList) {
a77189a6 114 if (reservation.getGroupId().equals(groupId))
be594ada
M
115 groupReservations.add(reservation);
116 }
117 return groupReservations;
118 }
119
a77189a6
M
120 /**
121 * {@inheritDoc}
122 */
123 @Override
124 public List<Reservation> userReservations(User user) {
125 if (user == null)
126 throw new IllegalArgumentException("User object must not be null!");
127 List<Reservation> allUserReservations = new ArrayList<Reservation>();
128 for (Reservation reservation : reservationList) {
129 if (reservation.getUser().equals(user))
130 allUserReservations.add(reservation);
131 }
132 return allUserReservations;
133 }
134
135 /**
136 * {@inheritDoc}
25d43fa9 137 */
be594ada
M
138 @Override
139 public List<Reservation> overlaps(DateRange range) {
a77189a6
M
140 if (range == null)
141 throw new IllegalArgumentException("DateRange object must not be null!");
25d43fa9
M
142 List<Reservation> overlappingReservations = new ArrayList<Reservation>();
143 for (Reservation reservation : reservationList) {
144 if (reservation.getDateRange().overlaps(range))
145 overlappingReservations.add(reservation);
146 }
147 return overlappingReservations;
be594ada 148 }
25d43fa9 149
a77189a6
M
150 /**
151 * {@inheritDoc}
152 */
153 @Override
154 public List<Reservation> findAllReservations() {
155 List<Reservation> allReservations = new ArrayList<Reservation>();
156 for (Reservation reservation : reservationList) {
157 allReservations.add(reservation);
158 }
159 return allReservations;
160 }
161
162 /**
163 * {@inheritDoc}
7903e50e
M
164 */
165 @Override
5ff98db0
M
166 public boolean addReservation(Reservation reservation) throws Exception {
167 if (reservation == null){
25d43fa9 168 throw new IllegalArgumentException("Reservation object must not be null!");
5ff98db0
M
169 }
170 else if(resourceList.contains(reservation)) {
171 return false;
172 }
173 else{
e86af888
M
174 for (Reservation res : reservationList) {
175 if (res.getId() == reservation.getId()){
176 return false;
177 }
178 }
5ff98db0
M
179 reservationList.add(reservation);
180 return true;
181 }
7903e50e 182 }
be594ada 183
003130bd
M
184 /**
185 * {@inheritDoc}
186 */
187 @Override
188 public void saveChanges() {
189 }
190
191 /**
192 * {@inheritDoc}
193 * @throws Exception no implementation for <code>true</code> possible
194 */
195 @Override
196 public void refresh(boolean discardChanges) throws Exception {
197 if(discardChanges == true) throw new Exception("No implementation on DataHandlerForTests possible");
198 }
199
e16f8b0c
M
200 @Override
201 public boolean editReservation(Reservation reservation) {
e86af888
M
202 if (reservation == null){
203 throw new IllegalArgumentException("Reservation object must not be null!");
204 }
205 for (Reservation res : reservationList) {
206 if (res.getId() == (reservation.getId())){
207 reservationList.remove(res);
208 reservationList.add(reservation);
209 return true;
210 }
211 }
e16f8b0c
M
212 return false;
213 }
214
215 @Override
216 public boolean deleteReservation(Reservation reservation) {
e86af888
M
217 if (reservation == null){
218 throw new IllegalArgumentException("Reservation object must not be null!");
219 }
220 else if(resourceList.contains(reservation)) {
221 resourceList.remove(reservation);
222 return true;
223 }
224 else{
225 return false;
226 }
e16f8b0c
M
227 }
228
229 @Override
e86af888
M
230 public Set<Resource> getAllResources() {
231
232 return resourceList;
e16f8b0c
M
233 }
234
7903e50e 235}