com.sfs.dao.SettingsDAOImpl.java Source code

Java tutorial

Introduction

Here is the source code for com.sfs.dao.SettingsDAOImpl.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.io.IOException;
import java.io.Reader;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.sfs.beans.SettingsBean;

import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.apache.log4j.Logger;
import org.springframework.dao.IncorrectResultSizeDataAccessException;
import org.springframework.jdbc.core.RowMapper;

/**
 * The Class SettingsDAOImpl.
 *
 * @author David Harrison 4th June 2008
 */
public class SettingsDAOImpl extends BaseDAOImpl implements SettingsDAO {

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

    /**
     * Loads a menu xml document from the database and returns it within a
     * SettingsBean.
     *
     * @param type the type
     *
     * @return the settings bean
     *
     * @throws SFSDaoException the SFS dao exception
     */
    public final SettingsBean loadSettings(final String type) throws SFSDaoException {

        if (type == null) {
            throw new SFSDaoException("The settings type cannot be null");
        }

        SettingsBean settingsBean = null;

        try {
            settingsBean = (SettingsBean) this.getJdbcTemplateReader().queryForObject(
                    this.getSQL().getValue("settings/load"), new Object[] { type }, new RowMapper() {
                        public Object mapRow(final ResultSet rs, final int rowNum) throws SQLException {

                            SettingsBean loadedSettings = new SettingsBean();
                            loadedSettings.setType(type);

                            Reader settingsStream = rs.getCharacterStream("Settings");
                            SAXBuilder builder = new SAXBuilder();
                            try {
                                loadedSettings.setXmlDocument(builder.build(settingsStream));
                                dataLogger.info("XML settings file parsed " + "successfully");
                            } catch (JDOMException jde) {
                                dataLogger.fatal("Error parsing XML settings: " + jde.getMessage());
                            } catch (IOException ioe) {
                                dataLogger.fatal("Error reading XML character " + "stream: " + ioe.getMessage());
                            }
                            return loadedSettings;
                        }
                    });

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

    /**
     * Saves menu information to the database Data saved as an XML document.
     *
     * @param settingsBean the settings bean
     *
     * @return true, if save settings
     *
     * @throws SFSDaoException the SFS dao exception
     */
    public final boolean saveSettings(final SettingsBean settingsBean) throws SFSDaoException {

        if (settingsBean.getType() == null) {
            throw new SFSDaoException("The settings type cannot be null");
        }
        if (settingsBean.getXmlDocument() == null) {
            throw new SFSDaoException("The settings XML document cannot be null");
        }

        boolean success = false;

        // Load the existing set of settings.
        // On a successful save create a backup using these values

        SettingsBean existingSettings = new SettingsBean();
        try {
            existingSettings = loadSettings(settingsBean.getType());
        } catch (Exception e) {
            dataLogger.error("Error loading existing settings: " + e.getMessage());
        }

        boolean backupCreated = false;

        try {
            // Save a copy of the existing settings as a backup
            existingSettings.setType(existingSettings.getType() + ".backup");
            recordSettings(existingSettings);
            backupCreated = true;
        } catch (Exception e) {
            dataLogger.error("Error saving backup settings: " + e.getMessage());
        }

        if (backupCreated) {
            try {
                // Save the new settings
                recordSettings(settingsBean);
                success = true;
            } catch (Exception e) {
                dataLogger.error("Error saving new settings: " + e.getMessage());
            }
        } else {
            dataLogger.error("A backup of the existing settings could not be saved");
        }
        return success;
    }

    /**
     * Record settings.
     *
     * @param settingsBean the settings bean
     *
     * @return true, if successful
     *
     * @throws Exception the exception
     */
    private boolean recordSettings(final SettingsBean settingsBean) throws Exception {

        boolean success = false;

        final String xmlString = settingsBean.getXmlDocumentAsString();

        try {
            final int updateCount = this.getJdbcTemplateWriter().update(this.getSQL().getValue("settings/create"),
                    new Object[] { settingsBean.getType(), xmlString, settingsBean.getType(), xmlString });

            if (updateCount > 0) {
                dataLogger.info("Menu saved to database");
                success = true;
            }
        } catch (IncorrectResultSizeDataAccessException ie) {
            dataLogger.debug("No results found for this search: " + ie.getMessage());
        }
        return success;
    }
}