Merge remote-tracking branch 'origin/development'
[staff/due1/sed-hs15-srs-purple.git] / src / main / java / ch / bfh / ti / srs / datahandler / DataHandlerForTests.java
index 8f33e83..39d8897 100644 (file)
@@ -18,91 +18,265 @@ 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{
-       Set<User> userList = new HashSet<User>();
-       Set<Resource> resourceList = new HashSet<Resource>();
-       Set<Reservation> reservationList = new HashSet<Reservation>();
-       
-       public DataHandlerForTests(){
-               
+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() {
+
        }
 
-       /* (non-Javadoc)
-        * @see ch.bfh.ti.srs.dataschema.DataHandler#findUser(java.lang.String)
+       /**
+        * {@inheritDoc}
         */
        @Override
        public User findUser(String userName) {
                for (User user : userList) {
-                       if(user.getUserName().equals(userName)) return user;
+                       if (user.getUserName().equals(userName))
+                               return user;
                }
                return null;
        }
 
-       /* (non-Javadoc)
-        * @see ch.bfh.ti.srs.dataschema.DataHandler#addUser(ch.bfh.ti.srs.dataschema.User)
+       /**
+        * {@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);
+       public boolean addUser(User user) throws Exception {
+               if (user == null) {
+                       throw new IllegalArgumentException("User object must not be null!");
+               } else if (userList.contains(user)) {
+                       return false;
+               } else {
+                       userList.add(user);
+                       return true;
+               }
        }
 
-       /* (non-Javadoc)
-        * @see ch.bfh.ti.srs.dataschema.DataHandler#findResource(java.lang.String)
+       /**
+        * {@inheritDoc}
         */
        @Override
        public Resource findResource(String resourceName) {
                for (Resource resource : resourceList) {
-                       if(resource.getName().equals(resourceName)) return resource;
+                       if (resource.getName().equals(resourceName))
+                               return resource;
                }
                return null;
        }
-       /* (non-Javadoc)
-        * @see ch.bfh.ti.srs.dataschema.DataHandler#addResource(ch.bfh.ti.srs.dataschema.Resource)
+
+       /**
+        * {@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);
+       public boolean addResource(Resource resource) throws Exception {
+               if (resource == null) {
+                       throw new IllegalArgumentException("Resource object must not be null!");
+               } else if (resourceList.contains(resource)) {
+                       return false;
+               } else {
+                       for (Resource res : resourceList) {
+                               if (res.getId() == resource.getId()) {
+                                       return false;
+                               }
+                       }
+                       resourceList.add(resource);
+                       return true;
+               }
        }
 
-       /* (non-Javadoc)
-        * @see ch.bfh.ti.srs.dataschema.DataHandler#findReservation(java.lang.String)
+       /**
+        * {@inheritDoc}
         */
        @Override
        public Reservation findReservation(UUID id) {
                for (Reservation reservation : reservationList) {
-                       if(reservation.getId().equals(id)) return reservation;
+                       if (reservation.getId().equals(id))
+                               return reservation;
                }
                return null;
        }
-       
+
+       /**
+        * {@inheritDoc}
+        */
        @Override
-       public List<Reservation> findGroupReservations(UUID groupID) {
+       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))
+                       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) {
-               // TODO Auto-generated method stub
-               return null;
+               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 boolean addReservation(Reservation reservation) throws Exception {
+               if (reservation == null) {
+                       throw new IllegalArgumentException("Reservation object must not be null!");
+               } else if (reservationList.contains(reservation)) {
+                       return false;
+               } else {
+                       for (Reservation res : reservationList) {
+                               if (res.getId() == reservation.getId()) {
+                                       return false;
+                               } else if (res.getResource() == reservation.getResource()
+                                               && res.getDateRange().overlaps(reservation.getDateRange())) {
+                                       return false;
+                               }
+                       }
+                       reservationList.add(reservation);
+                       return true;
+               }
+       }
+
+       /**
+        * {@inheritDoc}
+        */
+       @Override
+       public void saveChanges() {
        }
-       
-       /* (non-Javadoc)
-        * @see ch.bfh.ti.srs.dataschema.DataHandler#addReservation(ch.bfh.ti.srs.dataschema.Reservation)
+
+       /**
+        * {@inheritDoc}
+        * @throws Exception
+        *             no implementation for <code>true</code> possible
+        */
+       @Override
+       public void refresh(boolean discardChanges) throws Exception {
+               if (discardChanges == true)
+                       throw new Exception("No implementation on DataHandlerForTests possible");
+       }
+
+       /**
+        * {@inheritDoc}
+        */
+       @Override
+       public boolean editReservation(Reservation reservation) {
+               if (reservation == null) {
+                       throw new IllegalArgumentException("Reservation object must not be null!");
+               }
+               if (findReservation(reservation.getId()) != null) {
+                       reservationList.remove(findReservation(reservation.getId()));
+                       reservationList.add(reservation);
+                       return true;
+               }
+               return false;
+       }
+
+       /**
+        * {@inheritDoc}
+        */
+       @Override
+       public boolean deleteReservation(Reservation reservation) {
+               if (reservation == null) {
+                       throw new IllegalArgumentException("Reservation object must not be null!");
+               } else if (reservationList.contains(reservation)) {
+                       reservationList.remove(reservation);
+                       return true;
+               } else {
+                       return false;
+               }
+       }
+
+       /**
+        * {@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);       
+       public Set<Resource> getAllResources() {
+
+               return resourceList;
+       }
+
+       /**
+        * {@inheritDoc}
+        */
+       @Override
+       public boolean editResource(Resource resource) {
+               if (resource == null) {
+                       throw new IllegalArgumentException("Resource object must not be null!");
+               }
+               if (findResource(resource.getName()) != null) {
+                       resourceList.remove(findResource(resource.getName()));
+                       resourceList.add(resource);
+                       return true;
+               }
+               return false;
+       }
+
+       /**
+        * {@inheritDoc}
+        */
+       @Override
+       public boolean deleteResource(Resource resource) {
+               if (resource == null) {
+                       throw new IllegalArgumentException("Resource object must not be null!");
+               } else if (resourceList.contains(resource)) {
+                       resourceList.remove(resource);
+                       return true;
+               } else {
+                       return false;
+               }
        }
 
+       @Override
+       public boolean editUser(User user) {
+               if (user == null) {
+                       throw new IllegalArgumentException("User object must not be null!");
+               }
+               if (findUser(user.getUserName()) != null) {
+                       userList.remove(findUser(user.getUserName()));
+                       userList.add(user);
+                       return true;
+               }
+               return false;
+       }
 }