com.sfs.dao.UserDAOImpl.java Source code

Java tutorial

Introduction

Here is the source code for com.sfs.dao.UserDAOImpl.java

Source

/*******************************************************************************
 * Copyright (c) 2010 David Harrison.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v3.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/gpl-3.0.html
 *
 * Contributors:
 *     David Harrison - initial API and implementation
 ******************************************************************************/
package com.sfs.dao;

import com.sfs.beans.UserBean;

import java.sql.ResultSet;
import java.sql.SQLException;

import org.apache.log4j.Logger;
import org.springframework.dao.IncorrectResultSizeDataAccessException;
import org.springframework.jdbc.core.RowMapper;

/**
 * The Class UserDAOImpl.
 */
public class UserDAOImpl extends BaseDAOImpl implements UserDAO {

    /** The data logger. */
    private static Logger dataLogger = Logger.getLogger(UserDAOImpl.class);

    /**
     * Load the cached user details.
     *
     * @param dn the distinguished name of the user
     *
     * @return the user bean
     *
     * @throws SFSDaoException the SFS dao exception
     */
    public final UserBean loadCached(final String dn) throws SFSDaoException {

        UserBean cachedUser = null;
        try {
            // Create a new entry as none exists...
            cachedUser = (UserBean) this.getJdbcTemplateReader().queryForObject(this.getSQL().getValue("user/load"),
                    new Object[] { dn }, new RowMapper() {
                        public Object mapRow(final ResultSet rs, final int rowNum) throws SQLException {
                            UserBean loadedUser = new UserBean();
                            loadedUser.setDN(rs.getString("UserDn"));
                            loadedUser.setPreferredName(rs.getString("FirstName"));
                            loadedUser.setLastName(rs.getString("LastName"));
                            loadedUser.setEmail(rs.getString("Email"));

                            return loadedUser;
                        }
                    });

        } catch (IncorrectResultSizeDataAccessException ie) {
            dataLogger.debug("No results found for this search: " + ie.getMessage());
        }
        return cachedUser;
    }

    /**
     * Cache the user details.
     *
     * @param user the user
     * @return true, if successful
     * @throws SFSDaoException the sFS dao exception
     */
    public final boolean cache(final UserBean user) throws SFSDaoException {

        if (user == null) {
            throw new NullPointerException("The user object cannot be null");
        }
        if (user.getDN() == null) {
            throw new SFSDaoException("Valid User DN required");
        }

        boolean success = false;

        final UserBean cachedUser = this.loadCached(user.getDN());

        if (cachedUser == null) {
            // Create a new entry as none exists.
            final int updateCount = this.getJdbcTemplateWriter().update(this.getSQL().getValue("user/create"),
                    new Object[] { user.getDN(), user.getPreferredName(), user.getLastName(), user.getEmail() });

            if (updateCount > 0) {
                success = true;
                dataLogger.info("The user credentials were cached");
            } else {
                dataLogger.info("The user credentials were not cached");
            }
        }
        return success;
    }

    /**
     * Delete the cached user details.
     *
     * @param user the user
     * @return true, if successful
     * @throws SFSDaoException the sFS dao exception
     */
    public final boolean delete(final UserBean user) throws SFSDaoException {

        if (user == null) {
            throw new NullPointerException("The user object cannot be null");
        }
        if (user.getDN() == null) {
            throw new SFSDaoException("Valid User DN required");
        }

        boolean success = false;

        final UserBean cachedUser = this.loadCached(user.getDN());

        if (cachedUser != null) {
            // Delete the entry if one exists.
            final int deleteCount = this.getJdbcTemplateWriter().update(this.getSQL().getValue("user/delete"),
                    new Object[] { cachedUser.getDN() });

            if (deleteCount > 0) {
                success = true;
                dataLogger.info("The user credentials were deleted");
            } else {
                dataLogger.info("The user credentials were not deleted");
            }
        }
        return success;
    }
}