Merge remote-tracking branch 'origin/development'
[staff/due1/sed-hs15-srs-purple.git] / src / main / java / ch / bfh / ti / srs / datahandler / DataHandlerForTests.java
index f8939da..39d8897 100644 (file)
@@ -44,13 +44,11 @@ public class DataHandlerForTests implements DataHandler {
         */
        @Override
        public boolean addUser(User user) throws Exception {
-               if (user == null){
+               if (user == null) {
                        throw new IllegalArgumentException("User object must not be null!");
-               }
-               else if (userList.contains(user)){
+               } else if (userList.contains(user)) {
                        return false;
-               }
-               else{
+               } else {
                        userList.add(user);
                        return true;
                }
@@ -73,15 +71,13 @@ public class DataHandlerForTests implements DataHandler {
         */
        @Override
        public boolean addResource(Resource resource) throws Exception {
-               if (resource == null){
+               if (resource == null) {
                        throw new IllegalArgumentException("Resource object must not be null!");
-               }
-               else if (resourceList.contains(resource)){
+               } else if (resourceList.contains(resource)) {
                        return false;
-               }
-               else{
+               } else {
                        for (Resource res : resourceList) {
-                               if (res.getId() == resource.getId()){
+                               if (res.getId() == resource.getId()) {
                                        return false;
                                }
                        }
@@ -164,15 +160,16 @@ public class DataHandlerForTests implements DataHandler {
         */
        @Override
        public boolean addReservation(Reservation reservation) throws Exception {
-               if (reservation == null){
+               if (reservation == null) {
                        throw new IllegalArgumentException("Reservation object must not be null!");
-               }
-               else if(reservationList.contains(reservation)) {
+               } else if (reservationList.contains(reservation)) {
                        return false;
-               }
-               else{
+               } else {
                        for (Reservation res : reservationList) {
-                               if (res.getId() == reservation.getId()){
+                               if (res.getId() == reservation.getId()) {
+                                       return false;
+                               } else if (res.getResource() == reservation.getResource()
+                                               && res.getDateRange().overlaps(reservation.getDateRange())) {
                                        return false;
                                }
                        }
@@ -190,19 +187,24 @@ public class DataHandlerForTests implements DataHandler {
 
        /**
         * {@inheritDoc}
-        * @throws Exception no implementation for <code>true</code> possible
+        * @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");
+               if (discardChanges == true)
+                       throw new Exception("No implementation on DataHandlerForTests possible");
        }
 
+       /**
+        * {@inheritDoc}
+        */
        @Override
        public boolean editReservation(Reservation reservation) {
-               if (reservation == null){
+               if (reservation == null) {
                        throw new IllegalArgumentException("Reservation object must not be null!");
                }
-               if(findReservation(reservation.getId()) != null){
+               if (findReservation(reservation.getId()) != null) {
                        reservationList.remove(findReservation(reservation.getId()));
                        reservationList.add(reservation);
                        return true;
@@ -210,24 +212,71 @@ public class DataHandlerForTests implements DataHandler {
                return false;
        }
 
+       /**
+        * {@inheritDoc}
+        */
        @Override
        public boolean deleteReservation(Reservation reservation) {
-               if (reservation == null){
+               if (reservation == null) {
                        throw new IllegalArgumentException("Reservation object must not be null!");
-               }
-               else if(reservationList.contains(reservation)) {
+               } else if (reservationList.contains(reservation)) {
                        reservationList.remove(reservation);
                        return true;
-               }
-               else{
+               } else {
                        return false;
                }
        }
 
+       /**
+        * {@inheritDoc}
+        */
        @Override
        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;
+       }
 }