package server.servlets;

/**
 * The Authorization class encapsulates the functionality
 * required to validate a user id and password.
 *
 * @author Robert Lysik
 * @version 1.00
 */
class Authorization {

    /**
     * This is the default constructor for the
     * Authorization class.
     */
    public Authorization() {

    }

    /**
     * The verify function takes two parameters, the user id
     * and password, and performs a query against the user
     * database. If a matching record is found then the user is
     * validated and true is returned, otherwise flase is returned.
     *
     * @param String user id
     * @param String password
     */
    public boolean verify(String UserId, String password) {
        String sqlQuery = "select count(*) from udbTable where " +
                "userId = '" + UserId + "' and " +
                "password = '" + password + "'";
        int recordMatchCount = 0,
                columnIndex = 1;

        SqlBean sb = new SqlBean();

        // The UDB ODBC connection must be defined, this
        // is the user database which will be used for validation of
        // the user id and password passed in as parameters.
        sb.setUrl("jdbc:odbc:udb");

        sb.init();

        recordMatchCount = sb.getResultSetIntegerColumnData(sb.query(sqlQuery),
                columnIndex);

        sb.close();

        // return true if a matching record has been found.
        return (recordMatchCount > 0) ? true : false;
    }
}