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{
83 resourceList.add(resource);
84 return true;
85 }
7903e50e
M
86 }
87
a77189a6
M
88 /**
89 * {@inheritDoc}
7903e50e
M
90 */
91 @Override
be594ada 92 public Reservation findReservation(UUID id) {
7903e50e 93 for (Reservation reservation : reservationList) {
25d43fa9
M
94 if (reservation.getId().equals(id))
95 return reservation;
7903e50e
M
96 }
97 return null;
98 }
25d43fa9 99
a77189a6
M
100 /**
101 * {@inheritDoc}
25d43fa9 102 */
be594ada 103 @Override
a77189a6
M
104 public List<Reservation> findGroupReservations(UUID groupId) {
105 if (groupId == null)
106 throw new IllegalArgumentException("DateRange object must not be null!");
be594ada
M
107 List<Reservation> groupReservations = new ArrayList<Reservation>();
108 for (Reservation reservation : reservationList) {
a77189a6 109 if (reservation.getGroupId().equals(groupId))
be594ada
M
110 groupReservations.add(reservation);
111 }
112 return groupReservations;
113 }
114
a77189a6
M
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}
25d43fa9 132 */
be594ada
M
133 @Override
134 public List<Reservation> overlaps(DateRange range) {
a77189a6
M
135 if (range == null)
136 throw new IllegalArgumentException("DateRange object must not be null!");
25d43fa9
M
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;
be594ada 143 }
25d43fa9 144
a77189a6
M
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}
7903e50e
M
159 */
160 @Override
5ff98db0
M
161 public boolean addReservation(Reservation reservation) throws Exception {
162 if (reservation == null){
25d43fa9 163 throw new IllegalArgumentException("Reservation object must not be null!");
5ff98db0
M
164 }
165 else if(resourceList.contains(reservation)) {
166 return false;
167 }
168 else{
169 reservationList.add(reservation);
170 return true;
171 }
7903e50e 172 }
be594ada 173
003130bd
M
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
7903e50e 190}