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

Java tutorial

Introduction

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

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;

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

/**
 * The Class EmailRecipientDAOImpl.
 */
public class EmailRecipientDAOImpl extends BaseDAOImpl implements EmailRecipientDAO {

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

    /**
     * Creates an email recipient entry.
     *
     * @param referenceGUID the reference guid
     * @param personGUID the person guid
     * @param organisationGUID the organisation guid
     * @param customEmailAddress the custom email address
     * @param pending - boolean if the email message needs to sent
     * @param deliverOutOfHours - boolean if the email message should be sent out hours
     * @return true, if successful
     */
    public final boolean create(final int referenceGUID, final int personGUID, final int organisationGUID,
            final String customEmailAddress, final boolean pending, final boolean deliverOutOfHours) {

        boolean created = false;

        try {
            final int createCount = this.getJdbcTemplateWriter().update(
                    this.getSQL().getValue("emailRecipient/create"), new Object[] { referenceGUID, personGUID,
                            organisationGUID, customEmailAddress, false, false, pending, deliverOutOfHours });
            if (createCount > 0) {
                dataLogger.debug("Created EmailRecipient entry");
                created = true;
            }
        } catch (DataAccessException dae) {
            dataLogger.error("Error creating EmailRecipient entry", dae);
        }
        return created;
    }

    /**
     * Update the status of the email recipient entry.
     *
     * @param referenceGUID the reference guid
     * @param personGUID the person guid
     * @param organisationGUID the organisation guid
     * @param customEmailAddress the custom email address
     * @param success the success
     * @param logMessage the log message
     * @return true, if successful
     */
    public final boolean update(final int referenceGUID, final int personGUID, final int organisationGUID,
            final String customEmailAddress, final boolean success, final String logMessage) {

        boolean updated = false;

        boolean sent = false;
        boolean failure = false;
        final boolean pending = false;

        if (success) {
            sent = true;
        } else {
            failure = true;
        }

        try {
            final int updateCount = this.getJdbcTemplateWriter()
                    .update(this.getSQL().getValue("emailRecipient/update"), new Object[] { sent, failure, pending,
                            logMessage, referenceGUID, personGUID, organisationGUID, customEmailAddress });
            if (updateCount > 0) {
                dataLogger.debug("Updated EmailRecipient entry");
                updated = true;
            }
        } catch (DataAccessException dae) {
            dataLogger.error("Error updating EmailRecipient entry", dae);
        }
        return updated;
    }

    /**
     * Delete all the email recipients for the specified referenceGUID.
     *
     * @param referenceGUID the reference guid
     * @return true, if successful
     */
    public final boolean delete(final int referenceGUID) {

        boolean deleted = false;

        try {
            final int deleteCount = this.getJdbcTemplateWriter()
                    .update(this.getSQL().getValue("emailRecipient/deleteAll"), new Object[] { referenceGUID });
            if (deleteCount > 0) {
                dataLogger.debug("Deleted all EmailRecipient entries for: " + referenceGUID);
                deleted = true;
            }
        } catch (DataAccessException dae) {
            dataLogger.error("Error deleting all EmailRecipient entries", dae);
        }
        return deleted;
    }

    /**
     * Delete a specific email recipient entry.
     *
     * @param referenceGUID the reference guid
     * @param personGUID the person guid
     * @param organisationGUID the organisation guid
     * @param customEmailAddress the custom email address
     * @return true, if successful
     */
    public final boolean delete(final int referenceGUID, final int personGUID, final int organisationGUID,
            final String customEmailAddress) {

        boolean deleted = false;

        try {
            final int deleteCount = this.getJdbcTemplateWriter().update(
                    this.getSQL().getValue("emailRecipient/delete"),
                    new Object[] { referenceGUID, personGUID, organisationGUID, customEmailAddress });
            if (deleteCount > 0) {
                dataLogger.debug("Deleted all EmailRecipient entry for: " + referenceGUID + ", personGUID: "
                        + personGUID + ", organisationGUID: " + organisationGUID);
                deleted = true;
            }
        } catch (DataAccessException dae) {
            dataLogger.error("Error deleting EmailRecipient entry", dae);
        }
        return deleted;
    }

    /**
     * Load a collection of EmailRecipientBeans for a specified referenceGUID.
     *
     * @param referenceGUID the reference guid
     * @return the collection
     * @throws WhichDoctorDaoException the which doctor dao exception
     */
    @SuppressWarnings("unchecked")
    public final Collection<EmailRecipientBean> load(final int referenceGUID) throws WhichDoctorDaoException {

        dataLogger.info("Requested EmailRecipients for ReferenceGUID: " + referenceGUID);

        final String loadRecipients = getSQL().getValue("emailRecipient/load");

        Collection<EmailRecipientBean> emailRecipients = new ArrayList<EmailRecipientBean>();

        try {
            emailRecipients = this.getJdbcTemplateReader().query(loadRecipients, new Object[] { referenceGUID },
                    new RowMapper() {
                        public Object mapRow(final ResultSet rs, final int rowNum) throws SQLException {
                            return loadEmailRecipient(rs);
                        }
                    });

        } catch (IncorrectResultSizeDataAccessException ie) {
            // No results found for this search
            dataLogger.info("No results found for this EmailRecipient search");
        }
        return emailRecipients;
    }

    /**
     * Load a collection of pending EmailRecipientBeans.
     *
     * @param emailRecipientCount - the number of pending EmailRecipientBeans.
     * @return the collection
     * @throws WhichDoctorDaoException the which doctor dao exception
     */
    @SuppressWarnings("unchecked")
    public final Collection<EmailRecipientBean> loadPending(final int emailRecipientCount)
            throws WhichDoctorDaoException {

        dataLogger.info("Requested " + emailRecipientCount + " pending EmailRecipients");

        boolean duringOfficeHours = false;

        // Determine the current time - if during office hours filter out low priority
        Calendar currentTime = Calendar.getInstance();

        if (currentTime.get(Calendar.HOUR_OF_DAY) > 8 && currentTime.get(Calendar.HOUR_OF_DAY) < 18) {
            duringOfficeHours = true;
        }

        String loadRecipients = getSQL().getValue("emailRecipient/loadPending");

        if (duringOfficeHours) {
            loadRecipients += " AND DeliverOutOfHours = false";
        }
        // Append the limit
        loadRecipients += " LIMIT ?";

        Collection<EmailRecipientBean> emailRecipients = new ArrayList<EmailRecipientBean>();

        try {
            emailRecipients = this.getJdbcTemplateReader().query(loadRecipients,
                    new Object[] { emailRecipientCount }, new RowMapper() {
                        public Object mapRow(final ResultSet rs, final int rowNum) throws SQLException {
                            return loadEmailRecipient(rs);
                        }
                    });

        } catch (IncorrectResultSizeDataAccessException ie) {
            // No results found for this search
            dataLogger.info("No results found for this EmailRecipient search");
        }
        return emailRecipients;
    }

    /**
     * Load email recipient.
     *
     * @param rs the rs
     * @return the email recipient bean
     * @throws SQLException the sQL exception
     */
    private EmailRecipientBean loadEmailRecipient(final ResultSet rs) throws SQLException {

        EmailRecipientBean emailRecipient = new EmailRecipientBean();

        emailRecipient.setReferenceGUID(rs.getInt("ReferenceGUID"));
        emailRecipient.setPersonGUID(rs.getInt("PersonGUID"));
        emailRecipient.setOrganisationGUID(rs.getInt("OrganisationGUID"));
        emailRecipient.setCustomEmailAddress(rs.getString("CustomEmailAddress"));
        emailRecipient.setPending(rs.getBoolean("Pending"));
        emailRecipient.setSent(rs.getBoolean("Sent"));
        emailRecipient.setFailed(rs.getBoolean("Failed"));
        emailRecipient.setDeliverOutOfHours(rs.getBoolean("DeliverOutOfHours"));

        if (emailRecipient.getPersonGUID() > 0) {
            emailRecipient.setName(rs.getString("PersonName"));
            emailRecipient.setOrder(rs.getString("PersonOrder"));
        }
        if (emailRecipient.getOrganisationGUID() > 0) {
            emailRecipient.setName(rs.getString("OrganisationName"));
            emailRecipient.setOrder(rs.getString("OrganisationOrder"));
        }
        emailRecipient.setLogMessage(rs.getString("LogMessage"));

        return emailRecipient;
    }
}