DataHandler update
[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 resourceList.add(resource);
84 return true;
85 }
86 }
87
88 /**
89 * {@inheritDoc}
90 */
91 @Override
92 public Reservation findReservation(UUID id) {
93 for (Reservation reservation : reservationList) {
94 if (reservation.getId().equals(id))
95 return reservation;
96 }
97 return null;
98 }
99
100 /**
101 * {@inheritDoc}
102 */
103 @Override
104 public List<Reservation> findGroupReservations(UUID groupId) {
105 if (groupId == null)
106 throw new IllegalArgumentException("DateRange object must not be null!");
107 List<Reservation> groupReservations = new ArrayList<Reservation>();
108 for (Reservation reservation : reservationList) {
109 if (reservation.getGroupId().equals(groupId))
110 groupReservations.add(reservation);
111 }
112 return groupReservations;
113 }
114
115 /**
116 * {@inheritDoc}
117 */
118 @Override
119 public List<Reservation> userReservations(User user) {
120 if (user == null)
121 throw new IllegalArgumentException("User object must not be null!");
122 List<Reservation> allUserReservations = new ArrayList<Reservation>();
123 for (Reservation reservation : reservationList) {
124 if (reservation.getUser().equals(user))
125 allUserReservations.add(reservation);
126 }
127 return allUserReservations;
128 }
129
130 /**
131 * {@inheritDoc}
132 */
133 @Override
134 public List<Reservation> overlaps(DateRange range) {
135 if (range == null)
136 throw new IllegalArgumentException("DateRange object must not be null!");
137 List<Reservation> overlappingReservations = new ArrayList<Reservation>();
138 for (Reservation reservation : reservationList) {
139 if (reservation.getDateRange().overlaps(range))
140 overlappingReservations.add(reservation);
141 }
142 return overlappingReservations;
143 }
144
145 /**
146 * {@inheritDoc}
147 */
148 @Override
149 public List<Reservation> findAllReservations() {
150 List<Reservation> allReservations = new ArrayList<Reservation>();
151 for (Reservation reservation : reservationList) {
152 allReservations.add(reservation);
153 }
154 return allReservations;
155 }
156
157 /**
158 * {@inheritDoc}
159 */
160 @Override
161 public boolean addReservation(Reservation reservation) throws Exception {
162 if (reservation == null){
163 throw new IllegalArgumentException("Reservation object must not be null!");
164 }
165 else if(resourceList.contains(reservation)) {
166 return false;
167 }
168 else{
169 reservationList.add(reservation);
170 return true;
171 }
172 }
173
174 /**
175 * {@inheritDoc}
176 */
177 @Override
178 public void saveChanges() {
179 }
180
181 /**
182 * {@inheritDoc}
183 * @throws Exception no implementation for <code>true</code> possible
184 */
185 @Override
186 public void refresh(boolean discardChanges) throws Exception {
187 if(discardChanges == true) throw new Exception("No implementation on DataHandlerForTests possible");
188 }
189
190 }