Search in sources :

Example 16 with BodyMailMessage

use of org.kuali.kfs.sys.mail.BodyMailMessage in project cu-kfs by CU-CommunityApps.

the class CuPdpEmailServiceImpl method sendAchAdviceEmail.

/**
 * Send advice notification email to the payee receiving an ACH payment for both bundled and unbundled ACH payments.
 *
 * KFSPTS-1460:
 * New method signature due to need for refactoring to deal with both the unbundled and bundled cases.
 * The major change is that the paymentDetail input parameter is now a list of payment details instead of being a singleton.
 * The caller will pass the entire list of payment detail records and sendAchAdviceEmail will loop through them taking into
 * account cases for multiples and singletons when creating and sending the advice emails.
 *
 * @param paymentGroup Payment group corresponding to the payment detail records
 * @param paymentDetails List of all payment details to process for the single advice email being sent
 * @param customer Pdp customer profile for payment
 */
public void sendAchAdviceEmail(PaymentGroup paymentGroup, List<PaymentDetail> paymentDetails, CustomerProfile customer) {
    LOG.debug("sendAchAdviceEmail() with payment details list starting");
    Integer numPayments = 0;
    String productionEnvironmentCode = kualiConfigurationService.getPropertyValueAsString(Config.PROD_ENVIRONMENT_CODE);
    String environmentCode = kualiConfigurationService.getPropertyValueAsString(Config.ENVIRONMENT);
    boolean shouldBundleAchPayments = this.getAchBundlerHelperService().shouldBundleAchPayments();
    if (shouldBundleAchPayments) {
        // Send out one email to the payee listing all the payment details for the specified payment group
        BodyMailMessage bundledMessage = createAdviceMessageAndPopulateHeader(paymentGroup, customer, productionEnvironmentCode, environmentCode);
        // create the formatted body
        // this seems wasteful, but since the total net amount is needed in the message body before the payment details...it's needed
        KualiDecimal totalNetAmount = new KualiDecimal(0);
        Iterator<PaymentDetail> pdToNetAmountIter = paymentDetails.iterator();
        while (pdToNetAmountIter.hasNext()) {
            numPayments = numPayments + 1;
            PaymentDetail pd = pdToNetAmountIter.next();
            totalNetAmount = totalNetAmount.add(pd.getNetPaymentAmount());
        }
        // max # of payment detail records to include in email body as well as in the attachment, only used for non-DV advices
        int maxNumDetailsForBody = 10;
        StringBuffer bundledBody = createAdviceMessageBody(paymentGroup, customer, totalNetAmount, numPayments);
        // format payment details based on the whether it is a DV or a PREQ
        // formatting of payment details for DV is different than for PREQ
        boolean adviceIsForDV = false;
        // first time through loop
        boolean firstPass = true;
        StringBuffer bundledAtachmentData = new StringBuffer();
        for (Iterator<PaymentDetail> payDetailsIter = paymentDetails.iterator(); payDetailsIter.hasNext(); ) {
            PaymentDetail paymentDetail = payDetailsIter.next();
            // initialize data the first time through the loop
            if (firstPass) {
                adviceIsForDV = (paymentDetail.getFinancialDocumentTypeCode().equalsIgnoreCase(DisbursementVoucherConstants.DOCUMENT_TYPE_CHECKACH)) ? true : false;
                if (adviceIsForDV) {
                    // we will NOT be sending an attachment, all payment detail will be in the body of the message
                    bundledBody.append(getMessage(CUPdpKeyConstants.MESSAGE_PDP_ACH_ADVICE_EMAIL_BODY_PAYMENT_HEADER_LINE_ONE));
                    bundledBody.append(customer.getAdviceHeaderText());
                    bundledBody.append(getMessage(CUPdpKeyConstants.MESSAGE_PDP_ACH_ADVICE_EMAIL_BODY_PAYMENT_SEPARATOR));
                } else {
                    // we will be sending an attachment
                    bundledAtachmentData = new StringBuffer();
                    // creating the payment detail table header
                    bundledAtachmentData.append(getMessage(CUPdpKeyConstants.MESSAGE_PDP_ACH_ADVICE_EMAIL_ATTACHMENT_HEADING_SUMMARY_LINE_ONE));
                    bundledAtachmentData.append(getMessage(CUPdpKeyConstants.MESSAGE_PDP_ACH_ADVICE_EMAIL_ATTACHMENT_HEADING_SUMMARY_LINE_TWO));
                    bundledAtachmentData.append(getMessage(CUPdpKeyConstants.MESSAGE_PDP_ACH_ADVICE_EMAIL_ATTACHMENT_HEADING_SUMMARY_LINE_THREE));
                    // verbiage describing the payment details and attachment
                    bundledBody.append(getMessage(CUPdpKeyConstants.MESSAGE_PDP_ACH_ADVICE_EMAIL_BODY_DETAIL_INFO_MSG, numPayments));
                    if (numPayments <= maxNumDetailsForBody) {
                        // email body will have details and an attachment will be sent
                        bundledBody.append(getMessage(CUPdpKeyConstants.MESSAGE_PDP_ACH_ADVICE_EMAIL_BODY_DETAIL_UNDER_LIMIT_MSG));
                        // individual customer message for the payment
                        bundledBody.append(customer.getAdviceHeaderText());
                        bundledBody.append(getMessage(CUPdpKeyConstants.MESSAGE_PDP_ACH_ADVICE_EMAIL_BODY_PAYMENT_SEPARATOR));
                    }
                // implied else that numPayments is over the max so only send an attachment
                }
                // ensure headers only included once
                firstPass = false;
            }
            if (adviceIsForDV) {
                // format payment detail information and include it in the message body, do not send an attachment
                bundledBody.append(createAdviceMessagePaymentDetail(paymentGroup, paymentDetail, adviceIsForDV, shouldBundleAchPayments));
            } else {
                // put payment detail information in the attachment
                bundledAtachmentData.append(createAdviceMessagePaymentDetail(paymentGroup, paymentDetail, adviceIsForDV, shouldBundleAchPayments));
                // also put payment detail in the body when the number of payments is fewer than the max
                if (numPayments <= maxNumDetailsForBody) {
                    // explicitly using false instead of shouldBundleAchPayments so that we get the correct format for the email body
                    bundledBody.append(createAdviceMessagePaymentDetail(paymentGroup, paymentDetail, adviceIsForDV, false));
                }
            }
        }
        // for-loop
        bundledMessage.setMessage(bundledBody.toString());
        if (!adviceIsForDV) {
            // only create the attachment file when the payments are NOT for DV's
            Formatter integerFormatter = new IntegerFormatter();
            String attachmentFileName = new String("paymentDetailsForDisbursement_" + (String) integerFormatter.formatForPresentation(paymentGroup.getDisbursementNbr()) + ".csv");
            bundledMessage.setAttachmentFileName(attachmentFileName);
            bundledMessage.setAttachmentContent(bundledAtachmentData.toString().getBytes());
            bundledMessage.setAttachmentContentType(new String("text/csv"));
        }
        sendFormattedAchAdviceEmail(bundledMessage, customer, paymentGroup, productionEnvironmentCode, environmentCode);
    } else {
        // so that the appropriate mail "send" is invoked based on the message type that we created and passed.
        for (PaymentDetail paymentDetail : paymentGroup.getPaymentDetails()) {
            numPayments = paymentGroup.getPaymentDetails().size();
            BodyMailMessage nonBundledMessage = createAdviceMessageAndPopulateHeader(paymentGroup, customer, productionEnvironmentCode, environmentCode);
            StringBuffer nonBundledBody = createAdviceMessageBody(paymentGroup, customer, paymentDetail.getNetPaymentAmount(), numPayments);
            nonBundledBody.append(getMessage(CUPdpKeyConstants.MESSAGE_PDP_ACH_ADVICE_EMAIL_BODY_PAYMENT_HEADER_LINE_ONE));
            nonBundledBody.append(customer.getAdviceHeaderText());
            nonBundledBody.append(getMessage(CUPdpKeyConstants.MESSAGE_PDP_ACH_ADVICE_EMAIL_BODY_PAYMENT_SEPARATOR));
            boolean adviceIsForDV = (paymentDetail.getFinancialDocumentTypeCode().equalsIgnoreCase(DisbursementVoucherConstants.DOCUMENT_TYPE_CHECKACH)) ? true : false;
            nonBundledBody.append(createAdviceMessagePaymentDetail(paymentGroup, paymentDetail, adviceIsForDV, shouldBundleAchPayments));
            nonBundledMessage.setMessage(nonBundledBody.toString());
            sendFormattedAchAdviceEmail(nonBundledMessage, customer, paymentGroup, productionEnvironmentCode, environmentCode);
        }
    }
}
Also used : PaymentDetail(org.kuali.kfs.pdp.businessobject.PaymentDetail) CurrencyFormatter(org.kuali.kfs.core.web.format.CurrencyFormatter) DateFormatter(org.kuali.kfs.core.web.format.DateFormatter) Formatter(org.kuali.kfs.core.web.format.Formatter) IntegerFormatter(org.kuali.kfs.core.web.format.IntegerFormatter) KualiDecimal(org.kuali.kfs.core.api.util.type.KualiDecimal) IntegerFormatter(org.kuali.kfs.core.web.format.IntegerFormatter) BodyMailMessage(org.kuali.kfs.sys.mail.BodyMailMessage)

Example 17 with BodyMailMessage

use of org.kuali.kfs.sys.mail.BodyMailMessage in project cu-kfs by CU-CommunityApps.

the class CuPdpEmailServiceImpl method createAdviceMessageAndPopulateHeader.

/**
 * KFSPTS-1460: New method. created from code in sendAchAdviceEmail.
 * @return
 */
private BodyMailMessage createAdviceMessageAndPopulateHeader(PaymentGroup paymentGroup, CustomerProfile customer, String productionEnvironmentCode, String environmentCode) {
    LOG.debug("createAdviceMessageAndPopulateHeader() starting");
    BodyMailMessage message = new BodyMailMessage();
    String fromAddress = customer.getAdviceReturnEmailAddr();
    if ((fromAddress == null) || (fromAddress.isEmpty())) {
        // get the default from address
        fromAddress = parameterService.getParameterValueAsString(CUKFSParameterKeyConstants.KFS_PDP, CUKFSParameterKeyConstants.ALL_COMPONENTS, CUKFSParameterKeyConstants.PDP_CUSTOMER_MISSING_ADVICE_RETURN_EMAIL);
    }
    if (StringUtils.equals(productionEnvironmentCode, environmentCode)) {
        message.addToAddress(paymentGroup.getAdviceEmailAddress());
        message.addCcAddress(paymentGroup.getAdviceEmailAddress());
        message.addBccAddress(paymentGroup.getAdviceEmailAddress());
        message.setFromAddress(fromAddress);
        message.setSubject(customer.getAdviceSubjectLine());
    } else {
        message.addToAddress(emailService.getDefaultToAddress());
        message.addCcAddress(emailService.getDefaultToAddress());
        message.addBccAddress(emailService.getDefaultToAddress());
        message.setFromAddress(fromAddress);
        message.setSubject(environmentCode + ": " + customer.getAdviceSubjectLine() + ":" + paymentGroup.getAdviceEmailAddress());
    }
    LOG.debug("sending email to " + paymentGroup.getAdviceEmailAddress() + " for disb # " + paymentGroup.getDisbursementNbr());
    return message;
}
Also used : BodyMailMessage(org.kuali.kfs.sys.mail.BodyMailMessage)

Example 18 with BodyMailMessage

use of org.kuali.kfs.sys.mail.BodyMailMessage in project cu-kfs by CU-CommunityApps.

the class RassReportServiceImpl method sendReportEmail.

protected void sendReportEmail() {
    BodyMailMessage message = new BodyMailMessage();
    String fromAddress = getFromAddress();
    Collection<String> toAddresses = getToAddresses();
    message.setFromAddress(fromAddress);
    String subject = reportWriterService.getTitle();
    message.setSubject(subject);
    message.getToAddresses().addAll(toAddresses);
    String body = getFileContents(reportWriterService.getReportFile().getAbsolutePath());
    message.setMessage(body);
    boolean htmlMessage = false;
    if (LOG.isDebugEnabled()) {
        LOG.debug("sendEmail, from address: " + fromAddress + "  to address: " + toAddresses.toString());
        LOG.debug("sendEmail, the email subject: " + subject);
        LOG.debug("sendEmail, the email budy: " + body);
    }
    try {
        emailService.sendMessage(message, htmlMessage);
    } catch (Exception e) {
        LOG.error("sendEmail, the email could not be sent", e);
    }
}
Also used : BodyMailMessage(org.kuali.kfs.sys.mail.BodyMailMessage)

Example 19 with BodyMailMessage

use of org.kuali.kfs.sys.mail.BodyMailMessage in project cu-kfs by CU-CommunityApps.

the class CUTransactionFilter method doFilter.

public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
    boolean error = false;
    String errorText = "";
    HttpServletRequest httpReq = (HttpServletRequest) req;
    if (!TransactionSynchronizationManager.getResourceMap().isEmpty()) {
        errorText = errorText + ("Before: The Resource map is not empty.   THIS IS SOOO BAD.   URL: " + httpReq.getRequestURL());
        LOG.error("Before: The Resource map is not empty.   THIS IS SOOO BAD.   URL: " + httpReq.getRequestURL());
        Map aMap = TransactionSynchronizationManager.getResourceMap();
        int mapsize = aMap.size();
        Iterator keyValuePairs1 = aMap.entrySet().iterator();
        for (int i = 0; i < mapsize; i++) {
            Map.Entry entry = (Map.Entry) keyValuePairs1.next();
            errorText = errorText + (";  Resources:  key: " + entry.getKey().toString() + " ; Value " + entry.getValue().toString());
            LOG.error("Resources:  key: " + entry.getKey().toString() + " ; Value " + entry.getValue().toString());
        }
        error = true;
    }
    try {
        chain.doFilter(req, res);
    } finally {
        if (TransactionSynchronizationManager.isSynchronizationActive()) {
            LOG.error("JTA synchronizations are not active.   THIS IS SOOO BAD.   " + httpReq.getRequestURL());
            error = true;
        }
        if (!TransactionSynchronizationManager.getResourceMap().isEmpty()) {
            errorText = errorText + "After: The Resource map is not empty.   THIS IS SOOO BAD.";
            LOG.error("After: The Resource map is not empty.   THIS IS SOOO BAD.   URL: " + httpReq.getRequestURL());
            Map aMap = TransactionSynchronizationManager.getResourceMap();
            int mapsize = aMap.size();
            Iterator keyValuePairs1 = aMap.entrySet().iterator();
            for (int i = 0; i < mapsize; i++) {
                Map.Entry entry = (Map.Entry) keyValuePairs1.next();
                errorText = errorText + (";  Resources:  key: " + entry.getKey().toString() + " ; Value " + entry.getValue().toString());
                LOG.error("Resources:  key: " + entry.getKey().toString() + " ; Value " + entry.getValue().toString());
            }
            error = true;
        }
        if (error) {
            try {
                LOG.error("ERROR Found!  Sending an email...");
                BodyMailMessage mm = new BodyMailMessage();
                mm.addToAddress(SpringContext.getBean(EmailService.class).getDefaultToAddress());
                mm.setFromAddress(SpringContext.getBean(EmailService.class).getDefaultFromAddress());
                mm.setSubject("There might be a closed connection problem.  Please check the logs. Seach for the following phrase: SOOO BAD");
                mm.setMessage("Request URL which had the problem: " + httpReq.getRequestURL() + "    errorText = " + errorText);
                SpringContext.getBean(EmailService.class).sendMessage(mm, false);
            } catch (Throwable t) {
                LOG.error("Error sending email.", t);
            }
        }
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) Iterator(java.util.Iterator) EmailService(org.kuali.kfs.sys.service.EmailService) Map(java.util.Map) BodyMailMessage(org.kuali.kfs.sys.mail.BodyMailMessage)

Aggregations

BodyMailMessage (org.kuali.kfs.sys.mail.BodyMailMessage)19 ArrayList (java.util.ArrayList)3 LoadAchIncomeFileStep (edu.cornell.kfs.fp.batch.LoadAchIncomeFileStep)1 TemplateException (freemarker.template.TemplateException)1 IOException (java.io.IOException)1 Iterator (java.util.Iterator)1 Map (java.util.Map)1 AddressException (javax.mail.internet.AddressException)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 KualiDecimal (org.kuali.kfs.core.api.util.type.KualiDecimal)1 CurrencyFormatter (org.kuali.kfs.core.web.format.CurrencyFormatter)1 DateFormatter (org.kuali.kfs.core.web.format.DateFormatter)1 Formatter (org.kuali.kfs.core.web.format.Formatter)1 IntegerFormatter (org.kuali.kfs.core.web.format.IntegerFormatter)1 ParameterService (org.kuali.kfs.coreservice.framework.parameter.ParameterService)1 WorkflowDocument (org.kuali.kfs.kew.api.WorkflowDocument)1 Person (org.kuali.kfs.kim.api.identity.Person)1 ValidationException (org.kuali.kfs.krad.exception.ValidationException)1 PaymentDetail (org.kuali.kfs.pdp.businessobject.PaymentDetail)1 EmailService (org.kuali.kfs.sys.service.EmailService)1