com.webbfontaine.valuewebb.utils.TTMailService.java Source code

Java tutorial

Introduction

Here is the source code for com.webbfontaine.valuewebb.utils.TTMailService.java

Source

package com.webbfontaine.valuewebb.utils;

import com.webbfontaine.tools.email.Attachment;
import com.webbfontaine.tools.email.EmailSender;
import com.webbfontaine.valuewebb.action.tt.TtPrinting;
import com.webbfontaine.valuewebb.model.TtGen;
import com.webbfontaine.valuewebb.props.ApplicationProperties;
import org.apache.commons.lang3.StringUtils;
import org.jboss.seam.Component;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.annotations.async.Asynchronous;
import org.jboss.seam.international.LocaleSelector;
import org.jboss.seam.log.Log;
import org.jboss.seam.log.Logging;

import java.util.Collection;
import java.util.List;
import java.util.Locale;

/**
 * Created by IntelliJ IDEA.
 * User: Arabyan Aram
 * Date: Sep 28, 2010
 * <p/>
 * This software is the proprietary information of Webb Fontaine.
 * Its use is subject to License terms.
 * <p/>
 */

@Name("ttMailService")
@Scope(ScopeType.STATELESS)
public class TTMailService {

    private static final Log LOGGER = Logging.getLog(TTMailService.class);

    @Asynchronous
    public void sendMailForResponseOk(TtGen ttGen, Locale locale) {
        LocaleSelector.instance().setLocale(locale);
        if (!ApplicationProperties.isTestMode() && !StringUtils.isEmpty(ApplicationProperties.getSmtpServer())) {
            TTMailUtils ttMailUtils = new TTMailUtils(ttGen);
            List<String> recepients = ttMailUtils.getMailRecepientsForResponseOk();

            if (recepients == null || recepients.isEmpty()) {
                LOGGER.warn("No mail recipients specified for Sending Response Ok message for TT {0}",
                        ttGen.getId());
            } else {
                TtPrinting ttPrinting = new TtPrinting();

                byte[] report = ttPrinting.prepareFCVRReport(ttGen, false).generateReport();

                try {
                    EmailSender.sendMail(ttMailUtils.getResponseOkMailSubject(),
                            ttMailUtils.getResponseOkMailBody(), false,
                            new Attachment(report, "application/pdf", "", ttGen.getFcvrNum() + ".pdf"),
                            ApplicationProperties.getEmailFrom(), ApplicationProperties.getSmtpServer(),
                            ApplicationProperties.getSmtpServerPort(), recepients.toArray());
                    LOGGER.info("TT ID: {0}. Sending Mail For ResponseOk to {1} done. Attachment size {2}",
                            ttGen.getId(), recepients, report.length);
                } catch (Exception e) {
                    LOGGER.error("TT ID: {0}. Error during sending Mail For ResponseOk to {1}. Attachment size {2}",
                            e, ttGen.getId(), recepients, report.length);
                }
            }
        } else {
            LOGGER.info("Response Ok Mail message sending skipped for TT {0}. Conf: Test Mode {1}, SMTP Server {2}",
                    ttGen.getId(), ApplicationProperties.isTestMode(), ApplicationProperties.getSmtpServer());
        }
    }

    @Asynchronous
    public void sendSMSForResponseOk(TtGen ttGen, Locale locale) {
        LocaleSelector.instance().setLocale(locale);
        if (!ApplicationProperties.isTestMode() && !StringUtils.isEmpty(ApplicationProperties.getSmsGateway())) {
            TTMailUtils ttMailUtils = new TTMailUtils(ttGen);
            Collection<String> recepients = ttMailUtils.getSMSRecepientsForResponseOk();

            if (recepients == null || recepients.isEmpty()) {
                LOGGER.warn("No sms recipients specified for Sending Response Ok message for TT {0}",
                        ttGen.getId());
            } else {
                for (String recepient : recepients) {
                    recepient = StringUtils.trim(recepient);
                    try {
                        EmailSender.sendMail(recepient, ttMailUtils.getResponseOkSMSBody(), false, null,
                                ApplicationProperties.getSmsFrom(), ApplicationProperties.getSmtpServer(),
                                ApplicationProperties.getSmtpServerPort(), ApplicationProperties.getSmsGateway());
                        LOGGER.info("TT ID: {0}. Sending SMS for ResponseOk to {1} done.", ttGen.getId(),
                                recepient);
                    } catch (Exception e) {
                        LOGGER.error("TT ID: {0}. Error during sending sms to {1}", e, ttGen.getId(), recepient);
                    }
                }
            }
        } else {
            LOGGER.info("Response Ok SMS message sending skipped for TT {0}. Conf: Test Mode {1}, SMS Gateway {2}",
                    ttGen.getId(), ApplicationProperties.isTestMode(), ApplicationProperties.getSmsGateway());
        }
    }

    @Asynchronous
    public void sendMailForDuplicationWarning(TtGen ttGen) {
        if (!ApplicationProperties.isTestMode() && !StringUtils.isEmpty(ApplicationProperties.getSmtpServer())) {
            TTMailUtils ttMailUtils = new TTMailUtils(ttGen);

            List<String> recepients = ttMailUtils.getDuplicationWarningMailingList();
            if (recepients == null || recepients.isEmpty()) {
                LOGGER.warn("No mail recipients specified for Sending Duplication Warning message for TT {0}",
                        ttGen.getId());
            } else {
                try {
                    EmailSender.sendMail(ttMailUtils.getDuplicationWarningMailSubject(ttGen.getId()),
                            ttMailUtils.getDuplicationWarningMailBody(), true, null,
                            ApplicationProperties.getEmailFrom(), ApplicationProperties.getSmtpServer(),
                            ApplicationProperties.getSmtpServerPort(), recepients.toArray());
                    LOGGER.info("TT ID: {0}. Sending Mail For Duplication Warning to {1} done.", ttGen.getId(),
                            recepients);
                } catch (Exception e) {
                    LOGGER.error("TT ID: {0}. Error during sending Mail For Duplication Warning to {1}", e,
                            ttGen.getId(), recepients);
                }
            }
        } else {
            LOGGER.info("Duplication Mail message sending skipped for TT {0}. Conf: Test Mode {1}, SMTP Server {2}",
                    ttGen.getId(), ApplicationProperties.isTestMode(), ApplicationProperties.getSmtpServer());
        }
    }

    public static TTMailService getInstance() {
        return (TTMailService) Component.getInstance(TTMailService.class, true);
    }

}