com.sfs.dao.bugreport.EmailBugReportHandler.java Source code

Java tutorial

Introduction

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

import com.sfs.beans.BugReportBean;
import com.sfs.beans.EmailMessageBean;
import com.sfs.dao.SFSDaoException;
import com.sfs.dao.EmailMessageDAO;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;

/**
 * The Class EmailBugReportHandler.
 *
 * @author David Harrison
 */
public class EmailBugReportHandler implements BugReportHandler {

    /** The recipients. */
    private List<String> recipients;

    /** The email message dao. */
    @Resource
    private EmailMessageDAO emailMessageDAO;

    /**
     * Set the recipients for the bug report.
     *
     * @param recipientsList - email addresses that will receive bug reports
     */
    public final void setRecipients(final List<String> recipientsList) {
        this.recipients = recipientsList;
    }

    /**
     * File a bug report via email.
     *
     * @param bugReport - the bug report details
     *
     * @throws SFSDaoException the SFS dao exception
     */
    public final void submitReport(final BugReportBean bugReport) throws SFSDaoException {
        if (this.emailMessageDAO == null) {
            throw new SFSDaoException("The EmailMessageDAO object cannot be null");
        }
        if (this.recipients == null) {
            throw new SFSDaoException("At least one bug report recipient must be identified");
        }
        if (bugReport == null) {
            throw new SFSDaoException("BugReportBean cannot be null");
        }
        if (bugReport.getUser() == null) {
            throw new SFSDaoException("Submission of a bug report requires a valid user object");
        }
        if (bugReport.getReportClass() == null) {
            throw new SFSDaoException("The bug report class cannot be null");
        }

        StringBuffer title = new StringBuffer();
        if (StringUtils.isNotBlank(bugReport.getPriority())) {
            title.append(bugReport.getPriority());
            title.append(": ");
        }
        if (StringUtils.isNotBlank(bugReport.getReportClass())) {
            title.append(bugReport.getReportClass());
            if (StringUtils.isNotBlank(bugReport.getReportType())) {
                title.append(" - ");
                title.append(bugReport.getReportType());
            }
            title.append(": ");
        }
        if (StringUtils.isNotBlank(bugReport.getTitle())) {
            title.append(bugReport.getTitle());
        }

        StringBuffer emailRecipients = new StringBuffer();
        for (String recipient : this.recipients) {
            if (emailRecipients.length() > 0) {
                emailRecipients.append(",");
            }
            emailRecipients.append(recipient);
        }

        EmailMessageBean emailMessage = new EmailMessageBean();
        emailMessage.setSubject(title.toString());
        emailMessage.setMessage(bugReport.getDescription());
        emailMessage.setFrom(bugReport.getUser().getEmail());
        emailMessage.setFromName(bugReport.getUser().getPreferredName() + " " + bugReport.getUser().getLastName());
        emailMessage.setTo(emailRecipients.toString());

        this.emailMessageDAO.send(emailMessage);

    }

}