From 6d567f2a5dd58b9ded7672f4f4ba441989a8c624 Mon Sep 17 00:00:00 2001 From: Naresh Maharjan Date: Thu, 11 Aug 2016 03:09:48 +0545 Subject: [PATCH 1/9] #15 login and entities creation done --- .gitignore | 2 +- naresh/pom.xml | 24 ++- .../phpjava/ems/autorun/executedFiles.txt | 1 + .../phpjava/ems/autorun/inititalTables.sql | 3 + .../phpjava/ems/configs/db.properties | 4 + .../phpjava/ems/constants/Constant.java | 16 ++ .../ems/controllers/EmployeeController.java | 10 ++ .../ems/controllers/LoginController.java | 38 +++++ .../phpjava/ems/dao/DaoSignature.java | 49 ++++++ .../phpjava/ems/dao/EmployeeDaoImpl.java | 157 ++++++++++++++++++ .../phpjava/ems/dao/UserDaoImpl.java | 124 ++++++++++++++ .../phpjava/ems/entities/Employee.java | 66 ++++++++ .../phpjava/ems/entities/User.java | 56 +++++++ .../lftechnology/phpjava/ems/enums/Role.java | 21 +++ .../phpjava/ems/services/EmployeeService.java | 68 ++++++++ .../phpjava/ems/services/UserService.java | 39 +++++ .../phpjava/ems/utlis/AbstractDbAdapater.java | 18 ++ .../phpjava/ems/utlis/ConsoleWriter.java | 21 +++ .../phpjava/ems/utlis/DbFactory.java | 91 ++++++++++ .../ems/utlis/PasswordHashGenerator.java | 60 +++++++ .../phpjava/ems/utlis/Router.java | 57 +++++++ .../phpjava/ems/utlis/SqlRunner.java | 108 ++++++++++++ .../phpjava/ems/utlis/UserInput.java | 58 +++++++ 23 files changed, 1082 insertions(+), 9 deletions(-) create mode 100644 naresh/src/main/java/com/lftechnology/phpjava/ems/autorun/executedFiles.txt create mode 100644 naresh/src/main/java/com/lftechnology/phpjava/ems/autorun/inititalTables.sql create mode 100644 naresh/src/main/java/com/lftechnology/phpjava/ems/configs/db.properties create mode 100644 naresh/src/main/java/com/lftechnology/phpjava/ems/constants/Constant.java create mode 100644 naresh/src/main/java/com/lftechnology/phpjava/ems/controllers/EmployeeController.java create mode 100644 naresh/src/main/java/com/lftechnology/phpjava/ems/controllers/LoginController.java create mode 100644 naresh/src/main/java/com/lftechnology/phpjava/ems/dao/DaoSignature.java create mode 100644 naresh/src/main/java/com/lftechnology/phpjava/ems/dao/EmployeeDaoImpl.java create mode 100644 naresh/src/main/java/com/lftechnology/phpjava/ems/dao/UserDaoImpl.java create mode 100644 naresh/src/main/java/com/lftechnology/phpjava/ems/entities/Employee.java create mode 100644 naresh/src/main/java/com/lftechnology/phpjava/ems/entities/User.java create mode 100644 naresh/src/main/java/com/lftechnology/phpjava/ems/enums/Role.java create mode 100644 naresh/src/main/java/com/lftechnology/phpjava/ems/services/EmployeeService.java create mode 100644 naresh/src/main/java/com/lftechnology/phpjava/ems/services/UserService.java create mode 100644 naresh/src/main/java/com/lftechnology/phpjava/ems/utlis/AbstractDbAdapater.java create mode 100644 naresh/src/main/java/com/lftechnology/phpjava/ems/utlis/ConsoleWriter.java create mode 100644 naresh/src/main/java/com/lftechnology/phpjava/ems/utlis/DbFactory.java create mode 100644 naresh/src/main/java/com/lftechnology/phpjava/ems/utlis/PasswordHashGenerator.java create mode 100644 naresh/src/main/java/com/lftechnology/phpjava/ems/utlis/Router.java create mode 100644 naresh/src/main/java/com/lftechnology/phpjava/ems/utlis/SqlRunner.java create mode 100644 naresh/src/main/java/com/lftechnology/phpjava/ems/utlis/UserInput.java diff --git a/.gitignore b/.gitignore index 09acd94..a0df482 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,7 @@ *.class # IntelliJ IDEA coniguration folder -.idea/* +*/.idea/* # module file created by IntelliJ IDEA *.iml diff --git a/naresh/pom.xml b/naresh/pom.xml index c06e24e..d71b9a5 100644 --- a/naresh/pom.xml +++ b/naresh/pom.xml @@ -1,9 +1,17 @@ - - 4.0.0 - - com.lftechnology.phpjava - phpjava - 1.0.0 - - naresh + + 4.0.0 + + com.lftechnology.phpjava + phpjava + 1.0.0 + + + + mysql + mysql-connector-java + 5.1.6 + + + naresh \ No newline at end of file diff --git a/naresh/src/main/java/com/lftechnology/phpjava/ems/autorun/executedFiles.txt b/naresh/src/main/java/com/lftechnology/phpjava/ems/autorun/executedFiles.txt new file mode 100644 index 0000000..379b61c --- /dev/null +++ b/naresh/src/main/java/com/lftechnology/phpjava/ems/autorun/executedFiles.txt @@ -0,0 +1 @@ +inititalTables.sql diff --git a/naresh/src/main/java/com/lftechnology/phpjava/ems/autorun/inititalTables.sql b/naresh/src/main/java/com/lftechnology/phpjava/ems/autorun/inititalTables.sql new file mode 100644 index 0000000..e3ba7c8 --- /dev/null +++ b/naresh/src/main/java/com/lftechnology/phpjava/ems/autorun/inititalTables.sql @@ -0,0 +1,3 @@ +CREATE TABLE user (id INT PRIMARY KEY AUTO_INCREMENT,username VARCHAR(255),password VARCHAR(255),is_terminated BINARY DEFAULT FALSE); + +CREATE TABLE employee (id INT PRIMARY KEY AUTO_INCREMENT, fullname VARCHAR(255), department VARCHAR(255), address VARCHAR(255), role VARCHAR(10)); \ No newline at end of file diff --git a/naresh/src/main/java/com/lftechnology/phpjava/ems/configs/db.properties b/naresh/src/main/java/com/lftechnology/phpjava/ems/configs/db.properties new file mode 100644 index 0000000..8e50833 --- /dev/null +++ b/naresh/src/main/java/com/lftechnology/phpjava/ems/configs/db.properties @@ -0,0 +1,4 @@ +url = jdbc:mysql://localhost/ems +driver = com.mysql.jdbc.Driver +username = root +password = naresh \ No newline at end of file diff --git a/naresh/src/main/java/com/lftechnology/phpjava/ems/constants/Constant.java b/naresh/src/main/java/com/lftechnology/phpjava/ems/constants/Constant.java new file mode 100644 index 0000000..b946dca --- /dev/null +++ b/naresh/src/main/java/com/lftechnology/phpjava/ems/constants/Constant.java @@ -0,0 +1,16 @@ +package com.lftechnology.phpjava.ems.constants; + +/** + * Constant + * + * @author Naresh Maharjan + * @since August, 09 2016 + */ +public class Constant { + + public static final String PROPERTY_URL = "url"; + public static final String PROPERTY_DRIVER = "driver"; + public static final String PROPERTY_USERNAME = "username"; + public static final String PROPERTY_PASSWORD = "password"; + public static final String ADMIN = "admin"; +} diff --git a/naresh/src/main/java/com/lftechnology/phpjava/ems/controllers/EmployeeController.java b/naresh/src/main/java/com/lftechnology/phpjava/ems/controllers/EmployeeController.java new file mode 100644 index 0000000..d346188 --- /dev/null +++ b/naresh/src/main/java/com/lftechnology/phpjava/ems/controllers/EmployeeController.java @@ -0,0 +1,10 @@ +package com.lftechnology.phpjava.ems.controllers; + +/** + * EmployeeController + * + * @author Naresh Maharjan + * @since August, 08 2016 + */ +public class EmployeeController { +} diff --git a/naresh/src/main/java/com/lftechnology/phpjava/ems/controllers/LoginController.java b/naresh/src/main/java/com/lftechnology/phpjava/ems/controllers/LoginController.java new file mode 100644 index 0000000..67bd9b6 --- /dev/null +++ b/naresh/src/main/java/com/lftechnology/phpjava/ems/controllers/LoginController.java @@ -0,0 +1,38 @@ +package com.lftechnology.phpjava.ems.controllers; + +import com.lftechnology.phpjava.ems.entities.User; +import com.lftechnology.phpjava.ems.services.UserService; +import com.lftechnology.phpjava.ems.utlis.ConsoleWriter; +import com.lftechnology.phpjava.ems.utlis.PasswordHashGenerator; +import com.lftechnology.phpjava.ems.utlis.UserInput; + +/** + * LoginController + * + * @author Naresh Maharjan + * @since August, 08 2016 + */ +public class LoginController { + + private static UserService userService = new UserService(); + + public static void login() { + User user = new User(); + ConsoleWriter.writeBlankLine(3); + ConsoleWriter.writeUserInputRequestMessage("Enter your username:"); + user.setUsername(UserInput.getStringUserInput()); + + ConsoleWriter.writeBlankLine(3); + ConsoleWriter.writeUserInputRequestMessage("Enter your password:"); + try { + user.setPassword(UserInput.getStringUserInput()); + } catch (Exception e) { + e.printStackTrace(); + } + userService.login(user); + } + + public static void logout() { + userService.logout(); + } +} diff --git a/naresh/src/main/java/com/lftechnology/phpjava/ems/dao/DaoSignature.java b/naresh/src/main/java/com/lftechnology/phpjava/ems/dao/DaoSignature.java new file mode 100644 index 0000000..7b9c078 --- /dev/null +++ b/naresh/src/main/java/com/lftechnology/phpjava/ems/dao/DaoSignature.java @@ -0,0 +1,49 @@ +package com.lftechnology.phpjava.ems.dao; + +import java.sql.SQLException; +import java.util.List; +import java.util.Map; + +/** + * DaoSignature + * + * @author Naresh Maharjan + * @since August, 08 2016 + */ +public interface DaoSignature { + + /** + * + * @author Naresh Maharjan + * @return + * @throws SQLException + */ + List findAll() throws SQLException; + + + /** + * @author Naresh Maharjan + * @param s + * @return + * @throws SQLException + */ + int insert(T t) throws SQLException; + + /** + * @author Naresh Maharjan + * @param s + * @return + * @throws SQLException + */ + int update(T t) throws SQLException; + + + /** + * + * @author Naresh Maharjan + * @param s + * @return + * @throws SQLException + */ + boolean delete(T t) throws SQLException; +} diff --git a/naresh/src/main/java/com/lftechnology/phpjava/ems/dao/EmployeeDaoImpl.java b/naresh/src/main/java/com/lftechnology/phpjava/ems/dao/EmployeeDaoImpl.java new file mode 100644 index 0000000..12eb920 --- /dev/null +++ b/naresh/src/main/java/com/lftechnology/phpjava/ems/dao/EmployeeDaoImpl.java @@ -0,0 +1,157 @@ +package com.lftechnology.phpjava.ems.dao; + +import com.lftechnology.phpjava.ems.entities.Employee; +import com.lftechnology.phpjava.ems.enums.Role; +import com.lftechnology.phpjava.ems.utlis.DbFactory; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.List; + +/** + * EmployeeDaoImpl + * + * @author Naresh Maharjan + * @since August, 08 2016 + */ +public class EmployeeDaoImpl implements DaoSignature { + + protected Connection conn = DbFactory.getConnection(); + protected PreparedStatement stmt = null; + + @Override + public List findAll() throws SQLException { + return null; + } + + @Override + public int insert(Employee employee) throws SQLException { + String sql = "INSERT INTO employee (fullname, address, department, role) VALUES ( ?, ? , ?, ?);"; + stmt = conn.prepareStatement(sql); + stmt.setString(1, employee.getFullname()); + stmt.setString(2, employee.getAddress()); + stmt.setString(3, employee.getDepartment()); + stmt.setString(4, employee.getRole().toString()); + return stmt.executeUpdate(); + } + + @Override + public int update(Employee employee) throws SQLException { + return 0; + } + + @Override + public boolean delete(Employee employee) throws SQLException { + return false; + } + + + /** + * Find the employee by fullname + * + * @author Naresh Maharjan + * @param fullName + * @return Employee + * @throws SQLException + */ + public Employee findByFullName(String fullName) throws SQLException { + + String sql = "SELECT * FROM employee WHERE fullname = ?"; + + return findBy(sql, fullName); + } + + /** + * Find the employee by address + * + * @author Naresh Maharjan + * @param address + * @return Employee + * @throws SQLException + */ + public Employee findByAddress(String address) throws SQLException { + + String sql = "SELECT * FROM employee WHERE address = ?"; + + return findBy(sql, address); + } + + /** + * Find the employee by department + * + * @param department + * @return Employee + * @throws SQLException + */ + + public Employee findByDepartment(String department) throws SQLException { + + String sql = "SELECT * FROM employee WHERE department = ?"; + + return findBy(sql, department); + } + + /** + * Find the employee by role + * + * @author Naresh Maharjan + * @param + * @return Employee + * @throws SQLException + */ + + public Employee findByRole(Role role) throws SQLException { + + String sql = "SELECT * FROM employee WHERE role = ?"; + + return findBy(sql, role.toString()); + } + + + + /** + * + * @author Naresh Maharjan + * @param sql + * @param bindValue + * @return Employee + */ + private Employee findBy(String sql, String bindValue){ + Employee employee = new Employee(); + try { + stmt = conn.prepareStatement(sql); + stmt.setString(1, bindValue); + ResultSet resultSet = stmt.executeQuery(); + employee = setEmployeeData(resultSet); + } catch (SQLException e) { + e.printStackTrace(); + }finally { + DbFactory.closeConnection(); + } + return employee; + } + + /** + * set employee data from the resultset + * + * @author Naresh Maharjan + * @param resultSet + * @return Employee + */ + private Employee setEmployeeData(ResultSet resultSet){ + Employee employee = new Employee(); + try { + if (resultSet.next()) { + employee.setAddress(resultSet.getString("address")); + employee.setDepartment(resultSet.getString("department")); + employee.setFullname(resultSet.getString("fullname")); + employee.setRole((resultSet.getString("role").toLowerCase()).equals(Role.ADMIN.toString()) ? Role.ADMIN :Role.USER); + } + } catch (SQLException e) { + e.printStackTrace(); + } + return employee; + } +} diff --git a/naresh/src/main/java/com/lftechnology/phpjava/ems/dao/UserDaoImpl.java b/naresh/src/main/java/com/lftechnology/phpjava/ems/dao/UserDaoImpl.java new file mode 100644 index 0000000..b296b76 --- /dev/null +++ b/naresh/src/main/java/com/lftechnology/phpjava/ems/dao/UserDaoImpl.java @@ -0,0 +1,124 @@ +package com.lftechnology.phpjava.ems.dao; + +import com.lftechnology.phpjava.ems.entities.Employee; +import com.lftechnology.phpjava.ems.entities.User; +import com.lftechnology.phpjava.ems.enums.Role; +import com.lftechnology.phpjava.ems.utlis.DbFactory; +import com.lftechnology.phpjava.ems.utlis.PasswordHashGenerator; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.List; +import java.util.Map; + +/** + * UserDaoImpl + * + * @author Naresh Maharjan + * @since August, 08 2016 + */ +public class UserDaoImpl implements DaoSignature { + protected Connection conn = DbFactory.getConnection(); + protected PreparedStatement stmt = null; + + + @Override + public List findAll() throws SQLException { + return null; + } + + @Override + public int insert(User user) throws SQLException { + String sql = "INSERT INTO user (username, password, is_terminated) VALUES ( ?, ? , 0);"; + stmt = conn.prepareStatement(sql); + stmt.setString(1, user.getUsername()); + stmt.setString(2, user.getPassword()); + return stmt.executeUpdate(); + } + + @Override + public int update(User user) throws SQLException { + return 0; + } + + @Override + public boolean delete(User user) throws SQLException { + return false; + } + + + /** + * Find the employee by role + * + * @param User user + * @return User + * @throws SQLException + * @author Naresh Maharjan + */ + + public User findByUsernamePassword(User user) throws SQLException { + + String sql = "SELECT * FROM user WHERE is_terminated = 0 AND username = ?"; + String[] bindValues = {user.getUsername()}; + + User result = findBy(sql, bindValues); + try { + if (!PasswordHashGenerator.check(user.getPassword(), result.getPassword())) { + result = new User(); + } + } catch (Exception e) { + e.printStackTrace(); + } + return result; + } + + + /** + * @param sql + * @param bindValues + * @return User + * @author Naresh Maharjan + */ + private User findBy(String sql, String[] bindValues) { + User user = new User(); + try { + stmt = conn.prepareStatement(sql); + int counter = 1; + for (String bindvalue : bindValues) { + stmt.setString(counter, bindvalue); + counter++; + } + ResultSet resultSet = stmt.executeQuery(); + user = setUserData(resultSet); + } catch (SQLException e) { + e.printStackTrace(); + } finally { + DbFactory.closeConnection(); + } + return user; + } + + /** + * set user data from the resultset + * + * @param resultSet + * @return User + * @author Naresh Maharjan + */ + private User setUserData(ResultSet resultSet) { + User user = new User(); + try { + if (resultSet.next()) { + user.setUsername(resultSet.getString("username")); + user.setPassword(resultSet.getString("password")); + user.setTerminated(resultSet.getBoolean("is_terminated")); + } + } catch (SQLException e) { + e.printStackTrace(); + } + return user; + } + +} diff --git a/naresh/src/main/java/com/lftechnology/phpjava/ems/entities/Employee.java b/naresh/src/main/java/com/lftechnology/phpjava/ems/entities/Employee.java new file mode 100644 index 0000000..2fe197c --- /dev/null +++ b/naresh/src/main/java/com/lftechnology/phpjava/ems/entities/Employee.java @@ -0,0 +1,66 @@ +package com.lftechnology.phpjava.ems.entities; + +import com.lftechnology.phpjava.ems.enums.Role; + +/** + * Employee + * + * @author Naresh Maharjan + * @since August, 08 2016 + */ +public class Employee extends User { + private static final String TABLE = "employee"; + protected String fullname; + protected String address; + protected String department; + protected Role role; + + public Employee(){ + super(); + } + + public Employee(String fullname, String address, String department, Role role) { + super(); + this.fullname = fullname; + this.address = address; + this.department = department; + this.role = role; + } + + public String getFullname() { + return fullname; + } + + public Employee setFullname(String fullname) { + this.fullname = fullname; + return this; + } + + public String getAddress() { + return address; + } + + public Employee setAddress(String address) { + this.address = address; + return this; + } + + public String getDepartment() { + return department; + } + + public Employee setDepartment(String department) { + this.department = department; + return this; + } + + + public Role getRole() { + return role; + } + + public Employee setRole(Role role) { + this.role = role; + return this; + } +} diff --git a/naresh/src/main/java/com/lftechnology/phpjava/ems/entities/User.java b/naresh/src/main/java/com/lftechnology/phpjava/ems/entities/User.java new file mode 100644 index 0000000..b09f068 --- /dev/null +++ b/naresh/src/main/java/com/lftechnology/phpjava/ems/entities/User.java @@ -0,0 +1,56 @@ +package com.lftechnology.phpjava.ems.entities; + +/** + * User + * + * @author Naresh Maharjan + * @since August, 08 2016 + */ +public class User { + private final static String TABLE = "users"; + protected int id; + protected String username; + protected String password; + protected boolean isTerminated; + + + public User() { + } + + public User(String username, String password, boolean isTerminated) { + this.username = username; + this.password = password; + this.isTerminated = isTerminated; + } + + public int getId() { + return id; + } + + public String getUsername() { + return username; + } + + public User setUsername(String username) { + this.username = username; + return this; + } + + public String getPassword() { + return password; + } + + public User setPassword(String password) { + this.password = password; + return this; + } + + public boolean isTerminated() { + return isTerminated; + } + + public User setTerminated(boolean terminated) { + isTerminated = terminated; + return this; + } +} diff --git a/naresh/src/main/java/com/lftechnology/phpjava/ems/enums/Role.java b/naresh/src/main/java/com/lftechnology/phpjava/ems/enums/Role.java new file mode 100644 index 0000000..b0dd105 --- /dev/null +++ b/naresh/src/main/java/com/lftechnology/phpjava/ems/enums/Role.java @@ -0,0 +1,21 @@ +package com.lftechnology.phpjava.ems.enums; + +/** + * Role + * + * @author Naresh Maharjan + * @since August, 08 2016 + */ +public enum Role { + ADMIN("admin"), USER("user"); + private final String role; + + private Role(String role) { + this.role = role; + } + + public String getRole() { + return this.role; + } + +} diff --git a/naresh/src/main/java/com/lftechnology/phpjava/ems/services/EmployeeService.java b/naresh/src/main/java/com/lftechnology/phpjava/ems/services/EmployeeService.java new file mode 100644 index 0000000..765d6fd --- /dev/null +++ b/naresh/src/main/java/com/lftechnology/phpjava/ems/services/EmployeeService.java @@ -0,0 +1,68 @@ +package com.lftechnology.phpjava.ems.services; + +import com.lftechnology.phpjava.ems.dao.EmployeeDaoImpl; +import com.lftechnology.phpjava.ems.dao.UserDaoImpl; +import com.lftechnology.phpjava.ems.entities.Employee; +import com.lftechnology.phpjava.ems.entities.User; +import com.lftechnology.phpjava.ems.enums.Role; +import com.lftechnology.phpjava.ems.utlis.PasswordHashGenerator; + +import java.sql.SQLException; + +/** + * EmployeeService + * + * @author Naresh Maharjan + * @since August, 09 2016 + */ +public class EmployeeService { + + /** + * check if admin user exists or not and create a new one if it doesn't + * + * @author Naresh Maharjan + */ + public void checkAndCreateAdminUser(){ + if(!this.checkAdminUserExists()){ + Employee employee = new Employee(); + employee.setFullname("Admin User"); + employee.setAddress("Somewhere"); + employee.setRole(Role.ADMIN); + employee.setDepartment("Admin"); + employee.setUsername("admin"); + employee.setTerminated(false); + try { + employee.setPassword(PasswordHashGenerator.getSaltedHash("password")); + } catch (Exception e) { + e.printStackTrace(); + } + UserDaoImpl userDao = new UserDaoImpl(); + EmployeeDaoImpl employeeDao = new EmployeeDaoImpl(); + try { + userDao.insert(employee); + employeeDao.insert(employee); + } catch (SQLException e) { + e.printStackTrace(); + } + + } + } + + /** + * check if admin user exists or not + * + * @author Naresh Maharjan + * @return boolean + */ + public boolean checkAdminUserExists() { + EmployeeDaoImpl employeeDao = new EmployeeDaoImpl(); + Boolean result = false; + try { + Employee employee = employeeDao.findByRole(Role.ADMIN); + result = (employee.getRole() != null) ? employee.getRole().equals(Role.ADMIN) ? true : false : false; + } catch (SQLException e) { + e.printStackTrace(); + } + return result; + } +} diff --git a/naresh/src/main/java/com/lftechnology/phpjava/ems/services/UserService.java b/naresh/src/main/java/com/lftechnology/phpjava/ems/services/UserService.java new file mode 100644 index 0000000..a922279 --- /dev/null +++ b/naresh/src/main/java/com/lftechnology/phpjava/ems/services/UserService.java @@ -0,0 +1,39 @@ +package com.lftechnology.phpjava.ems.services; + +import com.lftechnology.phpjava.ems.dao.UserDaoImpl; +import com.lftechnology.phpjava.ems.entities.User; + +import java.sql.SQLException; + +/** + * UserService + * + * @author Naresh Maharjan + * @since August, 09 2016 + */ +public class UserService { + + private boolean isLoggedIn = false; + + UserDaoImpl userDao = new UserDaoImpl(); + + + public void login(User user) { + try { + User result = userDao.findByUsernamePassword(user); + if (user.getUsername().equals(result.getUsername())) { + isLoggedIn = true; + } + } catch (SQLException e) { + e.printStackTrace(); + } + } + + public boolean isUserLoggedIn() { + return isLoggedIn; + } + + public void logout() { + this.isLoggedIn = false; + } +} diff --git a/naresh/src/main/java/com/lftechnology/phpjava/ems/utlis/AbstractDbAdapater.java b/naresh/src/main/java/com/lftechnology/phpjava/ems/utlis/AbstractDbAdapater.java new file mode 100644 index 0000000..d0d146b --- /dev/null +++ b/naresh/src/main/java/com/lftechnology/phpjava/ems/utlis/AbstractDbAdapater.java @@ -0,0 +1,18 @@ +package com.lftechnology.phpjava.ems.utlis; + +import java.util.Map; + +/** + * AbstractDbAdapater + * + * @author Naresh Maharjan + * @since August, 09 2016 + */ +public abstract class AbstractDbAdapater { + protected String query = ""; + + protected AbstractDbAdapater where(Map condition){ + condition.entrySet(); + return this; + } +} diff --git a/naresh/src/main/java/com/lftechnology/phpjava/ems/utlis/ConsoleWriter.java b/naresh/src/main/java/com/lftechnology/phpjava/ems/utlis/ConsoleWriter.java new file mode 100644 index 0000000..bfee1fa --- /dev/null +++ b/naresh/src/main/java/com/lftechnology/phpjava/ems/utlis/ConsoleWriter.java @@ -0,0 +1,21 @@ +package com.lftechnology.phpjava.ems.utlis; + +/** + * ConsoleWriter + * + * @author Naresh Maharjan + * @since August, 10 2016 + */ +public class ConsoleWriter { + public static void writeBlankLine(int limit){ + int counter = 0; + while (counter < limit){ + System.out.println(); + counter ++; + } + } + + public static void writeUserInputRequestMessage(String message){ + System.out.print("\t\t\t\t\t\t\t\t " + message +" \t"); + } +} diff --git a/naresh/src/main/java/com/lftechnology/phpjava/ems/utlis/DbFactory.java b/naresh/src/main/java/com/lftechnology/phpjava/ems/utlis/DbFactory.java new file mode 100644 index 0000000..f7cebe5 --- /dev/null +++ b/naresh/src/main/java/com/lftechnology/phpjava/ems/utlis/DbFactory.java @@ -0,0 +1,91 @@ +package com.lftechnology.phpjava.ems.utlis; + +import com.lftechnology.phpjava.ems.constants.Constant; + +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.SQLException; +import java.util.Properties; + +/** + * DbFactory + * + * @author Naresh Maharjan + * @since August, 08 2016 + */ +public abstract class DbFactory { + private static Properties properties = new Properties(); + private static Connection connection = null; + + + /** + * load database configuration for properties file + * + * @author Naresh Maharjan + * @return + * @throws IOException + */ + private static void loadProperties() { + + InputStream input = null; + + try { + String filePath = System.getProperty("user.dir") + "/src/main/java/com/lftechnology/phpjava/ems/configs/db.properties"; + input = new FileInputStream(filePath); + properties.load(input); + } catch (IOException ex) { + ex.printStackTrace(); + } finally { + if (input != null) { + try { + input.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + } + + /** + * get database connection + * + * @author Naresh Maharjan + * @return + */ + public static Connection getConnection() { + loadProperties(); + try { + Class.forName(properties.getProperty(Constant.PROPERTY_DRIVER)); + connection = DriverManager.getConnection( + properties.getProperty(Constant.PROPERTY_URL), + properties.getProperty(Constant.PROPERTY_USERNAME), + properties.getProperty(Constant.PROPERTY_PASSWORD) + ); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } catch (SQLException e) { + e.printStackTrace(); + } + + return connection; + } + + /** + * + */ + public static void closeConnection(){ + + try { + + if (connection != null) { + connection.close(); + } + } + catch (SQLException e) { + e.printStackTrace(); + } + } +} diff --git a/naresh/src/main/java/com/lftechnology/phpjava/ems/utlis/PasswordHashGenerator.java b/naresh/src/main/java/com/lftechnology/phpjava/ems/utlis/PasswordHashGenerator.java new file mode 100644 index 0000000..df6f03a --- /dev/null +++ b/naresh/src/main/java/com/lftechnology/phpjava/ems/utlis/PasswordHashGenerator.java @@ -0,0 +1,60 @@ +package com.lftechnology.phpjava.ems.utlis; + + +import javax.crypto.SecretKey; +import javax.crypto.SecretKeyFactory; +import javax.crypto.spec.PBEKeySpec; +import java.security.SecureRandom; +import java.util.Base64; + +/** + * PasswordHashGenerator + * + * @author Naresh Maharjan + * @since August, 09 2016 + */ +public class PasswordHashGenerator { + // The higher the number of iterations the more + // expensive computing the hash is for us and + // also for an attacker. + private static final int iterations = 20 * 1000; + private static final int saltLen = 32; + private static final int desiredKeyLen = 256; + + /** + * Computes a salted PBKDF2 hash of given plaintext password + * suitable for storing in a database. + * Empty passwords are not supported. + */ + public static String getSaltedHash(String password) throws Exception { + byte[] salt = SecureRandom.getInstance("SHA1PRNG").generateSeed(saltLen); + // store the salt with the password + return Base64.getUrlEncoder().withoutPadding().encodeToString(salt) + "$" + hash(password, salt); + } + + /** + * Checks whether given plaintext password corresponds + * to a stored salted hash of the password. + */ + public static boolean check(String password, String stored) throws Exception { + String[] saltAndPass = stored.split("\\$"); + if (saltAndPass.length != 2) { + throw new IllegalStateException( + "The stored password have the form 'salt$hash'"); + } + String hashOfInput = hash(password, Base64.getUrlDecoder().decode(saltAndPass[0])); + return hashOfInput.equals(saltAndPass[1]); + } + + // using PBKDF2 from Sun, an alternative is https://github.com/wg/scrypt + // cf. http://www.unlimitednovelty.com/2012/03/dont-use-bcrypt.html + private static String hash(String password, byte[] salt) throws Exception { + if (password == null || password.length() == 0) + throw new IllegalArgumentException("Empty passwords are not supported."); + SecretKeyFactory f = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1"); + SecretKey key = f.generateSecret(new PBEKeySpec( + password.toCharArray(), salt, iterations, desiredKeyLen) + ); + return Base64.getUrlEncoder().withoutPadding().encodeToString(key.getEncoded()); + } +} \ No newline at end of file diff --git a/naresh/src/main/java/com/lftechnology/phpjava/ems/utlis/Router.java b/naresh/src/main/java/com/lftechnology/phpjava/ems/utlis/Router.java new file mode 100644 index 0000000..2abc4c6 --- /dev/null +++ b/naresh/src/main/java/com/lftechnology/phpjava/ems/utlis/Router.java @@ -0,0 +1,57 @@ +package com.lftechnology.phpjava.ems.utlis; + +import com.lftechnology.phpjava.ems.controllers.LoginController; +import com.lftechnology.phpjava.ems.services.EmployeeService; +import com.lftechnology.phpjava.ems.services.UserService; + + +/** + * RouterService + * + * @author Naresh Maharjan + * @since August, 09 2016 + */ +public class Router { + + + static { + onRouteBootstrap(); + } + + /** + * Method to be executed on router startup. + * Will invoke checkAndCreateAdminUser() method + * to create admin user if the system doesn't have one. + * + * @author Naresh Maharjan + */ + private static void onRouteBootstrap() { + + SqlRunner sqlRunner = new SqlRunner(); + sqlRunner.executeSql(); + + EmployeeService employeeService = new EmployeeService(); + employeeService.checkAndCreateAdminUser(); + } + + private static void showWelcomeScreen() { + + ConsoleWriter.writeBlankLine(3); + ConsoleWriter.writeUserInputRequestMessage("Welcome To Employee Management System !!!"); + + ConsoleWriter.writeBlankLine(3); + ConsoleWriter.writeUserInputRequestMessage("Enter your credentails to log into the system"); + } + + private static void showMenu() { + UserService userService = new UserService(); + if (!userService.isUserLoggedIn()) { + showWelcomeScreen(); + LoginController.login(); + } + } + + public static void main(String[] args) { + showMenu(); + } +} diff --git a/naresh/src/main/java/com/lftechnology/phpjava/ems/utlis/SqlRunner.java b/naresh/src/main/java/com/lftechnology/phpjava/ems/utlis/SqlRunner.java new file mode 100644 index 0000000..ea72bf5 --- /dev/null +++ b/naresh/src/main/java/com/lftechnology/phpjava/ems/utlis/SqlRunner.java @@ -0,0 +1,108 @@ +package com.lftechnology.phpjava.ems.utlis; + +import java.io.*; +import java.nio.charset.Charset; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.nio.file.StandardOpenOption; +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.List; + +/** + * SqlRunner + * + * @author Naresh Maharjan + * @since August, 11 2016 + */ +public class SqlRunner { + private static final String basePath = System.getProperty("user.dir") + "/src/main/java/com/lftechnology/phpjava/ems/autorun/"; + private List files; + private List sql = new ArrayList<>(); + + public List getSqlFileList() { + File folder = new File(basePath); + File[] listOfFiles = folder.listFiles(); + List sqlFiles = new ArrayList<>(); + + for (int i = 0; i < listOfFiles.length; i++) { + String fileName = listOfFiles[i].getName(); + if (listOfFiles[i].isFile() && fileName.endsWith(".sql")) { + sqlFiles.add(fileName); + } + } + return sqlFiles; + } + + public List getSql() { + this.files = this.getSqlFileList(); + List executedFilesList = this.getExecutedFileList(); + for (String file : files) { + if (!executedFilesList.contains(file)) { + getFileContent(file); + + } + } + return this.sql; + } + + private void getFileContent(String file) { + try { + try (BufferedReader br = new BufferedReader(new FileReader(basePath + file))) { + String line = br.readLine(); + while (line != null) { + this.sql.add(line); + line = br.readLine(); + } + } + } catch (IOException e) { + e.printStackTrace(); + } + } + + private List getExecutedFileList() { + List executedFiles = new ArrayList<>(); + try { + try (BufferedReader br = new BufferedReader(new FileReader(basePath + "executedFiles.txt"))) { + String line = br.readLine(); + while (line != null) { + executedFiles.add(line); + line = br.readLine(); + } + } + } catch (IOException e) { + e.printStackTrace(); + } + return executedFiles; + } + + + public void executeSql() { + this.getSql(); + Connection connection = DbFactory.getConnection(); + try { + if (!this.sql.isEmpty()) { + for (String query : this.sql) { + if (!query.isEmpty()) { + PreparedStatement stmt = connection.prepareStatement(query.toString()); + stmt.execute(); + } + } + Path excludedFileList = Paths.get(basePath + "executedFiles.txt"); + try { + Files.write(excludedFileList, this.files, Charset.forName("UTF-8"), StandardOpenOption.APPEND); + } catch (IOException e) { + e.printStackTrace(); + } + } + } catch (SQLException e) { + e.printStackTrace(); + } finally { + DbFactory.closeConnection(); + } + + } +} diff --git a/naresh/src/main/java/com/lftechnology/phpjava/ems/utlis/UserInput.java b/naresh/src/main/java/com/lftechnology/phpjava/ems/utlis/UserInput.java new file mode 100644 index 0000000..b170ff3 --- /dev/null +++ b/naresh/src/main/java/com/lftechnology/phpjava/ems/utlis/UserInput.java @@ -0,0 +1,58 @@ +package com.lftechnology.phpjava.ems.utlis; + +import java.util.Scanner; + +/** + * UserInput + * + * @author Naresh Maharjan + * @since August, 10 2016 + */ +public class UserInput { + + /** + * Get String user input from the user + * + * @return + * @author Naresh Maharjan + */ + public static String getStringUserInput() { + Scanner scanner = new Scanner(System.in); + String userInput = ""; + while (true) { + if (!scanner.hasNextLine()) { + scanner.nextLine(); + } else { + userInput = scanner.nextLine(); + if (userInput.isEmpty()) { + continue; + } else { + break; + } + } + } + return userInput; + } + + + /** + * Get int user input from the user + * + * @return int + * @author Naresh Maharjan + */ + public static int getIntegerUserInput() { + Scanner scanner = new Scanner(System.in); + int userInput = 0; + while (true) { + if (!scanner.hasNextLine()) { + scanner.nextLine(); + } else { + userInput = Integer.parseInt(scanner.nextLine()); + break; + } + } + return userInput; + } + +} From 61c4cee2c6ead2b15cb6a4bd312b6916f6bba47a Mon Sep 17 00:00:00 2001 From: Naresh Maharjan Date: Thu, 11 Aug 2016 21:40:29 +0545 Subject: [PATCH 2/9] #15 gitignore file updated --- .gitignore | 1 + .../phpjava/ems/autorun/executedFiles.txt | 1 - .../phpjava/ems/autorun/inititalTables.sql | 2 +- .../phpjava/ems/dao/EmployeeDaoImpl.java | 3 +- .../phpjava/ems/dao/UserDaoImpl.java | 7 ++-- .../phpjava/ems/entities/Employee.java | 14 +++++++- .../phpjava/ems/services/EmployeeService.java | 3 +- .../phpjava/ems/services/UserService.java | 8 ++++- .../phpjava/ems/utlis/Router.java | 33 +++++++++++++++++-- 9 files changed, 62 insertions(+), 10 deletions(-) delete mode 100644 naresh/src/main/java/com/lftechnology/phpjava/ems/autorun/executedFiles.txt diff --git a/.gitignore b/.gitignore index a0df482..1bb223a 100644 --- a/.gitignore +++ b/.gitignore @@ -21,3 +21,4 @@ hs_err_pid* *.classpath **/target/** /bin/ +*/executedFiles.txt diff --git a/naresh/src/main/java/com/lftechnology/phpjava/ems/autorun/executedFiles.txt b/naresh/src/main/java/com/lftechnology/phpjava/ems/autorun/executedFiles.txt deleted file mode 100644 index 379b61c..0000000 --- a/naresh/src/main/java/com/lftechnology/phpjava/ems/autorun/executedFiles.txt +++ /dev/null @@ -1 +0,0 @@ -inititalTables.sql diff --git a/naresh/src/main/java/com/lftechnology/phpjava/ems/autorun/inititalTables.sql b/naresh/src/main/java/com/lftechnology/phpjava/ems/autorun/inititalTables.sql index e3ba7c8..53ac5f9 100644 --- a/naresh/src/main/java/com/lftechnology/phpjava/ems/autorun/inititalTables.sql +++ b/naresh/src/main/java/com/lftechnology/phpjava/ems/autorun/inititalTables.sql @@ -1,3 +1,3 @@ CREATE TABLE user (id INT PRIMARY KEY AUTO_INCREMENT,username VARCHAR(255),password VARCHAR(255),is_terminated BINARY DEFAULT FALSE); -CREATE TABLE employee (id INT PRIMARY KEY AUTO_INCREMENT, fullname VARCHAR(255), department VARCHAR(255), address VARCHAR(255), role VARCHAR(10)); \ No newline at end of file +CREATE TABLE employee (id INT PRIMARY KEY AUTO_INCREMENT, fullname VARCHAR(255), department VARCHAR(255), address VARCHAR(255), role VARCHAR(10), user_id INT NOT NULL,CONSTRAINT emp_user_id_fk FOREIGN KEY (user_id) REFERENCES user (id)); \ No newline at end of file diff --git a/naresh/src/main/java/com/lftechnology/phpjava/ems/dao/EmployeeDaoImpl.java b/naresh/src/main/java/com/lftechnology/phpjava/ems/dao/EmployeeDaoImpl.java index 12eb920..8ebc3e0 100644 --- a/naresh/src/main/java/com/lftechnology/phpjava/ems/dao/EmployeeDaoImpl.java +++ b/naresh/src/main/java/com/lftechnology/phpjava/ems/dao/EmployeeDaoImpl.java @@ -28,12 +28,13 @@ public List findAll() throws SQLException { @Override public int insert(Employee employee) throws SQLException { - String sql = "INSERT INTO employee (fullname, address, department, role) VALUES ( ?, ? , ?, ?);"; + String sql = "INSERT INTO employee (fullname, address, department, role, user_id) VALUES ( ?, ? , ?, ?,?);"; stmt = conn.prepareStatement(sql); stmt.setString(1, employee.getFullname()); stmt.setString(2, employee.getAddress()); stmt.setString(3, employee.getDepartment()); stmt.setString(4, employee.getRole().toString()); + stmt.setInt(5, employee.getUserId()); return stmt.executeUpdate(); } diff --git a/naresh/src/main/java/com/lftechnology/phpjava/ems/dao/UserDaoImpl.java b/naresh/src/main/java/com/lftechnology/phpjava/ems/dao/UserDaoImpl.java index b296b76..5cbf293 100644 --- a/naresh/src/main/java/com/lftechnology/phpjava/ems/dao/UserDaoImpl.java +++ b/naresh/src/main/java/com/lftechnology/phpjava/ems/dao/UserDaoImpl.java @@ -52,7 +52,7 @@ public boolean delete(User user) throws SQLException { /** * Find the employee by role * - * @param User user + * @param * @return User * @throws SQLException * @author Naresh Maharjan @@ -65,7 +65,10 @@ public User findByUsernamePassword(User user) throws SQLException { User result = findBy(sql, bindValues); try { - if (!PasswordHashGenerator.check(user.getPassword(), result.getPassword())) { + if(result.getPassword() == null){ + result = new User(); + } + else if (!PasswordHashGenerator.check(user.getPassword(), result.getPassword())) { result = new User(); } } catch (Exception e) { diff --git a/naresh/src/main/java/com/lftechnology/phpjava/ems/entities/Employee.java b/naresh/src/main/java/com/lftechnology/phpjava/ems/entities/Employee.java index 2fe197c..e348467 100644 --- a/naresh/src/main/java/com/lftechnology/phpjava/ems/entities/Employee.java +++ b/naresh/src/main/java/com/lftechnology/phpjava/ems/entities/Employee.java @@ -14,17 +14,20 @@ public class Employee extends User { protected String address; protected String department; protected Role role; + protected int userId; public Employee(){ super(); } - public Employee(String fullname, String address, String department, Role role) { + + public Employee(String fullname, String address, String department, Role role, int userId) { super(); this.fullname = fullname; this.address = address; this.department = department; this.role = role; + this.userId = userId; } public String getFullname() { @@ -63,4 +66,13 @@ public Employee setRole(Role role) { this.role = role; return this; } + + public int getUserId() { + return userId; + } + + public Employee setUserId(int userId) { + this.userId = userId; + return this; + } } diff --git a/naresh/src/main/java/com/lftechnology/phpjava/ems/services/EmployeeService.java b/naresh/src/main/java/com/lftechnology/phpjava/ems/services/EmployeeService.java index 765d6fd..aa9f481 100644 --- a/naresh/src/main/java/com/lftechnology/phpjava/ems/services/EmployeeService.java +++ b/naresh/src/main/java/com/lftechnology/phpjava/ems/services/EmployeeService.java @@ -39,7 +39,8 @@ public void checkAndCreateAdminUser(){ UserDaoImpl userDao = new UserDaoImpl(); EmployeeDaoImpl employeeDao = new EmployeeDaoImpl(); try { - userDao.insert(employee); + int userId = userDao.insert(employee); + employee.setUserId(userId); employeeDao.insert(employee); } catch (SQLException e) { e.printStackTrace(); diff --git a/naresh/src/main/java/com/lftechnology/phpjava/ems/services/UserService.java b/naresh/src/main/java/com/lftechnology/phpjava/ems/services/UserService.java index a922279..e00d6a5 100644 --- a/naresh/src/main/java/com/lftechnology/phpjava/ems/services/UserService.java +++ b/naresh/src/main/java/com/lftechnology/phpjava/ems/services/UserService.java @@ -14,6 +14,7 @@ public class UserService { private boolean isLoggedIn = false; + private String loggedInUserRole = ""; UserDaoImpl userDao = new UserDaoImpl(); @@ -22,7 +23,8 @@ public void login(User user) { try { User result = userDao.findByUsernamePassword(user); if (user.getUsername().equals(result.getUsername())) { - isLoggedIn = true; + this.isLoggedIn = true; +// this.loggedInUserRole = user } } catch (SQLException e) { e.printStackTrace(); @@ -33,6 +35,10 @@ public boolean isUserLoggedIn() { return isLoggedIn; } + public String getLoggedInUserRole() { + return this.loggedInUserRole; + } + public void logout() { this.isLoggedIn = false; } diff --git a/naresh/src/main/java/com/lftechnology/phpjava/ems/utlis/Router.java b/naresh/src/main/java/com/lftechnology/phpjava/ems/utlis/Router.java index 2abc4c6..c490e06 100644 --- a/naresh/src/main/java/com/lftechnology/phpjava/ems/utlis/Router.java +++ b/naresh/src/main/java/com/lftechnology/phpjava/ems/utlis/Router.java @@ -13,6 +13,7 @@ */ public class Router { + private static UserService userService = new UserService(); static { onRouteBootstrap(); @@ -34,6 +35,11 @@ private static void onRouteBootstrap() { employeeService.checkAndCreateAdminUser(); } + /** + * Show Welcome Screen + * + * @author Naresh Maharjan + */ private static void showWelcomeScreen() { ConsoleWriter.writeBlankLine(3); @@ -43,14 +49,37 @@ private static void showWelcomeScreen() { ConsoleWriter.writeUserInputRequestMessage("Enter your credentails to log into the system"); } - private static void showMenu() { - UserService userService = new UserService(); + private static void showLoginMenuAndLoginToSystem() { if (!userService.isUserLoggedIn()) { showWelcomeScreen(); LoginController.login(); } } + private static void showMenu() { + showLoginMenuAndLoginToSystem(); +// if(userService.isUserLoggedIn() && userService.){ +// +// } + + } + + private static void showAdminMenu() { + ConsoleWriter.writeBlankLine(3); + ConsoleWriter.writeUserInputRequestMessage("1. Add New User"); + ConsoleWriter.writeBlankLine(3); + ConsoleWriter.writeUserInputRequestMessage("2. Delete User"); + ConsoleWriter.writeBlankLine(3); + ConsoleWriter.writeUserInputRequestMessage("3. Search User(s)"); + } + + private static void showNormalUserMenu() { + ConsoleWriter.writeBlankLine(3); + ConsoleWriter.writeUserInputRequestMessage("1. Update Profile"); + ConsoleWriter.writeBlankLine(3); + ConsoleWriter.writeUserInputRequestMessage("2. Search User(s)"); + } + public static void main(String[] args) { showMenu(); } From a94fd2818b68f384bdc231e13ec3ec42f4b41afa Mon Sep 17 00:00:00 2001 From: Naresh Maharjan Date: Thu, 11 Aug 2016 22:17:32 +0545 Subject: [PATCH 3/9] service updated --- .../phpjava/ems/autorun/executedFiles.txt | 1 + .../phpjava/ems/dao/UserDaoImpl.java | 16 ++++++++++------ .../phpjava/ems/services/UserService.java | 2 +- .../lftechnology/phpjava/ems/utlis/Router.java | 18 ++++++++++++++++++ .../phpjava/ems/utlis/SqlRunner.java | 11 +++++++++++ 5 files changed, 41 insertions(+), 7 deletions(-) create mode 100644 naresh/src/main/java/com/lftechnology/phpjava/ems/autorun/executedFiles.txt diff --git a/naresh/src/main/java/com/lftechnology/phpjava/ems/autorun/executedFiles.txt b/naresh/src/main/java/com/lftechnology/phpjava/ems/autorun/executedFiles.txt new file mode 100644 index 0000000..379b61c --- /dev/null +++ b/naresh/src/main/java/com/lftechnology/phpjava/ems/autorun/executedFiles.txt @@ -0,0 +1 @@ +inititalTables.sql diff --git a/naresh/src/main/java/com/lftechnology/phpjava/ems/dao/UserDaoImpl.java b/naresh/src/main/java/com/lftechnology/phpjava/ems/dao/UserDaoImpl.java index 5cbf293..75f14f1 100644 --- a/naresh/src/main/java/com/lftechnology/phpjava/ems/dao/UserDaoImpl.java +++ b/naresh/src/main/java/com/lftechnology/phpjava/ems/dao/UserDaoImpl.java @@ -6,10 +6,7 @@ import com.lftechnology.phpjava.ems.utlis.DbFactory; import com.lftechnology.phpjava.ems.utlis.PasswordHashGenerator; -import java.sql.Connection; -import java.sql.PreparedStatement; -import java.sql.ResultSet; -import java.sql.SQLException; +import java.sql.*; import java.util.List; import java.util.Map; @@ -31,11 +28,18 @@ public List findAll() throws SQLException { @Override public int insert(User user) throws SQLException { + int lastInsertedId = 0; String sql = "INSERT INTO user (username, password, is_terminated) VALUES ( ?, ? , 0);"; - stmt = conn.prepareStatement(sql); + stmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS); stmt.setString(1, user.getUsername()); stmt.setString(2, user.getPassword()); - return stmt.executeUpdate(); + stmt.executeUpdate(); + ResultSet rs = stmt.getGeneratedKeys(); + if(rs.next()) + { + lastInsertedId = rs.getInt(1); + } + return lastInsertedId; } @Override diff --git a/naresh/src/main/java/com/lftechnology/phpjava/ems/services/UserService.java b/naresh/src/main/java/com/lftechnology/phpjava/ems/services/UserService.java index e00d6a5..076815e 100644 --- a/naresh/src/main/java/com/lftechnology/phpjava/ems/services/UserService.java +++ b/naresh/src/main/java/com/lftechnology/phpjava/ems/services/UserService.java @@ -24,7 +24,7 @@ public void login(User user) { User result = userDao.findByUsernamePassword(user); if (user.getUsername().equals(result.getUsername())) { this.isLoggedIn = true; -// this.loggedInUserRole = user +// this.loggedInUserRole = user.get } } catch (SQLException e) { e.printStackTrace(); diff --git a/naresh/src/main/java/com/lftechnology/phpjava/ems/utlis/Router.java b/naresh/src/main/java/com/lftechnology/phpjava/ems/utlis/Router.java index c490e06..6290844 100644 --- a/naresh/src/main/java/com/lftechnology/phpjava/ems/utlis/Router.java +++ b/naresh/src/main/java/com/lftechnology/phpjava/ems/utlis/Router.java @@ -1,9 +1,13 @@ package com.lftechnology.phpjava.ems.utlis; import com.lftechnology.phpjava.ems.controllers.LoginController; +import com.lftechnology.phpjava.ems.dao.UserDaoImpl; +import com.lftechnology.phpjava.ems.entities.User; import com.lftechnology.phpjava.ems.services.EmployeeService; import com.lftechnology.phpjava.ems.services.UserService; +import java.sql.SQLException; + /** * RouterService @@ -81,6 +85,20 @@ private static void showNormalUserMenu() { } public static void main(String[] args) { + UserDaoImpl userDao = new UserDaoImpl(); + User user = new User(); + user.setUsername("naresh" + Math.random()); + try { + user.setPassword(PasswordHashGenerator.getSaltedHash("maharjan")); + } catch (Exception e) { + e.printStackTrace(); + } + try { + int result = userDao.insert(user); + System.out.println(result); + } catch (SQLException e) { + e.printStackTrace(); + } showMenu(); } } diff --git a/naresh/src/main/java/com/lftechnology/phpjava/ems/utlis/SqlRunner.java b/naresh/src/main/java/com/lftechnology/phpjava/ems/utlis/SqlRunner.java index ea72bf5..8b60fdb 100644 --- a/naresh/src/main/java/com/lftechnology/phpjava/ems/utlis/SqlRunner.java +++ b/naresh/src/main/java/com/lftechnology/phpjava/ems/utlis/SqlRunner.java @@ -66,6 +66,7 @@ private void getFileContent(String file) { private List getExecutedFileList() { List executedFiles = new ArrayList<>(); try { + this.checkAndCreateExecutedListFile(); try (BufferedReader br = new BufferedReader(new FileReader(basePath + "executedFiles.txt"))) { String line = br.readLine(); while (line != null) { @@ -103,6 +104,16 @@ public void executeSql() { } finally { DbFactory.closeConnection(); } + } + private void checkAndCreateExecutedListFile() { + File f = new File(basePath + "executedFiles.txt"); + try { + if (!f.exists()) { + f.createNewFile(); + } + } catch (IOException e) { + e.printStackTrace(); + } } } From 9df3b74deb7006ff7cd4b61a75bddf0c95a741b4 Mon Sep 17 00:00:00 2001 From: Naresh Maharjan Date: Thu, 11 Aug 2016 22:20:13 +0545 Subject: [PATCH 4/9] exected files added in gitignore --- .gitignore | 2 +- .../java/com/lftechnology/phpjava/ems/autorun/executedFiles.txt | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) delete mode 100644 naresh/src/main/java/com/lftechnology/phpjava/ems/autorun/executedFiles.txt diff --git a/.gitignore b/.gitignore index 1bb223a..36e7ec2 100644 --- a/.gitignore +++ b/.gitignore @@ -21,4 +21,4 @@ hs_err_pid* *.classpath **/target/** /bin/ -*/executedFiles.txt +executedFiles.txt diff --git a/naresh/src/main/java/com/lftechnology/phpjava/ems/autorun/executedFiles.txt b/naresh/src/main/java/com/lftechnology/phpjava/ems/autorun/executedFiles.txt deleted file mode 100644 index 379b61c..0000000 --- a/naresh/src/main/java/com/lftechnology/phpjava/ems/autorun/executedFiles.txt +++ /dev/null @@ -1 +0,0 @@ -inititalTables.sql From 682043859db10dbe277a89ad5272631e9457cb98 Mon Sep 17 00:00:00 2001 From: Naresh Maharjan Date: Wed, 17 Aug 2016 02:12:20 +0545 Subject: [PATCH 5/9] separated view logic and implemented terminate and delete user feature --- naresh/pom.xml | 6 + .../phpjava/ems/EmployeeManagementSystem.java | 43 +++ .../phpjava/ems/autorun/inititalTables.sql | 4 +- .../phpjava/ems/constants/Constant.java | 16 + .../ems/controllers/ControllerSignature.java | 15 + .../ems/controllers/EmployeeController.java | 150 ++++++++- .../ems/controllers/LoginController.java | 60 ++-- .../phpjava/ems/dao/DaoSignature.java | 3 +- .../phpjava/ems/dao/EmployeeDaoImpl.java | 145 +++++--- .../phpjava/ems/dao/UserDaoImpl.java | 68 +++- .../phpjava/ems/entities/Employee.java | 20 +- .../phpjava/ems/entities/User.java | 7 +- .../phpjava/ems/services/EmployeeService.java | 130 ++++++-- .../phpjava/ems/services/UserService.java | 16 +- .../phpjava/ems/utlis/CommonUtility.java | 34 ++ .../phpjava/ems/utlis/ConsoleWriter.java | 28 +- .../phpjava/ems/utlis/DbFactory.java | 20 +- .../phpjava/ems/utlis/Router.java | 102 +++--- .../phpjava/ems/utlis/SqlRunner.java | 5 +- .../phpjava/ems/utlis/UserInput.java | 43 ++- .../phpjava/ems/views/AbstractBaseView.java | 25 ++ .../phpjava/ems/views/CommonViewUtility.java | 60 ++++ .../phpjava/ems/views/EmployeeView.java | 315 ++++++++++++++++++ .../phpjava/ems/views/LoginView.java | 75 +++++ .../phpjava/ems/views/TableGenerator.java | 42 +++ .../phpjava/ems/views/ViewSignature.java | 17 + 26 files changed, 1243 insertions(+), 206 deletions(-) create mode 100644 naresh/src/main/java/com/lftechnology/phpjava/ems/EmployeeManagementSystem.java create mode 100644 naresh/src/main/java/com/lftechnology/phpjava/ems/controllers/ControllerSignature.java create mode 100644 naresh/src/main/java/com/lftechnology/phpjava/ems/utlis/CommonUtility.java create mode 100644 naresh/src/main/java/com/lftechnology/phpjava/ems/views/AbstractBaseView.java create mode 100644 naresh/src/main/java/com/lftechnology/phpjava/ems/views/CommonViewUtility.java create mode 100644 naresh/src/main/java/com/lftechnology/phpjava/ems/views/EmployeeView.java create mode 100644 naresh/src/main/java/com/lftechnology/phpjava/ems/views/LoginView.java create mode 100644 naresh/src/main/java/com/lftechnology/phpjava/ems/views/TableGenerator.java create mode 100644 naresh/src/main/java/com/lftechnology/phpjava/ems/views/ViewSignature.java diff --git a/naresh/pom.xml b/naresh/pom.xml index d71b9a5..a1d7ca9 100644 --- a/naresh/pom.xml +++ b/naresh/pom.xml @@ -12,6 +12,12 @@ mysql-connector-java 5.1.6 + + + de.vandermeer + asciitable + 0.2.5 + naresh \ No newline at end of file diff --git a/naresh/src/main/java/com/lftechnology/phpjava/ems/EmployeeManagementSystem.java b/naresh/src/main/java/com/lftechnology/phpjava/ems/EmployeeManagementSystem.java new file mode 100644 index 0000000..915c43c --- /dev/null +++ b/naresh/src/main/java/com/lftechnology/phpjava/ems/EmployeeManagementSystem.java @@ -0,0 +1,43 @@ +package com.lftechnology.phpjava.ems; + +import com.lftechnology.phpjava.ems.utlis.Router; + +/** + * EmployeeManagementSystem + * + * @author Naresh Maharjan + * @since August, 12 2016 + */ +public class EmployeeManagementSystem { + public static void main(String[] args) { +// V2_AsciiTable at = new V2_AsciiTable(); +// V2_AsciiTableRenderer rend = new V2_AsciiTableRenderer(); +// rend.setTheme(V2_E_TableThemes.UTF_HEAVY.get()); +// rend.setWidth(new WidthAbsoluteEven(160)); +// String[] rows = new String[6]; +// rows[0] = "one"; +// rows[1] = "two"; +// rows[2] = "three"; +// rows[3] = "four"; +// rows[4] = "five"; +// rows[5] = "six"; +// at.addRule(); +// at.addRow(rows); +// at.addRule(); +// EmployeeDaoImpl employeeDao = new EmployeeDaoImpl(); +// try { +// List employees = employeeDao.findAll(); +// for (Employee e: employees) { +// at.addRow(e.toArray()); +// } +// } catch (SQLException e) { +// e.printStackTrace(); +// } +// at.addRule(); +// RenderedTable rt = rend.render(at); +// System.out.println(rt); + + Router.showMenu(); +// CommonViewUtility.showWelcomeScreen(); + } +} diff --git a/naresh/src/main/java/com/lftechnology/phpjava/ems/autorun/inititalTables.sql b/naresh/src/main/java/com/lftechnology/phpjava/ems/autorun/inititalTables.sql index 53ac5f9..320aab2 100644 --- a/naresh/src/main/java/com/lftechnology/phpjava/ems/autorun/inititalTables.sql +++ b/naresh/src/main/java/com/lftechnology/phpjava/ems/autorun/inititalTables.sql @@ -1,3 +1,5 @@ CREATE TABLE user (id INT PRIMARY KEY AUTO_INCREMENT,username VARCHAR(255),password VARCHAR(255),is_terminated BINARY DEFAULT FALSE); -CREATE TABLE employee (id INT PRIMARY KEY AUTO_INCREMENT, fullname VARCHAR(255), department VARCHAR(255), address VARCHAR(255), role VARCHAR(10), user_id INT NOT NULL,CONSTRAINT emp_user_id_fk FOREIGN KEY (user_id) REFERENCES user (id)); \ No newline at end of file +CREATE TABLE employee (id INT PRIMARY KEY AUTO_INCREMENT, fullname VARCHAR(255), department VARCHAR(255), address VARCHAR(255), role VARCHAR(10), user_id INT NOT NULL,CONSTRAINT emp_user_id_fk FOREIGN KEY (user_id) REFERENCES user (id)); +ALTER TABLE employee DROP FOREIGN KEY emp_user_id_fk; +ALTER TABLE employee ADD CONSTRAINT emp_user_id_fk FOREIGN KEY (user_id) REFERENCES user (id) ON DELETE CASCADE ON UPDATE CASCADE; \ No newline at end of file diff --git a/naresh/src/main/java/com/lftechnology/phpjava/ems/constants/Constant.java b/naresh/src/main/java/com/lftechnology/phpjava/ems/constants/Constant.java index b946dca..6ddad94 100644 --- a/naresh/src/main/java/com/lftechnology/phpjava/ems/constants/Constant.java +++ b/naresh/src/main/java/com/lftechnology/phpjava/ems/constants/Constant.java @@ -13,4 +13,20 @@ public class Constant { public static final String PROPERTY_USERNAME = "username"; public static final String PROPERTY_PASSWORD = "password"; public static final String ADMIN = "admin"; + public static final int ADD_NEW_USER = 1; + public static final int UPDATE_PROFILE = 1; + public static final int SEARCH_USER = 2; + public static final int TERMINATE_USER = 3; + public static final int DELETE_USER = 4; + public static final int EXIT = 0; + public static final int AMIN_USER_INPUT_ID = 1; + public static final int NORMAL_USER_INPUT_ID = 2; + public static final String POST_LOGIN_ACTION = "postLogin"; + public static final String ADD_USER_ACTION = "addUser"; + public static final String SEARCH_USER_ACTION = "searchUser"; + public static final String TERMINATE_USER_ACTION = "terminateUser"; + public static final String DELETE_USER_ACTION = "deleteUser"; + public static final String EXIT_ACTION = "exit"; + public static final String UPDATE_PROFILE_ACTION = "updateProfile"; + } diff --git a/naresh/src/main/java/com/lftechnology/phpjava/ems/controllers/ControllerSignature.java b/naresh/src/main/java/com/lftechnology/phpjava/ems/controllers/ControllerSignature.java new file mode 100644 index 0000000..8760d62 --- /dev/null +++ b/naresh/src/main/java/com/lftechnology/phpjava/ems/controllers/ControllerSignature.java @@ -0,0 +1,15 @@ +package com.lftechnology.phpjava.ems.controllers; + +import java.util.Map; + +/** + * ControllerSignature + * + * @author Naresh Maharjan + * @since August, 16 2016 + */ +public interface ControllerSignature { + public void setData(Map data); + public Map getData(); + public boolean isPost(); +} diff --git a/naresh/src/main/java/com/lftechnology/phpjava/ems/controllers/EmployeeController.java b/naresh/src/main/java/com/lftechnology/phpjava/ems/controllers/EmployeeController.java index d346188..ef36a83 100644 --- a/naresh/src/main/java/com/lftechnology/phpjava/ems/controllers/EmployeeController.java +++ b/naresh/src/main/java/com/lftechnology/phpjava/ems/controllers/EmployeeController.java @@ -1,10 +1,158 @@ package com.lftechnology.phpjava.ems.controllers; +import com.lftechnology.phpjava.ems.constants.Constant; +import com.lftechnology.phpjava.ems.entities.Employee; +import com.lftechnology.phpjava.ems.services.EmployeeService; +import com.lftechnology.phpjava.ems.views.CommonViewUtility; +import com.lftechnology.phpjava.ems.views.EmployeeView; + +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + /** * EmployeeController * * @author Naresh Maharjan * @since August, 08 2016 */ -public class EmployeeController { +public class EmployeeController implements ControllerSignature { + private static EmployeeService employeeService = new EmployeeService(); + private static EmployeeView employeeView = new EmployeeView(); + private static Map postData = new HashMap<>(); + private boolean isPost = false; + + public void postLoginScreen() { + employeeView.setAction(Constant.POST_LOGIN_ACTION); + employeeView.setData(postData); + employeeView.render(); + } + + public void addUser() { + if (this.isPost) { + Employee employee = (Employee) postData.get("employee"); + int result = 0; + try { + result = employeeService.addEmployee(employee); + } catch (Exception e) { + e.printStackTrace(); + } + String message; + if (result > 0) { + message = "Employee added successfully"; + } else { + message = "Unable to add employee"; + } + postData.clear(); + this.isPost = false; + CommonViewUtility.showMessageAndContinue(message); + } else { + employeeView.setAction(Constant.ADD_USER_ACTION); + employeeView.render(); + } + + } + + public void searchUser() { + if (this.isPost) { + Employee employee = (Employee) postData.get("employee"); + List result = new ArrayList<>(); + try { + result = employeeService.searchEmployee(employee); + } catch (SQLException e) { + e.printStackTrace(); + } + employeeView.renderEmployeeList(employeeService.getColumns(), result); + this.isPost = false; + postData.clear(); + CommonViewUtility.pressKeyToContinue(); + } else { + employeeView.setAction(Constant.SEARCH_USER_ACTION); + employeeView.render(); + } + } + + public void terminateUser() { + if (this.isPost()){ + Employee employee = (Employee) postData.get("employee"); + int result= 0; + try { + result = employeeService.terminateUser(employee); + } catch (SQLException e) { + e.printStackTrace(); + } + String message = result + " users were terminated"; + postData.clear(); + this.isPost = false; + CommonViewUtility.showMessageAndContinue(message); + }else { + employeeView.setAction(Constant.TERMINATE_USER_ACTION); + employeeView.render(); + } + + } + + public void deleteUser() { + if (this.isPost()){ + Employee employee = (Employee) postData.get("employee"); + int result = 0; + try { + result = employeeService.deleteUser(employee); + } catch (Exception e) { + e.printStackTrace(); + } + String message = result + " users were deleted"; + postData.clear(); + this.isPost = false; + CommonViewUtility.showMessageAndContinue(message); + }else { + employeeView.setAction(Constant.DELETE_USER_ACTION); + employeeView.render(); + } + } + + public void updateProfile() { + if (this.isPost) { + Employee employee = (Employee) postData.get("employee"); + int result = 0; + try { + result = employeeService.updateEmployee(employee); + } catch (Exception e) { + e.printStackTrace(); + } + this.isPost = false; + postData.clear(); + String message; + if (result > 0) { + message = "Data updated successfully."; + } else { + message = "Unable to update profile."; + } + CommonViewUtility.showMessageAndContinue(message); + } else { + employeeView.setAction(Constant.UPDATE_PROFILE_ACTION); + employeeView.render(); + } + } + + @Override + public void setData(Map data) { + postData = data; + } + + @Override + public Map getData() { + return null; + } + + @Override + public boolean isPost() { + return this.isPost; + } + + public void makeCurrentRequestPost() { + this.isPost = true; + } } diff --git a/naresh/src/main/java/com/lftechnology/phpjava/ems/controllers/LoginController.java b/naresh/src/main/java/com/lftechnology/phpjava/ems/controllers/LoginController.java index 67bd9b6..0d1fea3 100644 --- a/naresh/src/main/java/com/lftechnology/phpjava/ems/controllers/LoginController.java +++ b/naresh/src/main/java/com/lftechnology/phpjava/ems/controllers/LoginController.java @@ -2,9 +2,11 @@ import com.lftechnology.phpjava.ems.entities.User; import com.lftechnology.phpjava.ems.services.UserService; -import com.lftechnology.phpjava.ems.utlis.ConsoleWriter; -import com.lftechnology.phpjava.ems.utlis.PasswordHashGenerator; -import com.lftechnology.phpjava.ems.utlis.UserInput; +import com.lftechnology.phpjava.ems.utlis.CommonUtility; +import com.lftechnology.phpjava.ems.views.LoginView; + +import java.util.HashMap; +import java.util.Map; /** * LoginController @@ -12,27 +14,39 @@ * @author Naresh Maharjan * @since August, 08 2016 */ -public class LoginController { - - private static UserService userService = new UserService(); - - public static void login() { - User user = new User(); - ConsoleWriter.writeBlankLine(3); - ConsoleWriter.writeUserInputRequestMessage("Enter your username:"); - user.setUsername(UserInput.getStringUserInput()); - - ConsoleWriter.writeBlankLine(3); - ConsoleWriter.writeUserInputRequestMessage("Enter your password:"); - try { - user.setPassword(UserInput.getStringUserInput()); - } catch (Exception e) { - e.printStackTrace(); - } - userService.login(user); +public class LoginController implements ControllerSignature{ + + private static LoginView loginView = new LoginView(); + private UserService userService = new UserService(); + private CommonUtility commonUtility = new CommonUtility(); + private static Map postData = new HashMap<>(); + public void login() { + loginView.setAction("login"); + loginView.render(); + + User user = (User) this.postData.get("user"); + this.userService.login(user); + this.commonUtility.setUserService(this.userService); + } + + public void logout() { + loginView.logout(); + } + + @Override + public void setData(Map data) { + this.postData = data; + } + + @Override + public Map getData() { + Map data = new HashMap<>(); + data.put("commonUtility", this.commonUtility); + return data; } - public static void logout() { - userService.logout(); + @Override + public boolean isPost() { + return false; } } diff --git a/naresh/src/main/java/com/lftechnology/phpjava/ems/dao/DaoSignature.java b/naresh/src/main/java/com/lftechnology/phpjava/ems/dao/DaoSignature.java index 7b9c078..5bc0820 100644 --- a/naresh/src/main/java/com/lftechnology/phpjava/ems/dao/DaoSignature.java +++ b/naresh/src/main/java/com/lftechnology/phpjava/ems/dao/DaoSignature.java @@ -2,7 +2,6 @@ import java.sql.SQLException; import java.util.List; -import java.util.Map; /** * DaoSignature @@ -45,5 +44,5 @@ public interface DaoSignature { * @return * @throws SQLException */ - boolean delete(T t) throws SQLException; + int delete(T t) throws SQLException; } diff --git a/naresh/src/main/java/com/lftechnology/phpjava/ems/dao/EmployeeDaoImpl.java b/naresh/src/main/java/com/lftechnology/phpjava/ems/dao/EmployeeDaoImpl.java index 8ebc3e0..905e623 100644 --- a/naresh/src/main/java/com/lftechnology/phpjava/ems/dao/EmployeeDaoImpl.java +++ b/naresh/src/main/java/com/lftechnology/phpjava/ems/dao/EmployeeDaoImpl.java @@ -8,6 +8,7 @@ import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; +import java.util.ArrayList; import java.util.List; /** @@ -16,14 +17,29 @@ * @author Naresh Maharjan * @since August, 08 2016 */ -public class EmployeeDaoImpl implements DaoSignature { +public class EmployeeDaoImpl implements DaoSignature { protected Connection conn = DbFactory.getConnection(); protected PreparedStatement stmt = null; @Override public List findAll() throws SQLException { - return null; + List employees = new ArrayList<>(); + String sql = "SELECT * FROM employee e INNER JOIN user u ON e.user_id = u.id WHERE u.is_terminated = 0"; + stmt = conn.prepareStatement(sql); + ResultSet result = stmt.executeQuery(); + if (result.next()) { + Employee employee = new Employee(); + employee.setFullname(result.getString("fullname")); + employee.setAddress(result.getString("address")); + employee.setDepartment(result.getString("department")); + employee.setRole(result.getString("role").equals(Role.ADMIN.toString()) ? Role.ADMIN : Role.USER); + employee.setUserId(result.getInt("user_id")); + employee.setUsername(result.getString("username")); + employees.add(employee); + System.out.println(employee.toString()); + } + return employees; } @Override @@ -40,43 +56,71 @@ public int insert(Employee employee) throws SQLException { @Override public int update(Employee employee) throws SQLException { + StringBuilder sql = new StringBuilder(); + sql.append("UPDATE employee SET"); + boolean update = false; + String value = ""; + if (employee.getFullname() != null && !employee.getFullname().isEmpty()) { + update = true; + value = employee.getFullname(); + sql.append(" fullname = ?"); + } else if (employee.getAddress() != null && !employee.getAddress().isEmpty()) { + update = true; + value = employee.getAddress(); + sql.append(" address = ?"); + } else if (employee.getDepartment() != null && !employee.getDepartment().isEmpty()) { + update = true; + value = employee.getDepartment(); + sql.append(" department = ?"); + } + if (update) { + sql.append(" WHERE user_id = ? "); + stmt = conn.prepareStatement(sql.toString()); + stmt.setString(1, value); + stmt.setInt(2, employee.getUserId()); + return stmt.executeUpdate(); + } return 0; } @Override - public boolean delete(Employee employee) throws SQLException { - return false; + public int delete(Employee employee) throws SQLException { + return 0; } /** * Find the employee by fullname * - * @author Naresh Maharjan * @param fullName * @return Employee * @throws SQLException + * @author Naresh Maharjan */ - public Employee findByFullName(String fullName) throws SQLException { + public List findByFullName(String fullName) throws SQLException { String sql = "SELECT * FROM employee WHERE fullname = ?"; - return findBy(sql, fullName); + stmt = conn.prepareStatement(sql); + stmt.setString(1, fullName); + return findBy(stmt); } /** * Find the employee by address * - * @author Naresh Maharjan * @param address * @return Employee * @throws SQLException + * @author Naresh Maharjan */ - public Employee findByAddress(String address) throws SQLException { + public List findByAddress(String address) throws SQLException { String sql = "SELECT * FROM employee WHERE address = ?"; - return findBy(sql, address); + stmt = conn.prepareStatement(sql); + stmt.setString(1, address); + return findBy(stmt); } /** @@ -87,72 +131,83 @@ public Employee findByAddress(String address) throws SQLException { * @throws SQLException */ - public Employee findByDepartment(String department) throws SQLException { + public List findByDepartment(String department) throws SQLException { String sql = "SELECT * FROM employee WHERE department = ?"; - return findBy(sql, department); + stmt = conn.prepareStatement(sql); + stmt.setString(1, department); + return findBy(stmt); } /** * Find the employee by role * - * @author Naresh Maharjan * @param * @return Employee * @throws SQLException + * @author Naresh Maharjan */ - public Employee findByRole(Role role) throws SQLException { + public List findByRole(Role role) throws SQLException { String sql = "SELECT * FROM employee WHERE role = ?"; - return findBy(sql, role.toString()); + stmt = conn.prepareStatement(sql); + stmt.setString(1, role.toString()); + return findBy(stmt); } - - /** + * Find the employee by role * + * @param + * @return Employee + * @throws SQLException * @author Naresh Maharjan - * @param sql - * @param bindValue + */ + + public List findByUserId(int userId) throws SQLException { + + String sql = "SELECT * FROM employee WHERE user_id = ?"; + + stmt = conn.prepareStatement(sql); + stmt.setInt(1, userId); + return findBy(stmt); + } + + + /** + * @param stmt * @return Employee + * @author Naresh Maharjan */ - private Employee findBy(String sql, String bindValue){ - Employee employee = new Employee(); - try { - stmt = conn.prepareStatement(sql); - stmt.setString(1, bindValue); - ResultSet resultSet = stmt.executeQuery(); - employee = setEmployeeData(resultSet); - } catch (SQLException e) { - e.printStackTrace(); - }finally { - DbFactory.closeConnection(); - } - return employee; + private List findBy(PreparedStatement stmt) throws SQLException { + ResultSet resultSet = stmt.executeQuery(); + List employees = setEmployeeData(resultSet); + return employees; } + /** * set employee data from the resultset * - * @author Naresh Maharjan * @param resultSet * @return Employee + * @author Naresh Maharjan */ - private Employee setEmployeeData(ResultSet resultSet){ - Employee employee = new Employee(); - try { - if (resultSet.next()) { - employee.setAddress(resultSet.getString("address")); - employee.setDepartment(resultSet.getString("department")); - employee.setFullname(resultSet.getString("fullname")); - employee.setRole((resultSet.getString("role").toLowerCase()).equals(Role.ADMIN.toString()) ? Role.ADMIN :Role.USER); - } - } catch (SQLException e) { - e.printStackTrace(); + private List setEmployeeData(ResultSet resultSet) throws SQLException { + List employees = new ArrayList<>(); + while (resultSet.next()) { + Employee employee = new Employee(); + employee.setAddress(resultSet.getString("address")); + employee.setDepartment(resultSet.getString("department")); + employee.setFullname(resultSet.getString("fullname")); + employee.setRole((resultSet.getString("role")).equals(Role.ADMIN.toString()) ? Role.ADMIN : Role.USER); + employee.setUserId(resultSet.getInt("user_id")); + employees.add(employee); } - return employee; + return employees; } + } diff --git a/naresh/src/main/java/com/lftechnology/phpjava/ems/dao/UserDaoImpl.java b/naresh/src/main/java/com/lftechnology/phpjava/ems/dao/UserDaoImpl.java index 75f14f1..51bd024 100644 --- a/naresh/src/main/java/com/lftechnology/phpjava/ems/dao/UserDaoImpl.java +++ b/naresh/src/main/java/com/lftechnology/phpjava/ems/dao/UserDaoImpl.java @@ -1,14 +1,13 @@ package com.lftechnology.phpjava.ems.dao; -import com.lftechnology.phpjava.ems.entities.Employee; import com.lftechnology.phpjava.ems.entities.User; -import com.lftechnology.phpjava.ems.enums.Role; import com.lftechnology.phpjava.ems.utlis.DbFactory; import com.lftechnology.phpjava.ems.utlis.PasswordHashGenerator; import java.sql.*; +import java.util.Collections; import java.util.List; -import java.util.Map; +import java.util.Set; /** * UserDaoImpl @@ -35,8 +34,7 @@ public int insert(User user) throws SQLException { stmt.setString(2, user.getPassword()); stmt.executeUpdate(); ResultSet rs = stmt.getGeneratedKeys(); - if(rs.next()) - { + if (rs.next()) { lastInsertedId = rs.getInt(1); } return lastInsertedId; @@ -44,15 +42,59 @@ public int insert(User user) throws SQLException { @Override public int update(User user) throws SQLException { + StringBuilder sql = new StringBuilder(); + sql.append("UPDATE user SET "); + boolean update = false; + String value = ""; + if (user.getUsername() != null && !user.getUsername().isEmpty()) { + update = true; + value = user.getUsername(); + sql.append(" username = ?"); + } else if (user.getPassword() != null && !user.getPassword().isEmpty()) { + update = true; + value = user.getPassword(); + sql.append(" password = ?"); + } + if(update){ + sql.append(" where id = ?"); + stmt = conn.prepareStatement(sql.toString()); + stmt.setString(1, value); + stmt.setInt(2, user.getId()); + return stmt.executeUpdate(); + } return 0; } @Override - public boolean delete(User user) throws SQLException { - return false; + public int delete(User user) throws SQLException { + return 0; } + public int delete(Set userIds) throws SQLException { + String sql = "DELETE FROM user WHERE id IN(%s)"; + String sqlNew = String.format(sql, this.preparePlaceHolders(userIds.size())); + stmt = conn.prepareStatement(sqlNew); + this.setValues(stmt, userIds.toArray()); + return stmt.executeUpdate(); + } + + public int terminateUsers(Set userIds) throws SQLException { + String sql = "UPDATE user SET is_terminated = 1 WHERE id IN(%s)"; + String sqlNew = String.format(sql, this.preparePlaceHolders(userIds.size())); + stmt = conn.prepareStatement(sqlNew); + this.setValues(stmt, userIds.toArray()); + return stmt.executeUpdate(); + } + public static String preparePlaceHolders(int length) { + return String.join(",", Collections.nCopies(length, "?")); + } + + public static void setValues(PreparedStatement preparedStatement, Object... values) throws SQLException { + for (int i = 0; i < values.length; i++) { + preparedStatement.setObject(i + 1, values[i]); + } + } /** * Find the employee by role * @@ -69,10 +111,9 @@ public User findByUsernamePassword(User user) throws SQLException { User result = findBy(sql, bindValues); try { - if(result.getPassword() == null){ + if (result.getPassword() == null) { result = new User(); - } - else if (!PasswordHashGenerator.check(user.getPassword(), result.getPassword())) { + } else if (!PasswordHashGenerator.check(user.getPassword(), result.getPassword())) { result = new User(); } } catch (Exception e) { @@ -118,9 +159,10 @@ private User setUserData(ResultSet resultSet) { User user = new User(); try { if (resultSet.next()) { - user.setUsername(resultSet.getString("username")); - user.setPassword(resultSet.getString("password")); - user.setTerminated(resultSet.getBoolean("is_terminated")); + user.setUsername(resultSet.getString("username")) + .setPassword(resultSet.getString("password")) + .setTerminated(resultSet.getBoolean("is_terminated")) + .setId(resultSet.getInt("id")); } } catch (SQLException e) { e.printStackTrace(); diff --git a/naresh/src/main/java/com/lftechnology/phpjava/ems/entities/Employee.java b/naresh/src/main/java/com/lftechnology/phpjava/ems/entities/Employee.java index e348467..1a7b489 100644 --- a/naresh/src/main/java/com/lftechnology/phpjava/ems/entities/Employee.java +++ b/naresh/src/main/java/com/lftechnology/phpjava/ems/entities/Employee.java @@ -31,7 +31,7 @@ public Employee(String fullname, String address, String department, Role role, i } public String getFullname() { - return fullname; + return this.fullname; } public Employee setFullname(String fullname) { @@ -40,7 +40,7 @@ public Employee setFullname(String fullname) { } public String getAddress() { - return address; + return this.address; } public Employee setAddress(String address) { @@ -49,7 +49,7 @@ public Employee setAddress(String address) { } public String getDepartment() { - return department; + return this.department; } public Employee setDepartment(String department) { @@ -59,7 +59,7 @@ public Employee setDepartment(String department) { public Role getRole() { - return role; + return this.role; } public Employee setRole(Role role) { @@ -68,11 +68,21 @@ public Employee setRole(Role role) { } public int getUserId() { - return userId; + return this.userId; } public Employee setUserId(int userId) { this.userId = userId; return this; } + + public String[] toArray(){ + String[] emp = new String[5]; + emp[0] = String.valueOf(this.getUserId()); + emp[1] = this.getFullname(); + emp[2] = this.getAddress(); + emp[3] = this.getRole().toString(); + emp[4] = this.getDepartment(); + return emp; + } } diff --git a/naresh/src/main/java/com/lftechnology/phpjava/ems/entities/User.java b/naresh/src/main/java/com/lftechnology/phpjava/ems/entities/User.java index b09f068..4857f2d 100644 --- a/naresh/src/main/java/com/lftechnology/phpjava/ems/entities/User.java +++ b/naresh/src/main/java/com/lftechnology/phpjava/ems/entities/User.java @@ -23,8 +23,13 @@ public User(String username, String password, boolean isTerminated) { this.isTerminated = isTerminated; } + public User setId(int id) { + this.id = id; + return this; + } + public int getId() { - return id; + return this.id; } public String getUsername() { diff --git a/naresh/src/main/java/com/lftechnology/phpjava/ems/services/EmployeeService.java b/naresh/src/main/java/com/lftechnology/phpjava/ems/services/EmployeeService.java index aa9f481..dff7784 100644 --- a/naresh/src/main/java/com/lftechnology/phpjava/ems/services/EmployeeService.java +++ b/naresh/src/main/java/com/lftechnology/phpjava/ems/services/EmployeeService.java @@ -8,6 +8,11 @@ import com.lftechnology.phpjava.ems.utlis.PasswordHashGenerator; import java.sql.SQLException; +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; +import java.util.stream.Collectors; /** * EmployeeService @@ -22,29 +27,17 @@ public class EmployeeService { * * @author Naresh Maharjan */ - public void checkAndCreateAdminUser(){ - if(!this.checkAdminUserExists()){ + public void checkAndCreateAdminUser() throws Exception { + if (!this.checkAdminUserExists()) { Employee employee = new Employee(); - employee.setFullname("Admin User"); - employee.setAddress("Somewhere"); - employee.setRole(Role.ADMIN); - employee.setDepartment("Admin"); - employee.setUsername("admin"); - employee.setTerminated(false); - try { - employee.setPassword(PasswordHashGenerator.getSaltedHash("password")); - } catch (Exception e) { - e.printStackTrace(); - } - UserDaoImpl userDao = new UserDaoImpl(); - EmployeeDaoImpl employeeDao = new EmployeeDaoImpl(); - try { - int userId = userDao.insert(employee); - employee.setUserId(userId); - employeeDao.insert(employee); - } catch (SQLException e) { - e.printStackTrace(); - } + employee.setFullname("Admin User") + .setAddress("Somewhere") + .setRole(Role.ADMIN) + .setDepartment("Admin") + .setUsername("admin") + .setTerminated(false); + employee.setPassword(PasswordHashGenerator.getSaltedHash("password")); + insertUserAndEmployee(employee); } } @@ -52,18 +45,95 @@ public void checkAndCreateAdminUser(){ /** * check if admin user exists or not * - * @author Naresh Maharjan * @return boolean + * @author Naresh Maharjan */ - public boolean checkAdminUserExists() { + public boolean checkAdminUserExists() throws SQLException { EmployeeDaoImpl employeeDao = new EmployeeDaoImpl(); - Boolean result = false; - try { - Employee employee = employeeDao.findByRole(Role.ADMIN); - result = (employee.getRole() != null) ? employee.getRole().equals(Role.ADMIN) ? true : false : false; - } catch (SQLException e) { - e.printStackTrace(); + List employees = employeeDao.findByRole(Role.ADMIN); + if (employees.isEmpty()){ + return false; } + Employee employee = employees.get(0); + Boolean result = (employee.getRole() != null) ? employee.getRole().equals(Role.ADMIN) ? true : false : false; return result; } + + + public int addEmployee(Employee employee) throws Exception { + employee.setFullname(employee.getFullname().trim()); + employee.setAddress(employee.getAddress().trim()); + employee.setDepartment(employee.getDepartment().trim()); + employee.setUsername(employee.getUsername().trim()); + employee.setPassword(PasswordHashGenerator.getSaltedHash(employee.getPassword())); + return insertUserAndEmployee(employee); + } + + private int insertUserAndEmployee(Employee employee) throws SQLException { + UserDaoImpl userDao = new UserDaoImpl(); + EmployeeDaoImpl employeeDao = new EmployeeDaoImpl(); + int userId = userDao.insert(employee); + employee.setUserId(userId); + return employeeDao.insert(employee); + } + + public int updateEmployee(Employee employee) throws Exception { + if (employee.getPassword() != null || employee.getUsername() != null) { + UserDaoImpl userDao = new UserDaoImpl(); + User user = new User(); + user.setUsername(employee.getUsername()); + user.setId(employee.getUserId()); + user.setPassword(PasswordHashGenerator.getSaltedHash(employee.getPassword())); + return userDao.update(user); + } else { + EmployeeDaoImpl employeeDao = new EmployeeDaoImpl(); + return employeeDao.update(employee); + } + } + + public List searchEmployee(Employee employee) throws SQLException { + List employees = new ArrayList<>(); + EmployeeDaoImpl employeeDao = new EmployeeDaoImpl(); + if (employee.getDepartment() != null && !employee.getDepartment().isEmpty()) { + employees = employeeDao.findByDepartment(employee.getDepartment()); + } else if (employee.getFullname() != null&& !employee.getFullname().isEmpty()) { + employees = employeeDao.findByFullName(employee.getFullname()); + } else if (employee.getAddress() != null && !employee.getAddress().isEmpty()) { + employees = employeeDao.findByAddress(employee.getAddress()); + } + return employees; + } + + public int terminateUser(Employee employee) throws SQLException { + UserDaoImpl userDao = new UserDaoImpl(); + Set userIds = this.getUserIds(employee); + return userDao.terminateUsers(userIds); + } + + public int deleteUser(Employee employee) throws SQLException { + UserDaoImpl userDao = new UserDaoImpl(); + Set userIds = this.getUserIds(employee); + return userDao.delete(userIds); + } + + private Set getUserIds(Employee employee) throws SQLException { + List employees = new ArrayList<>(); + EmployeeDaoImpl employeeDao = new EmployeeDaoImpl(); + if (employee.getFullname() != null&& !employee.getFullname().isEmpty()) { + employees = employeeDao.findByFullName(employee.getFullname()); + } + Set userIds = employees.stream().map(Employee::getUserId).collect(Collectors.toSet()); + return userIds; + } + + + public String[] getColumns(){ + String[] columnNames = new String[5]; + columnNames[0] = "User ID"; + columnNames[1] = "Full Name"; + columnNames[2] = "Address"; + columnNames[3] = "Role(User Type)"; + columnNames[4] = "Department"; + return columnNames; + } } diff --git a/naresh/src/main/java/com/lftechnology/phpjava/ems/services/UserService.java b/naresh/src/main/java/com/lftechnology/phpjava/ems/services/UserService.java index 076815e..1f59cff 100644 --- a/naresh/src/main/java/com/lftechnology/phpjava/ems/services/UserService.java +++ b/naresh/src/main/java/com/lftechnology/phpjava/ems/services/UserService.java @@ -1,9 +1,12 @@ package com.lftechnology.phpjava.ems.services; +import com.lftechnology.phpjava.ems.dao.EmployeeDaoImpl; import com.lftechnology.phpjava.ems.dao.UserDaoImpl; +import com.lftechnology.phpjava.ems.entities.Employee; import com.lftechnology.phpjava.ems.entities.User; import java.sql.SQLException; +import java.util.List; /** * UserService @@ -15,6 +18,7 @@ public class UserService { private boolean isLoggedIn = false; private String loggedInUserRole = ""; + private int userId; UserDaoImpl userDao = new UserDaoImpl(); @@ -24,7 +28,11 @@ public void login(User user) { User result = userDao.findByUsernamePassword(user); if (user.getUsername().equals(result.getUsername())) { this.isLoggedIn = true; -// this.loggedInUserRole = user.get + this.userId = result.getId(); + EmployeeDaoImpl employeeDao = new EmployeeDaoImpl(); + List employees = employeeDao.findByUserId(this.userId); + Employee employee = employees.get(0); + this.loggedInUserRole = employee.getRole().toString(); } } catch (SQLException e) { e.printStackTrace(); @@ -32,13 +40,17 @@ public void login(User user) { } public boolean isUserLoggedIn() { - return isLoggedIn; + return this.isLoggedIn; } public String getLoggedInUserRole() { return this.loggedInUserRole; } + public int getUserId() { + return this.userId; + } + public void logout() { this.isLoggedIn = false; } diff --git a/naresh/src/main/java/com/lftechnology/phpjava/ems/utlis/CommonUtility.java b/naresh/src/main/java/com/lftechnology/phpjava/ems/utlis/CommonUtility.java new file mode 100644 index 0000000..9e14c61 --- /dev/null +++ b/naresh/src/main/java/com/lftechnology/phpjava/ems/utlis/CommonUtility.java @@ -0,0 +1,34 @@ +package com.lftechnology.phpjava.ems.utlis; + +import com.lftechnology.phpjava.ems.enums.Role; +import com.lftechnology.phpjava.ems.services.UserService; + +/** + * CommonUtility + * + * @author Naresh Maharjan + * @since August, 15 2016 + */ +public class CommonUtility { + public UserService userService = new UserService(); + + public UserService getUserService() { + return userService; + } + + public void setUserService(UserService userService) { + this.userService = userService; + } + + public boolean isLoggedIn() { + return this.userService.isUserLoggedIn(); + } + + public boolean isAdmin() { + return this.getUserService().getLoggedInUserRole().equals(Role.ADMIN.toString()); + } + + public boolean isUser() { + return this.getUserService().getLoggedInUserRole().equals(Role.USER.toString()); + } +} diff --git a/naresh/src/main/java/com/lftechnology/phpjava/ems/utlis/ConsoleWriter.java b/naresh/src/main/java/com/lftechnology/phpjava/ems/utlis/ConsoleWriter.java index bfee1fa..f8b7f88 100644 --- a/naresh/src/main/java/com/lftechnology/phpjava/ems/utlis/ConsoleWriter.java +++ b/naresh/src/main/java/com/lftechnology/phpjava/ems/utlis/ConsoleWriter.java @@ -1,5 +1,7 @@ package com.lftechnology.phpjava.ems.utlis; +import java.io.IOException; + /** * ConsoleWriter * @@ -7,15 +9,31 @@ * @since August, 10 2016 */ public class ConsoleWriter { - public static void writeBlankLine(int limit){ + public static void writeBlankLine(int limit) { int counter = 0; - while (counter < limit){ + while (counter < limit) { System.out.println(); - counter ++; + counter++; } } - public static void writeUserInputRequestMessage(String message){ - System.out.print("\t\t\t\t\t\t\t\t " + message +" \t"); + public static void writeUserInputRequestMessage(String message) { + System.out.print("\t\t\t\t\t\t\t\t " + message + " \t"); + } + + public static void clearConsole() + + { + final String operatingSystem = System.getProperty("os.name"); + + try { + if (operatingSystem.contains("Windows")) { + Runtime.getRuntime().exec("cls"); + } else { + Runtime.getRuntime().exec("clear"); + } + } catch (IOException e) { + e.printStackTrace(); + } } } diff --git a/naresh/src/main/java/com/lftechnology/phpjava/ems/utlis/DbFactory.java b/naresh/src/main/java/com/lftechnology/phpjava/ems/utlis/DbFactory.java index f7cebe5..106eefa 100644 --- a/naresh/src/main/java/com/lftechnology/phpjava/ems/utlis/DbFactory.java +++ b/naresh/src/main/java/com/lftechnology/phpjava/ems/utlis/DbFactory.java @@ -33,7 +33,7 @@ private static void loadProperties() { InputStream input = null; try { - String filePath = System.getProperty("user.dir") + "/src/main/java/com/lftechnology/phpjava/ems/configs/db.properties"; + String filePath = "src/main/java/com/lftechnology/phpjava/ems/configs/db.properties"; input = new FileInputStream(filePath); properties.load(input); } catch (IOException ex) { @@ -78,14 +78,14 @@ public static Connection getConnection() { */ public static void closeConnection(){ - try { - - if (connection != null) { - connection.close(); - } - } - catch (SQLException e) { - e.printStackTrace(); - } +// try { +// +// if (connection != null) { +// connection.close(); +// } +// } +// catch (SQLException e) { +// e.printStackTrace(); +// } } } diff --git a/naresh/src/main/java/com/lftechnology/phpjava/ems/utlis/Router.java b/naresh/src/main/java/com/lftechnology/phpjava/ems/utlis/Router.java index 6290844..a252bfd 100644 --- a/naresh/src/main/java/com/lftechnology/phpjava/ems/utlis/Router.java +++ b/naresh/src/main/java/com/lftechnology/phpjava/ems/utlis/Router.java @@ -1,13 +1,15 @@ package com.lftechnology.phpjava.ems.utlis; +import com.lftechnology.phpjava.ems.constants.Constant; +import com.lftechnology.phpjava.ems.controllers.EmployeeController; import com.lftechnology.phpjava.ems.controllers.LoginController; -import com.lftechnology.phpjava.ems.dao.UserDaoImpl; -import com.lftechnology.phpjava.ems.entities.User; +import com.lftechnology.phpjava.ems.entities.Employee; import com.lftechnology.phpjava.ems.services.EmployeeService; -import com.lftechnology.phpjava.ems.services.UserService; +import com.lftechnology.phpjava.ems.views.CommonViewUtility; import java.sql.SQLException; - +import java.util.HashMap; +import java.util.Map; /** * RouterService @@ -17,10 +19,18 @@ */ public class Router { - private static UserService userService = new UserService(); + private static LoginController loginController = new LoginController(); + private static EmployeeController employeeController = new EmployeeController(); + private static CommonUtility commonUtility = new CommonUtility(); static { - onRouteBootstrap(); + try { + onRouteBootstrap(); + } catch (SQLException e) { + e.printStackTrace(); + } catch (Exception e) { + e.printStackTrace(); + } } /** @@ -30,7 +40,7 @@ public class Router { * * @author Naresh Maharjan */ - private static void onRouteBootstrap() { + public static void onRouteBootstrap() throws Exception { SqlRunner sqlRunner = new SqlRunner(); sqlRunner.executeSql(); @@ -44,61 +54,51 @@ private static void onRouteBootstrap() { * * @author Naresh Maharjan */ - private static void showWelcomeScreen() { - - ConsoleWriter.writeBlankLine(3); - ConsoleWriter.writeUserInputRequestMessage("Welcome To Employee Management System !!!"); + public static void showWelcomeScreen() { - ConsoleWriter.writeBlankLine(3); - ConsoleWriter.writeUserInputRequestMessage("Enter your credentails to log into the system"); + CommonViewUtility.showWelcomeScreen(); } - private static void showLoginMenuAndLoginToSystem() { - if (!userService.isUserLoggedIn()) { + public static void showLoginMenuAndLoginToSystem() { + if (!commonUtility.getUserService().isUserLoggedIn()) { showWelcomeScreen(); - LoginController.login(); + loginController.login(); + commonUtility = (CommonUtility) loginController.getData().get("commonUtility"); } } - private static void showMenu() { + public static void showMenu() { showLoginMenuAndLoginToSystem(); -// if(userService.isUserLoggedIn() && userService.){ -// -// } - - } - - private static void showAdminMenu() { - ConsoleWriter.writeBlankLine(3); - ConsoleWriter.writeUserInputRequestMessage("1. Add New User"); - ConsoleWriter.writeBlankLine(3); - ConsoleWriter.writeUserInputRequestMessage("2. Delete User"); - ConsoleWriter.writeBlankLine(3); - ConsoleWriter.writeUserInputRequestMessage("3. Search User(s)"); + if (!commonUtility.isLoggedIn()) { + CommonViewUtility.showMessageAndContinue("Invalid Credentails"); + }else { + Map commonUtilityMap = new HashMap<>(); + commonUtilityMap.put("commonUtility", commonUtility); + employeeController.setData(commonUtilityMap); + employeeController.postLoginScreen(); + } } - private static void showNormalUserMenu() { - ConsoleWriter.writeBlankLine(3); - ConsoleWriter.writeUserInputRequestMessage("1. Update Profile"); - ConsoleWriter.writeBlankLine(3); - ConsoleWriter.writeUserInputRequestMessage("2. Search User(s)"); - } - public static void main(String[] args) { - UserDaoImpl userDao = new UserDaoImpl(); - User user = new User(); - user.setUsername("naresh" + Math.random()); - try { - user.setPassword(PasswordHashGenerator.getSaltedHash("maharjan")); - } catch (Exception e) { - e.printStackTrace(); + public static void initiateInitialMenuAction(int menu) { + + if (commonUtility.isAdmin() && menu == Constant.ADD_NEW_USER) { + employeeController.addUser(); + } else if (commonUtility.isAdmin() && menu == Constant.SEARCH_USER) { + employeeController.searchUser(); + } else if (commonUtility.isAdmin() && menu == Constant.TERMINATE_USER) { + employeeController.terminateUser(); + } else if (commonUtility.isAdmin() && menu == Constant.DELETE_USER) { + employeeController.deleteUser(); + } else if (commonUtility.isUser() && menu == Constant.UPDATE_PROFILE) { + employeeController.updateProfile(); + } else if (commonUtility.isUser() && menu == Constant.SEARCH_USER) { + employeeController.searchUser(); + } else if (menu == Constant.EXIT) { + CommonViewUtility.showExitMessageAndExit(); + } else { + showMenu(); } - try { - int result = userDao.insert(user); - System.out.println(result); - } catch (SQLException e) { - e.printStackTrace(); - } - showMenu(); } + } diff --git a/naresh/src/main/java/com/lftechnology/phpjava/ems/utlis/SqlRunner.java b/naresh/src/main/java/com/lftechnology/phpjava/ems/utlis/SqlRunner.java index 8b60fdb..82e8a5d 100644 --- a/naresh/src/main/java/com/lftechnology/phpjava/ems/utlis/SqlRunner.java +++ b/naresh/src/main/java/com/lftechnology/phpjava/ems/utlis/SqlRunner.java @@ -1,6 +1,9 @@ package com.lftechnology.phpjava.ems.utlis; -import java.io.*; +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; import java.nio.charset.Charset; import java.nio.file.Files; import java.nio.file.Path; diff --git a/naresh/src/main/java/com/lftechnology/phpjava/ems/utlis/UserInput.java b/naresh/src/main/java/com/lftechnology/phpjava/ems/utlis/UserInput.java index b170ff3..f6a432d 100644 --- a/naresh/src/main/java/com/lftechnology/phpjava/ems/utlis/UserInput.java +++ b/naresh/src/main/java/com/lftechnology/phpjava/ems/utlis/UserInput.java @@ -18,19 +18,7 @@ public class UserInput { */ public static String getStringUserInput() { Scanner scanner = new Scanner(System.in); - String userInput = ""; - while (true) { - if (!scanner.hasNextLine()) { - scanner.nextLine(); - } else { - userInput = scanner.nextLine(); - if (userInput.isEmpty()) { - continue; - } else { - break; - } - } - } + String userInput = scanUserInput(scanner); return userInput; } @@ -43,16 +31,39 @@ public static String getStringUserInput() { */ public static int getIntegerUserInput() { Scanner scanner = new Scanner(System.in); - int userInput = 0; + String userInput = scanUserInput(scanner); + while (!isInteger(userInput)) { + ConsoleWriter.writeUserInputRequestMessage("Invalid option. Please enter again"); + ConsoleWriter.writeBlankLine(5); + userInput = scanUserInput(scanner); + } + int result = Integer.parseInt(userInput); + return result; + } + + private static String scanUserInput(Scanner scanner) { + String userInput = ""; while (true) { if (!scanner.hasNextLine()) { scanner.nextLine(); } else { - userInput = Integer.parseInt(scanner.nextLine()); - break; + userInput = scanner.nextLine(); + if (userInput.isEmpty()) { + continue; + } else { + break; + } } } return userInput; } + public static boolean isInteger(String self) { + try { + Integer.valueOf(self.trim()); + return true; + } catch (NumberFormatException nfe) { + return false; + } + } } diff --git a/naresh/src/main/java/com/lftechnology/phpjava/ems/views/AbstractBaseView.java b/naresh/src/main/java/com/lftechnology/phpjava/ems/views/AbstractBaseView.java new file mode 100644 index 0000000..fb52df9 --- /dev/null +++ b/naresh/src/main/java/com/lftechnology/phpjava/ems/views/AbstractBaseView.java @@ -0,0 +1,25 @@ +package com.lftechnology.phpjava.ems.views; + +/** + * AbstractBaseView + * + * @author Naresh Maharjan + * @since August, 15 2016 + */ +public abstract class AbstractBaseView implements ViewSignature { + + protected String action = ""; + + public abstract void render(); + + @Override + public void setAction(String action) { + this.action = action; + } + + @Override + public String getAction() { + return this.action; + } + +} diff --git a/naresh/src/main/java/com/lftechnology/phpjava/ems/views/CommonViewUtility.java b/naresh/src/main/java/com/lftechnology/phpjava/ems/views/CommonViewUtility.java new file mode 100644 index 0000000..1047d26 --- /dev/null +++ b/naresh/src/main/java/com/lftechnology/phpjava/ems/views/CommonViewUtility.java @@ -0,0 +1,60 @@ +package com.lftechnology.phpjava.ems.views; + +import com.lftechnology.phpjava.ems.utlis.ConsoleWriter; +import com.lftechnology.phpjava.ems.utlis.Router; +import com.lftechnology.phpjava.ems.utlis.UserInput; + +/** + * CommonViewUtility + * + * @author Naresh Maharjan + * @since August, 15 2016 + */ +public class CommonViewUtility { + + /** + * Show Welcome Screen + * + * @author Naresh Maharjan + */ + public static void showWelcomeScreen() { + ConsoleWriter.writeBlankLine(3); + ConsoleWriter.writeUserInputRequestMessage("Welcome To Employee Management System !!!"); + + ConsoleWriter.writeBlankLine(3); + ConsoleWriter.writeUserInputRequestMessage("Enter your credentails to log into the system"); + } + + public static void showExitMessageAndExit(){ + ConsoleWriter.writeBlankLine(100); + ConsoleWriter.writeUserInputRequestMessage("Bye, Felicia"); + ConsoleWriter.writeBlankLine(5); + System.exit(0); + } + + public static void showMessageAndContinue(String message) { + ConsoleWriter.writeBlankLine(100); + ConsoleWriter.writeUserInputRequestMessage(message); + ConsoleWriter.writeBlankLine(3); + ConsoleWriter.writeUserInputRequestMessage("Do you want to continue?"); + ConsoleWriter.writeBlankLine(3); + ConsoleWriter.writeUserInputRequestMessage("1. Yes"); + ConsoleWriter.writeBlankLine(3); + ConsoleWriter.writeUserInputRequestMessage("2. No"); + if (UserInput.getIntegerUserInput() == 1) { + Router.showMenu(); + }else{ + showExitMessageAndExit(); + } + } + + public static void pressKeyToContinue(){ + ConsoleWriter.writeBlankLine(3); + ConsoleWriter.writeUserInputRequestMessage("Press 1 to continue or any other number to exit"); + if (UserInput.getIntegerUserInput() == 1) { + Router.showMenu(); + }else{ + showExitMessageAndExit(); + } + } +} diff --git a/naresh/src/main/java/com/lftechnology/phpjava/ems/views/EmployeeView.java b/naresh/src/main/java/com/lftechnology/phpjava/ems/views/EmployeeView.java new file mode 100644 index 0000000..53b903b --- /dev/null +++ b/naresh/src/main/java/com/lftechnology/phpjava/ems/views/EmployeeView.java @@ -0,0 +1,315 @@ +package com.lftechnology.phpjava.ems.views; + +import com.lftechnology.phpjava.ems.constants.Constant; +import com.lftechnology.phpjava.ems.controllers.EmployeeController; +import com.lftechnology.phpjava.ems.entities.Employee; +import com.lftechnology.phpjava.ems.enums.Role; +import com.lftechnology.phpjava.ems.utlis.CommonUtility; +import com.lftechnology.phpjava.ems.utlis.ConsoleWriter; +import com.lftechnology.phpjava.ems.utlis.Router; +import com.lftechnology.phpjava.ems.utlis.UserInput; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * EmployeeView + * + * @author Naresh Maharjan + * @since August, 16 2016 + */ +public class EmployeeView implements ViewSignature { + + private static CommonUtility commonUtility = new CommonUtility(); + private EmployeeController employeeController = new EmployeeController(); + private String action; + + @Override + public void render() { + switch (action) { + case Constant.POST_LOGIN_ACTION: + if (this.commonUtility.isLoggedIn() && this.commonUtility.isAdmin()) { + this.showAdminMenu(); + } else { + this.showNormalUserMenu(); + } + break; + case Constant.ADD_USER_ACTION: + this.showAddUserMenu(); + break; + case Constant.DELETE_USER_ACTION: + this.showDeleteUserMenu(); + break; + case Constant.SEARCH_USER_ACTION: + this.showSearchUserMenu(); + break; + case Constant.TERMINATE_USER_ACTION: + this.showTerminateUserMenu(); + break; + case Constant.UPDATE_PROFILE_ACTION: + this.showUpdateUserMenu(); + break; + case Constant.EXIT_ACTION: + CommonViewUtility.showExitMessageAndExit(); + break; + default: + Router.showMenu(); + } + } + + @Override + public void setAction(String action) { + this.action = action; + } + + @Override + public String getAction() { + return this.action; + } + + @Override + public void setData(Map data) { + commonUtility = (CommonUtility) data.get("commonUtility"); + } + + @Override + public Map getData() { + return null; + } + + public void showAdminMenu() { + ConsoleWriter.writeBlankLine(100); + ConsoleWriter.writeUserInputRequestMessage("1. Add New User"); + ConsoleWriter.writeBlankLine(3); + ConsoleWriter.writeUserInputRequestMessage("2. Search User(s)"); + ConsoleWriter.writeBlankLine(3); + ConsoleWriter.writeUserInputRequestMessage("3. Terminate User"); + ConsoleWriter.writeBlankLine(3); + ConsoleWriter.writeUserInputRequestMessage("4. Delete User"); + ConsoleWriter.writeBlankLine(3); + ConsoleWriter.writeUserInputRequestMessage("0. Exit"); + ConsoleWriter.writeBlankLine(5); + + Router.initiateInitialMenuAction(UserInput.getIntegerUserInput()); + } + + public void showNormalUserMenu() { + ConsoleWriter.writeBlankLine(100); + ConsoleWriter.writeUserInputRequestMessage("1. Update Profile"); + ConsoleWriter.writeBlankLine(3); + ConsoleWriter.writeUserInputRequestMessage("2. Search User(s)"); + ConsoleWriter.writeBlankLine(3); + ConsoleWriter.writeUserInputRequestMessage("0. Exit"); + ConsoleWriter.writeBlankLine(5); + + Router.initiateInitialMenuAction(UserInput.getIntegerUserInput()); + } + + public void showAddUserMenu() { + ConsoleWriter.writeBlankLine(100); + ConsoleWriter.writeUserInputRequestMessage("Choose One Of The Following User Type"); + ConsoleWriter.writeBlankLine(3); + ConsoleWriter.writeUserInputRequestMessage("1. Add Admin User"); + ConsoleWriter.writeBlankLine(3); + ConsoleWriter.writeUserInputRequestMessage("2. Add Normal User"); + ConsoleWriter.writeBlankLine(5); + + Employee employee = new Employee(); + Map postData = new HashMap<>(); + employee.setRole(UserInput.getIntegerUserInput() == 1 ? Role.ADMIN : Role.USER); + + ConsoleWriter.writeBlankLine(3); + ConsoleWriter.writeUserInputRequestMessage("Enter User Details"); + ConsoleWriter.writeBlankLine(3); + ConsoleWriter.writeUserInputRequestMessage("1. Enter Fullname"); + ConsoleWriter.writeBlankLine(5); + employee.setFullname(UserInput.getStringUserInput()); + + ConsoleWriter.writeBlankLine(3); + ConsoleWriter.writeUserInputRequestMessage("2. Enter Department"); + ConsoleWriter.writeBlankLine(5); + employee.setDepartment(UserInput.getStringUserInput()); + + ConsoleWriter.writeBlankLine(3); + ConsoleWriter.writeUserInputRequestMessage("3. Enter Address"); + ConsoleWriter.writeBlankLine(5); + employee.setAddress(UserInput.getStringUserInput()); + + ConsoleWriter.writeBlankLine(3); + ConsoleWriter.writeUserInputRequestMessage("4. Enter Username"); + ConsoleWriter.writeBlankLine(5); + employee.setUsername(UserInput.getStringUserInput()); + + ConsoleWriter.writeBlankLine(3); + ConsoleWriter.writeUserInputRequestMessage("5. Enter Password"); + ConsoleWriter.writeBlankLine(5); + employee.setPassword(UserInput.getStringUserInput()); + + postData.put("employee", employee); + employeeController.setData(postData); + + employeeController.makeCurrentRequestPost(); + employeeController.addUser(); + + } + + public void showSearchUserMenu() { + ConsoleWriter.writeBlankLine(100); + ConsoleWriter.writeUserInputRequestMessage("Choose One Of The Following Search Criteria"); + ConsoleWriter.writeBlankLine(3); + ConsoleWriter.writeUserInputRequestMessage("1. Search By Fullname"); + ConsoleWriter.writeBlankLine(3); + ConsoleWriter.writeUserInputRequestMessage("2. Search By Department"); + ConsoleWriter.writeBlankLine(3); + ConsoleWriter.writeUserInputRequestMessage("3. Search By Address"); + ConsoleWriter.writeBlankLine(3); + ConsoleWriter.writeUserInputRequestMessage("4. Exit"); + ConsoleWriter.writeBlankLine(5); + int userInput = UserInput.getIntegerUserInput(); + + ConsoleWriter.writeBlankLine(3); + ConsoleWriter.writeUserInputRequestMessage("Enter search value"); + String searchValue = UserInput.getStringUserInput(); + + Employee employee = new Employee(); + switch (userInput) { + case 1: + employee.setFullname(searchValue); + break; + case 2: + employee.setDepartment(searchValue); + break; + case 3: + employee.setAddress(searchValue); + break; + case 4: + CommonViewUtility.showExitMessageAndExit(); + break; + default: + Router.showMenu(); + break; + } + + Map postData = new HashMap<>(); + postData.put("employee", employee); + this.employeeController.setData(postData); + this.employeeController.makeCurrentRequestPost(); + this.employeeController.searchUser(); + } + + public void renderEmployeeList(String[] columns, List employees) { + TableGenerator tableGenerator = new TableGenerator(columns); + for (Employee employee: employees) { + tableGenerator.addRow(employee.toArray()); + } + tableGenerator.renderTable(); + } + + + public void showUpdateUserMenu() { + ConsoleWriter.writeBlankLine(100); + ConsoleWriter.writeUserInputRequestMessage("Update Profile"); + ConsoleWriter.writeBlankLine(3); + ConsoleWriter.writeUserInputRequestMessage("1. Update Fullname"); + ConsoleWriter.writeBlankLine(3); + ConsoleWriter.writeUserInputRequestMessage("2. Update Department"); + ConsoleWriter.writeBlankLine(3); + ConsoleWriter.writeUserInputRequestMessage("3. Update Address"); + ConsoleWriter.writeBlankLine(3); + ConsoleWriter.writeUserInputRequestMessage("4. Update Username"); + ConsoleWriter.writeBlankLine(3); + ConsoleWriter.writeUserInputRequestMessage("5. Update Password"); + ConsoleWriter.writeBlankLine(5); + int userInput = UserInput.getIntegerUserInput(); + ConsoleWriter.writeUserInputRequestMessage("Enter value"); + String value = UserInput.getStringUserInput(); + Employee employee = new Employee(); + employee.setUserId(commonUtility.getUserService().getUserId()); + switch (userInput) { + case 1: + employee.setFullname(value); + break; + case 2: + employee.setDepartment(value); + break; + case 3: + employee.setAddress(value); + break; + case 4: + employee.setUsername(value); + break; + case 5: + employee.setPassword(value); + break; + case 6: + CommonViewUtility.showExitMessageAndExit(); + break; + default: + Router.showMenu(); + break; + } + Map postData = new HashMap<>(); + postData.put("employee", employee); + this.employeeController.setData(postData); + this.employeeController.makeCurrentRequestPost(); + this.employeeController.updateProfile(); + } + + public void showDeleteUserMenu() { + ConsoleWriter.writeBlankLine(100); + ConsoleWriter.writeUserInputRequestMessage("Delete User"); + ConsoleWriter.writeBlankLine(3); + ConsoleWriter.writeUserInputRequestMessage("1. Enter Fullname of Fullname"); + ConsoleWriter.writeBlankLine(3); + ConsoleWriter.writeUserInputRequestMessage("2. Return to main menu"); + ConsoleWriter.writeBlankLine(3); + ConsoleWriter.writeUserInputRequestMessage("0. Exit"); + ConsoleWriter.writeBlankLine(5); + int userInput = UserInput.getIntegerUserInput(); + Employee employee = new Employee(); + employee = getTerminateAndDeleterUserInput(userInput, employee); + Map postData = new HashMap<>(); + postData.put("employee", employee); + this.employeeController.setData(postData); + this.employeeController.makeCurrentRequestPost(); + this.employeeController.deleteUser(); + } + + public void showTerminateUserMenu() { + ConsoleWriter.writeBlankLine(100); + ConsoleWriter.writeUserInputRequestMessage("Terminate User"); + ConsoleWriter.writeBlankLine(3); + ConsoleWriter.writeUserInputRequestMessage("1. Enter Fullname Of User"); + ConsoleWriter.writeBlankLine(3); + ConsoleWriter.writeUserInputRequestMessage("2. Return to main menu"); + ConsoleWriter.writeBlankLine(3); + ConsoleWriter.writeUserInputRequestMessage("0. Exit"); + ConsoleWriter.writeBlankLine(5); + int userInput = UserInput.getIntegerUserInput(); + Employee employee = new Employee(); + employee = getTerminateAndDeleterUserInput(userInput, employee); + Map postData = new HashMap<>(); + postData.put("employee", employee); + this.employeeController.setData(postData); + this.employeeController.makeCurrentRequestPost(); + this.employeeController.terminateUser(); + } + + private Employee getTerminateAndDeleterUserInput(int userInput, Employee employee) { + switch (userInput) { + case 1: + ConsoleWriter.writeUserInputRequestMessage("Enter value"); + String value = UserInput.getStringUserInput(); + employee.setFullname(value); + break; + case 2: + CommonViewUtility.showExitMessageAndExit(); + break; + default: + Router.showMenu(); + break; + } + return employee; + } +} diff --git a/naresh/src/main/java/com/lftechnology/phpjava/ems/views/LoginView.java b/naresh/src/main/java/com/lftechnology/phpjava/ems/views/LoginView.java new file mode 100644 index 0000000..77b54bc --- /dev/null +++ b/naresh/src/main/java/com/lftechnology/phpjava/ems/views/LoginView.java @@ -0,0 +1,75 @@ +package com.lftechnology.phpjava.ems.views; + +import com.lftechnology.phpjava.ems.controllers.LoginController; +import com.lftechnology.phpjava.ems.entities.User; +import com.lftechnology.phpjava.ems.services.UserService; +import com.lftechnology.phpjava.ems.utlis.CommonUtility; +import com.lftechnology.phpjava.ems.utlis.ConsoleWriter; +import com.lftechnology.phpjava.ems.utlis.UserInput; + +import java.util.HashMap; +import java.util.Map; + +/** + * LoginView + * + * @author Naresh Maharjan + * @since August, 15 2016 + */ +public class LoginView implements ViewSignature { + private CommonUtility commonUtility = new CommonUtility(); + private User user = new User(); + private LoginController loginController = new LoginController(); + private UserService userService = new UserService(); + private String action; + + @Override + public void render() { + if (this.getAction().equals("login")) { + this.login(); + } else { + this.logout(); + } + + } + + @Override + public void setAction(String action) { + this.action = action; + } + + @Override + public String getAction() { + return this.action; + } + + @Override + public void setData(Map data) { + } + + @Override + public Map getData() { + Map data = new HashMap<>(); + data.put("commonUtility", this.commonUtility); + return data; + } + + public void login() { + Map postData = new HashMap<>(); + + ConsoleWriter.writeBlankLine(3); + ConsoleWriter.writeUserInputRequestMessage("Enter your username:"); + this.user.setUsername(UserInput.getStringUserInput()); + + ConsoleWriter.writeUserInputRequestMessage("Enter your password:"); + this.user.setPassword(UserInput.getStringUserInput()); + + postData.put("user", this.user); + this.loginController.setData(postData); + } + + public void logout() { + this.userService.logout(); + } + +} diff --git a/naresh/src/main/java/com/lftechnology/phpjava/ems/views/TableGenerator.java b/naresh/src/main/java/com/lftechnology/phpjava/ems/views/TableGenerator.java new file mode 100644 index 0000000..86279dc --- /dev/null +++ b/naresh/src/main/java/com/lftechnology/phpjava/ems/views/TableGenerator.java @@ -0,0 +1,42 @@ +package com.lftechnology.phpjava.ems.views; + +import de.vandermeer.asciitable.v2.RenderedTable; +import de.vandermeer.asciitable.v2.V2_AsciiTable; +import de.vandermeer.asciitable.v2.render.V2_AsciiTableRenderer; +import de.vandermeer.asciitable.v2.render.WidthAbsoluteEven; +import de.vandermeer.asciitable.v2.themes.V2_E_TableThemes; + +/** + * TableGenerator + * + * @author Naresh Maharjan + * @since August, 15 2016 + */ +public class TableGenerator { + private int numberOfColums; + private V2_AsciiTable table = new V2_AsciiTable(); + private V2_AsciiTableRenderer renderer = new V2_AsciiTableRenderer(); + + public TableGenerator(String[] columns) { + this.renderer.setTheme(V2_E_TableThemes.UTF_HEAVY.get()); + this.renderer.setWidth(new WidthAbsoluteEven(160)); + this.generateHeader(columns); + } + + public void renderTable() { + RenderedTable rt = this.renderer.render(this.table); + System.out.println(rt); + } + + private void generateHeader(String[] colums) { + this.table.addRule(); + this.table.addRow(colums); + this.table.addRule(); + } + + public void addRow(String[] row) { + this.table.addRow(row); + this.table.addRule(); + } + +} diff --git a/naresh/src/main/java/com/lftechnology/phpjava/ems/views/ViewSignature.java b/naresh/src/main/java/com/lftechnology/phpjava/ems/views/ViewSignature.java new file mode 100644 index 0000000..a597ccb --- /dev/null +++ b/naresh/src/main/java/com/lftechnology/phpjava/ems/views/ViewSignature.java @@ -0,0 +1,17 @@ +package com.lftechnology.phpjava.ems.views; + +import java.util.Map; + +/** + * ViewSignature + * + * @author Naresh Maharjan + * @since August, 15 2016 + */ +public interface ViewSignature { + public void render(); + public void setAction(String action); + public String getAction(); + public void setData(Map data); + public Map getData(); +} From 0c399ce6bca87a72c4700418085178c87be70217 Mon Sep 17 00:00:00 2001 From: Naresh Maharjan Date: Wed, 17 Aug 2016 02:18:50 +0545 Subject: [PATCH 6/9] indentation rectified --- .../phpjava/ems/configs/db.properties | 8 ++++---- .../ems/controllers/ControllerSignature.java | 4 +++- .../ems/controllers/EmployeeController.java | 18 ++++++++--------- .../ems/controllers/LoginController.java | 15 +++++++------- .../phpjava/ems/dao/DaoSignature.java | 10 ++++------ .../phpjava/ems/dao/UserDaoImpl.java | 20 +++++++++---------- .../phpjava/ems/entities/Employee.java | 6 +++--- .../phpjava/ems/entities/User.java | 8 ++++---- .../phpjava/ems/services/EmployeeService.java | 9 ++++----- .../phpjava/ems/services/UserService.java | 4 +--- .../phpjava/ems/utlis/AbstractDbAdapater.java | 2 +- .../phpjava/ems/utlis/DbFactory.java | 6 +++--- .../phpjava/ems/utlis/Router.java | 3 +-- .../phpjava/ems/views/AbstractBaseView.java | 8 ++++---- .../phpjava/ems/views/CommonViewUtility.java | 8 ++++---- .../phpjava/ems/views/EmployeeView.java | 16 +++++++-------- .../phpjava/ems/views/LoginView.java | 12 +++++------ .../phpjava/ems/views/ViewSignature.java | 8 ++++++-- 18 files changed, 83 insertions(+), 82 deletions(-) diff --git a/naresh/src/main/java/com/lftechnology/phpjava/ems/configs/db.properties b/naresh/src/main/java/com/lftechnology/phpjava/ems/configs/db.properties index 8e50833..1026c39 100644 --- a/naresh/src/main/java/com/lftechnology/phpjava/ems/configs/db.properties +++ b/naresh/src/main/java/com/lftechnology/phpjava/ems/configs/db.properties @@ -1,4 +1,4 @@ -url = jdbc:mysql://localhost/ems -driver = com.mysql.jdbc.Driver -username = root -password = naresh \ No newline at end of file +url=jdbc:mysql://localhost/ems +driver=com.mysql.jdbc.Driver +username=root +password=naresh \ No newline at end of file diff --git a/naresh/src/main/java/com/lftechnology/phpjava/ems/controllers/ControllerSignature.java b/naresh/src/main/java/com/lftechnology/phpjava/ems/controllers/ControllerSignature.java index 8760d62..71f5993 100644 --- a/naresh/src/main/java/com/lftechnology/phpjava/ems/controllers/ControllerSignature.java +++ b/naresh/src/main/java/com/lftechnology/phpjava/ems/controllers/ControllerSignature.java @@ -9,7 +9,9 @@ * @since August, 16 2016 */ public interface ControllerSignature { - public void setData(Map data); public Map getData(); + + public void setData(Map data); + public boolean isPost(); } diff --git a/naresh/src/main/java/com/lftechnology/phpjava/ems/controllers/EmployeeController.java b/naresh/src/main/java/com/lftechnology/phpjava/ems/controllers/EmployeeController.java index ef36a83..ef0d0d5 100644 --- a/naresh/src/main/java/com/lftechnology/phpjava/ems/controllers/EmployeeController.java +++ b/naresh/src/main/java/com/lftechnology/phpjava/ems/controllers/EmployeeController.java @@ -75,9 +75,9 @@ public void searchUser() { } public void terminateUser() { - if (this.isPost()){ + if (this.isPost()) { Employee employee = (Employee) postData.get("employee"); - int result= 0; + int result = 0; try { result = employeeService.terminateUser(employee); } catch (SQLException e) { @@ -87,7 +87,7 @@ public void terminateUser() { postData.clear(); this.isPost = false; CommonViewUtility.showMessageAndContinue(message); - }else { + } else { employeeView.setAction(Constant.TERMINATE_USER_ACTION); employeeView.render(); } @@ -95,7 +95,7 @@ public void terminateUser() { } public void deleteUser() { - if (this.isPost()){ + if (this.isPost()) { Employee employee = (Employee) postData.get("employee"); int result = 0; try { @@ -107,7 +107,7 @@ public void deleteUser() { postData.clear(); this.isPost = false; CommonViewUtility.showMessageAndContinue(message); - }else { + } else { employeeView.setAction(Constant.DELETE_USER_ACTION); employeeView.render(); } @@ -138,13 +138,13 @@ public void updateProfile() { } @Override - public void setData(Map data) { - postData = data; + public Map getData() { + return null; } @Override - public Map getData() { - return null; + public void setData(Map data) { + postData = data; } @Override diff --git a/naresh/src/main/java/com/lftechnology/phpjava/ems/controllers/LoginController.java b/naresh/src/main/java/com/lftechnology/phpjava/ems/controllers/LoginController.java index 0d1fea3..a03ff7c 100644 --- a/naresh/src/main/java/com/lftechnology/phpjava/ems/controllers/LoginController.java +++ b/naresh/src/main/java/com/lftechnology/phpjava/ems/controllers/LoginController.java @@ -14,12 +14,13 @@ * @author Naresh Maharjan * @since August, 08 2016 */ -public class LoginController implements ControllerSignature{ +public class LoginController implements ControllerSignature { private static LoginView loginView = new LoginView(); + private static Map postData = new HashMap<>(); private UserService userService = new UserService(); private CommonUtility commonUtility = new CommonUtility(); - private static Map postData = new HashMap<>(); + public void login() { loginView.setAction("login"); loginView.render(); @@ -33,11 +34,6 @@ public void logout() { loginView.logout(); } - @Override - public void setData(Map data) { - this.postData = data; - } - @Override public Map getData() { Map data = new HashMap<>(); @@ -45,6 +41,11 @@ public Map getData() { return data; } + @Override + public void setData(Map data) { + this.postData = data; + } + @Override public boolean isPost() { return false; diff --git a/naresh/src/main/java/com/lftechnology/phpjava/ems/dao/DaoSignature.java b/naresh/src/main/java/com/lftechnology/phpjava/ems/dao/DaoSignature.java index 5bc0820..795fe0f 100644 --- a/naresh/src/main/java/com/lftechnology/phpjava/ems/dao/DaoSignature.java +++ b/naresh/src/main/java/com/lftechnology/phpjava/ems/dao/DaoSignature.java @@ -12,37 +12,35 @@ public interface DaoSignature { /** - * - * @author Naresh Maharjan * @return * @throws SQLException + * @author Naresh Maharjan */ List findAll() throws SQLException; /** - * @author Naresh Maharjan * @param s * @return * @throws SQLException + * @author Naresh Maharjan */ int insert(T t) throws SQLException; /** - * @author Naresh Maharjan * @param s * @return * @throws SQLException + * @author Naresh Maharjan */ int update(T t) throws SQLException; /** - * - * @author Naresh Maharjan * @param s * @return * @throws SQLException + * @author Naresh Maharjan */ int delete(T t) throws SQLException; } diff --git a/naresh/src/main/java/com/lftechnology/phpjava/ems/dao/UserDaoImpl.java b/naresh/src/main/java/com/lftechnology/phpjava/ems/dao/UserDaoImpl.java index 51bd024..38873b9 100644 --- a/naresh/src/main/java/com/lftechnology/phpjava/ems/dao/UserDaoImpl.java +++ b/naresh/src/main/java/com/lftechnology/phpjava/ems/dao/UserDaoImpl.java @@ -19,6 +19,15 @@ public class UserDaoImpl implements DaoSignature { protected Connection conn = DbFactory.getConnection(); protected PreparedStatement stmt = null; + public static String preparePlaceHolders(int length) { + return String.join(",", Collections.nCopies(length, "?")); + } + + public static void setValues(PreparedStatement preparedStatement, Object... values) throws SQLException { + for (int i = 0; i < values.length; i++) { + preparedStatement.setObject(i + 1, values[i]); + } + } @Override public List findAll() throws SQLException { @@ -55,7 +64,7 @@ public int update(User user) throws SQLException { value = user.getPassword(); sql.append(" password = ?"); } - if(update){ + if (update) { sql.append(" where id = ?"); stmt = conn.prepareStatement(sql.toString()); stmt.setString(1, value); @@ -86,15 +95,6 @@ public int terminateUsers(Set userIds) throws SQLException { return stmt.executeUpdate(); } - public static String preparePlaceHolders(int length) { - return String.join(",", Collections.nCopies(length, "?")); - } - - public static void setValues(PreparedStatement preparedStatement, Object... values) throws SQLException { - for (int i = 0; i < values.length; i++) { - preparedStatement.setObject(i + 1, values[i]); - } - } /** * Find the employee by role * diff --git a/naresh/src/main/java/com/lftechnology/phpjava/ems/entities/Employee.java b/naresh/src/main/java/com/lftechnology/phpjava/ems/entities/Employee.java index 1a7b489..f80da67 100644 --- a/naresh/src/main/java/com/lftechnology/phpjava/ems/entities/Employee.java +++ b/naresh/src/main/java/com/lftechnology/phpjava/ems/entities/Employee.java @@ -16,7 +16,7 @@ public class Employee extends User { protected Role role; protected int userId; - public Employee(){ + public Employee() { super(); } @@ -76,13 +76,13 @@ public Employee setUserId(int userId) { return this; } - public String[] toArray(){ + public String[] toArray() { String[] emp = new String[5]; emp[0] = String.valueOf(this.getUserId()); emp[1] = this.getFullname(); emp[2] = this.getAddress(); emp[3] = this.getRole().toString(); emp[4] = this.getDepartment(); - return emp; + return emp; } } diff --git a/naresh/src/main/java/com/lftechnology/phpjava/ems/entities/User.java b/naresh/src/main/java/com/lftechnology/phpjava/ems/entities/User.java index 4857f2d..8cc30f1 100644 --- a/naresh/src/main/java/com/lftechnology/phpjava/ems/entities/User.java +++ b/naresh/src/main/java/com/lftechnology/phpjava/ems/entities/User.java @@ -23,15 +23,15 @@ public User(String username, String password, boolean isTerminated) { this.isTerminated = isTerminated; } + public int getId() { + return this.id; + } + public User setId(int id) { this.id = id; return this; } - public int getId() { - return this.id; - } - public String getUsername() { return username; } diff --git a/naresh/src/main/java/com/lftechnology/phpjava/ems/services/EmployeeService.java b/naresh/src/main/java/com/lftechnology/phpjava/ems/services/EmployeeService.java index dff7784..4b34805 100644 --- a/naresh/src/main/java/com/lftechnology/phpjava/ems/services/EmployeeService.java +++ b/naresh/src/main/java/com/lftechnology/phpjava/ems/services/EmployeeService.java @@ -9,7 +9,6 @@ import java.sql.SQLException; import java.util.ArrayList; -import java.util.HashSet; import java.util.List; import java.util.Set; import java.util.stream.Collectors; @@ -51,7 +50,7 @@ public void checkAndCreateAdminUser() throws Exception { public boolean checkAdminUserExists() throws SQLException { EmployeeDaoImpl employeeDao = new EmployeeDaoImpl(); List employees = employeeDao.findByRole(Role.ADMIN); - if (employees.isEmpty()){ + if (employees.isEmpty()) { return false; } Employee employee = employees.get(0); @@ -96,7 +95,7 @@ public List searchEmployee(Employee employee) throws SQLException { EmployeeDaoImpl employeeDao = new EmployeeDaoImpl(); if (employee.getDepartment() != null && !employee.getDepartment().isEmpty()) { employees = employeeDao.findByDepartment(employee.getDepartment()); - } else if (employee.getFullname() != null&& !employee.getFullname().isEmpty()) { + } else if (employee.getFullname() != null && !employee.getFullname().isEmpty()) { employees = employeeDao.findByFullName(employee.getFullname()); } else if (employee.getAddress() != null && !employee.getAddress().isEmpty()) { employees = employeeDao.findByAddress(employee.getAddress()); @@ -119,7 +118,7 @@ public int deleteUser(Employee employee) throws SQLException { private Set getUserIds(Employee employee) throws SQLException { List employees = new ArrayList<>(); EmployeeDaoImpl employeeDao = new EmployeeDaoImpl(); - if (employee.getFullname() != null&& !employee.getFullname().isEmpty()) { + if (employee.getFullname() != null && !employee.getFullname().isEmpty()) { employees = employeeDao.findByFullName(employee.getFullname()); } Set userIds = employees.stream().map(Employee::getUserId).collect(Collectors.toSet()); @@ -127,7 +126,7 @@ private Set getUserIds(Employee employee) throws SQLException { } - public String[] getColumns(){ + public String[] getColumns() { String[] columnNames = new String[5]; columnNames[0] = "User ID"; columnNames[1] = "Full Name"; diff --git a/naresh/src/main/java/com/lftechnology/phpjava/ems/services/UserService.java b/naresh/src/main/java/com/lftechnology/phpjava/ems/services/UserService.java index 1f59cff..37ca8fb 100644 --- a/naresh/src/main/java/com/lftechnology/phpjava/ems/services/UserService.java +++ b/naresh/src/main/java/com/lftechnology/phpjava/ems/services/UserService.java @@ -16,13 +16,11 @@ */ public class UserService { + UserDaoImpl userDao = new UserDaoImpl(); private boolean isLoggedIn = false; private String loggedInUserRole = ""; private int userId; - UserDaoImpl userDao = new UserDaoImpl(); - - public void login(User user) { try { User result = userDao.findByUsernamePassword(user); diff --git a/naresh/src/main/java/com/lftechnology/phpjava/ems/utlis/AbstractDbAdapater.java b/naresh/src/main/java/com/lftechnology/phpjava/ems/utlis/AbstractDbAdapater.java index d0d146b..d410e04 100644 --- a/naresh/src/main/java/com/lftechnology/phpjava/ems/utlis/AbstractDbAdapater.java +++ b/naresh/src/main/java/com/lftechnology/phpjava/ems/utlis/AbstractDbAdapater.java @@ -11,7 +11,7 @@ public abstract class AbstractDbAdapater { protected String query = ""; - protected AbstractDbAdapater where(Map condition){ + protected AbstractDbAdapater where(Map condition) { condition.entrySet(); return this; } diff --git a/naresh/src/main/java/com/lftechnology/phpjava/ems/utlis/DbFactory.java b/naresh/src/main/java/com/lftechnology/phpjava/ems/utlis/DbFactory.java index 106eefa..0c2c81f 100644 --- a/naresh/src/main/java/com/lftechnology/phpjava/ems/utlis/DbFactory.java +++ b/naresh/src/main/java/com/lftechnology/phpjava/ems/utlis/DbFactory.java @@ -24,9 +24,9 @@ public abstract class DbFactory { /** * load database configuration for properties file * - * @author Naresh Maharjan * @return * @throws IOException + * @author Naresh Maharjan */ private static void loadProperties() { @@ -52,8 +52,8 @@ private static void loadProperties() { /** * get database connection * - * @author Naresh Maharjan * @return + * @author Naresh Maharjan */ public static Connection getConnection() { loadProperties(); @@ -76,7 +76,7 @@ public static Connection getConnection() { /** * */ - public static void closeConnection(){ + public static void closeConnection() { // try { // diff --git a/naresh/src/main/java/com/lftechnology/phpjava/ems/utlis/Router.java b/naresh/src/main/java/com/lftechnology/phpjava/ems/utlis/Router.java index a252bfd..f073978 100644 --- a/naresh/src/main/java/com/lftechnology/phpjava/ems/utlis/Router.java +++ b/naresh/src/main/java/com/lftechnology/phpjava/ems/utlis/Router.java @@ -3,7 +3,6 @@ import com.lftechnology.phpjava.ems.constants.Constant; import com.lftechnology.phpjava.ems.controllers.EmployeeController; import com.lftechnology.phpjava.ems.controllers.LoginController; -import com.lftechnology.phpjava.ems.entities.Employee; import com.lftechnology.phpjava.ems.services.EmployeeService; import com.lftechnology.phpjava.ems.views.CommonViewUtility; @@ -71,7 +70,7 @@ public static void showMenu() { showLoginMenuAndLoginToSystem(); if (!commonUtility.isLoggedIn()) { CommonViewUtility.showMessageAndContinue("Invalid Credentails"); - }else { + } else { Map commonUtilityMap = new HashMap<>(); commonUtilityMap.put("commonUtility", commonUtility); employeeController.setData(commonUtilityMap); diff --git a/naresh/src/main/java/com/lftechnology/phpjava/ems/views/AbstractBaseView.java b/naresh/src/main/java/com/lftechnology/phpjava/ems/views/AbstractBaseView.java index fb52df9..47c8283 100644 --- a/naresh/src/main/java/com/lftechnology/phpjava/ems/views/AbstractBaseView.java +++ b/naresh/src/main/java/com/lftechnology/phpjava/ems/views/AbstractBaseView.java @@ -13,13 +13,13 @@ public abstract class AbstractBaseView implements ViewSignature { public abstract void render(); @Override - public void setAction(String action) { - this.action = action; + public String getAction() { + return this.action; } @Override - public String getAction() { - return this.action; + public void setAction(String action) { + this.action = action; } } diff --git a/naresh/src/main/java/com/lftechnology/phpjava/ems/views/CommonViewUtility.java b/naresh/src/main/java/com/lftechnology/phpjava/ems/views/CommonViewUtility.java index 1047d26..9a71385 100644 --- a/naresh/src/main/java/com/lftechnology/phpjava/ems/views/CommonViewUtility.java +++ b/naresh/src/main/java/com/lftechnology/phpjava/ems/views/CommonViewUtility.java @@ -25,7 +25,7 @@ public static void showWelcomeScreen() { ConsoleWriter.writeUserInputRequestMessage("Enter your credentails to log into the system"); } - public static void showExitMessageAndExit(){ + public static void showExitMessageAndExit() { ConsoleWriter.writeBlankLine(100); ConsoleWriter.writeUserInputRequestMessage("Bye, Felicia"); ConsoleWriter.writeBlankLine(5); @@ -43,17 +43,17 @@ public static void showMessageAndContinue(String message) { ConsoleWriter.writeUserInputRequestMessage("2. No"); if (UserInput.getIntegerUserInput() == 1) { Router.showMenu(); - }else{ + } else { showExitMessageAndExit(); } } - public static void pressKeyToContinue(){ + public static void pressKeyToContinue() { ConsoleWriter.writeBlankLine(3); ConsoleWriter.writeUserInputRequestMessage("Press 1 to continue or any other number to exit"); if (UserInput.getIntegerUserInput() == 1) { Router.showMenu(); - }else{ + } else { showExitMessageAndExit(); } } diff --git a/naresh/src/main/java/com/lftechnology/phpjava/ems/views/EmployeeView.java b/naresh/src/main/java/com/lftechnology/phpjava/ems/views/EmployeeView.java index 53b903b..98ba048 100644 --- a/naresh/src/main/java/com/lftechnology/phpjava/ems/views/EmployeeView.java +++ b/naresh/src/main/java/com/lftechnology/phpjava/ems/views/EmployeeView.java @@ -58,19 +58,14 @@ public void render() { } } - @Override - public void setAction(String action) { - this.action = action; - } - @Override public String getAction() { return this.action; } @Override - public void setData(Map data) { - commonUtility = (CommonUtility) data.get("commonUtility"); + public void setAction(String action) { + this.action = action; } @Override @@ -78,6 +73,11 @@ public Map getData() { return null; } + @Override + public void setData(Map data) { + commonUtility = (CommonUtility) data.get("commonUtility"); + } + public void showAdminMenu() { ConsoleWriter.writeBlankLine(100); ConsoleWriter.writeUserInputRequestMessage("1. Add New User"); @@ -200,7 +200,7 @@ public void showSearchUserMenu() { public void renderEmployeeList(String[] columns, List employees) { TableGenerator tableGenerator = new TableGenerator(columns); - for (Employee employee: employees) { + for (Employee employee : employees) { tableGenerator.addRow(employee.toArray()); } tableGenerator.renderTable(); diff --git a/naresh/src/main/java/com/lftechnology/phpjava/ems/views/LoginView.java b/naresh/src/main/java/com/lftechnology/phpjava/ems/views/LoginView.java index 77b54bc..9cbd8d8 100644 --- a/naresh/src/main/java/com/lftechnology/phpjava/ems/views/LoginView.java +++ b/naresh/src/main/java/com/lftechnology/phpjava/ems/views/LoginView.java @@ -33,18 +33,14 @@ public void render() { } - @Override - public void setAction(String action) { - this.action = action; - } - @Override public String getAction() { return this.action; } @Override - public void setData(Map data) { + public void setAction(String action) { + this.action = action; } @Override @@ -54,6 +50,10 @@ public Map getData() { return data; } + @Override + public void setData(Map data) { + } + public void login() { Map postData = new HashMap<>(); diff --git a/naresh/src/main/java/com/lftechnology/phpjava/ems/views/ViewSignature.java b/naresh/src/main/java/com/lftechnology/phpjava/ems/views/ViewSignature.java index a597ccb..e789f0b 100644 --- a/naresh/src/main/java/com/lftechnology/phpjava/ems/views/ViewSignature.java +++ b/naresh/src/main/java/com/lftechnology/phpjava/ems/views/ViewSignature.java @@ -10,8 +10,12 @@ */ public interface ViewSignature { public void render(); - public void setAction(String action); + public String getAction(); - public void setData(Map data); + + public void setAction(String action); + public Map getData(); + + public void setData(Map data); } From 7c31c8c95d66b1a3d71a73f1ccc6cf71d5c93f38 Mon Sep 17 00:00:00 2001 From: Naresh Maharjan Date: Wed, 17 Aug 2016 12:47:43 +0545 Subject: [PATCH 7/9] strings converted to constants --- .../phpjava/ems/EmployeeManagementSystem.java | 33 +--- .../phpjava/ems/constants/Constant.java | 65 ++++++- .../ems/controllers/ControllerSignature.java | 18 +- .../ems/controllers/EmployeeController.java | 33 ++++ .../ems/controllers/LoginController.java | 13 +- .../phpjava/ems/dao/EmployeeDaoImpl.java | 23 +++ .../phpjava/ems/dao/UserDaoImpl.java | 110 ++++++++---- .../phpjava/ems/entities/Employee.java | 61 ++++++- .../lftechnology/phpjava/ems/enums/Role.java | 8 + .../phpjava/ems/services/EmployeeService.java | 11 +- .../phpjava/ems/services/UserService.java | 44 +++-- .../phpjava/ems/utlis/AbstractDbAdapater.java | 18 -- .../phpjava/ems/utlis/CommonUtility.java | 20 +++ .../phpjava/ems/utlis/ConsoleWriter.java | 25 +-- .../phpjava/ems/utlis/DbFactory.java | 15 -- .../ems/utlis/PasswordHashGenerator.java | 6 - .../phpjava/ems/utlis/Router.java | 17 +- .../phpjava/ems/utlis/SqlRunner.java | 25 ++- .../phpjava/ems/utlis/UserInput.java | 14 +- .../phpjava/ems/views/AbstractBaseView.java | 25 --- .../phpjava/ems/views/CommonViewUtility.java | 28 ++- .../phpjava/ems/views/EmployeeView.java | 168 ++++++++++++------ .../phpjava/ems/views/LoginView.java | 36 +++- .../phpjava/ems/views/TableGenerator.java | 16 +- .../phpjava/ems/views/ViewSignature.java | 29 ++- 25 files changed, 598 insertions(+), 263 deletions(-) delete mode 100644 naresh/src/main/java/com/lftechnology/phpjava/ems/utlis/AbstractDbAdapater.java delete mode 100644 naresh/src/main/java/com/lftechnology/phpjava/ems/views/AbstractBaseView.java diff --git a/naresh/src/main/java/com/lftechnology/phpjava/ems/EmployeeManagementSystem.java b/naresh/src/main/java/com/lftechnology/phpjava/ems/EmployeeManagementSystem.java index 915c43c..9ebefc9 100644 --- a/naresh/src/main/java/com/lftechnology/phpjava/ems/EmployeeManagementSystem.java +++ b/naresh/src/main/java/com/lftechnology/phpjava/ems/EmployeeManagementSystem.java @@ -9,35 +9,12 @@ * @since August, 12 2016 */ public class EmployeeManagementSystem { - public static void main(String[] args) { -// V2_AsciiTable at = new V2_AsciiTable(); -// V2_AsciiTableRenderer rend = new V2_AsciiTableRenderer(); -// rend.setTheme(V2_E_TableThemes.UTF_HEAVY.get()); -// rend.setWidth(new WidthAbsoluteEven(160)); -// String[] rows = new String[6]; -// rows[0] = "one"; -// rows[1] = "two"; -// rows[2] = "three"; -// rows[3] = "four"; -// rows[4] = "five"; -// rows[5] = "six"; -// at.addRule(); -// at.addRow(rows); -// at.addRule(); -// EmployeeDaoImpl employeeDao = new EmployeeDaoImpl(); -// try { -// List employees = employeeDao.findAll(); -// for (Employee e: employees) { -// at.addRow(e.toArray()); -// } -// } catch (SQLException e) { -// e.printStackTrace(); -// } -// at.addRule(); -// RenderedTable rt = rend.render(at); -// System.out.println(rt); + /** + * @author Naresh Maharjan + * @param args + */ + public static void main(String[] args) { Router.showMenu(); -// CommonViewUtility.showWelcomeScreen(); } } diff --git a/naresh/src/main/java/com/lftechnology/phpjava/ems/constants/Constant.java b/naresh/src/main/java/com/lftechnology/phpjava/ems/constants/Constant.java index 6ddad94..4c5c666 100644 --- a/naresh/src/main/java/com/lftechnology/phpjava/ems/constants/Constant.java +++ b/naresh/src/main/java/com/lftechnology/phpjava/ems/constants/Constant.java @@ -1,5 +1,6 @@ package com.lftechnology.phpjava.ems.constants; + /** * Constant * @@ -7,20 +8,16 @@ * @since August, 09 2016 */ public class Constant { - - public static final String PROPERTY_URL = "url"; - public static final String PROPERTY_DRIVER = "driver"; - public static final String PROPERTY_USERNAME = "username"; - public static final String PROPERTY_PASSWORD = "password"; - public static final String ADMIN = "admin"; public static final int ADD_NEW_USER = 1; public static final int UPDATE_PROFILE = 1; public static final int SEARCH_USER = 2; public static final int TERMINATE_USER = 3; public static final int DELETE_USER = 4; public static final int EXIT = 0; - public static final int AMIN_USER_INPUT_ID = 1; - public static final int NORMAL_USER_INPUT_ID = 2; + public static final String PROPERTY_URL = "url"; + public static final String PROPERTY_DRIVER = "driver"; + public static final String PROPERTY_USERNAME = "username"; + public static final String PROPERTY_PASSWORD = "password"; public static final String POST_LOGIN_ACTION = "postLogin"; public static final String ADD_USER_ACTION = "addUser"; public static final String SEARCH_USER_ACTION = "searchUser"; @@ -28,5 +25,55 @@ public class Constant { public static final String DELETE_USER_ACTION = "deleteUser"; public static final String EXIT_ACTION = "exit"; public static final String UPDATE_PROFILE_ACTION = "updateProfile"; - + public static final String TERMINATE_USER_MESSAGE = "3. Terminate User"; + public static final String ADD_NEW_USER_MESSAGE = "1. Add New User"; + public static final String SEARCH_USERS = "2. Search User(s)"; + public static final String DELETE_USER_MESSAGE = "4. Delete User"; + public static final String EXIT_MESSAGE = "0. Exit"; + public static final String UPDATE_PROFILE_MESSAGE = "1. Update Profile"; + public static final String CHOOSE_ONE_OF_THE_FOLLOWING_USER_TYPE = "Choose One Of The Following User Type"; + public static final String ADD_ADMIN_USER = "1. Add Admin User"; + public static final String ADD_NORMAL_USER = "2. Add Normal User"; + public static final String EMPLOYEE = "employee"; + public static final String COMMON_UTILITY = "commonUtility"; + public static final String ENTER_USER_DETAILS = "Enter User Details"; + public static final String ENTER_FULLNAME = "1. Enter Fullname"; + public static final String ENTER_DEPARTMENT = "2. Enter Department"; + public static final String ENTER_ADDRESS = "3. Enter Address"; + public static final String ENTER_USERNAME = "4. Enter Username"; + public static final String ENTER_PASSWORD = "5. Enter Password"; + public static final String CHOOSE_ONE_OF_THE_FOLLOWING_SEARCH_CRITERIA = "Choose One Of The Following Search Criteria"; + public static final String SEARCH_BY_FULLNAME = "1. Search By Fullname"; + public static final String SEARCH_BY_DEPARTMENT = "2. Search By Department"; + public static final String SEARCH_BY_ADDRESS = "3. Search By Address"; + public static final String ENTER_SEARCH_VALUE = "Enter search value"; + public static final String UPDATE_PROFILE_MENU = "Update Profile"; + public static final String UPDATE_FULLNAME = "1. Update Fullname"; + public static final String UPDATE_DEPARTMENT = "2. Update Department"; + public static final String UPDATE_ADDRESS = "3. Update Address"; + public static final String UPDATE_USERNAME = "4. Update Username"; + public static final String UPDATE_PASSWORD = "5. Update Password"; + public static final String ENTER_VALUE = "Enter value"; + public static final String DELETE_USER_MENU = "Delete User"; + public static final String ENTER_FULLNAME_OF_EMPLOYEE = "1. Enter Fullname of Employee"; + public static final String RETURN_TO_MAIN_MENU = "2. Return to main menu"; + public static final String TERMINATE_USER_MENU = "Terminate User"; + public static final String LOGIN = "login"; + public static final String ENTER_YOUR_USERNAME_TO_LOGIN = "Enter your username:"; + public static final String ENTER_YOUR_PASSWORD_TO_LOGIN = "Enter your password:"; + public static final String USER = "user"; + public static final String WELCOME_TO_EMPLOYEE_MANAGEMENT_SYSTEM = "Welcome To Employee Management System !!!"; + public static final String ENTER_YOUR_CREDENTAILS_TO_LOG_INTO_THE_SYSTEM = "Enter your credentails to log into the system"; + public static final String BYE_MESSAGE = "Bye, Felicia"; + public static final String DO_YOU_WANT_TO_CONTINUE = "Do you want to continue?"; + public static final String YES = "1. Yes"; + public static final String NO = "2. No"; + public static final String PRESS_1_TO_CONTINUE_OR_ANY_OTHER_NUMBER_TO_EXIT = "Press 1 to continue or any other number to exit"; + public static final String INVALID_OPTION_PLEASE_ENTER_AGAIN = "Invalid option. Please enter again"; + public static final String INVALID_CREDENTAILS = "Invalid Credentails"; + public static final String USER_ID = "User ID"; + public static final String FULL_NAME = "Full Name"; + public static final String ADDRESS = "Address"; + public static final String ROLE_USER_TYPE = "Role(User Type)"; + public static final String DEPARTMENT = "Department"; } diff --git a/naresh/src/main/java/com/lftechnology/phpjava/ems/controllers/ControllerSignature.java b/naresh/src/main/java/com/lftechnology/phpjava/ems/controllers/ControllerSignature.java index 71f5993..c3906c0 100644 --- a/naresh/src/main/java/com/lftechnology/phpjava/ems/controllers/ControllerSignature.java +++ b/naresh/src/main/java/com/lftechnology/phpjava/ems/controllers/ControllerSignature.java @@ -9,9 +9,21 @@ * @since August, 16 2016 */ public interface ControllerSignature { - public Map getData(); + /** + * @author Naresh Maharjan + * @return + */ + Map getData(); - public void setData(Map data); + /** + * @author Naresh Maharjan + * @param data + */ + void setData(Map data); - public boolean isPost(); + /** + * @author Naresh Maharjan + * @return + */ + boolean isPost(); } diff --git a/naresh/src/main/java/com/lftechnology/phpjava/ems/controllers/EmployeeController.java b/naresh/src/main/java/com/lftechnology/phpjava/ems/controllers/EmployeeController.java index ef0d0d5..43bbaa3 100644 --- a/naresh/src/main/java/com/lftechnology/phpjava/ems/controllers/EmployeeController.java +++ b/naresh/src/main/java/com/lftechnology/phpjava/ems/controllers/EmployeeController.java @@ -24,12 +24,18 @@ public class EmployeeController implements ControllerSignature { private static Map postData = new HashMap<>(); private boolean isPost = false; + /** + * @author Naresh Maharjan + */ public void postLoginScreen() { employeeView.setAction(Constant.POST_LOGIN_ACTION); employeeView.setData(postData); employeeView.render(); } + /** + * @author Naresh Maharjan + */ public void addUser() { if (this.isPost) { Employee employee = (Employee) postData.get("employee"); @@ -55,6 +61,9 @@ public void addUser() { } + /** + * @author Naresh Maharjan + */ public void searchUser() { if (this.isPost) { Employee employee = (Employee) postData.get("employee"); @@ -74,6 +83,9 @@ public void searchUser() { } } + /** + * @author Naresh Maharjan + */ public void terminateUser() { if (this.isPost()) { Employee employee = (Employee) postData.get("employee"); @@ -94,6 +106,9 @@ public void terminateUser() { } + /** + * @author Naresh Maharjan + */ public void deleteUser() { if (this.isPost()) { Employee employee = (Employee) postData.get("employee"); @@ -113,6 +128,9 @@ public void deleteUser() { } } + /** + * @author Naresh Maharjan + */ public void updateProfile() { if (this.isPost) { Employee employee = (Employee) postData.get("employee"); @@ -137,21 +155,36 @@ public void updateProfile() { } } + /** + * @author Naresh Maharjan + * @return + */ @Override public Map getData() { return null; } + /** + * @author Naresh Maharjan + * @param data + */ @Override public void setData(Map data) { postData = data; } + /** + * @author Naresh Maharjan + * @return + */ @Override public boolean isPost() { return this.isPost; } + /** + * @author Naresh Maharjan + */ public void makeCurrentRequestPost() { this.isPost = true; } diff --git a/naresh/src/main/java/com/lftechnology/phpjava/ems/controllers/LoginController.java b/naresh/src/main/java/com/lftechnology/phpjava/ems/controllers/LoginController.java index a03ff7c..9644d54 100644 --- a/naresh/src/main/java/com/lftechnology/phpjava/ems/controllers/LoginController.java +++ b/naresh/src/main/java/com/lftechnology/phpjava/ems/controllers/LoginController.java @@ -1,5 +1,6 @@ package com.lftechnology.phpjava.ems.controllers; +import com.lftechnology.phpjava.ems.constants.Constant; import com.lftechnology.phpjava.ems.entities.User; import com.lftechnology.phpjava.ems.services.UserService; import com.lftechnology.phpjava.ems.utlis.CommonUtility; @@ -22,11 +23,15 @@ public class LoginController implements ControllerSignature { private CommonUtility commonUtility = new CommonUtility(); public void login() { - loginView.setAction("login"); + loginView.setAction(Constant.LOGIN); loginView.render(); - User user = (User) this.postData.get("user"); - this.userService.login(user); + User user = (User) this.postData.get(Constant.USER); + try { + this.userService.login(user); + } catch (Exception e) { + e.printStackTrace(); + } this.commonUtility.setUserService(this.userService); } @@ -37,7 +42,7 @@ public void logout() { @Override public Map getData() { Map data = new HashMap<>(); - data.put("commonUtility", this.commonUtility); + data.put(Constant.COMMON_UTILITY, this.commonUtility); return data; } diff --git a/naresh/src/main/java/com/lftechnology/phpjava/ems/dao/EmployeeDaoImpl.java b/naresh/src/main/java/com/lftechnology/phpjava/ems/dao/EmployeeDaoImpl.java index 905e623..9c06b18 100644 --- a/naresh/src/main/java/com/lftechnology/phpjava/ems/dao/EmployeeDaoImpl.java +++ b/naresh/src/main/java/com/lftechnology/phpjava/ems/dao/EmployeeDaoImpl.java @@ -22,6 +22,11 @@ public class EmployeeDaoImpl implements DaoSignature { protected Connection conn = DbFactory.getConnection(); protected PreparedStatement stmt = null; + /** + * @author Naresh Maharjan + * @return + * @throws SQLException + */ @Override public List findAll() throws SQLException { List employees = new ArrayList<>(); @@ -42,6 +47,12 @@ public List findAll() throws SQLException { return employees; } + /** + * @author Naresh Maharjan + * @param employee + * @return + * @throws SQLException + */ @Override public int insert(Employee employee) throws SQLException { String sql = "INSERT INTO employee (fullname, address, department, role, user_id) VALUES ( ?, ? , ?, ?,?);"; @@ -54,6 +65,12 @@ public int insert(Employee employee) throws SQLException { return stmt.executeUpdate(); } + /** + * @author Naresh Maharjan + * @param employee + * @return + * @throws SQLException + */ @Override public int update(Employee employee) throws SQLException { StringBuilder sql = new StringBuilder(); @@ -83,6 +100,12 @@ public int update(Employee employee) throws SQLException { return 0; } + /** + * @author Naresh Maharjan + * @param employee + * @return + * @throws SQLException + */ @Override public int delete(Employee employee) throws SQLException { return 0; diff --git a/naresh/src/main/java/com/lftechnology/phpjava/ems/dao/UserDaoImpl.java b/naresh/src/main/java/com/lftechnology/phpjava/ems/dao/UserDaoImpl.java index 38873b9..fbd4e45 100644 --- a/naresh/src/main/java/com/lftechnology/phpjava/ems/dao/UserDaoImpl.java +++ b/naresh/src/main/java/com/lftechnology/phpjava/ems/dao/UserDaoImpl.java @@ -19,21 +19,43 @@ public class UserDaoImpl implements DaoSignature { protected Connection conn = DbFactory.getConnection(); protected PreparedStatement stmt = null; + /** + * @param length + * @return + * @author Naresh Maharjan + */ public static String preparePlaceHolders(int length) { return String.join(",", Collections.nCopies(length, "?")); } + /** + * @param preparedStatement + * @param values + * @throws SQLException + * @author Naresh Maharjan + */ public static void setValues(PreparedStatement preparedStatement, Object... values) throws SQLException { for (int i = 0; i < values.length; i++) { preparedStatement.setObject(i + 1, values[i]); } } + /** + * @return + * @throws SQLException + * @author Naresh Maharjan + */ @Override public List findAll() throws SQLException { return null; } + /** + * @param user + * @return + * @throws SQLException + * @author Naresh Maharjan + */ @Override public int insert(User user) throws SQLException { int lastInsertedId = 0; @@ -49,6 +71,12 @@ public int insert(User user) throws SQLException { return lastInsertedId; } + /** + * @param user + * @return + * @throws SQLException + * @author Naresh Maharjan + */ @Override public int update(User user) throws SQLException { StringBuilder sql = new StringBuilder(); @@ -74,11 +102,23 @@ public int update(User user) throws SQLException { return 0; } + /** + * @param user + * @return + * @throws SQLException + * @author Naresh Maharjan + */ @Override public int delete(User user) throws SQLException { return 0; } + /** + * @param userIds + * @return + * @throws SQLException + * @author Naresh Maharjan + */ public int delete(Set userIds) throws SQLException { String sql = "DELETE FROM user WHERE id IN(%s)"; String sqlNew = String.format(sql, this.preparePlaceHolders(userIds.size())); @@ -87,6 +127,12 @@ public int delete(Set userIds) throws SQLException { return stmt.executeUpdate(); } + /** + * @param userIds + * @return + * @throws SQLException + * @author Naresh Maharjan + */ public int terminateUsers(Set userIds) throws SQLException { String sql = "UPDATE user SET is_terminated = 1 WHERE id IN(%s)"; String sqlNew = String.format(sql, this.preparePlaceHolders(userIds.size())); @@ -100,72 +146,60 @@ public int terminateUsers(Set userIds) throws SQLException { * * @param * @return User - * @throws SQLException + * @throws Exception * @author Naresh Maharjan */ - - public User findByUsernamePassword(User user) throws SQLException { + public User findByUsernamePassword(User user) throws Exception { String sql = "SELECT * FROM user WHERE is_terminated = 0 AND username = ?"; String[] bindValues = {user.getUsername()}; User result = findBy(sql, bindValues); - try { - if (result.getPassword() == null) { - result = new User(); - } else if (!PasswordHashGenerator.check(user.getPassword(), result.getPassword())) { - result = new User(); - } - } catch (Exception e) { - e.printStackTrace(); + if (result.getPassword() == null) { + result = new User(); + } else if (!PasswordHashGenerator.check(user.getPassword(), result.getPassword())) { + result = new User(); } return result; } /** + * @author Naresh Maharjan * @param sql * @param bindValues - * @return User - * @author Naresh Maharjan + * @return + * @throws SQLException */ - private User findBy(String sql, String[] bindValues) { + private User findBy(String sql, String[] bindValues) throws SQLException { User user = new User(); - try { - stmt = conn.prepareStatement(sql); - int counter = 1; - for (String bindvalue : bindValues) { - stmt.setString(counter, bindvalue); - counter++; - } - ResultSet resultSet = stmt.executeQuery(); - user = setUserData(resultSet); - } catch (SQLException e) { - e.printStackTrace(); - } finally { - DbFactory.closeConnection(); + stmt = conn.prepareStatement(sql); + int counter = 1; + for (String bindvalue : bindValues) { + stmt.setString(counter, bindvalue); + counter++; } + ResultSet resultSet = stmt.executeQuery(); + user = setUserData(resultSet); + return user; } /** * set user data from the resultset * + * @author Naresh Maharjan * @param resultSet * @return User - * @author Naresh Maharjan + * @throws SQLException */ - private User setUserData(ResultSet resultSet) { + private User setUserData(ResultSet resultSet) throws SQLException { User user = new User(); - try { - if (resultSet.next()) { - user.setUsername(resultSet.getString("username")) - .setPassword(resultSet.getString("password")) - .setTerminated(resultSet.getBoolean("is_terminated")) - .setId(resultSet.getInt("id")); - } - } catch (SQLException e) { - e.printStackTrace(); + if (resultSet.next()) { + user.setUsername(resultSet.getString("username")) + .setPassword(resultSet.getString("password")) + .setTerminated(resultSet.getBoolean("is_terminated")) + .setId(resultSet.getInt("id")); } return user; } diff --git a/naresh/src/main/java/com/lftechnology/phpjava/ems/entities/Employee.java b/naresh/src/main/java/com/lftechnology/phpjava/ems/entities/Employee.java index f80da67..ee1ff42 100644 --- a/naresh/src/main/java/com/lftechnology/phpjava/ems/entities/Employee.java +++ b/naresh/src/main/java/com/lftechnology/phpjava/ems/entities/Employee.java @@ -16,11 +16,22 @@ public class Employee extends User { protected Role role; protected int userId; + /** + * @author Naresh Maharjan + */ public Employee() { super(); } + /** + * @author Naresh Maharjan + * @param fullname + * @param address + * @param department + * @param role + * @param userId + */ public Employee(String fullname, String address, String department, Role role, int userId) { super(); this.fullname = fullname; @@ -30,52 +41,100 @@ public Employee(String fullname, String address, String department, Role role, i this.userId = userId; } + /** + * @author Naresh Maharjan + * @return + */ public String getFullname() { return this.fullname; } + /** + * @author Naresh Maharjan + * @param fullname + * @return + */ public Employee setFullname(String fullname) { this.fullname = fullname; return this; } + /** + * @author Naresh Maharjan + * @return + */ public String getAddress() { return this.address; } + /** + * @author Naresh Maharjan + * @param address + * @return + */ public Employee setAddress(String address) { this.address = address; return this; } + /** + * @author Naresh Maharjan + * @return + */ public String getDepartment() { return this.department; } + /** + * @author Naresh Maharjan + * @param department + * @return + */ public Employee setDepartment(String department) { this.department = department; return this; } - + /** + * @author Naresh Maharjan + * @return + */ public Role getRole() { return this.role; } + /** + * @author Naresh Maharjan + * @param role + * @return + */ public Employee setRole(Role role) { this.role = role; return this; } + /** + * @author Naresh Maharjan + * @return + */ public int getUserId() { return this.userId; } + /** + * @author Naresh Maharjan + * @param userId + * @return + */ public Employee setUserId(int userId) { this.userId = userId; return this; } + /** + * @author Naresh Maharjan + * @return + */ public String[] toArray() { String[] emp = new String[5]; emp[0] = String.valueOf(this.getUserId()); diff --git a/naresh/src/main/java/com/lftechnology/phpjava/ems/enums/Role.java b/naresh/src/main/java/com/lftechnology/phpjava/ems/enums/Role.java index b0dd105..e1922cd 100644 --- a/naresh/src/main/java/com/lftechnology/phpjava/ems/enums/Role.java +++ b/naresh/src/main/java/com/lftechnology/phpjava/ems/enums/Role.java @@ -10,10 +10,18 @@ public enum Role { ADMIN("admin"), USER("user"); private final String role; + /** + * @author Naresh Maharjan + * @param role + */ private Role(String role) { this.role = role; } + /** + * @author Naresh Maharjan + * @return + */ public String getRole() { return this.role; } diff --git a/naresh/src/main/java/com/lftechnology/phpjava/ems/services/EmployeeService.java b/naresh/src/main/java/com/lftechnology/phpjava/ems/services/EmployeeService.java index 4b34805..64d8819 100644 --- a/naresh/src/main/java/com/lftechnology/phpjava/ems/services/EmployeeService.java +++ b/naresh/src/main/java/com/lftechnology/phpjava/ems/services/EmployeeService.java @@ -1,5 +1,6 @@ package com.lftechnology.phpjava.ems.services; +import com.lftechnology.phpjava.ems.constants.Constant; import com.lftechnology.phpjava.ems.dao.EmployeeDaoImpl; import com.lftechnology.phpjava.ems.dao.UserDaoImpl; import com.lftechnology.phpjava.ems.entities.Employee; @@ -128,11 +129,11 @@ private Set getUserIds(Employee employee) throws SQLException { public String[] getColumns() { String[] columnNames = new String[5]; - columnNames[0] = "User ID"; - columnNames[1] = "Full Name"; - columnNames[2] = "Address"; - columnNames[3] = "Role(User Type)"; - columnNames[4] = "Department"; + columnNames[0] = Constant.USER_ID; + columnNames[1] = Constant.FULL_NAME; + columnNames[2] = Constant.ADDRESS; + columnNames[3] = Constant.ROLE_USER_TYPE; + columnNames[4] = Constant.DEPARTMENT; return columnNames; } } diff --git a/naresh/src/main/java/com/lftechnology/phpjava/ems/services/UserService.java b/naresh/src/main/java/com/lftechnology/phpjava/ems/services/UserService.java index 37ca8fb..0dae574 100644 --- a/naresh/src/main/java/com/lftechnology/phpjava/ems/services/UserService.java +++ b/naresh/src/main/java/com/lftechnology/phpjava/ems/services/UserService.java @@ -5,7 +5,6 @@ import com.lftechnology.phpjava.ems.entities.Employee; import com.lftechnology.phpjava.ems.entities.User; -import java.sql.SQLException; import java.util.List; /** @@ -21,34 +20,51 @@ public class UserService { private String loggedInUserRole = ""; private int userId; - public void login(User user) { - try { - User result = userDao.findByUsernamePassword(user); - if (user.getUsername().equals(result.getUsername())) { - this.isLoggedIn = true; - this.userId = result.getId(); - EmployeeDaoImpl employeeDao = new EmployeeDaoImpl(); - List employees = employeeDao.findByUserId(this.userId); - Employee employee = employees.get(0); - this.loggedInUserRole = employee.getRole().toString(); - } - } catch (SQLException e) { - e.printStackTrace(); + /** + * @author Naresh Maharjan + * @param user + * @throws Exception + */ + public void login(User user) throws Exception { + User result = userDao.findByUsernamePassword(user); + if (user.getUsername().equals(result.getUsername())) { + this.isLoggedIn = true; + this.userId = result.getId(); + EmployeeDaoImpl employeeDao = new EmployeeDaoImpl(); + List employees = employeeDao.findByUserId(this.userId); + Employee employee = employees.get(0); + this.loggedInUserRole = employee.getRole().toString(); } + } + /** + * @author Naresh Maharjan + * @return + */ public boolean isUserLoggedIn() { return this.isLoggedIn; } + /** + * @author Naresh Maharjan + * @return + */ public String getLoggedInUserRole() { return this.loggedInUserRole; } + /** + * @author Naresh Maharjan + * @return + */ public int getUserId() { return this.userId; } + /** + * @author Naresh Maharjan + */ public void logout() { this.isLoggedIn = false; } diff --git a/naresh/src/main/java/com/lftechnology/phpjava/ems/utlis/AbstractDbAdapater.java b/naresh/src/main/java/com/lftechnology/phpjava/ems/utlis/AbstractDbAdapater.java deleted file mode 100644 index d410e04..0000000 --- a/naresh/src/main/java/com/lftechnology/phpjava/ems/utlis/AbstractDbAdapater.java +++ /dev/null @@ -1,18 +0,0 @@ -package com.lftechnology.phpjava.ems.utlis; - -import java.util.Map; - -/** - * AbstractDbAdapater - * - * @author Naresh Maharjan - * @since August, 09 2016 - */ -public abstract class AbstractDbAdapater { - protected String query = ""; - - protected AbstractDbAdapater where(Map condition) { - condition.entrySet(); - return this; - } -} diff --git a/naresh/src/main/java/com/lftechnology/phpjava/ems/utlis/CommonUtility.java b/naresh/src/main/java/com/lftechnology/phpjava/ems/utlis/CommonUtility.java index 9e14c61..d7c135d 100644 --- a/naresh/src/main/java/com/lftechnology/phpjava/ems/utlis/CommonUtility.java +++ b/naresh/src/main/java/com/lftechnology/phpjava/ems/utlis/CommonUtility.java @@ -12,22 +12,42 @@ public class CommonUtility { public UserService userService = new UserService(); + /** + * @author Naresh Maharjan + * @return + */ public UserService getUserService() { return userService; } + /** + * @author Naresh Maharjan + * @param userService + */ public void setUserService(UserService userService) { this.userService = userService; } + /** + * @author Naresh Maharjan + * @return + */ public boolean isLoggedIn() { return this.userService.isUserLoggedIn(); } + /** + * @author Naresh Maharjan + * @return + */ public boolean isAdmin() { return this.getUserService().getLoggedInUserRole().equals(Role.ADMIN.toString()); } + /** + * @author Naresh Maharjan + * @return + */ public boolean isUser() { return this.getUserService().getLoggedInUserRole().equals(Role.USER.toString()); } diff --git a/naresh/src/main/java/com/lftechnology/phpjava/ems/utlis/ConsoleWriter.java b/naresh/src/main/java/com/lftechnology/phpjava/ems/utlis/ConsoleWriter.java index f8b7f88..8e4b6dc 100644 --- a/naresh/src/main/java/com/lftechnology/phpjava/ems/utlis/ConsoleWriter.java +++ b/naresh/src/main/java/com/lftechnology/phpjava/ems/utlis/ConsoleWriter.java @@ -9,6 +9,11 @@ * @since August, 10 2016 */ public class ConsoleWriter { + + /** + * @author Naresh Maharjan + * @param limit + */ public static void writeBlankLine(int limit) { int counter = 0; while (counter < limit) { @@ -17,23 +22,11 @@ public static void writeBlankLine(int limit) { } } + /** + * @author Naresh Maharjan + * @param message + */ public static void writeUserInputRequestMessage(String message) { System.out.print("\t\t\t\t\t\t\t\t " + message + " \t"); } - - public static void clearConsole() - - { - final String operatingSystem = System.getProperty("os.name"); - - try { - if (operatingSystem.contains("Windows")) { - Runtime.getRuntime().exec("cls"); - } else { - Runtime.getRuntime().exec("clear"); - } - } catch (IOException e) { - e.printStackTrace(); - } - } } diff --git a/naresh/src/main/java/com/lftechnology/phpjava/ems/utlis/DbFactory.java b/naresh/src/main/java/com/lftechnology/phpjava/ems/utlis/DbFactory.java index 0c2c81f..c56fe92 100644 --- a/naresh/src/main/java/com/lftechnology/phpjava/ems/utlis/DbFactory.java +++ b/naresh/src/main/java/com/lftechnology/phpjava/ems/utlis/DbFactory.java @@ -73,19 +73,4 @@ public static Connection getConnection() { return connection; } - /** - * - */ - public static void closeConnection() { - -// try { -// -// if (connection != null) { -// connection.close(); -// } -// } -// catch (SQLException e) { -// e.printStackTrace(); -// } - } } diff --git a/naresh/src/main/java/com/lftechnology/phpjava/ems/utlis/PasswordHashGenerator.java b/naresh/src/main/java/com/lftechnology/phpjava/ems/utlis/PasswordHashGenerator.java index df6f03a..8ab89ab 100644 --- a/naresh/src/main/java/com/lftechnology/phpjava/ems/utlis/PasswordHashGenerator.java +++ b/naresh/src/main/java/com/lftechnology/phpjava/ems/utlis/PasswordHashGenerator.java @@ -7,12 +7,6 @@ import java.security.SecureRandom; import java.util.Base64; -/** - * PasswordHashGenerator - * - * @author Naresh Maharjan - * @since August, 09 2016 - */ public class PasswordHashGenerator { // The higher the number of iterations the more // expensive computing the hash is for us and diff --git a/naresh/src/main/java/com/lftechnology/phpjava/ems/utlis/Router.java b/naresh/src/main/java/com/lftechnology/phpjava/ems/utlis/Router.java index f073978..aa9a646 100644 --- a/naresh/src/main/java/com/lftechnology/phpjava/ems/utlis/Router.java +++ b/naresh/src/main/java/com/lftechnology/phpjava/ems/utlis/Router.java @@ -58,27 +58,36 @@ public static void showWelcomeScreen() { CommonViewUtility.showWelcomeScreen(); } + /** + * @author Naresh Maharjan + */ public static void showLoginMenuAndLoginToSystem() { if (!commonUtility.getUserService().isUserLoggedIn()) { showWelcomeScreen(); loginController.login(); - commonUtility = (CommonUtility) loginController.getData().get("commonUtility"); + commonUtility = (CommonUtility) loginController.getData().get(Constant.COMMON_UTILITY); } } + /** + * @author Naresh Maharjan + */ public static void showMenu() { showLoginMenuAndLoginToSystem(); if (!commonUtility.isLoggedIn()) { - CommonViewUtility.showMessageAndContinue("Invalid Credentails"); + CommonViewUtility.showMessageAndContinue(Constant.INVALID_CREDENTAILS); } else { Map commonUtilityMap = new HashMap<>(); - commonUtilityMap.put("commonUtility", commonUtility); + commonUtilityMap.put(Constant.COMMON_UTILITY, commonUtility); employeeController.setData(commonUtilityMap); employeeController.postLoginScreen(); } } - + /** + * @param menu + * @author Naresh Maharjan + */ public static void initiateInitialMenuAction(int menu) { if (commonUtility.isAdmin() && menu == Constant.ADD_NEW_USER) { diff --git a/naresh/src/main/java/com/lftechnology/phpjava/ems/utlis/SqlRunner.java b/naresh/src/main/java/com/lftechnology/phpjava/ems/utlis/SqlRunner.java index 82e8a5d..2e19744 100644 --- a/naresh/src/main/java/com/lftechnology/phpjava/ems/utlis/SqlRunner.java +++ b/naresh/src/main/java/com/lftechnology/phpjava/ems/utlis/SqlRunner.java @@ -26,6 +26,10 @@ public class SqlRunner { private List files; private List sql = new ArrayList<>(); + /** + * @author Naresh Maharjan + * @return + */ public List getSqlFileList() { File folder = new File(basePath); File[] listOfFiles = folder.listFiles(); @@ -40,6 +44,10 @@ public List getSqlFileList() { return sqlFiles; } + /** + * @author Naresh Maharjan + * @return + */ public List getSql() { this.files = this.getSqlFileList(); List executedFilesList = this.getExecutedFileList(); @@ -52,6 +60,10 @@ public List getSql() { return this.sql; } + /** + * @author Naresh Maharjan + * @param file + */ private void getFileContent(String file) { try { try (BufferedReader br = new BufferedReader(new FileReader(basePath + file))) { @@ -66,6 +78,10 @@ private void getFileContent(String file) { } } + /** + * @author Naresh Maharjan + * @return + */ private List getExecutedFileList() { List executedFiles = new ArrayList<>(); try { @@ -83,7 +99,9 @@ private List getExecutedFileList() { return executedFiles; } - + /** + * @author Naresh Maharjan + */ public void executeSql() { this.getSql(); Connection connection = DbFactory.getConnection(); @@ -104,11 +122,12 @@ public void executeSql() { } } catch (SQLException e) { e.printStackTrace(); - } finally { - DbFactory.closeConnection(); } } + /** + * @author Naresh Maharjan + */ private void checkAndCreateExecutedListFile() { File f = new File(basePath + "executedFiles.txt"); try { diff --git a/naresh/src/main/java/com/lftechnology/phpjava/ems/utlis/UserInput.java b/naresh/src/main/java/com/lftechnology/phpjava/ems/utlis/UserInput.java index f6a432d..4a817af 100644 --- a/naresh/src/main/java/com/lftechnology/phpjava/ems/utlis/UserInput.java +++ b/naresh/src/main/java/com/lftechnology/phpjava/ems/utlis/UserInput.java @@ -1,5 +1,7 @@ package com.lftechnology.phpjava.ems.utlis; +import com.lftechnology.phpjava.ems.constants.Constant; + import java.util.Scanner; /** @@ -33,7 +35,7 @@ public static int getIntegerUserInput() { Scanner scanner = new Scanner(System.in); String userInput = scanUserInput(scanner); while (!isInteger(userInput)) { - ConsoleWriter.writeUserInputRequestMessage("Invalid option. Please enter again"); + ConsoleWriter.writeUserInputRequestMessage(Constant.INVALID_OPTION_PLEASE_ENTER_AGAIN); ConsoleWriter.writeBlankLine(5); userInput = scanUserInput(scanner); } @@ -41,6 +43,11 @@ public static int getIntegerUserInput() { return result; } + /** + * @author Naresh Maharjan + * @param scanner + * @return + */ private static String scanUserInput(Scanner scanner) { String userInput = ""; while (true) { @@ -58,6 +65,11 @@ private static String scanUserInput(Scanner scanner) { return userInput; } + /** + * @author Naresh Maharjan + * @param self + * @return + */ public static boolean isInteger(String self) { try { Integer.valueOf(self.trim()); diff --git a/naresh/src/main/java/com/lftechnology/phpjava/ems/views/AbstractBaseView.java b/naresh/src/main/java/com/lftechnology/phpjava/ems/views/AbstractBaseView.java deleted file mode 100644 index 47c8283..0000000 --- a/naresh/src/main/java/com/lftechnology/phpjava/ems/views/AbstractBaseView.java +++ /dev/null @@ -1,25 +0,0 @@ -package com.lftechnology.phpjava.ems.views; - -/** - * AbstractBaseView - * - * @author Naresh Maharjan - * @since August, 15 2016 - */ -public abstract class AbstractBaseView implements ViewSignature { - - protected String action = ""; - - public abstract void render(); - - @Override - public String getAction() { - return this.action; - } - - @Override - public void setAction(String action) { - this.action = action; - } - -} diff --git a/naresh/src/main/java/com/lftechnology/phpjava/ems/views/CommonViewUtility.java b/naresh/src/main/java/com/lftechnology/phpjava/ems/views/CommonViewUtility.java index 9a71385..b0d10f9 100644 --- a/naresh/src/main/java/com/lftechnology/phpjava/ems/views/CommonViewUtility.java +++ b/naresh/src/main/java/com/lftechnology/phpjava/ems/views/CommonViewUtility.java @@ -1,5 +1,6 @@ package com.lftechnology.phpjava.ems.views; +import com.lftechnology.phpjava.ems.constants.Constant; import com.lftechnology.phpjava.ems.utlis.ConsoleWriter; import com.lftechnology.phpjava.ems.utlis.Router; import com.lftechnology.phpjava.ems.utlis.UserInput; @@ -19,28 +20,38 @@ public class CommonViewUtility { */ public static void showWelcomeScreen() { ConsoleWriter.writeBlankLine(3); - ConsoleWriter.writeUserInputRequestMessage("Welcome To Employee Management System !!!"); + ConsoleWriter.writeUserInputRequestMessage(Constant.WELCOME_TO_EMPLOYEE_MANAGEMENT_SYSTEM); ConsoleWriter.writeBlankLine(3); - ConsoleWriter.writeUserInputRequestMessage("Enter your credentails to log into the system"); + ConsoleWriter.writeUserInputRequestMessage(Constant.ENTER_YOUR_CREDENTAILS_TO_LOG_INTO_THE_SYSTEM); } + + /** + * @author Naresh Maharjan + */ public static void showExitMessageAndExit() { ConsoleWriter.writeBlankLine(100); - ConsoleWriter.writeUserInputRequestMessage("Bye, Felicia"); + ConsoleWriter.writeUserInputRequestMessage(Constant.BYE_MESSAGE); ConsoleWriter.writeBlankLine(5); System.exit(0); } + + /** + * + * @param message + * @author Naresh Maharjan + */ public static void showMessageAndContinue(String message) { ConsoleWriter.writeBlankLine(100); ConsoleWriter.writeUserInputRequestMessage(message); ConsoleWriter.writeBlankLine(3); - ConsoleWriter.writeUserInputRequestMessage("Do you want to continue?"); + ConsoleWriter.writeUserInputRequestMessage(Constant.DO_YOU_WANT_TO_CONTINUE); ConsoleWriter.writeBlankLine(3); - ConsoleWriter.writeUserInputRequestMessage("1. Yes"); + ConsoleWriter.writeUserInputRequestMessage(Constant.YES); ConsoleWriter.writeBlankLine(3); - ConsoleWriter.writeUserInputRequestMessage("2. No"); + ConsoleWriter.writeUserInputRequestMessage(Constant.NO); if (UserInput.getIntegerUserInput() == 1) { Router.showMenu(); } else { @@ -48,9 +59,12 @@ public static void showMessageAndContinue(String message) { } } + /** + * @author Naresh Maharjan + */ public static void pressKeyToContinue() { ConsoleWriter.writeBlankLine(3); - ConsoleWriter.writeUserInputRequestMessage("Press 1 to continue or any other number to exit"); + ConsoleWriter.writeUserInputRequestMessage(Constant.PRESS_1_TO_CONTINUE_OR_ANY_OTHER_NUMBER_TO_EXIT); if (UserInput.getIntegerUserInput() == 1) { Router.showMenu(); } else { diff --git a/naresh/src/main/java/com/lftechnology/phpjava/ems/views/EmployeeView.java b/naresh/src/main/java/com/lftechnology/phpjava/ems/views/EmployeeView.java index 98ba048..c2198fa 100644 --- a/naresh/src/main/java/com/lftechnology/phpjava/ems/views/EmployeeView.java +++ b/naresh/src/main/java/com/lftechnology/phpjava/ems/views/EmployeeView.java @@ -20,16 +20,18 @@ * @since August, 16 2016 */ public class EmployeeView implements ViewSignature { - - private static CommonUtility commonUtility = new CommonUtility(); + public static CommonUtility commonUtility = new CommonUtility(); private EmployeeController employeeController = new EmployeeController(); private String action; + /** + * @author Naresh Maharjan + */ @Override public void render() { switch (action) { case Constant.POST_LOGIN_ACTION: - if (this.commonUtility.isLoggedIn() && this.commonUtility.isAdmin()) { + if (commonUtility.isLoggedIn() && commonUtility.isAdmin()) { this.showAdminMenu(); } else { this.showNormalUserMenu(); @@ -58,118 +60,154 @@ public void render() { } } + /** + * @author Naresh Maharjan + */ @Override public String getAction() { return this.action; } + /** + * @author Naresh Maharjan + */ @Override public void setAction(String action) { this.action = action; } + /** + * @author Naresh Maharjan + */ @Override public Map getData() { return null; } + /** + * @author Naresh Maharjan + */ @Override public void setData(Map data) { - commonUtility = (CommonUtility) data.get("commonUtility"); + commonUtility = (CommonUtility) data.get(Constant.COMMON_UTILITY); } + /** + * @author Naresh Maharjan + */ public void showAdminMenu() { ConsoleWriter.writeBlankLine(100); - ConsoleWriter.writeUserInputRequestMessage("1. Add New User"); + ConsoleWriter.writeUserInputRequestMessage(Constant.ADD_NEW_USER_MESSAGE); ConsoleWriter.writeBlankLine(3); - ConsoleWriter.writeUserInputRequestMessage("2. Search User(s)"); + ConsoleWriter.writeUserInputRequestMessage(Constant.SEARCH_USERS); ConsoleWriter.writeBlankLine(3); - ConsoleWriter.writeUserInputRequestMessage("3. Terminate User"); + ConsoleWriter.writeUserInputRequestMessage(Constant.TERMINATE_USER_MESSAGE); ConsoleWriter.writeBlankLine(3); - ConsoleWriter.writeUserInputRequestMessage("4. Delete User"); + ConsoleWriter.writeUserInputRequestMessage(Constant.DELETE_USER_MESSAGE); ConsoleWriter.writeBlankLine(3); - ConsoleWriter.writeUserInputRequestMessage("0. Exit"); + ConsoleWriter.writeUserInputRequestMessage(Constant.EXIT_MESSAGE); ConsoleWriter.writeBlankLine(5); Router.initiateInitialMenuAction(UserInput.getIntegerUserInput()); } + /** + * @author Naresh Maharjan + */ public void showNormalUserMenu() { ConsoleWriter.writeBlankLine(100); - ConsoleWriter.writeUserInputRequestMessage("1. Update Profile"); + ConsoleWriter.writeUserInputRequestMessage(Constant.UPDATE_PROFILE_MESSAGE); ConsoleWriter.writeBlankLine(3); - ConsoleWriter.writeUserInputRequestMessage("2. Search User(s)"); + ConsoleWriter.writeUserInputRequestMessage(Constant.SEARCH_USERS); ConsoleWriter.writeBlankLine(3); - ConsoleWriter.writeUserInputRequestMessage("0. Exit"); + ConsoleWriter.writeUserInputRequestMessage(Constant.EXIT_MESSAGE); ConsoleWriter.writeBlankLine(5); Router.initiateInitialMenuAction(UserInput.getIntegerUserInput()); } + /** + * @author Naresh Maharjan + */ public void showAddUserMenu() { ConsoleWriter.writeBlankLine(100); - ConsoleWriter.writeUserInputRequestMessage("Choose One Of The Following User Type"); + ConsoleWriter.writeUserInputRequestMessage(Constant.CHOOSE_ONE_OF_THE_FOLLOWING_USER_TYPE); ConsoleWriter.writeBlankLine(3); - ConsoleWriter.writeUserInputRequestMessage("1. Add Admin User"); + ConsoleWriter.writeUserInputRequestMessage(Constant.ADD_ADMIN_USER); ConsoleWriter.writeBlankLine(3); - ConsoleWriter.writeUserInputRequestMessage("2. Add Normal User"); + ConsoleWriter.writeUserInputRequestMessage(Constant.ADD_NORMAL_USER); ConsoleWriter.writeBlankLine(5); Employee employee = new Employee(); Map postData = new HashMap<>(); employee.setRole(UserInput.getIntegerUserInput() == 1 ? Role.ADMIN : Role.USER); + employee = showAddUserMenu(employee); + + postData.put(Constant.EMPLOYEE, employee); + employeeController.setData(postData); + + employeeController.makeCurrentRequestPost(); + employeeController.addUser(); + + } + + /** + * + * @author Naresh Maharjan + * @param employee + * @return + */ + private Employee showAddUserMenu(Employee employee) { ConsoleWriter.writeBlankLine(3); - ConsoleWriter.writeUserInputRequestMessage("Enter User Details"); + ConsoleWriter.writeUserInputRequestMessage(Constant.ENTER_USER_DETAILS); ConsoleWriter.writeBlankLine(3); - ConsoleWriter.writeUserInputRequestMessage("1. Enter Fullname"); + ConsoleWriter.writeUserInputRequestMessage(Constant.ENTER_FULLNAME); ConsoleWriter.writeBlankLine(5); employee.setFullname(UserInput.getStringUserInput()); ConsoleWriter.writeBlankLine(3); - ConsoleWriter.writeUserInputRequestMessage("2. Enter Department"); + ConsoleWriter.writeUserInputRequestMessage(Constant.ENTER_DEPARTMENT); ConsoleWriter.writeBlankLine(5); employee.setDepartment(UserInput.getStringUserInput()); ConsoleWriter.writeBlankLine(3); - ConsoleWriter.writeUserInputRequestMessage("3. Enter Address"); + ConsoleWriter.writeUserInputRequestMessage(Constant.ENTER_ADDRESS); ConsoleWriter.writeBlankLine(5); employee.setAddress(UserInput.getStringUserInput()); ConsoleWriter.writeBlankLine(3); - ConsoleWriter.writeUserInputRequestMessage("4. Enter Username"); + ConsoleWriter.writeUserInputRequestMessage(Constant.ENTER_USERNAME); ConsoleWriter.writeBlankLine(5); employee.setUsername(UserInput.getStringUserInput()); ConsoleWriter.writeBlankLine(3); - ConsoleWriter.writeUserInputRequestMessage("5. Enter Password"); + ConsoleWriter.writeUserInputRequestMessage(Constant.ENTER_PASSWORD); ConsoleWriter.writeBlankLine(5); employee.setPassword(UserInput.getStringUserInput()); - postData.put("employee", employee); - employeeController.setData(postData); - - employeeController.makeCurrentRequestPost(); - employeeController.addUser(); - + return employee; } + /** + * @author Naresh Maharjan + */ public void showSearchUserMenu() { ConsoleWriter.writeBlankLine(100); - ConsoleWriter.writeUserInputRequestMessage("Choose One Of The Following Search Criteria"); + ConsoleWriter.writeUserInputRequestMessage(Constant.CHOOSE_ONE_OF_THE_FOLLOWING_SEARCH_CRITERIA); ConsoleWriter.writeBlankLine(3); - ConsoleWriter.writeUserInputRequestMessage("1. Search By Fullname"); + ConsoleWriter.writeUserInputRequestMessage(Constant.SEARCH_BY_FULLNAME); ConsoleWriter.writeBlankLine(3); - ConsoleWriter.writeUserInputRequestMessage("2. Search By Department"); + ConsoleWriter.writeUserInputRequestMessage(Constant.SEARCH_BY_DEPARTMENT); ConsoleWriter.writeBlankLine(3); - ConsoleWriter.writeUserInputRequestMessage("3. Search By Address"); + ConsoleWriter.writeUserInputRequestMessage(Constant.SEARCH_BY_ADDRESS); ConsoleWriter.writeBlankLine(3); - ConsoleWriter.writeUserInputRequestMessage("4. Exit"); + ConsoleWriter.writeUserInputRequestMessage(Constant.EXIT_MESSAGE); ConsoleWriter.writeBlankLine(5); int userInput = UserInput.getIntegerUserInput(); ConsoleWriter.writeBlankLine(3); - ConsoleWriter.writeUserInputRequestMessage("Enter search value"); + ConsoleWriter.writeUserInputRequestMessage(Constant.ENTER_SEARCH_VALUE); String searchValue = UserInput.getStringUserInput(); Employee employee = new Employee(); @@ -183,7 +221,7 @@ public void showSearchUserMenu() { case 3: employee.setAddress(searchValue); break; - case 4: + case 0: CommonViewUtility.showExitMessageAndExit(); break; default: @@ -192,12 +230,18 @@ public void showSearchUserMenu() { } Map postData = new HashMap<>(); - postData.put("employee", employee); + postData.put(Constant.EMPLOYEE, employee); this.employeeController.setData(postData); this.employeeController.makeCurrentRequestPost(); this.employeeController.searchUser(); } + /** + * + * @author Naresh Maharjan + * @param columns + * @param employees + */ public void renderEmployeeList(String[] columns, List employees) { TableGenerator tableGenerator = new TableGenerator(columns); for (Employee employee : employees) { @@ -206,23 +250,25 @@ public void renderEmployeeList(String[] columns, List employees) { tableGenerator.renderTable(); } - + /** + * @author Naresh Maharjan + */ public void showUpdateUserMenu() { ConsoleWriter.writeBlankLine(100); - ConsoleWriter.writeUserInputRequestMessage("Update Profile"); + ConsoleWriter.writeUserInputRequestMessage(Constant.UPDATE_PROFILE_MENU); ConsoleWriter.writeBlankLine(3); - ConsoleWriter.writeUserInputRequestMessage("1. Update Fullname"); + ConsoleWriter.writeUserInputRequestMessage(Constant.UPDATE_FULLNAME); ConsoleWriter.writeBlankLine(3); - ConsoleWriter.writeUserInputRequestMessage("2. Update Department"); + ConsoleWriter.writeUserInputRequestMessage(Constant.UPDATE_DEPARTMENT); ConsoleWriter.writeBlankLine(3); - ConsoleWriter.writeUserInputRequestMessage("3. Update Address"); + ConsoleWriter.writeUserInputRequestMessage(Constant.UPDATE_ADDRESS); ConsoleWriter.writeBlankLine(3); - ConsoleWriter.writeUserInputRequestMessage("4. Update Username"); + ConsoleWriter.writeUserInputRequestMessage(Constant.UPDATE_USERNAME); ConsoleWriter.writeBlankLine(3); - ConsoleWriter.writeUserInputRequestMessage("5. Update Password"); + ConsoleWriter.writeUserInputRequestMessage(Constant.UPDATE_PASSWORD); ConsoleWriter.writeBlankLine(5); int userInput = UserInput.getIntegerUserInput(); - ConsoleWriter.writeUserInputRequestMessage("Enter value"); + ConsoleWriter.writeUserInputRequestMessage(Constant.ENTER_VALUE); String value = UserInput.getStringUserInput(); Employee employee = new Employee(); employee.setUserId(commonUtility.getUserService().getUserId()); @@ -250,56 +296,68 @@ public void showUpdateUserMenu() { break; } Map postData = new HashMap<>(); - postData.put("employee", employee); + postData.put(Constant.EMPLOYEE, employee); this.employeeController.setData(postData); this.employeeController.makeCurrentRequestPost(); this.employeeController.updateProfile(); } + /** + * @author Naresh Maharjan + */ public void showDeleteUserMenu() { ConsoleWriter.writeBlankLine(100); - ConsoleWriter.writeUserInputRequestMessage("Delete User"); + ConsoleWriter.writeUserInputRequestMessage(Constant.DELETE_USER_MENU); ConsoleWriter.writeBlankLine(3); - ConsoleWriter.writeUserInputRequestMessage("1. Enter Fullname of Fullname"); + ConsoleWriter.writeUserInputRequestMessage(Constant.ENTER_FULLNAME_OF_EMPLOYEE); ConsoleWriter.writeBlankLine(3); - ConsoleWriter.writeUserInputRequestMessage("2. Return to main menu"); + ConsoleWriter.writeUserInputRequestMessage(Constant.RETURN_TO_MAIN_MENU); ConsoleWriter.writeBlankLine(3); - ConsoleWriter.writeUserInputRequestMessage("0. Exit"); + ConsoleWriter.writeUserInputRequestMessage(Constant.EXIT_MESSAGE); ConsoleWriter.writeBlankLine(5); int userInput = UserInput.getIntegerUserInput(); Employee employee = new Employee(); employee = getTerminateAndDeleterUserInput(userInput, employee); Map postData = new HashMap<>(); - postData.put("employee", employee); + postData.put(Constant.EMPLOYEE, employee); this.employeeController.setData(postData); this.employeeController.makeCurrentRequestPost(); this.employeeController.deleteUser(); } + /** + * @author Naresh Maharjan + */ public void showTerminateUserMenu() { ConsoleWriter.writeBlankLine(100); - ConsoleWriter.writeUserInputRequestMessage("Terminate User"); + ConsoleWriter.writeUserInputRequestMessage(Constant.TERMINATE_USER_MENU); ConsoleWriter.writeBlankLine(3); - ConsoleWriter.writeUserInputRequestMessage("1. Enter Fullname Of User"); + ConsoleWriter.writeUserInputRequestMessage(Constant.ENTER_FULLNAME_OF_EMPLOYEE); ConsoleWriter.writeBlankLine(3); - ConsoleWriter.writeUserInputRequestMessage("2. Return to main menu"); + ConsoleWriter.writeUserInputRequestMessage(Constant.RETURN_TO_MAIN_MENU); ConsoleWriter.writeBlankLine(3); - ConsoleWriter.writeUserInputRequestMessage("0. Exit"); + ConsoleWriter.writeUserInputRequestMessage(Constant.EXIT_MESSAGE); ConsoleWriter.writeBlankLine(5); int userInput = UserInput.getIntegerUserInput(); Employee employee = new Employee(); employee = getTerminateAndDeleterUserInput(userInput, employee); Map postData = new HashMap<>(); - postData.put("employee", employee); + postData.put(Constant.EMPLOYEE, employee); this.employeeController.setData(postData); this.employeeController.makeCurrentRequestPost(); this.employeeController.terminateUser(); } + /** + * @author Naresh Maharjan + * @param userInput + * @param employee + * @return + */ private Employee getTerminateAndDeleterUserInput(int userInput, Employee employee) { switch (userInput) { case 1: - ConsoleWriter.writeUserInputRequestMessage("Enter value"); + ConsoleWriter.writeUserInputRequestMessage(Constant.ENTER_VALUE); String value = UserInput.getStringUserInput(); employee.setFullname(value); break; diff --git a/naresh/src/main/java/com/lftechnology/phpjava/ems/views/LoginView.java b/naresh/src/main/java/com/lftechnology/phpjava/ems/views/LoginView.java index 9cbd8d8..36e1fb3 100644 --- a/naresh/src/main/java/com/lftechnology/phpjava/ems/views/LoginView.java +++ b/naresh/src/main/java/com/lftechnology/phpjava/ems/views/LoginView.java @@ -1,5 +1,6 @@ package com.lftechnology.phpjava.ems.views; +import com.lftechnology.phpjava.ems.constants.Constant; import com.lftechnology.phpjava.ems.controllers.LoginController; import com.lftechnology.phpjava.ems.entities.User; import com.lftechnology.phpjava.ems.services.UserService; @@ -23,9 +24,12 @@ public class LoginView implements ViewSignature { private UserService userService = new UserService(); private String action; + /** + * @author Naresh Maharjan + */ @Override public void render() { - if (this.getAction().equals("login")) { + if (this.getAction().equals(Constant.LOGIN)) { this.login(); } else { this.logout(); @@ -33,41 +37,63 @@ public void render() { } + /** + * @author Naresh Maharjan + * @return + */ @Override public String getAction() { return this.action; } + /** + * @author Naresh Maharjan + * @param action + */ @Override public void setAction(String action) { this.action = action; } + /** + * @author Naresh Maharjan + * @return + */ @Override public Map getData() { Map data = new HashMap<>(); - data.put("commonUtility", this.commonUtility); + data.put(Constant.COMMON_UTILITY, this.commonUtility); return data; } + /** + * @author Naresh Maharjan + * @param data + */ @Override public void setData(Map data) { } + /** + * @author Naresh Maharjan + */ public void login() { Map postData = new HashMap<>(); ConsoleWriter.writeBlankLine(3); - ConsoleWriter.writeUserInputRequestMessage("Enter your username:"); + ConsoleWriter.writeUserInputRequestMessage(Constant.ENTER_YOUR_USERNAME_TO_LOGIN); this.user.setUsername(UserInput.getStringUserInput()); - ConsoleWriter.writeUserInputRequestMessage("Enter your password:"); + ConsoleWriter.writeUserInputRequestMessage(Constant.ENTER_YOUR_PASSWORD_TO_LOGIN); this.user.setPassword(UserInput.getStringUserInput()); - postData.put("user", this.user); + postData.put(Constant.USER, this.user); this.loginController.setData(postData); } + /** + * @author Naresh Maharjan + */ public void logout() { this.userService.logout(); } diff --git a/naresh/src/main/java/com/lftechnology/phpjava/ems/views/TableGenerator.java b/naresh/src/main/java/com/lftechnology/phpjava/ems/views/TableGenerator.java index 86279dc..b9ee17f 100644 --- a/naresh/src/main/java/com/lftechnology/phpjava/ems/views/TableGenerator.java +++ b/naresh/src/main/java/com/lftechnology/phpjava/ems/views/TableGenerator.java @@ -13,27 +13,41 @@ * @since August, 15 2016 */ public class TableGenerator { - private int numberOfColums; private V2_AsciiTable table = new V2_AsciiTable(); private V2_AsciiTableRenderer renderer = new V2_AsciiTableRenderer(); + /** + * @author Naresh Maharjan + * @param columns + */ public TableGenerator(String[] columns) { this.renderer.setTheme(V2_E_TableThemes.UTF_HEAVY.get()); this.renderer.setWidth(new WidthAbsoluteEven(160)); this.generateHeader(columns); } + /** + * @author Naresh Maharjan + */ public void renderTable() { RenderedTable rt = this.renderer.render(this.table); System.out.println(rt); } + /** + * @author Naresh Maharjan + * @param colums + */ private void generateHeader(String[] colums) { this.table.addRule(); this.table.addRow(colums); this.table.addRule(); } + /** + * @author Naresh Maharjan + * @param row + */ public void addRow(String[] row) { this.table.addRow(row); this.table.addRule(); diff --git a/naresh/src/main/java/com/lftechnology/phpjava/ems/views/ViewSignature.java b/naresh/src/main/java/com/lftechnology/phpjava/ems/views/ViewSignature.java index e789f0b..e5a076a 100644 --- a/naresh/src/main/java/com/lftechnology/phpjava/ems/views/ViewSignature.java +++ b/naresh/src/main/java/com/lftechnology/phpjava/ems/views/ViewSignature.java @@ -9,13 +9,32 @@ * @since August, 15 2016 */ public interface ViewSignature { - public void render(); + /** + * @author Naresh Maharjan + */ + void render(); - public String getAction(); + /** + * @author Naresh Maharjan + * @return + */ + String getAction(); - public void setAction(String action); + /** + * @author Naresh Maharjan + * @param action + */ + void setAction(String action); - public Map getData(); + /** + * @author Naresh Maharjan + * @return + */ + Map getData(); - public void setData(Map data); + /** + * @author Naresh Maharjan + * @param data + */ + void setData(Map data); } From f0174eeaf3b0ba0636fa1b128c5ca0b6fc2125a9 Mon Sep 17 00:00:00 2001 From: Naresh Maharjan Date: Wed, 17 Aug 2016 12:51:17 +0545 Subject: [PATCH 8/9] strings of employee controller converted to constants --- .../phpjava/ems/constants/Constant.java | 4 ++++ .../ems/controllers/EmployeeController.java | 18 +++++++++--------- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/naresh/src/main/java/com/lftechnology/phpjava/ems/constants/Constant.java b/naresh/src/main/java/com/lftechnology/phpjava/ems/constants/Constant.java index 4c5c666..c17226f 100644 --- a/naresh/src/main/java/com/lftechnology/phpjava/ems/constants/Constant.java +++ b/naresh/src/main/java/com/lftechnology/phpjava/ems/constants/Constant.java @@ -76,4 +76,8 @@ public class Constant { public static final String ADDRESS = "Address"; public static final String ROLE_USER_TYPE = "Role(User Type)"; public static final String DEPARTMENT = "Department"; + public static final String EMPLOYEE_ADDED_SUCCESSFULLY = "Employee added successfully"; + public static final String UNABLE_TO_ADD_EMPLOYEE = "Unable to add employee"; + public static final String DATA_UPDATED_SUCCESSFULLY = "Data updated successfully."; + public static final String UNABLE_TO_UPDATE_PROFILE = "Unable to update profile."; } diff --git a/naresh/src/main/java/com/lftechnology/phpjava/ems/controllers/EmployeeController.java b/naresh/src/main/java/com/lftechnology/phpjava/ems/controllers/EmployeeController.java index 43bbaa3..ae81512 100644 --- a/naresh/src/main/java/com/lftechnology/phpjava/ems/controllers/EmployeeController.java +++ b/naresh/src/main/java/com/lftechnology/phpjava/ems/controllers/EmployeeController.java @@ -38,7 +38,7 @@ public void postLoginScreen() { */ public void addUser() { if (this.isPost) { - Employee employee = (Employee) postData.get("employee"); + Employee employee = (Employee) postData.get(Constant.EMPLOYEE); int result = 0; try { result = employeeService.addEmployee(employee); @@ -47,9 +47,9 @@ public void addUser() { } String message; if (result > 0) { - message = "Employee added successfully"; + message = Constant.EMPLOYEE_ADDED_SUCCESSFULLY; } else { - message = "Unable to add employee"; + message = Constant.UNABLE_TO_ADD_EMPLOYEE; } postData.clear(); this.isPost = false; @@ -66,7 +66,7 @@ public void addUser() { */ public void searchUser() { if (this.isPost) { - Employee employee = (Employee) postData.get("employee"); + Employee employee = (Employee) postData.get(Constant.EMPLOYEE); List result = new ArrayList<>(); try { result = employeeService.searchEmployee(employee); @@ -88,7 +88,7 @@ public void searchUser() { */ public void terminateUser() { if (this.isPost()) { - Employee employee = (Employee) postData.get("employee"); + Employee employee = (Employee) postData.get(Constant.EMPLOYEE); int result = 0; try { result = employeeService.terminateUser(employee); @@ -111,7 +111,7 @@ public void terminateUser() { */ public void deleteUser() { if (this.isPost()) { - Employee employee = (Employee) postData.get("employee"); + Employee employee = (Employee) postData.get(Constant.EMPLOYEE); int result = 0; try { result = employeeService.deleteUser(employee); @@ -133,7 +133,7 @@ public void deleteUser() { */ public void updateProfile() { if (this.isPost) { - Employee employee = (Employee) postData.get("employee"); + Employee employee = (Employee) postData.get(Constant.EMPLOYEE); int result = 0; try { result = employeeService.updateEmployee(employee); @@ -144,9 +144,9 @@ public void updateProfile() { postData.clear(); String message; if (result > 0) { - message = "Data updated successfully."; + message = Constant.DATA_UPDATED_SUCCESSFULLY; } else { - message = "Unable to update profile."; + message = Constant.UNABLE_TO_UPDATE_PROFILE; } CommonViewUtility.showMessageAndContinue(message); } else { From fab3d7bb7f07730dd89087821dd861dff9bdd4b7 Mon Sep 17 00:00:00 2001 From: Naresh Maharjan Date: Wed, 17 Aug 2016 13:26:37 +0545 Subject: [PATCH 9/9] sql runner try catch statement updated --- .../phpjava/ems/utlis/SqlRunner.java | 81 ++++++++----------- 1 file changed, 32 insertions(+), 49 deletions(-) diff --git a/naresh/src/main/java/com/lftechnology/phpjava/ems/utlis/SqlRunner.java b/naresh/src/main/java/com/lftechnology/phpjava/ems/utlis/SqlRunner.java index 2e19744..9686375 100644 --- a/naresh/src/main/java/com/lftechnology/phpjava/ems/utlis/SqlRunner.java +++ b/naresh/src/main/java/com/lftechnology/phpjava/ems/utlis/SqlRunner.java @@ -27,8 +27,8 @@ public class SqlRunner { private List sql = new ArrayList<>(); /** - * @author Naresh Maharjan * @return + * @author Naresh Maharjan */ public List getSqlFileList() { File folder = new File(basePath); @@ -45,10 +45,10 @@ public List getSqlFileList() { } /** - * @author Naresh Maharjan * @return + * @author Naresh Maharjan */ - public List getSql() { + public List getSql() throws IOException { this.files = this.getSqlFileList(); List executedFilesList = this.getExecutedFileList(); for (String file : files) { @@ -61,81 +61,64 @@ public List getSql() { } /** - * @author Naresh Maharjan * @param file + * @author Naresh Maharjan */ - private void getFileContent(String file) { - try { - try (BufferedReader br = new BufferedReader(new FileReader(basePath + file))) { - String line = br.readLine(); - while (line != null) { - this.sql.add(line); - line = br.readLine(); - } + private void getFileContent(String file) throws IOException { + try (BufferedReader br = new BufferedReader(new FileReader(basePath + file))) { + String line = br.readLine(); + while (line != null) { + this.sql.add(line); + line = br.readLine(); } - } catch (IOException e) { - e.printStackTrace(); } } /** - * @author Naresh Maharjan * @return + * @author Naresh Maharjan */ - private List getExecutedFileList() { + private List getExecutedFileList() throws IOException { List executedFiles = new ArrayList<>(); - try { - this.checkAndCreateExecutedListFile(); - try (BufferedReader br = new BufferedReader(new FileReader(basePath + "executedFiles.txt"))) { - String line = br.readLine(); - while (line != null) { - executedFiles.add(line); - line = br.readLine(); - } + this.checkAndCreateExecutedListFile(); + try (BufferedReader br = new BufferedReader(new FileReader(basePath + "executedFiles.txt"))) { + String line = br.readLine(); + while (line != null) { + executedFiles.add(line); + line = br.readLine(); } - } catch (IOException e) { - e.printStackTrace(); } return executedFiles; } /** + * @throws SQLException + * @throws IOException * @author Naresh Maharjan */ - public void executeSql() { + public void executeSql() throws SQLException, IOException { this.getSql(); Connection connection = DbFactory.getConnection(); - try { - if (!this.sql.isEmpty()) { - for (String query : this.sql) { - if (!query.isEmpty()) { - PreparedStatement stmt = connection.prepareStatement(query.toString()); - stmt.execute(); - } - } - Path excludedFileList = Paths.get(basePath + "executedFiles.txt"); - try { - Files.write(excludedFileList, this.files, Charset.forName("UTF-8"), StandardOpenOption.APPEND); - } catch (IOException e) { - e.printStackTrace(); + if (!this.sql.isEmpty()) { + for (String query : this.sql) { + if (!query.isEmpty()) { + PreparedStatement stmt = connection.prepareStatement(query.toString()); + stmt.execute(); } } - } catch (SQLException e) { - e.printStackTrace(); + Path excludedFileList = Paths.get(basePath + "executedFiles.txt"); + Files.write(excludedFileList, this.files, Charset.forName("UTF-8"), StandardOpenOption.APPEND); } } /** * @author Naresh Maharjan */ - private void checkAndCreateExecutedListFile() { + private void checkAndCreateExecutedListFile() throws IOException { File f = new File(basePath + "executedFiles.txt"); - try { - if (!f.exists()) { - f.createNewFile(); - } - } catch (IOException e) { - e.printStackTrace(); + if (!f.exists()) { + f.createNewFile(); } + } }