From: Roland Häder Date: Tue, 17 May 2016 13:36:43 +0000 (+0200) Subject: Implemented sendDeliverableMail() which takes a wrapper object as parameter X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=193162a4bf9a157172165fa41e599c147066e8de;p=jfinancials-mailer-lib.git Implemented sendDeliverableMail() which takes a wrapper object as parameter --- diff --git a/src/org/mxchange/addressbook/mailer/model/delivery/AddressbookMailer.java b/src/org/mxchange/addressbook/mailer/model/delivery/AddressbookMailer.java index a1189a4..a074416 100644 --- a/src/org/mxchange/addressbook/mailer/model/delivery/AddressbookMailer.java +++ b/src/org/mxchange/addressbook/mailer/model/delivery/AddressbookMailer.java @@ -16,9 +16,15 @@ */ package org.mxchange.addressbook.mailer.model.delivery; +import java.text.MessageFormat; +import java.util.Map; import javax.annotation.Resource; +import javax.mail.MessagingException; import javax.mail.Session; +import org.apache.velocity.Template; +import org.apache.velocity.VelocityContext; import org.mxchange.jmailee.model.delivery.BaseMailer; +import org.mxchange.jmailee.model.delivery.wrapper.WrapableEmailDelivery; /** * A mailer for JJobs project @@ -38,4 +44,68 @@ public class AddressbookMailer extends BaseMailer implements DeliverableAddressb @Resource (name = "jmail/addressbook") private Session mailSession; + @Override + public void sendDeliverableMail (final WrapableEmailDelivery emailWrapper) throws MessagingException { + // Log trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("sendDeliverableMail: emailWrapper={0} - CALLED!", emailWrapper)); //NOI18N + + // The parameter must be valid + if (null == emailWrapper) { + // Throw NPE + throw new NullPointerException("emailWrapper is null"); //NOI18N + } else if (emailWrapper.getRecipient() == null) { + // Throw NPE again + throw new NullPointerException("emailWrapper.recipient is null"); //NOI18N + } else if (emailWrapper.getSubjectLine() == null) { + // ... and again + throw new NullPointerException("emailWrapper.subjectLine is null"); //NOI18N + } else if (emailWrapper.getSubjectLine().isEmpty()) { + // Is empty + throw new IllegalArgumentException("emailWrapper.subjectLine is empty"); //NOI18N + } else if (emailWrapper.getTemplateName() == null) { + // ... and again + throw new NullPointerException("emailWrapper.templateName is null"); //NOI18N + } else if (emailWrapper.getTemplateName().isEmpty()) { + // Is empty + throw new IllegalArgumentException("emailWrapper.templateName is empty"); //NOI18N + } else if (emailWrapper.getLocale() == null) { + // Throw NPE again + throw new NullPointerException("emailWrapper.locale is null"); //NOI18N + } + + // All required data is set, load template + Template template = this.getTemplateEngine().getTemplate(String.format("./templates/%s/%s.vm", emailWrapper.getLocale().getLanguage().toLowerCase(), emailWrapper.getTemplateName())); //NOI18N + + // Init context + VelocityContext context = new VelocityContext(); + + // Are some variables set? + if ((emailWrapper.getTemplateVariables() != null) && (!emailWrapper.getTemplateVariables().isEmpty())) { + // Add all variables + for (Map.Entry entry : emailWrapper.getTemplateVariables().entrySet()) { + // Get key/value + String key = (String) entry.getKey(); + String value = (String) entry.getValue(); + + // Both should not be empty + if (null == key) { + // Throw NPE + throw new NullPointerException("key is null"); //NOI18N + } else if (null == value) { + // Throw NPE again + throw new NullPointerException(MessageFormat.format("value for key={0} is null", key)); //NOI18N + } + + // Set it + context.put(key, value); + } + } + + // Send the email + this.sendMailTemplate(template, context, emailWrapper, this.mailSession); + + // Trace message + this.getLoggerBeanLocal().logTrace("sendDeliverableMail: EXIT!"); //NOI18N + } + } diff --git a/src/org/mxchange/addressbook/mailer/model/delivery/DeliverableAddressbookEmail.java b/src/org/mxchange/addressbook/mailer/model/delivery/DeliverableAddressbookEmail.java index 5203372..d5d598d 100644 --- a/src/org/mxchange/addressbook/mailer/model/delivery/DeliverableAddressbookEmail.java +++ b/src/org/mxchange/addressbook/mailer/model/delivery/DeliverableAddressbookEmail.java @@ -16,7 +16,9 @@ */ package org.mxchange.addressbook.mailer.model.delivery; +import javax.mail.MessagingException; import org.mxchange.jmailee.model.delivery.DeliverableEmail; +import org.mxchange.jmailee.model.delivery.wrapper.WrapableEmailDelivery; /** * An interface for deliverable emails by JJobs project. @@ -25,4 +27,13 @@ import org.mxchange.jmailee.model.delivery.DeliverableEmail; */ public interface DeliverableAddressbookEmail extends DeliverableEmail { + /** + * Sends given deliverable mail instance. This must have set a template + * name, a recipient address and a subject line set. + *

+ * @param emailWrapper Deliverable mail wrapper + * @throws MessagingException If something bad happened + */ + void sendDeliverableMail (final WrapableEmailDelivery emailWrapper) throws MessagingException; + }