f69988839a83020ef92fda9c527640e0a922b56e
[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 }
50 else if (userList.contains(user)){
51 return false;
52 }
53 else{
54 userList.add(user);
55 return true;
56 }
57 }
58
59 /**
60 * {@inheritDoc}
61 */
62 @Override
63 public Resource findResource(String resourceName) {
64 for (Resource resource : resourceList) {
65 if (resource.getName().equals(resourceName))
66 return resource;
67 }
68 return null;
69 }
70
71 /**
72 * {@inheritDoc}
73 */
74 @Override
75 public boolean addResource(Resource resource) throws Exception {
76 if (resource == null){
77 throw new IllegalArgumentException("Resource object must not be null!");
78 }
79 else if (resourceList.contains(resource)){
80 return false;
81 }
82 else{
83 for (Resource res : resourceList) {
84 if (res.getId() == resource.getId()){
85 return false;
86 }
87 }
88 resourceList.add(resource);
89 return true;
90 }
91 }
92
93 /**
94 * {@inheritDoc}
95 */
96 @Override
97 public Reservation findReservation(UUID id) {
98 for (Reservation reservation : reservationList) {
99 if (reservation.getId().equals(id))
100 return reservation;
101 }
102 return null;
103 }
104
105 /**
106 * {@inheritDoc}
107 */
108 @Override
109 public List<Reservation> findGroupReservations(UUID groupId) {
110 if (groupId == null)
111 throw new IllegalArgumentException("DateRange object must not be null!");
112 List<Reservation> groupReservations = new ArrayList<Reservation>();
113 for (Reservation reservation : reservationList) {
114 if (reservation.getGroupId().equals(groupId))
115 groupReservations.add(reservation);
116 }
117 return groupReservations;
118 }
119
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}
137 */
138 @Override
139 public List<Reservation> overlaps(DateRange range) {
140 if (range == null)
141 throw new IllegalArgumentException("DateRange object must not be null!");
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;
148 }
149
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}
164 */
165 @Override
166 public boolean addReservation(Reservation reservation) throws Exception {
167 if (reservation == null){
168 throw new IllegalArgumentException("Reservation object must not be null!");
169 }
170 else if(reservationList.contains(reservation)) {
171 return false;
172 }
173 else{
174 for (Reservation res : reservationList) {
175 if (res.getId() == reservation.getId()){
176 return false;
177 }
178 else if(res.getResource() == reservation.getResource() && res.getDateRange().overlaps(reservation.getDateRange())){
179 return false;
180 }
181 }
182 reservationList.add(reservation);
183 return true;
184 }
185 }
186
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
203 @Override
204 public boolean editReservation(Reservation reservation) {
205 if (reservation == null){
206 throw new IllegalArgumentException("Reservation object must not be null!");
207 }
208 if(findReservation(reservation.getId()) != null){
209 reservationList.remove(findReservation(reservation.getId()));
210 reservationList.add(reservation);
211 return true;
212 }
213 return false;
214 }
215
216 @Override
217 public boolean deleteReservation(Reservation reservation) {
218 if (reservation == null){
219 throw new IllegalArgumentException("Reservation object must not be null!");
220 }
221 else if(reservationList.contains(reservation)) {
222 reservationList.remove(reservation);
223 return true;
224 }
225 else{
226 return false;
227 }
228 }
229
230 @Override
231 public Set<Resource> getAllResources() {
232
233 return resourceList;
234 }
235
236 }