com.sfs.dao.FieldMapDAOImpl.java Source code

Java tutorial

Introduction

Here is the source code for com.sfs.dao.FieldMapDAOImpl.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 com.sfs.beans.FieldMapBean;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Map;
import java.util.HashMap;

import javax.annotation.Resource;

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

/**
 * The Class FieldMapDAOImpl.
 */
public class FieldMapDAOImpl extends BaseDAOImpl implements FieldMapDAO {

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

    /** The user dao. */
    @Resource
    private UserDAO userDAO;

    /**
     * Load a map of FieldMapBeans based on the supplied map class and type
     * variables.
     *
     * @param mapClass the map class
     * @param mapType the map type
     *
     * @return the map< string, field map bean>
     *
     * @throws SFSDaoException the SFS dao exception
     */
    @SuppressWarnings("unchecked")
    public final Map<String, FieldMapBean> load(final String mapClass, final String mapType)
            throws SFSDaoException {
        if (mapClass == null) {
            throw new SFSDaoException("Error: field map class cannot be null");
        }
        if (mapClass.compareTo("") == 0) {
            throw new SFSDaoException("Error: field map class cannot be an " + "empty string");
        }
        if (mapType == null) {
            throw new NullPointerException("Error: field map type cannot " + "be null");
        }
        dataLogger.info("Field maps for: " + mapClass + " - " + mapType + " requested");

        Map<String, FieldMapBean> fieldHash = new HashMap<String, FieldMapBean>();

        Collection<FieldMapBean> fieldMaps = new ArrayList<FieldMapBean>();
        try {
            fieldMaps = this.getJdbcTemplateReader().query(this.getSQL().getValue("fieldMap/loadName"),
                    new Object[] { mapClass, mapType }, new RowMapper() {
                        public Object mapRow(final ResultSet rs, final int rowNum) throws SQLException {
                            return loadFieldMap(rs);
                        }
                    });

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

        for (FieldMapBean fieldMap : fieldMaps) {
            fieldHash.put(fieldMap.getName(), fieldMap);
        }
        return fieldHash;
    }

    /**
     * Load a FieldMapBean object based on the supplied fieldMapId value.
     *
     * @param fieldMapId the field map id
     *
     * @return the field map bean
     *
     * @throws SFSDaoException the SFS dao exception
     */
    public final FieldMapBean load(final int fieldMapId) throws SFSDaoException {
        if (fieldMapId == 0) {
            throw new SFSDaoException("FieldMapId value cannot be 0");
        }

        FieldMapBean fieldMap = null;

        try {
            fieldMap = (FieldMapBean) this.getJdbcTemplateReader().queryForObject(
                    this.getSQL().getValue("fieldMap/loadId"), new Object[] { fieldMapId }, new RowMapper() {
                        public Object mapRow(final ResultSet rs, final int rowNum) throws SQLException {
                            return loadFieldMap(rs);
                        }
                    });

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

    /**
     * Load field map.
     *
     * @param rs the rs
     *
     * @return the field map bean
     *
     * @throws SQLException the SQL exception
     */
    private FieldMapBean loadFieldMap(final ResultSet rs) throws SQLException {

        FieldMapBean fieldMap = new FieldMapBean();

        // Create field map bean and fill with dataset info.
        fieldMap.setFieldMapId(rs.getInt("Id"));
        fieldMap.setMapClass(rs.getString("MapClass"));
        fieldMap.setMapType(rs.getString("MapType"));
        fieldMap.setFieldType(rs.getString("FieldType"));
        fieldMap.setName(rs.getString("FieldName"));
        fieldMap.setIndex(rs.getInt("FieldNo"));
        fieldMap.setFieldType(rs.getString("FieldType"));
        fieldMap.setDescription(rs.getString("Description"));
        fieldMap.setObjectType(rs.getString("ObjectType"));

        try {
            fieldMap.setCreated(rs.getTimestamp("Created"));
        } catch (SQLException sqe) {
            dataLogger.info("Could not load Created date: " + sqe.getMessage());
        }
        fieldMap.setCreatedBy(rs.getString("CreatedBy"));
        try {
            fieldMap.setModified(rs.getTimestamp("Modified"));
        } catch (SQLException sqe) {
            dataLogger.info("Could not load Modified date: " + sqe.getMessage());
        }
        fieldMap.setModifiedBy(rs.getString("ModifiedBy"));

        if (fieldMap.getCreatedBy() != null && this.userDAO != null) {
            try {
                fieldMap.setCreatedByUser(this.userDAO.loadCached(fieldMap.getCreatedBy()));
            } catch (Exception e) {
                dataLogger.info(
                        "Could not load CreatedBy UserBean for " + "FieldMapId = " + fieldMap.getFieldMapId());
            }
        }
        if (fieldMap.getModifiedBy() != null && this.userDAO != null) {
            try {
                fieldMap.setModifiedByUser(this.userDAO.loadCached(fieldMap.getModifiedBy()));
            } catch (Exception e) {
                dataLogger.info(
                        "Could not load ModifiedBy UserBean for " + "FieldMapId = " + fieldMap.getFieldMapId());
            }
        }
        return fieldMap;
    }
}