Split UserController into 'Controller' and 'UserController extends
[staff/due1/sed-hs15-srs-purple.git] / src / main / java / ch / bfh / ti / srs / controller / UserController.java
index 18ce351..201141c 100644 (file)
@@ -25,88 +25,10 @@ import ch.bfh.ti.srs.dataschema.User;
 /**\r
  * @author Michael Grossen\r
  */\r
-public class UserController {\r
-       private User currentUser;\r
-       private DataHandler dh;\r
+public class UserController extends Controller{\r
 \r
-       public UserController() {\r
-       }\r
-\r
-       /**\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
-               this.dh = dh;\r
-       }\r
-\r
-       /**\r
-        * Tries to log in a <code>User</code> with the given credentials\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)\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))\r
-                       return false;\r
-               else {\r
-                       currentUser = tempUser;\r
-                       return true;\r
-               }\r
-       }\r
-\r
-       /**\r
-        * Logs out the current logged user\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)\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
-        * Registers a new user to the current DataHandler\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)\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
-               dh.addUser(newUser);\r
+       public UserController(DataHandler dh) {\r
+               super(dh);\r
        }\r
 \r
        /**\r
@@ -191,52 +113,6 @@ public class UserController {
        }\r
 \r
        /**\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)\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
-                       returnSet.put(reservation, reservation.getUser().getUserName().equals(currentUser.getUserName()));\r
-               }\r
-               return returnSet;\r
-       }\r
-\r
-       /**\r
-        * Returns the reservation or null if no reservation was found\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)\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
         * Returns the reservations of the given user\r
         * @param user\r
         *            Owner, if <code>null</code> returns the reservations of the\r
@@ -247,96 +123,13 @@ public class UserController {
         *             logged in while user param = null\r
         */\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\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
-                       returnSet.put(reservation, reservation.getUser().getUserName().equals(currentUser.getUserName()));\r
-               }\r
-               return returnSet;\r
-       }\r
-\r
-       /**\r
-        * Returns <code>Map</code> of reservation group (recurring reservations)\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)\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
-                       returnSet.put(reservation, reservation.getUser().getUserName().equals(currentUser.getUserName()));\r
-               }\r
-               return returnSet;\r
-       }\r
-\r
-       /**\r
-        * Makes a reservation in the current set DataHandler\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)\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)\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))\r
-                               invalidReservations.add(reservation);\r
-               }\r
-               if (invalidReservations.isEmpty()) {\r
-                       dh.saveChanges();\r
-                       return null;\r
-               } else\r
-                       return invalidReservations;\r
+               return super.getUserReservations_Super(user);\r
        }\r
 \r
        /**\r
@@ -353,15 +146,13 @@ public class UserController {
         *             not authorized\r
         */\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
+               if (!reservation.getUser().equals(super.currentUser))\r
                        throw new Exception("You are not authorized to make this change");\r
-               return dh.editReservation(reservation);\r
+               return super.changeReservation_Super(reservation);\r
        }\r
 \r
        /**\r
@@ -378,30 +169,13 @@ public class UserController {
         *             is logged in or logged <code>User</code> is not authorized\r
         */\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
+               return super.deleteReservation_Super(reservation);\r
        }\r
 \r
-       /**\r
-        * Returns all stored resources of the set DataHandler\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)\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
-               }\r
-               return resourceList;\r
-       }\r
 }\r