DataHandler angepasst
[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 Set<User> userList = new HashSet<User>();
23 Set<Resource> resourceList = new HashSet<Resource>();
24 Set<Reservation> reservationList = new HashSet<Reservation>();
25
26 public DataHandlerForTests() {
27
28 }
29
30 /*
31 * (non-Javadoc)
32 * @see ch.bfh.ti.srs.dataschema.DataHandler#findUser(java.lang.String)
33 */
34 @Override
35 public User findUser(String userName) {
36 for (User user : userList) {
37 if (user.getUserName().equals(userName))
38 return user;
39 }
40 return null;
41 }
42
43 /*
44 * (non-Javadoc)
45 * @see
46 * ch.bfh.ti.srs.dataschema.DataHandler#addUser(ch.bfh.ti.srs.dataschema.
47 * User)
48 */
49 @Override
50 public void addUser(User user) throws Exception {
51 if (user == null)
52 throw new IllegalArgumentException("User object must not be null!");
53 if (userList.contains(user))
54 throw new IllegalArgumentException("User already exists!");
55 userList.add(user);
56 }
57
58 /*
59 * (non-Javadoc)
60 * @see ch.bfh.ti.srs.dataschema.DataHandler#findResource(java.lang.String)
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 * (non-Javadoc)
73 * @see
74 * ch.bfh.ti.srs.dataschema.DataHandler#addResource(ch.bfh.ti.srs.dataschema
75 * .Resource)
76 */
77 @Override
78 public void addResource(Resource resource) throws Exception {
79 if (resource == null)
80 throw new IllegalArgumentException("Resource object must not be null!");
81 if (resourceList.contains(resource))
82 throw new IllegalArgumentException("Resource already exists!");
83 resourceList.add(resource);
84 }
85
86 /*
87 * (non-Javadoc)
88 * @see
89 * ch.bfh.ti.srs.dataschema.DataHandler#findReservation(java.lang.String)
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 * (non-Javadoc)
102 * @see
103 * ch.bfh.ti.srs.dataschema.DataHandler#findGroupReservations(ch.bfh.ti.srs.
104 * dataschema.Reservation)
105 */
106 @Override
107 public List<Reservation> findGroupReservations(UUID groupID) {
108 List<Reservation> groupReservations = new ArrayList<Reservation>();
109 for (Reservation reservation : reservationList) {
110 if (reservation.getGroupId().equals(groupID))
111 groupReservations.add(reservation);
112 }
113 return groupReservations;
114 }
115
116 /*
117 * (non-Javadoc)
118 * @see
119 * ch.bfh.ti.srs.dataschema.DataHandler#overlaps(ch.bfh.ti.srs.dataschema.
120 * Reservation)
121 */
122 @Override
123 public List<Reservation> overlaps(DateRange range) {
124 List<Reservation> overlappingReservations = new ArrayList<Reservation>();
125 for (Reservation reservation : reservationList) {
126 if (reservation.getDateRange().overlaps(range))
127 overlappingReservations.add(reservation);
128 }
129 return overlappingReservations;
130 }
131
132 /*
133 * (non-Javadoc)
134 * @see ch.bfh.ti.srs.dataschema.DataHandler#addReservation(ch.bfh.ti.srs.
135 * dataschema.Reservation)
136 */
137 @Override
138 public void addReservation(Reservation reservation) throws Exception {
139 if (reservation == null)
140 throw new IllegalArgumentException("Reservation object must not be null!");
141 if (resourceList.contains(reservation))
142 throw new IllegalArgumentException("Reservation already exists!");
143 reservationList.add(reservation);
144 }
145
146 }