de.hybris.platform.lichextendtrail.emailservice.DefaultLichEmailService.java Source code

Java tutorial

Introduction

Here is the source code for de.hybris.platform.lichextendtrail.emailservice.DefaultLichEmailService.java

Source

/*
 * [y] hybris Platform
 * 
 * Copyright (c) 2000-2016 SAP SE
 * All rights reserved.
 * 
 * This software is the confidential and proprietary information of SAP 
 * Hybris ("Confidential Information"). You shall not disclose such 
 * Confidential Information and shall use it only in accordance with the 
 * terms of the license agreement you entered into with SAP Hybris.
 */
package de.hybris.platform.lichextendtrail.emailservice;

import de.hybris.platform.acceleratorservices.email.impl.DefaultEmailService;
import de.hybris.platform.acceleratorservices.model.email.EmailAddressModel;
import de.hybris.platform.acceleratorservices.model.email.EmailAttachmentModel;
import de.hybris.platform.acceleratorservices.model.email.EmailMessageModel;
import de.hybris.platform.lichextendtrail.global.LichValue;

import java.util.Date;
import java.util.List;

import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.HtmlEmail;
import org.apache.log4j.Logger;
import org.apache.poi.hssf.record.formula.functions.Value;

/**
 *
 */
public class DefaultLichEmailService extends DefaultEmailService {
    private static final Logger LOG = Logger.getLogger(DefaultLichEmailService.class);

    @Override
    public boolean send(EmailMessageModel message) {
        if (message == null) {
            throw new IllegalArgumentException("message must not be null");
        }

        final boolean sendEnabled = getConfigurationService().getConfiguration()
                .getBoolean(EMAILSERVICE_SEND_ENABLED_CONFIG_KEY, true);
        if (sendEnabled) {
            try {
                final HtmlEmail email = getPerConfiguredEmail();
                email.setCharset("UTF-8");

                final List<EmailAddressModel> toAddresses = message.getToAddresses();
                setAddresses(message, email, toAddresses);

                final EmailAddressModel fromAddress = message.getFromAddress();
                email.setFrom(fromAddress.getEmailAddress(), nullifyEmpty(fromAddress.getDisplayName()));
                addReplyTo(message, email);
                email.setSubject(message.getSubject());
                //???
                if (message.getSubject().equals("?Customer Registration\n")) {
                    email.setSubject("?");
                    //??
                    String emailToAddress = email.getToAddresses().get(0).toString();
                    emailToAddress = emailToAddress.substring(emailToAddress.indexOf('<') + 1,
                            emailToAddress.indexOf('>'));
                    //
                    email.setHtmlMsg(LichValue.getEmailContent(emailToAddress));
                }
                //??? 
                else {
                    email.setHtmlMsg(getBody(message));
                }
                // To support plain text parts use email.setTextMsg()

                final List<EmailAttachmentModel> attachments = message.getAttachments();
                if (!processAttachmentsSuccessful(email, attachments)) {
                    return false;
                }

                // Important to log all emails sent out
                LOG.info("Sending Email [" + message.getPk() + "] To [" + convertToStrings(toAddresses) + "] From ["
                        + fromAddress.getEmailAddress() + "] Subject [" + email.getSubject() + "]");

                // Send the email and capture the message ID
                final String messageID = email.send();

                message.setSent(true);
                message.setSentMessageID(messageID);
                message.setSentDate(new Date());
                getModelService().save(message);

                return true;
            } catch (final EmailException e) {
                logInfo(message, e);
            }
        } else {
            LOG.warn("Could not send e-mail pk [" + message.getPk() + "] subject [" + message.getSubject() + "]");
            LOG.info("Email sending has been disabled. Check the config property 'emailservice.send.enabled'");
            return true;
        }

        return false;
    }
}