removed all checkstyle errors, updated sprint backlog
[staff/due1/sed-hs15-srs-purple.git] / src / main / java / ch / bfh / ti / srs / controller / UserController.java
index 49dbbd1..18ce351 100644 (file)
@@ -28,64 +28,81 @@ import ch.bfh.ti.srs.dataschema.User;
 public class UserController {\r
        private User currentUser;\r
        private DataHandler dh;\r
-       \r
-       public UserController(){\r
+\r
+       public UserController() {\r
        }\r
-       \r
+\r
        /**\r
-        * Sets the <code>DataHandler</code> implementation for this Controller, must be set before using all other methods!\r
-        * \r
-        * @param dh    DataHandler implementation (i.e. DataHandlerForExcel)\r
+        * Sets the <code>DataHandler</code> implementation for this Controller,\r
+        * must be set before using all other methods!\r
+        * @param dh\r
+        *            DataHandler implementation (i.e. DataHandlerForExcel)\r
         */\r
-       public void setDataHandler(DataHandler dh){\r
+       public void setDataHandler(DataHandler dh) {\r
                this.dh = dh;\r
        }\r
-       \r
+\r
        /**\r
         * Tries to log in a <code>User</code> with the given credentials\r
-        * \r
-        * @param userName                                      user name\r
-        * @param password                                      the stored password for the given user name\r
-        * @return                                                      <code>True</code> if the user could log in successfully,\r
-        *                                                                      <code>False</code> either the username or the password was not correct.\r
-        * @throws Exception                            no <code>DataHandler</code> set\r
-        * @throws AlreadyBoundException        a <code>User</code> is already logged in\r
+        * @param userName\r
+        *            user name\r
+        * @param password\r
+        *            the stored password for the given user name\r
+        * @return <code>True</code> if the user could log in successfully,\r
+        *         <code>False</code> either the username or the password was not\r
+        *         correct.\r
+        * @throws Exception\r
+        *             no <code>DataHandler</code> set\r
+        * @throws AlreadyBoundException\r
+        *             a <code>User</code> is already logged in\r
         */\r
-       public boolean logIn(String userName, String password) throws Exception{\r
-               if(dh == null) throw new Exception("No DataHandler set");\r
-               if(currentUser != null) throw new AlreadyBoundException("A user is already logged in");\r
+       public boolean logIn(String userName, String password) throws Exception {\r
+               if (dh == null)\r
+                       throw new Exception("No DataHandler set");\r
+               if (currentUser != null)\r
+                       throw new AlreadyBoundException("A user is already logged in");\r
                User tempUser = dh.findUser(userName);\r
-               if(tempUser == null || !tempUser.checkPassword(password)) return false;\r
-               else{\r
+               if (tempUser == null || !tempUser.checkPassword(password))\r
+                       return false;\r
+               else {\r
                        currentUser = tempUser;\r
                        return true;\r
                }\r
        }\r
-       \r
+\r
        /**\r
         * Logs out the current logged user\r
-        * \r
-        * @throws Exception either no <code>DataHandler</code> is set or no user is logged in\r
+        * @throws Exception\r
+        *             either no <code>DataHandler</code> is set or no user is\r
+        *             logged in\r
         */\r
-       public void logOut() throws Exception{\r
-               if(dh == null) throw new Exception("No DataHandler set");\r
-               if(currentUser == null) throw new Exception("No user is logged in");\r
+       public void logOut() throws Exception {\r
+               if (dh == null)\r
+                       throw new Exception("No DataHandler set");\r
+               if (currentUser == null)\r
+                       throw new Exception("No user is logged in");\r
                currentUser = null;\r
        }\r
-       \r
+\r
        /**\r
         * Registers a new user to the current DataHandler\r
-        * \r
-        * @param userName              unique user name\r
-        * @param password              plain-text password\r
-        * @param email                 e-mail address\r
-        * @param name                  name\r
-        * @param lastname              last name\r
-        * @throws Exception    no <code>DataHandler</code> set\r
+        * @param userName\r
+        *            unique user name\r
+        * @param password\r
+        *            plain-text password\r
+        * @param email\r
+        *            e-mail address\r
+        * @param name\r
+        *            name\r
+        * @param lastname\r
+        *            last name\r
+        * @throws Exception\r
+        *             no <code>DataHandler</code> set\r
         */\r
-       public void addUser(String userName, String password, String email, String name, String lastname) throws Exception{\r
-               if(dh == null) throw new Exception("No DataHandler set");\r
-               User newUser = new User(userName,password);\r
+       public void addUser(String userName, String password, String email, String name, String lastname) throws Exception {\r
+               if (dh == null)\r
+                       throw new Exception("No DataHandler set");\r
+               User newUser = new User(userName, password);\r
                newUser.setMail(email);\r
                newUser.setName(name);\r
                newUser.setLastname(lastname);\r
@@ -94,91 +111,103 @@ public class UserController {
 \r
        /**\r
         * Gets the user name of the current user\r
-        * \r
-        * @deprecated                  use <code>getLoggedUser().getUserName()</code>\r
-        * @return                              user name\r
-        * @throws      Exception       no <code>DataHandler</code> is set\r
+        * @deprecated use <code>getLoggedUser().getUserName()</code>\r
+        * @return user name\r
+        * @throws Exception\r
+        *             no <code>DataHandler</code> is set\r
         */\r
-       @Deprecated \r
-       public String getLoggedUserName() throws Exception{\r
-               if(currentUser == null) throw new Exception("No user is logged in");\r
+       @Deprecated\r
+       public String getLoggedUserName() throws Exception {\r
+               if (currentUser == null)\r
+                       throw new Exception("No user is logged in");\r
                return currentUser.getUserName();\r
        }\r
 \r
        /**\r
         * Gets the mail address of the current user\r
-        * \r
         * @deprecated use <code>getLoggedUser().getMail()</code>\r
-        * @return      Mail address\r
-        * @throws      Exception       no user is logged in\r
+        * @return Mail address\r
+        * @throws Exception\r
+        *             no user is logged in\r
         */\r
        @Deprecated\r
-       public String getLoggedMail() throws Exception{\r
-               if(currentUser == null) throw new Exception("No user is logged in");\r
+       public String getLoggedMail() throws Exception {\r
+               if (currentUser == null)\r
+                       throw new Exception("No user is logged in");\r
                return currentUser.getMail();\r
        }\r
 \r
        /**\r
         * Gets the first name of the current user\r
-        * \r
         * @deprecated use <code>getLoggedUser().getName()</code>\r
-        * @return      First name\r
-        * @throws      Exception       no user is logged in\r
+        * @return First name\r
+        * @throws Exception\r
+        *             no user is logged in\r
         */\r
        @Deprecated\r
-       public String getLoggedName() throws Exception{\r
-               if(currentUser == null) throw new Exception("No user is logged in");\r
+       public String getLoggedName() throws Exception {\r
+               if (currentUser == null)\r
+                       throw new Exception("No user is logged in");\r
                return currentUser.getName();\r
        }\r
 \r
        /**\r
         * Gets the last name of the current user\r
-        * \r
         * @deprecated use <code>getLoggedUser().getLastName()</code>\r
-        * @return      Last name\r
-        * @throws      Exception       no user is logged in\r
+        * @return Last name\r
+        * @throws Exception\r
+        *             no user is logged in\r
         */\r
        @Deprecated\r
-       public String getLoggedLastname() throws Exception{\r
-               if(currentUser == null) throw new Exception("No user is logged in");\r
+       public String getLoggedLastname() throws Exception {\r
+               if (currentUser == null)\r
+                       throw new Exception("No user is logged in");\r
                return currentUser.getLastname();\r
        }\r
-       \r
+\r
        /**\r
         * Gets the current logged <code>User</code>\r
-        * \r
-        * @return      current <code>User</code>, or <code>null</code> if no <code>User</code> is logged in\r
+        * @return current <code>User</code>, or <code>null</code> if no\r
+        *         <code>User</code> is logged in\r
         */\r
-       public User getLoggedUser(){\r
-               if(currentUser == null) return null;\r
+       public User getLoggedUser() {\r
+               if (currentUser == null)\r
+                       return null;\r
                return currentUser;\r
        }\r
-       \r
+\r
        /**\r
         * Searches the set DataHandler for a specific <code>User</code>\r
-        * \r
-        * @param userName              <code>User</code>\r
-        * @return                              clone of <code>User</code>\r
-        * @throws Exception    No <code>DataHandler</code> is set\r
+        * @param userName\r
+        *            <code>User</code>\r
+        * @return clone of <code>User</code>\r
+        * @throws Exception\r
+        *             No <code>DataHandler</code> is set\r
         */\r
-       public User getUser(String userName) throws Exception{\r
-               if(dh == null) throw new Exception("No DataHandler set");\r
+       public User getUser(String userName) throws Exception {\r
+               if (dh == null)\r
+                       throw new Exception("No DataHandler set");\r
                return dh.findUser(userName).clone();\r
        }\r
-       \r
+\r
        /**\r
-        * Returns all reservations overlapping the given dateRange in a <code>Map</code>\r
-        * \r
-        * @param       dateRange       time span\r
-        * @return                              <code>Key</code> reservation, \r
-        *                                              <code>Value</code> is editable\r
-        * @throws      Exception       no <code>User</code> is logged in or no <code>DataHandler</code> is set\r
+        * Returns all reservations overlapping the given dateRange in a\r
+        * <code>Map</code>\r
+        * @param dateRange\r
+        *            time span\r
+        * @return <code>Key</code> reservation, <code>Value</code> is editable\r
+        * @throws Exception\r
+        *             no <code>User</code> is logged in or no\r
+        *             <code>DataHandler</code> is set\r
         */\r
-       public Map<Reservation, Boolean> getReservationsInDateRange(DateRange dateRange) throws Exception{\r
-               if(dh == null) throw new Exception("No DataHandler set");\r
-               if(dateRange == null) throw new Exception("dateRange must be defined");\r
-               \r
-               // Could be swapped out to private method for better performance (just check currentUser once)\r
+       public Map<Reservation, Boolean> getReservationsInDateRange(DateRange dateRange) throws Exception {\r
+               if (dh == null)\r
+                       throw new Exception("No DataHandler set");\r
+               if (dateRange == null)\r
+                       throw new Exception("dateRange must be defined");\r
+\r
+               // Could be swapped out to private method for better performance (just\r
+               // check currentUser once)\r
                // and easier code maintenance\r
                Map<Reservation, Boolean> returnSet = new HashMap<Reservation, Boolean>();\r
                for (Reservation reservation : dh.overlaps(dateRange)) {\r
@@ -186,37 +215,49 @@ public class UserController {
                }\r
                return returnSet;\r
        }\r
-       \r
+\r
        /**\r
         * Returns the reservation or null if no reservation was found\r
-        * \r
-        * @param id                    reservation identifier\r
-        * @return                              <code>Reservation</code> copy of reservation with given id, \r
-        *                                              <code>null</code> no reservation with this id found\r
-        * @throws Exception    No DataHandler set or id null\r
+        * @param id\r
+        *            reservation identifier\r
+        * @return <code>Reservation</code> copy of reservation with given id,\r
+        *         <code>null</code> no reservation with this id found\r
+        * @throws Exception\r
+        *             No DataHandler set or id null\r
         */\r
-       public Reservation getReservation(UUID id) throws Exception{\r
-               if(dh == null) throw new Exception("No DataHandler set");\r
-               if(id == null) throw new Exception("uuid must be defined");\r
-               if(dh.findReservation(id) == null) return null;\r
-               else return dh.findReservation(id).clone();\r
+       public Reservation getReservation(UUID id) throws Exception {\r
+               if (dh == null)\r
+                       throw new Exception("No DataHandler set");\r
+               if (id == null)\r
+                       throw new Exception("uuid must be defined");\r
+               if (dh.findReservation(id) == null)\r
+                       return null;\r
+               else\r
+                       return dh.findReservation(id).clone();\r
        }\r
-       \r
+\r
        /**\r
         * Returns the reservations of the given user\r
-        * \r
-        * @param user                          Owner, if <code>null</code> returns the reservations of the current logged in user\r
-        * @return                                      <code>List</code> of reservation copies of the given owner\r
-        * @throws Exception            either no <code>DataHandler</code> is set or no user is logged in while user param = null\r
+        * @param user\r
+        *            Owner, if <code>null</code> returns the reservations of the\r
+        *            current logged in user\r
+        * @return <code>List</code> of reservation copies of the given owner\r
+        * @throws Exception\r
+        *             either no <code>DataHandler</code> is set or no user is\r
+        *             logged in while user param = null\r
         */\r
-       public Map<Reservation, Boolean> getUserReservations(User user) throws Exception{\r
-               if(dh == null) throw new Exception("No DataHandler set");\r
-               if(user == null){\r
-                       if(currentUser == null) throw new Exception("user must be defined, if no user is logged in");\r
-                       else user = currentUser;\r
+       public Map<Reservation, Boolean> getUserReservations(User user) throws Exception {\r
+               if (dh == null)\r
+                       throw new Exception("No DataHandler set");\r
+               if (user == null) {\r
+                       if (currentUser == null)\r
+                               throw new Exception("user must be defined, if no user is logged in");\r
+                       else\r
+                               user = currentUser;\r
                }\r
-               \r
-               // Could be swapped out to private method for better performance (just check currentUser once)\r
+\r
+               // Could be swapped out to private method for better performance (just\r
+               // check currentUser once)\r
                // and easier code maintenance\r
                Map<Reservation, Boolean> returnSet = new HashMap<Reservation, Boolean>();\r
                for (Reservation reservation : dh.userReservations(user)) {\r
@@ -224,21 +265,25 @@ public class UserController {
                }\r
                return returnSet;\r
        }\r
-       \r
+\r
        /**\r
         * Returns <code>Map</code> of reservation group (recurring reservations)\r
-        * \r
-        * @param groupId                                       group identifier\r
-        * @return                                                      <code>Key</code> reservation, \r
-        *                                                                      <code>Value</code> is editable\r
-        * @throws IllegalArgumentException     groupId is null\r
-        * @throws Exception                            no <code>DataHandler</code> set\r
+        * @param groupId\r
+        *            group identifier\r
+        * @return <code>Key</code> reservation, <code>Value</code> is editable\r
+        * @throws IllegalArgumentException\r
+        *             groupId is null\r
+        * @throws Exception\r
+        *             no <code>DataHandler</code> set\r
         */\r
-       public Map<Reservation, Boolean> getReservationGroup(UUID groupId) throws Exception{\r
-               if(dh == null) throw new Exception("No DataHandler set");\r
-               if(groupId == null) throw new IllegalArgumentException("groupId has to be defined");\r
-               \r
-               // Could be swapped out to private method for better performance (just check currentUser once)\r
+       public Map<Reservation, Boolean> getReservationGroup(UUID groupId) throws Exception {\r
+               if (dh == null)\r
+                       throw new Exception("No DataHandler set");\r
+               if (groupId == null)\r
+                       throw new IllegalArgumentException("groupId has to be defined");\r
+\r
+               // Could be swapped out to private method for better performance (just\r
+               // check currentUser once)\r
                // and easier code maintenance\r
                Map<Reservation, Boolean> returnSet = new HashMap<Reservation, Boolean>();\r
                for (Reservation reservation : dh.findGroupReservations(groupId)) {\r
@@ -249,84 +294,110 @@ public class UserController {
 \r
        /**\r
         * Makes a reservation in the current set DataHandler\r
-        * \r
-        * @param user                                                  user owning the reservation, if null currentUser is set\r
-        * @param resource                                              resource to reserve\r
-        * @param dateRangeList                                 <code>List</code> of DateRanges defining when the resource is occupied\r
-        * @param remark                                                public remark for the reservation\r
-        * @return                                                              <code>Set</code> of Reservations that overlap an already stored reservation (conflict), if not empty no reservation was saved\r
-        * @throws IllegalArgumentException             <code>resource</code> or <code>dateRangeList</code> is null, or <code>dateRangeList</code> is empty\r
-        * @throws Exception                                    thrown by <code>DataHandler</code>\r
+        * @param user\r
+        *            user owning the reservation, if null currentUser is set\r
+        * @param resource\r
+        *            resource to reserve\r
+        * @param dateRangeList\r
+        *            <code>List</code> of DateRanges defining when the resource is\r
+        *            occupied\r
+        * @param remark\r
+        *            public remark for the reservation\r
+        * @return <code>Set</code> of Reservations that overlap an already stored\r
+        *         reservation (conflict), if not empty no reservation was saved\r
+        * @throws IllegalArgumentException\r
+        *             <code>resource</code> or <code>dateRangeList</code> is null,\r
+        *             or <code>dateRangeList</code> is empty\r
+        * @throws Exception\r
+        *             thrown by <code>DataHandler</code>\r
         */\r
-       public Set<Reservation> makeReservation(User user, Resource resource, List<DateRange> dateRangeList, String remark) throws Exception\r
-       {\r
-               if(user == null){\r
-                       if(currentUser == null) throw new IllegalArgumentException("User must be defined if no user is logged in");\r
+       public Set<Reservation> makeReservation(User user, Resource resource, List<DateRange> dateRangeList, String remark)\r
+                       throws Exception {\r
+               if (user == null) {\r
+                       if (currentUser == null)\r
+                               throw new IllegalArgumentException("User must be defined if no user is logged in");\r
                        user = currentUser;\r
                }\r
-               if(resource == null) throw new IllegalArgumentException("Resource must be definded");\r
-               if(dateRangeList == null) throw new IllegalArgumentException("dateRangeList can not be null");\r
-               if(dateRangeList.isEmpty()) throw new IllegalArgumentException("dateRangeList empty");\r
-               \r
+               if (resource == null)\r
+                       throw new IllegalArgumentException("Resource must be definded");\r
+               if (dateRangeList == null)\r
+                       throw new IllegalArgumentException("dateRangeList can not be null");\r
+               if (dateRangeList.isEmpty())\r
+                       throw new IllegalArgumentException("dateRangeList empty");\r
+\r
                Set<Reservation> invalidReservations = new HashSet<Reservation>();\r
                UUID groupId = UUID.randomUUID();\r
                for (DateRange dateRange : dateRangeList) {\r
                        Reservation reservation = new Reservation(groupId, user, resource, dateRange, remark);\r
-                       if(!dh.addReservation(reservation)) invalidReservations.add(reservation);\r
+                       if (!dh.addReservation(reservation))\r
+                               invalidReservations.add(reservation);\r
                }\r
-               if(invalidReservations.isEmpty())\r
-                       {\r
-                               dh.saveChanges();\r
-                               return null;\r
-                       }\r
-               else return invalidReservations;\r
+               if (invalidReservations.isEmpty()) {\r
+                       dh.saveChanges();\r
+                       return null;\r
+               } else\r
+                       return invalidReservations;\r
        }\r
-       \r
+\r
        /**\r
-        * Replaces the given reservation in the set DataHandler referring to the reservationid\r
-        * \r
-        * @param reservation                           Copy of the reservation to edit\r
-        * @return                                                      <code>True</code> Changes saved, \r
-        *                                                                      <code>False</code> Changes could not be saved\r
-        * @throws IllegalArgumentException     if reservation param is null\r
-        * @throws Exception                            if no <code>DataHandler</code> is set or the logged user is not authorized\r
+        * Replaces the given reservation in the set DataHandler referring to the\r
+        * reservationid\r
+        * @param reservation\r
+        *            Copy of the reservation to edit\r
+        * @return <code>True</code> Changes saved, <code>False</code> Changes could\r
+        *         not be saved\r
+        * @throws IllegalArgumentException\r
+        *             if reservation param is null\r
+        * @throws Exception\r
+        *             if no <code>DataHandler</code> is set or the logged user is\r
+        *             not authorized\r
         */\r
-       public boolean changeReservation(Reservation reservation) throws Exception\r
-       {\r
-               if(dh == null) throw new Exception("No DataHandler set");\r
-               if(currentUser == null) throw new Exception("No user is logged in");\r
-               if(reservation == null) throw new IllegalArgumentException("Reservation must be defined");\r
-               if(!reservation.getUser().equals(currentUser)) throw new Exception("You are not authorized to make this change");\r
+       public boolean changeReservation(Reservation reservation) throws Exception {\r
+               if (dh == null)\r
+                       throw new Exception("No DataHandler set");\r
+               if (currentUser == null)\r
+                       throw new Exception("No user is logged in");\r
+               if (reservation == null)\r
+                       throw new IllegalArgumentException("Reservation must be defined");\r
+               if (!reservation.getUser().equals(currentUser))\r
+                       throw new Exception("You are not authorized to make this change");\r
                return dh.editReservation(reservation);\r
        }\r
-       \r
+\r
        /**\r
-        * Deletes the given reservation in the set <code>DataHandler</code> referring to the reservation id\r
-        * \r
-        * @param reservation                           Copy of the reservation to edit\r
-        * @return                                                      <code>True</code> Changes saved\r
-        *                                                                      <code>False</code> Changes could not be saved\r
-        * @throws IllegalArgumentException     if reservation is null\r
-        * @throws Exception                            if no <code>DataHandler</code> is set, no <code>User</code> is logged in or logged <code>User</code> is not authorized\r
+        * Deletes the given reservation in the set <code>DataHandler</code>\r
+        * referring to the reservation id\r
+        * @param reservation\r
+        *            Copy of the reservation to edit\r
+        * @return <code>True</code> Changes saved <code>False</code> Changes could\r
+        *         not be saved\r
+        * @throws IllegalArgumentException\r
+        *             if reservation is null\r
+        * @throws Exception\r
+        *             if no <code>DataHandler</code> is set, no <code>User</code>\r
+        *             is logged in or logged <code>User</code> is not authorized\r
         */\r
-       public boolean deleteReservation(Reservation reservation) throws Exception\r
-       {\r
-               if(dh == null) throw new Exception("No DataHandler set");\r
-               if(currentUser == null) throw new Exception("No user is logged in");\r
-               if(reservation == null) throw new IllegalArgumentException("Reservation must be defined");\r
-               if(!reservation.getUser().equals(currentUser)) throw new Exception("You are not authorized to make this change");\r
+       public boolean deleteReservation(Reservation reservation) throws Exception {\r
+               if (dh == null)\r
+                       throw new Exception("No DataHandler set");\r
+               if (currentUser == null)\r
+                       throw new Exception("No user is logged in");\r
+               if (reservation == null)\r
+                       throw new IllegalArgumentException("Reservation must be defined");\r
+               if (!reservation.getUser().equals(currentUser))\r
+                       throw new Exception("You are not authorized to make this change");\r
                return dh.deleteReservation(reservation);\r
        }\r
-       \r
-       \r
+\r
        /**\r
         * Returns all stored resources of the set DataHandler\r
-        * \r
-        * @return                              <code>List</code> of resources\r
-        * @throws Exception    no <code>DataHandler</code> is set\r
+        * @return <code>List</code> of resources\r
+        * @throws Exception\r
+        *             no <code>DataHandler</code> is set\r
         */\r
-       public List<Resource> getResources() throws Exception{\r
-               if(dh == null) throw new Exception("No DataHandler set");\r
+       public List<Resource> getResources() throws Exception {\r
+               if (dh == null)\r
+                       throw new Exception("No DataHandler set");\r
                List<Resource> resourceList = new ArrayList<Resource>();\r
                for (Resource resource : dh.getAllResources()) {\r
                        resourceList.add(resource.clone());\r