throws Exception added on refresh in DataHander interface
[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 void addUser(User user) throws Exception {
47 if (user == null)
48 throw new IllegalArgumentException("User object must not be null!");
49 if (userList.contains(user))
50 throw new IllegalArgumentException("User already exists!");
51 userList.add(user);
52 }
53
54 /**
55 * {@inheritDoc}
56 */
57 @Override
58 public Resource findResource(String resourceName) {
59 for (Resource resource : resourceList) {
60 if (resource.getName().equals(resourceName))
61 return resource;
62 }
63 return null;
64 }
65
66 /**
67 * {@inheritDoc}
68 */
69 @Override
70 public void addResource(Resource resource) throws Exception {
71 if (resource == null)
72 throw new IllegalArgumentException("Resource object must not be null!");
73 if (resourceList.contains(resource))
74 throw new IllegalArgumentException("Resource already exists!");
75 resourceList.add(resource);
76 }
77
78 /**
79 * {@inheritDoc}
80 */
81 @Override
82 public Reservation findReservation(UUID id) {
83 for (Reservation reservation : reservationList) {
84 if (reservation.getId().equals(id))
85 return reservation;
86 }
87 return null;
88 }
89
90 /**
91 * {@inheritDoc}
92 */
93 @Override
94 public List<Reservation> findGroupReservations(UUID groupId) {
95 if (groupId == null)
96 throw new IllegalArgumentException("DateRange object must not be null!");
97 List<Reservation> groupReservations = new ArrayList<Reservation>();
98 for (Reservation reservation : reservationList) {
99 if (reservation.getGroupId().equals(groupId))
100 groupReservations.add(reservation);
101 }
102 return groupReservations;
103 }
104
105 /**
106 * {@inheritDoc}
107 */
108 @Override
109 public List<Reservation> userReservations(User user) {
110 if (user == null)
111 throw new IllegalArgumentException("User object must not be null!");
112 List<Reservation> allUserReservations = new ArrayList<Reservation>();
113 for (Reservation reservation : reservationList) {
114 if (reservation.getUser().equals(user))
115 allUserReservations.add(reservation);
116 }
117 return allUserReservations;
118 }
119
120 /**
121 * {@inheritDoc}
122 */
123 @Override
124 public List<Reservation> overlaps(DateRange range) {
125 if (range == null)
126 throw new IllegalArgumentException("DateRange object must not be null!");
127 List<Reservation> overlappingReservations = new ArrayList<Reservation>();
128 for (Reservation reservation : reservationList) {
129 if (reservation.getDateRange().overlaps(range))
130 overlappingReservations.add(reservation);
131 }
132 return overlappingReservations;
133 }
134
135 /**
136 * {@inheritDoc}
137 */
138 @Override
139 public List<Reservation> findAllReservations() {
140 List<Reservation> allReservations = new ArrayList<Reservation>();
141 for (Reservation reservation : reservationList) {
142 allReservations.add(reservation);
143 }
144 return allReservations;
145 }
146
147 /**
148 * {@inheritDoc}
149 */
150 @Override
151 public void addReservation(Reservation reservation) throws Exception {
152 if (reservation == null)
153 throw new IllegalArgumentException("Reservation object must not be null!");
154 if (resourceList.contains(reservation))
155 throw new IllegalArgumentException("Reservation already exists!");
156 reservationList.add(reservation);
157 }
158
159 /**
160 * {@inheritDoc}
161 */
162 @Override
163 public void saveChanges() {
164 }
165
166 /**
167 * {@inheritDoc}
168 * @throws Exception no implementation for <code>true</code> possible
169 */
170 @Override
171 public void refresh(boolean discardChanges) throws Exception {
172 if(discardChanges == true) throw new Exception("No implementation on DataHandlerForTests possible");
173 }
174
175 }