DataHandler angepasst
[staff/due1/sed-hs15-srs-purple.git] / src / main / java / ch / bfh / ti / srs / datahandler / DataHandlerForTests.java
index dcd7579..78d9126 100644 (file)
-/*\r
- * Copyright (c) 2015 Berner Fachhochschule, Switzerland.\r
- *\r
- * Project Smart Reservation System.\r
- *\r
- * Distributable under GPL license. See terms of license at gnu.org.\r
- */\r
-package ch.bfh.ti.srs.datahandler;\r
-\r
-import java.util.HashSet;\r
-import java.util.Set;\r
-\r
-import ch.bfh.ti.srs.dataschema.User;\r
-\r
-public class DataHandlerForTests implements DataHandler{\r
-       Set<User> userList = new HashSet<User>();\r
-       \r
-       public DataHandlerForTests(){\r
-               \r
-       }\r
-\r
-       /* (non-Javadoc)\r
-        * @see ch.bfh.ti.srs.core.DataHandler#findUser(java.lang.String)\r
-        */\r
-       @Override\r
-       public User findUser(String userName) {\r
-               for (User user : userList) {\r
-                       if(user.getUserName().equals(userName)) return user;\r
-               }\r
-               return null;\r
-       }\r
-\r
-       /* (non-Javadoc)\r
-        * @see ch.bfh.ti.srs.core.DataHandler#addUser(ch.bfh.ti.srs.core.User)\r
-        */\r
-       @Override\r
-       public void addUser(User user) throws Exception {\r
-               if(user == null) throw new IllegalArgumentException("User object must not be null!");\r
-               userList.add(user);\r
-       }\r
-}\r
+/*
+ * Copyright (c) 2015 Berner Fachhochschule, Switzerland.
+ *
+ * Project Smart Reservation System.
+ *
+ * Distributable under GPL license. See terms of license at gnu.org.
+ */
+package ch.bfh.ti.srs.datahandler;
+
+import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+import java.util.UUID;
+
+import ch.bfh.ti.daterange.DateRange;
+import ch.bfh.ti.srs.dataschema.Reservation;
+import ch.bfh.ti.srs.dataschema.Resource;
+import ch.bfh.ti.srs.dataschema.User;
+
+public class DataHandlerForTests implements DataHandler {
+       private Set<User> userList = new HashSet<User>();
+       private Set<Resource> resourceList = new HashSet<Resource>();
+       private Set<Reservation> reservationList = new HashSet<Reservation>();
+
+       public DataHandlerForTests() {
+
+       }
+
+       /**
+        * {@inheritDoc}
+        */
+       @Override
+       public User findUser(String userName) {
+               for (User user : userList) {
+                       if (user.getUserName().equals(userName))
+                               return user;
+               }
+               return null;
+       }
+
+       /**
+        * {@inheritDoc}
+        */
+       @Override
+       public void addUser(User user) throws Exception {
+               if (user == null)
+                       throw new IllegalArgumentException("User object must not be null!");
+               if (userList.contains(user))
+                       throw new IllegalArgumentException("User already  exists!");
+               userList.add(user);
+       }
+
+       /**
+        * {@inheritDoc}
+        */
+       @Override
+       public Resource findResource(String resourceName) {
+               for (Resource resource : resourceList) {
+                       if (resource.getName().equals(resourceName))
+                               return resource;
+               }
+               return null;
+       }
+
+       /**
+        * {@inheritDoc}
+        */
+       @Override
+       public void addResource(Resource resource) throws Exception {
+               if (resource == null)
+                       throw new IllegalArgumentException("Resource object must not be null!");
+               if (resourceList.contains(resource))
+                       throw new IllegalArgumentException("Resource already  exists!");
+               resourceList.add(resource);
+       }
+
+       /**
+        * {@inheritDoc}
+        */
+       @Override
+       public Reservation findReservation(UUID id) {
+               for (Reservation reservation : reservationList) {
+                       if (reservation.getId().equals(id))
+                               return reservation;
+               }
+               return null;
+       }
+
+       /**
+        * {@inheritDoc}
+        */
+       @Override
+       public List<Reservation> findGroupReservations(UUID groupId) {
+               if (groupId == null)
+                       throw new IllegalArgumentException("DateRange object must not be null!");
+               List<Reservation> groupReservations = new ArrayList<Reservation>();
+               for (Reservation reservation : reservationList) {
+                       if (reservation.getGroupId().equals(groupId))
+                               groupReservations.add(reservation);
+               }
+               return groupReservations;
+       }
+
+       /**
+        * {@inheritDoc}
+        */
+       @Override
+       public List<Reservation> userReservations(User user) {
+               if (user == null)
+                       throw new IllegalArgumentException("User object must not be null!");
+               List<Reservation> allUserReservations = new ArrayList<Reservation>();
+               for (Reservation reservation : reservationList) {
+                       if (reservation.getUser().equals(user))
+                               allUserReservations.add(reservation);
+               }
+               return allUserReservations;
+       }
+
+       /**
+        * {@inheritDoc}
+        */
+       @Override
+       public List<Reservation> overlaps(DateRange range) {
+               if (range == null)
+                       throw new IllegalArgumentException("DateRange object must not be null!");
+               List<Reservation> overlappingReservations = new ArrayList<Reservation>();
+               for (Reservation reservation : reservationList) {
+                       if (reservation.getDateRange().overlaps(range))
+                               overlappingReservations.add(reservation);
+               }
+               return overlappingReservations;
+       }
+
+       /**
+        * {@inheritDoc}
+        */
+       @Override
+       public List<Reservation> findAllReservations() {
+               List<Reservation> allReservations = new ArrayList<Reservation>();
+               for (Reservation reservation : reservationList) {
+                       allReservations.add(reservation);
+               }
+               return allReservations;
+       }
+
+       /**
+        * {@inheritDoc}
+        */
+       @Override
+       public void addReservation(Reservation reservation) throws Exception {
+               if (reservation == null)
+                       throw new IllegalArgumentException("Reservation object must not be null!");
+               if (resourceList.contains(reservation))
+                       throw new IllegalArgumentException("Reservation already  exists!");
+               reservationList.add(reservation);
+       }
+
+}