com.sfs.dao.UserPreferencesDAOImpl.java Source code

Java tutorial

Introduction

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

Source

/*******************************************************************************
 * Copyright (c) 2009 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 java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Collection;

import com.sfs.beans.UserPreferencesBean;

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

/**
 * The Class UserPreferencesDAOImpl.
 */
public class UserPreferencesDAOImpl extends BaseDAOImpl implements UserPreferencesDAO {

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

    /**
     * Load the UserPreferencesBean for the supplied dn.
     *
     * @param dn the dn
     *
     * @return the user preferences bean
     *
     * @throws SFSDaoException the SFS dao exception
     */
    @SuppressWarnings("unchecked")
    public final UserPreferencesBean load(final String dn) throws SFSDaoException {
        if (dn == null) {
            throw new SFSDaoException("Error: DN cannot be null");
        }
        if (dn.compareTo("") == 0) {
            throw new SFSDaoException("Error: DN cannot be an empty string");
        }

        UserPreferencesBean userPreferences = new UserPreferencesBean();
        userPreferences.setDN(dn);

        dataLogger.info("Loading privileges for: " + dn);

        try {
            Collection<String[]> indexValue = this.getJdbcTemplateReader().query(
                    this.getSQL().getValue("userPreferences/load"), new Object[] { userPreferences.getDN() },
                    new RowMapper() {
                        public Object mapRow(final ResultSet rs, final int rowNum) throws SQLException {
                            final String index = rs.getString("IndexValue");
                            final String value = rs.getString("Value");

                            return new String[] { index, value };
                        }
                    });

            for (String[] keyPair : indexValue) {
                if (keyPair[0] != null) {
                    userPreferences.setOption(keyPair[0], keyPair[1]);
                }
            }

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

    /**
     * Save the UserPreferencesBean.
     *
     * @param userPreferences the user preferences
     *
     * @throws SFSDaoException the SFS dao exception
     */
    public final void save(final UserPreferencesBean userPreferences) throws SFSDaoException {
        if (userPreferences.getDN() == null) {
            throw new SFSDaoException("User DN cannot have a null value");
        }

        try {
            this.getJdbcTemplateWriter().update(this.getSQL().getValue("userPreferences/delete"),
                    new Object[] { userPreferences.getDN() });

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

        for (String index : userPreferences.getOptions().keySet()) {
            String value = userPreferences.getOption(index);

            try {
                this.getJdbcTemplateWriter().update(this.getSQL().getValue("userPreferences/save"), new Object[] {
                        userPreferences.getDN(), index, value, userPreferences.getDN(), index, value });

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

}