com.sfs.whichdoctor.dao.IsbPayloadDAOImpl.java Source code

Java tutorial

Introduction

Here is the source code for com.sfs.whichdoctor.dao.IsbPayloadDAOImpl.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.whichdoctor.dao;

import com.sfs.whichdoctor.beans.IsbPayloadBean;

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

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

/**
 * The Class IsbPayloadDAOImpl.
 */
public class IsbPayloadDAOImpl extends BaseDAOImpl implements IsbPayloadDAO {

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

    /**
     * Used to get a IsbPayloadBean for the the specified ISB Payload Id.
     * Returns null if no isb message found
     *
     * @param isbPayloadId the isb payload id
     *
     * @return the isb payload bean
     *
     * @throws WhichDoctorDaoException the which doctor dao exception
     */
    public final IsbPayloadBean load(final int isbPayloadId) throws WhichDoctorDaoException {
        dataLogger.info("ISB payload id: " + isbPayloadId + " requested");

        IsbPayloadBean isbPayload = null;

        try {
            isbPayload = (IsbPayloadBean) this.getIsbJdbcTemplate().queryForObject(
                    this.getSQL().getValue("isbpayload/loadId"), new Object[] { isbPayloadId }, new RowMapper() {
                        public Object mapRow(final ResultSet rs, final int rowNum) throws SQLException {
                            return loadIsbPayloadBean(rs);
                        }
                    });

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

    /**
     * Used to get an IsbPayloadBean for the specified IsbMessageBean Id.
     *
     * @param isbMessageId the isb message id
     *
     * @return the isb payload bean
     *
     * @throws WhichDoctorDaoException the which doctor dao exception
     */
    public final IsbPayloadBean loadSiblingOf(final int isbMessageId) throws WhichDoctorDaoException {
        dataLogger.info("ISB payload with a parent id of : " + isbMessageId + " requested");

        IsbPayloadBean isbPayload = null;

        try {
            isbPayload = (IsbPayloadBean) this.getIsbJdbcTemplate().queryForObject(
                    this.getSQL().getValue("isbpayload/loadParent"), new Object[] { isbMessageId },
                    new RowMapper() {
                        public Object mapRow(final ResultSet rs, final int rowNum) throws SQLException {
                            return loadIsbPayloadBean(rs);
                        }
                    });

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

    /**
     * Creates the IsbPayloadBean.
     *
     * @param isbPayload the isb payload
     *
     * @return true, if successful
     *
     * @throws WhichDoctorDaoException the which doctor dao exception
     */
    public final boolean create(final IsbPayloadBean isbPayload) throws WhichDoctorDaoException {
        boolean success = false;

        try {
            final int createCount = this.getIsbJdbcTemplate().update(this.getSQL().getValue("isbpayload/create"),
                    new Object[] { isbPayload.getIsbMessageId(), isbPayload.getXmlPayload() });

            if (createCount > 0) {
                success = true;
            }
        } catch (DataAccessException de) {
            dataLogger.error("Error creating ISB payload: " + de.getMessage());
        }
        return success;
    }

    /**
     * Load isb payload bean.
     *
     * @param rs the rs
     *
     * @return the isb payload bean
     *
     * @throws SQLException the SQL exception
     */
    private IsbPayloadBean loadIsbPayloadBean(final ResultSet rs) throws SQLException {
        IsbPayloadBean isbPayload = new IsbPayloadBean();

        isbPayload.setId(rs.getInt("Id"));
        isbPayload.setIsbMessageId(rs.getInt("IsbMessageId"));
        isbPayload.setXmlPayload(rs.getString("XmlPayload"));

        return isbPayload;
    }
}