DataHandler update
[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{
076d9c07 83 for (Resource res : resourceList) {
e86af888
M
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 169 }
076d9c07 170 else if(reservationList.contains(reservation)) {
5ff98db0
M
171 return false;
172 }
173 else{
e86af888
M
174 for (Reservation res : reservationList) {
175 if (res.getId() == reservation.getId()){
176 return false;
177 }
aa252043
M
178 else if(res.getResource() == reservation.getResource() && res.getDateRange().overlaps(reservation.getDateRange())){
179 return false;
180 }
e86af888 181 }
5ff98db0
M
182 reservationList.add(reservation);
183 return true;
184 }
7903e50e 185 }
be594ada 186
003130bd
M
187 /**
188 * {@inheritDoc}
189 */
190 @Override
191 public void saveChanges() {
192 }
193
194 /**
195 * {@inheritDoc}
196 * @throws Exception no implementation for <code>true</code> possible
197 */
198 @Override
199 public void refresh(boolean discardChanges) throws Exception {
200 if(discardChanges == true) throw new Exception("No implementation on DataHandlerForTests possible");
201 }
202
e16f8b0c
M
203 @Override
204 public boolean editReservation(Reservation reservation) {
e86af888
M
205 if (reservation == null){
206 throw new IllegalArgumentException("Reservation object must not be null!");
207 }
076d9c07
M
208 if(findReservation(reservation.getId()) != null){
209 reservationList.remove(findReservation(reservation.getId()));
210 reservationList.add(reservation);
211 return true;
e86af888 212 }
e16f8b0c
M
213 return false;
214 }
215
216 @Override
217 public boolean deleteReservation(Reservation reservation) {
e86af888
M
218 if (reservation == null){
219 throw new IllegalArgumentException("Reservation object must not be null!");
220 }
076d9c07
M
221 else if(reservationList.contains(reservation)) {
222 reservationList.remove(reservation);
e86af888
M
223 return true;
224 }
225 else{
226 return false;
227 }
e16f8b0c
M
228 }
229
230 @Override
e86af888
M
231 public Set<Resource> getAllResources() {
232
233 return resourceList;
e16f8b0c
M
234 }
235
7903e50e 236}