diff --git a/.gitignore b/.gitignore index 09acd94..36e7ec2 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,7 @@ *.class # IntelliJ IDEA coniguration folder -.idea/* +*/.idea/* # module file created by IntelliJ IDEA *.iml @@ -21,3 +21,4 @@ hs_err_pid* *.classpath **/target/** /bin/ +executedFiles.txt diff --git a/naresh/pom.xml b/naresh/pom.xml index c06e24e..a1d7ca9 100644 --- a/naresh/pom.xml +++ b/naresh/pom.xml @@ -1,9 +1,23 @@ - - 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 + + + + 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..9ebefc9 --- /dev/null +++ b/naresh/src/main/java/com/lftechnology/phpjava/ems/EmployeeManagementSystem.java @@ -0,0 +1,20 @@ +package com.lftechnology.phpjava.ems; + +import com.lftechnology.phpjava.ems.utlis.Router; + +/** + * EmployeeManagementSystem + * + * @author Naresh Maharjan + * @since August, 12 2016 + */ +public class EmployeeManagementSystem { + + /** + * @author Naresh Maharjan + * @param args + */ + public static void main(String[] args) { + Router.showMenu(); + } +} 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..320aab2 --- /dev/null +++ b/naresh/src/main/java/com/lftechnology/phpjava/ems/autorun/inititalTables.sql @@ -0,0 +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)); +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/configs/db.properties b/naresh/src/main/java/com/lftechnology/phpjava/ems/configs/db.properties new file mode 100644 index 0000000..1026c39 --- /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..c17226f --- /dev/null +++ b/naresh/src/main/java/com/lftechnology/phpjava/ems/constants/Constant.java @@ -0,0 +1,83 @@ +package com.lftechnology.phpjava.ems.constants; + + +/** + * Constant + * + * @author Naresh Maharjan + * @since August, 09 2016 + */ +public class Constant { + 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 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"; + 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"; + 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"; + 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/ControllerSignature.java b/naresh/src/main/java/com/lftechnology/phpjava/ems/controllers/ControllerSignature.java new file mode 100644 index 0000000..c3906c0 --- /dev/null +++ b/naresh/src/main/java/com/lftechnology/phpjava/ems/controllers/ControllerSignature.java @@ -0,0 +1,29 @@ +package com.lftechnology.phpjava.ems.controllers; + +import java.util.Map; + +/** + * ControllerSignature + * + * @author Naresh Maharjan + * @since August, 16 2016 + */ +public interface ControllerSignature { + /** + * @author Naresh Maharjan + * @return + */ + Map getData(); + + /** + * @author Naresh Maharjan + * @param data + */ + void setData(Map data); + + /** + * @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 new file mode 100644 index 0000000..ae81512 --- /dev/null +++ b/naresh/src/main/java/com/lftechnology/phpjava/ems/controllers/EmployeeController.java @@ -0,0 +1,191 @@ +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 implements ControllerSignature { + private static EmployeeService employeeService = new EmployeeService(); + private static EmployeeView employeeView = new EmployeeView(); + 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(Constant.EMPLOYEE); + int result = 0; + try { + result = employeeService.addEmployee(employee); + } catch (Exception e) { + e.printStackTrace(); + } + String message; + if (result > 0) { + message = Constant.EMPLOYEE_ADDED_SUCCESSFULLY; + } else { + message = Constant.UNABLE_TO_ADD_EMPLOYEE; + } + postData.clear(); + this.isPost = false; + CommonViewUtility.showMessageAndContinue(message); + } else { + employeeView.setAction(Constant.ADD_USER_ACTION); + employeeView.render(); + } + + } + + /** + * @author Naresh Maharjan + */ + public void searchUser() { + if (this.isPost) { + Employee employee = (Employee) postData.get(Constant.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(); + } + } + + /** + * @author Naresh Maharjan + */ + public void terminateUser() { + if (this.isPost()) { + Employee employee = (Employee) postData.get(Constant.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(); + } + + } + + /** + * @author Naresh Maharjan + */ + public void deleteUser() { + if (this.isPost()) { + Employee employee = (Employee) postData.get(Constant.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(); + } + } + + /** + * @author Naresh Maharjan + */ + public void updateProfile() { + if (this.isPost) { + Employee employee = (Employee) postData.get(Constant.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 = Constant.DATA_UPDATED_SUCCESSFULLY; + } else { + message = Constant.UNABLE_TO_UPDATE_PROFILE; + } + CommonViewUtility.showMessageAndContinue(message); + } else { + employeeView.setAction(Constant.UPDATE_PROFILE_ACTION); + employeeView.render(); + } + } + + /** + * @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 new file mode 100644 index 0000000..9644d54 --- /dev/null +++ b/naresh/src/main/java/com/lftechnology/phpjava/ems/controllers/LoginController.java @@ -0,0 +1,58 @@ +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; +import com.lftechnology.phpjava.ems.views.LoginView; + +import java.util.HashMap; +import java.util.Map; + +/** + * LoginController + * + * @author Naresh Maharjan + * @since August, 08 2016 + */ +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(); + + public void login() { + loginView.setAction(Constant.LOGIN); + loginView.render(); + + User user = (User) this.postData.get(Constant.USER); + try { + this.userService.login(user); + } catch (Exception e) { + e.printStackTrace(); + } + this.commonUtility.setUserService(this.userService); + } + + public void logout() { + loginView.logout(); + } + + @Override + public Map getData() { + Map data = new HashMap<>(); + data.put(Constant.COMMON_UTILITY, this.commonUtility); + 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 new file mode 100644 index 0000000..795fe0f --- /dev/null +++ b/naresh/src/main/java/com/lftechnology/phpjava/ems/dao/DaoSignature.java @@ -0,0 +1,46 @@ +package com.lftechnology.phpjava.ems.dao; + +import java.sql.SQLException; +import java.util.List; + +/** + * DaoSignature + * + * @author Naresh Maharjan + * @since August, 08 2016 + */ +public interface DaoSignature { + + /** + * @return + * @throws SQLException + * @author Naresh Maharjan + */ + List findAll() throws SQLException; + + + /** + * @param s + * @return + * @throws SQLException + * @author Naresh Maharjan + */ + int insert(T t) throws SQLException; + + /** + * @param s + * @return + * @throws SQLException + * @author Naresh Maharjan + */ + int update(T t) throws SQLException; + + + /** + * @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/EmployeeDaoImpl.java b/naresh/src/main/java/com/lftechnology/phpjava/ems/dao/EmployeeDaoImpl.java new file mode 100644 index 0000000..9c06b18 --- /dev/null +++ b/naresh/src/main/java/com/lftechnology/phpjava/ems/dao/EmployeeDaoImpl.java @@ -0,0 +1,236 @@ +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.ArrayList; +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; + + /** + * @author Naresh Maharjan + * @return + * @throws SQLException + */ + @Override + public List findAll() throws SQLException { + 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; + } + + /** + * @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 ( ?, ? , ?, ?,?);"; + 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(); + } + + /** + * @author Naresh Maharjan + * @param employee + * @return + * @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; + } + + /** + * @author Naresh Maharjan + * @param employee + * @return + * @throws SQLException + */ + @Override + public int delete(Employee employee) throws SQLException { + return 0; + } + + + /** + * Find the employee by fullname + * + * @param fullName + * @return Employee + * @throws SQLException + * @author Naresh Maharjan + */ + public List findByFullName(String fullName) throws SQLException { + + String sql = "SELECT * FROM employee WHERE fullname = ?"; + + stmt = conn.prepareStatement(sql); + stmt.setString(1, fullName); + return findBy(stmt); + } + + /** + * Find the employee by address + * + * @param address + * @return Employee + * @throws SQLException + * @author Naresh Maharjan + */ + public List findByAddress(String address) throws SQLException { + + String sql = "SELECT * FROM employee WHERE address = ?"; + + stmt = conn.prepareStatement(sql); + stmt.setString(1, address); + return findBy(stmt); + } + + /** + * Find the employee by department + * + * @param department + * @return Employee + * @throws SQLException + */ + + public List findByDepartment(String department) throws SQLException { + + String sql = "SELECT * FROM employee WHERE department = ?"; + + stmt = conn.prepareStatement(sql); + stmt.setString(1, department); + return findBy(stmt); + } + + /** + * Find the employee by role + * + * @param + * @return Employee + * @throws SQLException + * @author Naresh Maharjan + */ + + public List findByRole(Role role) throws SQLException { + + String sql = "SELECT * FROM employee WHERE role = ?"; + + 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 + */ + + 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 List findBy(PreparedStatement stmt) throws SQLException { + ResultSet resultSet = stmt.executeQuery(); + List employees = setEmployeeData(resultSet); + return employees; + } + + + /** + * set employee data from the resultset + * + * @param resultSet + * @return Employee + * @author Naresh Maharjan + */ + 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 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 new file mode 100644 index 0000000..fbd4e45 --- /dev/null +++ b/naresh/src/main/java/com/lftechnology/phpjava/ems/dao/UserDaoImpl.java @@ -0,0 +1,207 @@ +package com.lftechnology.phpjava.ems.dao; + +import com.lftechnology.phpjava.ems.entities.User; +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.Set; + +/** + * UserDaoImpl + * + * @author Naresh Maharjan + * @since August, 08 2016 + */ +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; + String sql = "INSERT INTO user (username, password, is_terminated) VALUES ( ?, ? , 0);"; + stmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS); + stmt.setString(1, user.getUsername()); + stmt.setString(2, user.getPassword()); + stmt.executeUpdate(); + ResultSet rs = stmt.getGeneratedKeys(); + if (rs.next()) { + lastInsertedId = rs.getInt(1); + } + return lastInsertedId; + } + + /** + * @param user + * @return + * @throws SQLException + * @author Naresh Maharjan + */ + @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; + } + + /** + * @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())); + stmt = conn.prepareStatement(sqlNew); + this.setValues(stmt, userIds.toArray()); + 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())); + stmt = conn.prepareStatement(sqlNew); + this.setValues(stmt, userIds.toArray()); + return stmt.executeUpdate(); + } + + /** + * Find the employee by role + * + * @param + * @return User + * @throws Exception + * @author Naresh Maharjan + */ + 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); + 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 + * @throws SQLException + */ + private User findBy(String sql, String[] bindValues) throws SQLException { + User user = new User(); + 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 + * @throws SQLException + */ + private User setUserData(ResultSet resultSet) throws SQLException { + User user = new User(); + 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 new file mode 100644 index 0000000..ee1ff42 --- /dev/null +++ b/naresh/src/main/java/com/lftechnology/phpjava/ems/entities/Employee.java @@ -0,0 +1,147 @@ +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; + 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; + this.address = address; + this.department = department; + this.role = role; + 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()); + 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 new file mode 100644 index 0000000..8cc30f1 --- /dev/null +++ b/naresh/src/main/java/com/lftechnology/phpjava/ems/entities/User.java @@ -0,0 +1,61 @@ +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 this.id; + } + + public User setId(int id) { + this.id = id; + return this; + } + + 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..e1922cd --- /dev/null +++ b/naresh/src/main/java/com/lftechnology/phpjava/ems/enums/Role.java @@ -0,0 +1,29 @@ +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; + + /** + * @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 new file mode 100644 index 0000000..64d8819 --- /dev/null +++ b/naresh/src/main/java/com/lftechnology/phpjava/ems/services/EmployeeService.java @@ -0,0 +1,139 @@ +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; +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; +import java.util.ArrayList; +import java.util.List; +import java.util.Set; +import java.util.stream.Collectors; + +/** + * 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() throws Exception { + if (!this.checkAdminUserExists()) { + Employee employee = new Employee(); + employee.setFullname("Admin User") + .setAddress("Somewhere") + .setRole(Role.ADMIN) + .setDepartment("Admin") + .setUsername("admin") + .setTerminated(false); + employee.setPassword(PasswordHashGenerator.getSaltedHash("password")); + insertUserAndEmployee(employee); + + } + } + + /** + * check if admin user exists or not + * + * @return boolean + * @author Naresh Maharjan + */ + public boolean checkAdminUserExists() throws SQLException { + EmployeeDaoImpl employeeDao = new EmployeeDaoImpl(); + 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] = 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 new file mode 100644 index 0000000..0dae574 --- /dev/null +++ b/naresh/src/main/java/com/lftechnology/phpjava/ems/services/UserService.java @@ -0,0 +1,71 @@ +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.util.List; + +/** + * UserService + * + * @author Naresh Maharjan + * @since August, 09 2016 + */ +public class UserService { + + UserDaoImpl userDao = new UserDaoImpl(); + private boolean isLoggedIn = false; + private String loggedInUserRole = ""; + private int userId; + + /** + * @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/CommonUtility.java b/naresh/src/main/java/com/lftechnology/phpjava/ems/utlis/CommonUtility.java new file mode 100644 index 0000000..d7c135d --- /dev/null +++ b/naresh/src/main/java/com/lftechnology/phpjava/ems/utlis/CommonUtility.java @@ -0,0 +1,54 @@ +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(); + + /** + * @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 new file mode 100644 index 0000000..8e4b6dc --- /dev/null +++ b/naresh/src/main/java/com/lftechnology/phpjava/ems/utlis/ConsoleWriter.java @@ -0,0 +1,32 @@ +package com.lftechnology.phpjava.ems.utlis; + +import java.io.IOException; + +/** + * ConsoleWriter + * + * @author Naresh Maharjan + * @since August, 10 2016 + */ +public class ConsoleWriter { + + /** + * @author Naresh Maharjan + * @param limit + */ + public static void writeBlankLine(int limit) { + int counter = 0; + while (counter < limit) { + System.out.println(); + counter++; + } + } + + /** + * @author Naresh Maharjan + * @param message + */ + 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..c56fe92 --- /dev/null +++ b/naresh/src/main/java/com/lftechnology/phpjava/ems/utlis/DbFactory.java @@ -0,0 +1,76 @@ +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 + * + * @return + * @throws IOException + * @author Naresh Maharjan + */ + private static void loadProperties() { + + InputStream input = null; + + try { + String filePath = "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 + * + * @return + * @author Naresh Maharjan + */ + 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; + } + +} 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..8ab89ab --- /dev/null +++ b/naresh/src/main/java/com/lftechnology/phpjava/ems/utlis/PasswordHashGenerator.java @@ -0,0 +1,54 @@ +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; + +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..aa9a646 --- /dev/null +++ b/naresh/src/main/java/com/lftechnology/phpjava/ems/utlis/Router.java @@ -0,0 +1,112 @@ +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.services.EmployeeService; +import com.lftechnology.phpjava.ems.views.CommonViewUtility; + +import java.sql.SQLException; +import java.util.HashMap; +import java.util.Map; + +/** + * RouterService + * + * @author Naresh Maharjan + * @since August, 09 2016 + */ +public class Router { + + private static LoginController loginController = new LoginController(); + private static EmployeeController employeeController = new EmployeeController(); + private static CommonUtility commonUtility = new CommonUtility(); + + static { + try { + onRouteBootstrap(); + } catch (SQLException e) { + e.printStackTrace(); + } catch (Exception e) { + e.printStackTrace(); + } + } + + /** + * 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 + */ + public static void onRouteBootstrap() throws Exception { + + SqlRunner sqlRunner = new SqlRunner(); + sqlRunner.executeSql(); + + EmployeeService employeeService = new EmployeeService(); + employeeService.checkAndCreateAdminUser(); + } + + /** + * Show Welcome Screen + * + * @author Naresh Maharjan + */ + 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(Constant.COMMON_UTILITY); + } + } + + /** + * @author Naresh Maharjan + */ + public static void showMenu() { + showLoginMenuAndLoginToSystem(); + if (!commonUtility.isLoggedIn()) { + CommonViewUtility.showMessageAndContinue(Constant.INVALID_CREDENTAILS); + } else { + Map commonUtilityMap = new HashMap<>(); + 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) { + 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(); + } + } + +} 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..9686375 --- /dev/null +++ b/naresh/src/main/java/com/lftechnology/phpjava/ems/utlis/SqlRunner.java @@ -0,0 +1,124 @@ +package com.lftechnology.phpjava.ems.utlis; + +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; +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<>(); + + /** + * @return + * @author Naresh Maharjan + */ + 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; + } + + /** + * @return + * @author Naresh Maharjan + */ + public List getSql() throws IOException { + this.files = this.getSqlFileList(); + List executedFilesList = this.getExecutedFileList(); + for (String file : files) { + if (!executedFilesList.contains(file)) { + getFileContent(file); + + } + } + return this.sql; + } + + /** + * @param file + * @author Naresh Maharjan + */ + 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(); + } + } + } + + /** + * @return + * @author Naresh Maharjan + */ + private List getExecutedFileList() throws IOException { + List executedFiles = new ArrayList<>(); + 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(); + } + } + return executedFiles; + } + + /** + * @throws SQLException + * @throws IOException + * @author Naresh Maharjan + */ + public void executeSql() throws SQLException, IOException { + this.getSql(); + Connection connection = DbFactory.getConnection(); + 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"); + Files.write(excludedFileList, this.files, Charset.forName("UTF-8"), StandardOpenOption.APPEND); + } + } + + /** + * @author Naresh Maharjan + */ + private void checkAndCreateExecutedListFile() throws IOException { + File f = new File(basePath + "executedFiles.txt"); + if (!f.exists()) { + f.createNewFile(); + } + + } +} 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..4a817af --- /dev/null +++ b/naresh/src/main/java/com/lftechnology/phpjava/ems/utlis/UserInput.java @@ -0,0 +1,81 @@ +package com.lftechnology.phpjava.ems.utlis; + +import com.lftechnology.phpjava.ems.constants.Constant; + +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 = scanUserInput(scanner); + return userInput; + } + + + /** + * Get int user input from the user + * + * @return int + * @author Naresh Maharjan + */ + public static int getIntegerUserInput() { + Scanner scanner = new Scanner(System.in); + String userInput = scanUserInput(scanner); + while (!isInteger(userInput)) { + ConsoleWriter.writeUserInputRequestMessage(Constant.INVALID_OPTION_PLEASE_ENTER_AGAIN); + ConsoleWriter.writeBlankLine(5); + userInput = scanUserInput(scanner); + } + int result = Integer.parseInt(userInput); + return result; + } + + /** + * @author Naresh Maharjan + * @param scanner + * @return + */ + private static String scanUserInput(Scanner scanner) { + String userInput = ""; + while (true) { + if (!scanner.hasNextLine()) { + scanner.nextLine(); + } else { + userInput = scanner.nextLine(); + if (userInput.isEmpty()) { + continue; + } else { + break; + } + } + } + return userInput; + } + + /** + * @author Naresh Maharjan + * @param self + * @return + */ + 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/CommonViewUtility.java b/naresh/src/main/java/com/lftechnology/phpjava/ems/views/CommonViewUtility.java new file mode 100644 index 0000000..b0d10f9 --- /dev/null +++ b/naresh/src/main/java/com/lftechnology/phpjava/ems/views/CommonViewUtility.java @@ -0,0 +1,74 @@ +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; + +/** + * 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(Constant.WELCOME_TO_EMPLOYEE_MANAGEMENT_SYSTEM); + + ConsoleWriter.writeBlankLine(3); + ConsoleWriter.writeUserInputRequestMessage(Constant.ENTER_YOUR_CREDENTAILS_TO_LOG_INTO_THE_SYSTEM); + } + + + /** + * @author Naresh Maharjan + */ + public static void showExitMessageAndExit() { + ConsoleWriter.writeBlankLine(100); + 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(Constant.DO_YOU_WANT_TO_CONTINUE); + ConsoleWriter.writeBlankLine(3); + ConsoleWriter.writeUserInputRequestMessage(Constant.YES); + ConsoleWriter.writeBlankLine(3); + ConsoleWriter.writeUserInputRequestMessage(Constant.NO); + if (UserInput.getIntegerUserInput() == 1) { + Router.showMenu(); + } else { + showExitMessageAndExit(); + } + } + + /** + * @author Naresh Maharjan + */ + public static void pressKeyToContinue() { + ConsoleWriter.writeBlankLine(3); + ConsoleWriter.writeUserInputRequestMessage(Constant.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..c2198fa --- /dev/null +++ b/naresh/src/main/java/com/lftechnology/phpjava/ems/views/EmployeeView.java @@ -0,0 +1,373 @@ +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 { + 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 (commonUtility.isLoggedIn() && 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(); + } + } + + /** + * @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(Constant.COMMON_UTILITY); + } + + /** + * @author Naresh Maharjan + */ + public void showAdminMenu() { + ConsoleWriter.writeBlankLine(100); + ConsoleWriter.writeUserInputRequestMessage(Constant.ADD_NEW_USER_MESSAGE); + ConsoleWriter.writeBlankLine(3); + ConsoleWriter.writeUserInputRequestMessage(Constant.SEARCH_USERS); + ConsoleWriter.writeBlankLine(3); + ConsoleWriter.writeUserInputRequestMessage(Constant.TERMINATE_USER_MESSAGE); + ConsoleWriter.writeBlankLine(3); + ConsoleWriter.writeUserInputRequestMessage(Constant.DELETE_USER_MESSAGE); + ConsoleWriter.writeBlankLine(3); + ConsoleWriter.writeUserInputRequestMessage(Constant.EXIT_MESSAGE); + ConsoleWriter.writeBlankLine(5); + + Router.initiateInitialMenuAction(UserInput.getIntegerUserInput()); + } + + /** + * @author Naresh Maharjan + */ + public void showNormalUserMenu() { + ConsoleWriter.writeBlankLine(100); + ConsoleWriter.writeUserInputRequestMessage(Constant.UPDATE_PROFILE_MESSAGE); + ConsoleWriter.writeBlankLine(3); + ConsoleWriter.writeUserInputRequestMessage(Constant.SEARCH_USERS); + ConsoleWriter.writeBlankLine(3); + ConsoleWriter.writeUserInputRequestMessage(Constant.EXIT_MESSAGE); + ConsoleWriter.writeBlankLine(5); + + Router.initiateInitialMenuAction(UserInput.getIntegerUserInput()); + } + + /** + * @author Naresh Maharjan + */ + public void showAddUserMenu() { + ConsoleWriter.writeBlankLine(100); + ConsoleWriter.writeUserInputRequestMessage(Constant.CHOOSE_ONE_OF_THE_FOLLOWING_USER_TYPE); + ConsoleWriter.writeBlankLine(3); + ConsoleWriter.writeUserInputRequestMessage(Constant.ADD_ADMIN_USER); + ConsoleWriter.writeBlankLine(3); + 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(Constant.ENTER_USER_DETAILS); + ConsoleWriter.writeBlankLine(3); + ConsoleWriter.writeUserInputRequestMessage(Constant.ENTER_FULLNAME); + ConsoleWriter.writeBlankLine(5); + employee.setFullname(UserInput.getStringUserInput()); + + ConsoleWriter.writeBlankLine(3); + ConsoleWriter.writeUserInputRequestMessage(Constant.ENTER_DEPARTMENT); + ConsoleWriter.writeBlankLine(5); + employee.setDepartment(UserInput.getStringUserInput()); + + ConsoleWriter.writeBlankLine(3); + ConsoleWriter.writeUserInputRequestMessage(Constant.ENTER_ADDRESS); + ConsoleWriter.writeBlankLine(5); + employee.setAddress(UserInput.getStringUserInput()); + + ConsoleWriter.writeBlankLine(3); + ConsoleWriter.writeUserInputRequestMessage(Constant.ENTER_USERNAME); + ConsoleWriter.writeBlankLine(5); + employee.setUsername(UserInput.getStringUserInput()); + + ConsoleWriter.writeBlankLine(3); + ConsoleWriter.writeUserInputRequestMessage(Constant.ENTER_PASSWORD); + ConsoleWriter.writeBlankLine(5); + employee.setPassword(UserInput.getStringUserInput()); + + return employee; + } + + /** + * @author Naresh Maharjan + */ + public void showSearchUserMenu() { + ConsoleWriter.writeBlankLine(100); + ConsoleWriter.writeUserInputRequestMessage(Constant.CHOOSE_ONE_OF_THE_FOLLOWING_SEARCH_CRITERIA); + ConsoleWriter.writeBlankLine(3); + ConsoleWriter.writeUserInputRequestMessage(Constant.SEARCH_BY_FULLNAME); + ConsoleWriter.writeBlankLine(3); + ConsoleWriter.writeUserInputRequestMessage(Constant.SEARCH_BY_DEPARTMENT); + ConsoleWriter.writeBlankLine(3); + ConsoleWriter.writeUserInputRequestMessage(Constant.SEARCH_BY_ADDRESS); + ConsoleWriter.writeBlankLine(3); + ConsoleWriter.writeUserInputRequestMessage(Constant.EXIT_MESSAGE); + ConsoleWriter.writeBlankLine(5); + int userInput = UserInput.getIntegerUserInput(); + + ConsoleWriter.writeBlankLine(3); + ConsoleWriter.writeUserInputRequestMessage(Constant.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 0: + CommonViewUtility.showExitMessageAndExit(); + break; + default: + Router.showMenu(); + break; + } + + Map postData = new HashMap<>(); + 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) { + tableGenerator.addRow(employee.toArray()); + } + tableGenerator.renderTable(); + } + + /** + * @author Naresh Maharjan + */ + public void showUpdateUserMenu() { + ConsoleWriter.writeBlankLine(100); + ConsoleWriter.writeUserInputRequestMessage(Constant.UPDATE_PROFILE_MENU); + ConsoleWriter.writeBlankLine(3); + ConsoleWriter.writeUserInputRequestMessage(Constant.UPDATE_FULLNAME); + ConsoleWriter.writeBlankLine(3); + ConsoleWriter.writeUserInputRequestMessage(Constant.UPDATE_DEPARTMENT); + ConsoleWriter.writeBlankLine(3); + ConsoleWriter.writeUserInputRequestMessage(Constant.UPDATE_ADDRESS); + ConsoleWriter.writeBlankLine(3); + ConsoleWriter.writeUserInputRequestMessage(Constant.UPDATE_USERNAME); + ConsoleWriter.writeBlankLine(3); + ConsoleWriter.writeUserInputRequestMessage(Constant.UPDATE_PASSWORD); + ConsoleWriter.writeBlankLine(5); + int userInput = UserInput.getIntegerUserInput(); + ConsoleWriter.writeUserInputRequestMessage(Constant.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(Constant.EMPLOYEE, employee); + this.employeeController.setData(postData); + this.employeeController.makeCurrentRequestPost(); + this.employeeController.updateProfile(); + } + + /** + * @author Naresh Maharjan + */ + public void showDeleteUserMenu() { + ConsoleWriter.writeBlankLine(100); + ConsoleWriter.writeUserInputRequestMessage(Constant.DELETE_USER_MENU); + ConsoleWriter.writeBlankLine(3); + ConsoleWriter.writeUserInputRequestMessage(Constant.ENTER_FULLNAME_OF_EMPLOYEE); + ConsoleWriter.writeBlankLine(3); + ConsoleWriter.writeUserInputRequestMessage(Constant.RETURN_TO_MAIN_MENU); + ConsoleWriter.writeBlankLine(3); + 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(Constant.EMPLOYEE, employee); + this.employeeController.setData(postData); + this.employeeController.makeCurrentRequestPost(); + this.employeeController.deleteUser(); + } + + /** + * @author Naresh Maharjan + */ + public void showTerminateUserMenu() { + ConsoleWriter.writeBlankLine(100); + ConsoleWriter.writeUserInputRequestMessage(Constant.TERMINATE_USER_MENU); + ConsoleWriter.writeBlankLine(3); + ConsoleWriter.writeUserInputRequestMessage(Constant.ENTER_FULLNAME_OF_EMPLOYEE); + ConsoleWriter.writeBlankLine(3); + ConsoleWriter.writeUserInputRequestMessage(Constant.RETURN_TO_MAIN_MENU); + ConsoleWriter.writeBlankLine(3); + 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(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(Constant.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..36e1fb3 --- /dev/null +++ b/naresh/src/main/java/com/lftechnology/phpjava/ems/views/LoginView.java @@ -0,0 +1,101 @@ +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; +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; + + /** + * @author Naresh Maharjan + */ + @Override + public void render() { + if (this.getAction().equals(Constant.LOGIN)) { + this.login(); + } else { + this.logout(); + } + + } + + /** + * @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(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(Constant.ENTER_YOUR_USERNAME_TO_LOGIN); + this.user.setUsername(UserInput.getStringUserInput()); + + ConsoleWriter.writeUserInputRequestMessage(Constant.ENTER_YOUR_PASSWORD_TO_LOGIN); + this.user.setPassword(UserInput.getStringUserInput()); + + 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 new file mode 100644 index 0000000..b9ee17f --- /dev/null +++ b/naresh/src/main/java/com/lftechnology/phpjava/ems/views/TableGenerator.java @@ -0,0 +1,56 @@ +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 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 new file mode 100644 index 0000000..e5a076a --- /dev/null +++ b/naresh/src/main/java/com/lftechnology/phpjava/ems/views/ViewSignature.java @@ -0,0 +1,40 @@ +package com.lftechnology.phpjava.ems.views; + +import java.util.Map; + +/** + * ViewSignature + * + * @author Naresh Maharjan + * @since August, 15 2016 + */ +public interface ViewSignature { + /** + * @author Naresh Maharjan + */ + void render(); + + /** + * @author Naresh Maharjan + * @return + */ + String getAction(); + + /** + * @author Naresh Maharjan + * @param action + */ + void setAction(String action); + + /** + * @author Naresh Maharjan + * @return + */ + Map getData(); + + /** + * @author Naresh Maharjan + * @param data + */ + void setData(Map data); +}