Updating ExcelController
authorMichael <Michael@Michael-LT.bfh.ch>
Tue, 27 Oct 2015 14:22:49 +0000 (15:22 +0100)
committerMichael <Michael@Michael-LT.bfh.ch>
Tue, 27 Oct 2015 14:22:49 +0000 (15:22 +0100)
UserControllerTest added

src/main/java/ch/bfh/ti/srs/core/DataController.java
src/main/java/ch/bfh/ti/srs/core/ExcelController.java
src/main/java/ch/bfh/ti/srs/core/User.java
src/main/java/ch/bfh/ti/srs/core/UserController.java
src/test/java/ch/bfh/ti/srs/core/UserControllerTest.java [new file with mode: 0644]
src/test/java/ch/bfh/ti/srs/core/UserTest.java

index d80a81c..f2a8975 100644 (file)
@@ -7,6 +7,10 @@
  */\r
 package ch.bfh.ti.srs.core;\r
 \r
+/**\r
+ * @author Michael Grossen\r
+ */\r
 public interface DataController {\r
-       public User findUser(String Name);\r
+       public User findUser(String userName);\r
+       public void addUser(User user) throws Exception;\r
 }\r
index dfe2fee..4014687 100644 (file)
 package ch.bfh.ti.srs.core;\r
 \r
+import java.sql.*;\r
+import java.util.HashSet;\r
+import java.util.Set;\r
+/**\r
+ * @author Michael Grossen\r
+ */\r
 public class ExcelController implements DataController{\r
        String filePath;\r
+       Set<User> userList = new HashSet<User>();\r
        \r
-       public ExcelController(String filePath){\r
+       static\r
+       {\r
+               try  { \r
+            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");\r
+        } \r
+        catch (Exception e) {\r
+            System.err.println(e);\r
+        }\r
+       }\r
+       \r
+       public ExcelController(String filePath) throws Exception{\r
                this.filePath = filePath;\r
+               updateUserList();\r
        }\r
        \r
        @Override\r
-       public User findUser(String Name) {\r
-               // TODO Auto-generated method stub\r
+       public User findUser(String userName) {\r
+               if(userName == null) throw new IllegalArgumentException("Username must not be null");\r
+               for (User user : userList) {\r
+                       if(user.getUserName().equals(userName)) return user;\r
+               }\r
                return null;\r
        }\r
 \r
+       @Override\r
+       public void addUser(User user) throws Exception {\r
+               if(findUser(user.getUserName()) != null) throw new Exception("User already exists!");\r
+               insertNewUser(user);\r
+               updateUserList();\r
+       }\r
+\r
+       \r
+       private void updateUserList() throws Exception\r
+       {\r
+               Connection conn=null;\r
+        Statement stmt=null;\r
+        String sql="";\r
+        ResultSet rs=null;\r
\r
+        userList.clear();\r
+        \r
+        try {\r
+            conn=DriverManager.getConnection("jdbc:odbc:excel","","");\r
+            stmt=conn.createStatement();\r
+            sql="select * from [User$]";\r
+            rs=stmt.executeQuery(sql);\r
+\r
+            while(rs.next()){\r
+               User user = new User(rs.getString("USERNAME"),\r
+                               rs.getString("PASSWORD"),\r
+                               rs.getString("MAIL"),\r
+                               rs.getString("NAME"),\r
+                               rs.getString("LASTNAME"));\r
+                userList.add(user);\r
+            }\r
+        } \r
+        catch (Exception e){\r
+            System.err.println(e);\r
+        } \r
+        finally {\r
+            try{\r
+                rs.close();\r
+                stmt.close();\r
+                conn.close();\r
+                rs=null;\r
+                stmt=null;\r
+                conn=null; \r
+            }\r
+            catch(Exception e){}\r
+        }\r
+       }\r
+\r
+       private void insertNewUser(User user) throws Exception{\r
+               Connection conn=null;\r
+        Statement stmt=null;\r
+        String sql="";\r
+        ResultSet rs=null;\r
\r
+        sql = "insert into [User$] values (" +         user.getUserName() + "," +\r
+                                                                                       user.getEncryptedPassword() + "," +\r
+                                                                                       user.getMail() + "," +\r
+                                                                                       user.getName() + "," +\r
+                                                                                       user.getLastname() + ")";\r
+        \r
+        try {\r
+            conn=DriverManager.getConnection("jdbc:odbc:excel","","");\r
+            stmt=conn.createStatement();\r
+            stmt.executeQuery(sql);\r
+        } \r
+        catch (Exception e){\r
+            System.err.println(e);\r
+        } \r
+        finally {\r
+            try{\r
+                rs.close();\r
+                stmt.close();\r
+                conn.close();\r
+                rs=null;\r
+                stmt=null;\r
+                conn=null; \r
+            }\r
+            catch(Exception e){}\r
+        }\r
+       }\r
 }\r
index 444ef15..c92401d 100644 (file)
@@ -17,6 +17,13 @@ public class User {
        private String name;
        private String lastname;
 
+       public User(String userName, String password, String mail, String name, String lastname){
+               this.userName = userName;
+               this.password = password;
+               this.mail = mail;
+               this.name = name;
+               this.lastname = lastname;
+       }
 
        public User(String userName, String password) throws Exception{
                if(userName == null) throw new IllegalArgumentException("Username must not be null");
@@ -44,6 +51,9 @@ public class User {
                return lastname;
        }
        
+       public String getEncryptedPassword(){
+               return password;
+       }
        
        //Setter
        public boolean setMail(String mail){
index 8b542eb..f1b6bb5 100644 (file)
@@ -7,23 +7,56 @@
  */\r
 package ch.bfh.ti.srs.core;\r
 \r
+import java.rmi.AlreadyBoundException;\r
+import java.util.concurrent.ExecutionException;\r
+\r
+/**\r
+ * @author Michael Grossen\r
+ */\r
 public class UserController {\r
-       User LoggedUser;\r
+       final String filePath = "test.xls";\r
+       User currentUser;\r
        DataController dc;\r
        \r
-       public UserController() {\r
-               dc = new ExcelController("test.xls");\r
+       public UserController() throws Exception {\r
+               dc = new ExcelController(filePath);\r
        }\r
        \r
+       /**\r
+        * Tries to logIn a user with the given credentials\r
+        * \r
+        * @param userName      username\r
+        * @param password      the stored password for the given username\r
+        * @return                      <code>true</code> if the user could log in successfully;\r
+        *                                      <code>false</code> eighter the username or the password was not correct.\r
+        * @throws                      Exception\r
+        */\r
        public boolean logIn(String userName, String password) throws Exception{\r
+               if(currentUser != null) throw new AlreadyBoundException("A user is already logged in");\r
                User tempUser = dc.findUser(userName);\r
                if(tempUser == null) return false;\r
                if(tempUser.checkPassword(password)){\r
-                       LoggedUser = tempUser;\r
+                       currentUser = tempUser;\r
                        return true;\r
                }\r
                else return false;\r
        }\r
        \r
+       /**\r
+        * Logs out the current user, throws exception if no user is logged in\r
+        * \r
+        * @throws Exception \r
+        */\r
+       public void logOut() throws Exception{\r
+               if(currentUser == null) throw new Exception("No user is logged in");\r
+               currentUser = null;\r
+       }\r
        \r
+       public void addUser(String userName, String password, String email, String name, String lastname) throws Exception{\r
+               User newUser = new User(userName,password);\r
+               newUser.setMail(email);\r
+               newUser.setName(name);\r
+               newUser.setLastname(lastname);\r
+               dc.addUser(newUser);\r
+       }\r
 }\r
diff --git a/src/test/java/ch/bfh/ti/srs/core/UserControllerTest.java b/src/test/java/ch/bfh/ti/srs/core/UserControllerTest.java
new file mode 100644 (file)
index 0000000..2e26d45
--- /dev/null
@@ -0,0 +1,25 @@
+/*\r
+ * Copyright (c) 2015 Berner Fachhochschule, Switzerland.\r
+ *\r
+ * Project Smart Reservation System.\r
+ *\r
+ * Distributable under GPL license. See terms of license at gnu.org.\r
+ */\r
+package ch.bfh.ti.srs.core;\r
+\r
+import static org.junit.Assert.*;\r
+\r
+import org.junit.BeforeClass;\r
+import org.junit.Test;\r
+\r
+/**\r
+ * @author Michael Grossen\r
+ */\r
+public class UserControllerTest {\r
+\r
+       @Test\r
+       public void test() {\r
+               fail("Not yet implemented");\r
+       }\r
+\r
+}\r
index b01d257..6c5f414 100644 (file)
@@ -15,6 +15,9 @@ import org.junit.Rule;
 import org.junit.Test;\r
 import org.junit.rules.ExpectedException;\r
 \r
+/**\r
+ * @author Michael Grossen\r
+ */\r
 public class UserTest {\r
        //@Rule\r
        //public ExpectedException createUserException = ExpectedException.none();\r