FileDocCategorySizeDatePackage
EmployeeRegistryBean.javaAPI DocExample8624Tue Feb 28 11:34:06 GMT 2006com.ora.jsp.beans.emp

EmployeeRegistryBean

public class EmployeeRegistryBean extends Object implements Serializable
This class is an example of an application specific interface to a database. It contains methods for authenticating a user, and retrieving and updating user information.
author
Hans Bergsten, Gefion software
version
2.0

Fields Summary
private DataSource
dataSource
Constructors Summary
Methods Summary
public EmployeeBeanauthenticate(java.lang.String userName, java.lang.String password)
Returns an EmployeeBean if the specified user name and password match an employee in the database, otherwise null.

            
        EmployeeBean empInfo = getEmployee(userName);
        if (empInfo != null && empInfo.getPassword().equals(password)) {
            return empInfo;
        }
        return null;
    
public EmployeeBeangetEmployee(java.lang.String userName)
Returns an EmployeeBean initialized with the information found in the database for the specified employee, or null if not found.

        
        // Get the user info from the database
        Connection conn = dataSource.getConnection();
        Map empRow = null;
        Map[] projects = null;
        try {
            empRow = getSingleValueProps(userName, conn);
            projects = getProjects(userName, conn);
        }
        finally {
            try {
                conn.close();
            }
            catch (SQLException e) {} // Ignore
        }

        // Create a EmployeeBean if the user was found
        if (empRow == null) {
            // Not found
            return null;
        }
        
        EmployeeBean empInfo = new EmployeeBean();
        empInfo.setDept((String) empRow.get("Dept"));
        empInfo.setEmpDate((java.util.Date) empRow.get("EmpDate"));
        empInfo.setEmailAddr((String) empRow.get("EmailAddr"));
        empInfo.setFirstName((String) empRow.get("FirstName"));
        empInfo.setLastName((String) empRow.get("LastName"));
        empInfo.setPassword((String) empRow.get("Password"));
        empInfo.setUserName((String) empRow.get("UserName"));
        empInfo.setProjects(toProjectsArray(projects));
        return empInfo;
    
private java.util.Map[]getProjects(java.lang.String userName, java.sql.Connection conn)
Returns a Map[] with a Map per project for the specified employee, or null if not found.


        if (userName == null) {
            return null;
        }
        
        SQLCommandBean sqlCommandBean = new SQLCommandBean();
        sqlCommandBean.setConnection(conn);
        StringBuffer sql = new StringBuffer();
        sql.append("SELECT * FROM EmployeeProjects ")
            .append("WHERE UserName = ?");
        sqlCommandBean.setSqlValue(sql.toString());
        List values = new ArrayList();
        values.add(userName);
        sqlCommandBean.setValues(values);
        return sqlCommandBean.executeQuery().getRows();
    
private java.util.MapgetSingleValueProps(java.lang.String userName, java.sql.Connection conn)
Returns a Map with all information about the specified employee except the project list, or null if not found.


        if (userName == null) {
            return null;
        }
        
        SQLCommandBean sqlCommandBean = new SQLCommandBean();
        sqlCommandBean.setConnection(conn);
        StringBuffer sql = new StringBuffer();
        sql.append("SELECT * FROM Employee ")
            .append("WHERE UserName = ?");
        sqlCommandBean.setSqlValue(sql.toString());
        List values = new ArrayList();
        values.add(userName);
        sqlCommandBean.setValues(values);
        Result result = sqlCommandBean.executeQuery();
        if (result == null || result.getRowCount() == 0) {
            // User not found
            return null;
        }
        return result.getRows()[0];
    
public voidsaveEmployee(EmployeeBean empInfo)
Inserts the information about the specified employee, or updates the information if it's already defined.

        
        // Save the user info from the database
        Connection conn = dataSource.getConnection();
        conn.setAutoCommit(false);
        try {
            saveSingleValueProps(empInfo, conn);
            saveProjects(empInfo, conn);
            conn.commit();
        }
        catch (SQLException e) {
            conn.rollback();
        }
        finally {
            try {
                conn.setAutoCommit(true);
                conn.close();
            }
            catch (SQLException e) {} // Ignore
        }
    
private voidsaveProjects(EmployeeBean empInfo, java.sql.Connection conn)
Updates or inserts project information for the specified employee.


        if (empInfo == null) {
            return;
        }
        
        SQLCommandBean sqlCommandBean = new SQLCommandBean();
        sqlCommandBean.setConnection(conn);
        sqlCommandBean.setSqlValue("DELETE FROM EmployeeProjects WHERE UserName = ?");
        List values = new ArrayList();
        values.add(empInfo.getUserName());
        sqlCommandBean.setValues(values);
        sqlCommandBean.executeUpdate();
        
        String[] projects = empInfo.getProjects();
        sqlCommandBean.setSqlValue("INSERT INTO EmployeeProjects VALUES(?, ?)");
        for (int i = 0; i < projects.length; i++) {
            values.clear();
            values.add(empInfo.getUserName());
            values.add(projects[i]);
            sqlCommandBean.executeUpdate();
        }
    
private voidsaveSingleValueProps(EmployeeBean empInfo, java.sql.Connection conn)
Inserts the information about the specified employee (except projects), or updates the information if it's already defined.


        if (empInfo == null) {
            return;
        }
        
        StringBuffer sql = new StringBuffer();
        EmployeeBean dbInfo = getEmployee(empInfo.getUserName());
        if (dbInfo == null) {
            // Use INSERT statement
            sql.append("INSERT INTO Employee ").
                append("(Dept, EmpDate, EmailAddr, FirstName, LastName, ").
                append("Password, ModDate, UserName) ").
                append("VALUES(?, ?, ?, ?, ?, ?, ?, ?)");
        }
        else {
            // Use UPDATE statement
            sql.append("UPDATE Employee SET Dept = ?, EmpDate = ?, ")
                .append("EmailAddr = ?, FirstName = ?, LastName = ?,")
                .append("Password = ?, ModDate = ? WHERE Username = ?");
        }
        SQLCommandBean sqlCommandBean = new SQLCommandBean();
        sqlCommandBean.setConnection(conn);
        sqlCommandBean.setSqlValue(sql.toString());
        List values = new ArrayList();
        values.add(empInfo.getDept());
        // Converts java.util.Date to java.sql.Date
        java.sql.Date empDate = 
            new java.sql.Date(empInfo.getEmpDate().getTime());
        values.add(empDate);
        values.add(empInfo.getEmailAddr());
        values.add(empInfo.getFirstName());
        values.add(empInfo.getLastName());
        values.add(empInfo.getPassword());
        values.add(new Timestamp(System.currentTimeMillis()));
        values.add(empInfo.getUserName());
        sqlCommandBean.setValues(values);
        sqlCommandBean.executeUpdate();
    
public voidsetDataSource(javax.sql.DataSource dataSource)
Sets the dataSource property value.

        this.dataSource = dataSource;
    
private java.lang.String[]toProjectsArray(java.util.Map[] projectRows)
Converts a Vector with Row elements containing a ProjectName column to a String[] with the project names.

        if (projectRows == null) {
            return new String[0];
        }
        
        String[] arr = new String[projectRows.length];
        for (int i = 0; i < arr.length; i++) {
            arr[i] = (String) projectRows[i].get("ProjectName");
        }
        return arr;