DataHandler angepasst
authorMatthias <balmm1@bfh.ch>
Wed, 18 Nov 2015 10:01:11 +0000 (11:01 +0100)
committerMatthias <balmm1@bfh.ch>
Wed, 18 Nov 2015 10:01:11 +0000 (11:01 +0100)
src/main/java/ch/bfh/ti/srs/datahandler/DataHandler.java
src/main/java/ch/bfh/ti/srs/datahandler/DataHandlerForTests.java
src/main/java/ch/bfh/ti/srs/noTest/DataHandlerForExcel.java

index 76cfd60..ded3ff3 100644 (file)
@@ -63,7 +63,7 @@ public interface DataHandler {
         * @param id
         *            id of the Reservation you are looking for
         * @return <code>Reservation</code>reservation object <code>null</code>if no
-        *         reservation found with this resource and this daterange
+        *         reservation found with this ID
         */
        public Reservation findReservation(UUID id);
 
@@ -72,11 +72,19 @@ public interface DataHandler {
         * and returns the Reservation object or null if not found
         * @param groupID
         *            id of the Group Reservations you are looking for
-        * @return <code>Reservation</code>reservation object <code>null</code>if no
-        *         reservation found with this resource and this daterange
+        * @return <code>Reservation</code>reservation objects <code>null</code>if
+        *         no reservation found with this groupID
         */
        public List<Reservation> findGroupReservations(UUID groupID);
 
+       /**
+        * Finds reservations that overlaps with the given DateRange and returns the
+        * Reservation objects or null if not found
+        * @param range
+        *            the range you want to get overlapping reservations for
+        * @return <code>Reservation</code>reservation object <code>null</code>if no
+        *         overlapping reservation found with this range
+        */
        public List<Reservation> overlaps(DateRange range);
 
        /**
index 8f33e83..97d088a 100644 (file)
@@ -18,91 +18,129 @@ 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{
+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 DataHandlerForTests() {
+
        }
 
-       /* (non-Javadoc)
+       /*
+        * (non-Javadoc)
         * @see ch.bfh.ti.srs.dataschema.DataHandler#findUser(java.lang.String)
         */
        @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)
+       /*
+        * (non-Javadoc)
+        * @see
+        * ch.bfh.ti.srs.dataschema.DataHandler#addUser(ch.bfh.ti.srs.dataschema.
+        * User)
         */
        @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!");
+               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);
        }
 
-       /* (non-Javadoc)
+       /*
+        * (non-Javadoc)
         * @see ch.bfh.ti.srs.dataschema.DataHandler#findResource(java.lang.String)
         */
        @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)
+
+       /*
+        * (non-Javadoc)
+        * @see
+        * ch.bfh.ti.srs.dataschema.DataHandler#addResource(ch.bfh.ti.srs.dataschema
+        * .Resource)
         */
        @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!");
+               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);
        }
 
-       /* (non-Javadoc)
-        * @see ch.bfh.ti.srs.dataschema.DataHandler#findReservation(java.lang.String)
+       /*
+        * (non-Javadoc)
+        * @see
+        * ch.bfh.ti.srs.dataschema.DataHandler#findReservation(java.lang.String)
         */
        @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;
        }
-       
+
+       /*
+        * (non-Javadoc)
+        * @see
+        * ch.bfh.ti.srs.dataschema.DataHandler#findGroupReservations(ch.bfh.ti.srs.
+        * dataschema.Reservation)
+        */
        @Override
        public List<Reservation> findGroupReservations(UUID groupID) {
                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;
        }
 
+       /*
+        * (non-Javadoc)
+        * @see
+        * ch.bfh.ti.srs.dataschema.DataHandler#overlaps(ch.bfh.ti.srs.dataschema.
+        * Reservation)
+        */
        @Override
        public List<Reservation> overlaps(DateRange range) {
-               // TODO Auto-generated method stub
-               return null;
+               List<Reservation> overlappingReservations = new ArrayList<Reservation>();
+               for (Reservation reservation : reservationList) {
+                       if (reservation.getDateRange().overlaps(range))
+                               overlappingReservations.add(reservation);
+               }
+               return overlappingReservations;
        }
-       
-       /* (non-Javadoc)
-        * @see ch.bfh.ti.srs.dataschema.DataHandler#addReservation(ch.bfh.ti.srs.dataschema.Reservation)
+
+       /*
+        * (non-Javadoc)
+        * @see ch.bfh.ti.srs.dataschema.DataHandler#addReservation(ch.bfh.ti.srs.
+        * dataschema.Reservation)
         */
        @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);       
+               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);
        }
 
 }
index 94d8e2f..0addfbb 100644 (file)
@@ -14,115 +14,106 @@ import ch.bfh.ti.srs.datahandler.DataHandler;
 import ch.bfh.ti.srs.dataschema.Reservation;
 import ch.bfh.ti.srs.dataschema.Resource;
 import ch.bfh.ti.srs.dataschema.User;
+
 /**
  * @author Michael Grossen
  */
-public class DataHandlerForExcel implements DataHandler{
+public class DataHandlerForExcel implements DataHandler {
        String filePath;
        Set<User> userList = new HashSet<User>();
-       
-       static
-       {
-               try  { 
-            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
-        } 
-        catch (Exception e) {
-            System.err.println(e);
-        }
+
+       static {
+               try {
+                       Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
+               } catch (Exception e) {
+                       System.err.println(e);
+               }
        }
-       
-       public DataHandlerForExcel(String filePath) throws Exception{
+
+       public DataHandlerForExcel(String filePath) throws Exception {
                this.filePath = filePath;
                updateUserList();
        }
-       
+
        @Override
        public User findUser(String userName) {
-               if(userName == null) throw new IllegalArgumentException("Username must not be null");
+               if (userName == null)
+                       throw new IllegalArgumentException("Username must not be null");
                for (User user : userList) {
-                       if(user.getUserName().equals(userName)) return user;
+                       if (user.getUserName().equals(userName))
+                               return user;
                }
                return null;
        }
 
        @Override
        public void addUser(User user) throws Exception {
-               if(findUser(user.getUserName()) != null) throw new Exception("User already exists!");
+               if (findUser(user.getUserName()) != null)
+                       throw new Exception("User already exists!");
                insertNewUser(user);
                updateUserList();
        }
 
-       private void updateUserList() throws Exception
-       {
-               Connection conn=null;
-        Statement stmt=null;
-        String sql="";
-        ResultSet rs=null;
-        userList.clear();
-        
-        try {
-            conn=DriverManager.getConnection("jdbc:odbc:excel","","");
-            stmt=conn.createStatement();
-            sql="select * from [User$]";
-            rs=stmt.executeQuery(sql);
-
-            while(rs.next()){
-               User user = new User(rs.getString("USERNAME"),
-                               rs.getString("PASSWORD"),
-                               rs.getString("MAIL"),
-                               rs.getString("NAME"),
-                               rs.getString("LASTNAME"));
-                userList.add(user);
-            }
-        } 
-        catch (Exception e){
-            System.err.println(e);
-        } 
-        finally {
-            try{
-                rs.close();
-                stmt.close();
-                conn.close();
-                rs=null;
-                stmt=null;
-                conn=null; 
-            }
-            catch(Exception e){}
-        }
+       private void updateUserList() throws Exception {
+               Connection conn = null;
+               Statement stmt = null;
+               String sql = "";
+               ResultSet rs = null;
+
+               userList.clear();
+
+               try {
+                       conn = DriverManager.getConnection("jdbc:odbc:excel", "", "");
+                       stmt = conn.createStatement();
+                       sql = "select * from [User$]";
+                       rs = stmt.executeQuery(sql);
+
+                       while (rs.next()) {
+                               User user = new User(rs.getString("USERNAME"), rs.getString("PASSWORD"), rs.getString("MAIL"),
+                                               rs.getString("NAME"), rs.getString("LASTNAME"));
+                               userList.add(user);
+                       }
+               } catch (Exception e) {
+                       System.err.println(e);
+               } finally {
+                       try {
+                               rs.close();
+                               stmt.close();
+                               conn.close();
+                               rs = null;
+                               stmt = null;
+                               conn = null;
+                       } catch (Exception e) {
+                       }
+               }
        }
 
-       private void insertNewUser(User user) throws Exception{
-               Connection conn=null;
-        Statement stmt=null;
-        String sql="";
-        ResultSet rs=null;
-        sql = "insert into [User$] values (" +         user.getUserName() + "," +
-                                                                                       user.getEncryptedPassword() + "," +
-                                                                                       user.getMail() + "," +
-                                                                                       user.getName() + "," +
-                                                                                       user.getLastname() + ")";
-        
-        try {
-            conn=DriverManager.getConnection("jdbc:odbc:excel","","");
-            stmt=conn.createStatement();
-            stmt.executeQuery(sql);
-        } 
-        catch (Exception e){
-            System.err.println(e);
-        } 
-        finally {
-            try{
-                rs.close();
-                stmt.close();
-                conn.close();
-                rs=null;
-                stmt=null;
-                conn=null; 
-            }
-            catch(Exception e){}
-        }
+       private void insertNewUser(User user) throws Exception {
+               Connection conn = null;
+               Statement stmt = null;
+               String sql = "";
+               ResultSet rs = null;
+
+               sql = "insert into [User$] values (" + user.getUserName() + "," + user.getEncryptedPassword() + ","
+                               + user.getMail() + "," + user.getName() + "," + user.getLastname() + ")";
+
+               try {
+                       conn = DriverManager.getConnection("jdbc:odbc:excel", "", "");
+                       stmt = conn.createStatement();
+                       stmt.executeQuery(sql);
+               } catch (Exception e) {
+                       System.err.println(e);
+               } finally {
+                       try {
+                               rs.close();
+                               stmt.close();
+                               conn.close();
+                               rs = null;
+                               stmt = null;
+                               conn = null;
+                       } catch (Exception e) {
+                       }
+               }
        }
 
        @Override
@@ -134,13 +125,13 @@ public class DataHandlerForExcel implements DataHandler{
        @Override
        public void addResource(Resource resource) throws Exception {
                // TODO Auto-generated method stub
-               
+
        }
 
        @Override
        public void addReservation(Reservation reservation) throws Exception {
                // TODO Auto-generated method stub
-               
+
        }
 
        @Override