]> git.mxchange.org Git - jjobs-mailer-lib.git/commitdiff
Implemented sendDeliverableMail() which takes a wrapper object as parameter
authorRoland Häder <roland@mxchange.org>
Tue, 17 May 2016 13:36:43 +0000 (15:36 +0200)
committerRoland Haeder <roland@mxchange.org>
Sat, 21 May 2016 12:12:57 +0000 (14:12 +0200)
src/org/mxchange/addressbook/mailer/model/delivery/AddressbookMailer.java
src/org/mxchange/addressbook/mailer/model/delivery/DeliverableAddressbookEmail.java

index a1189a4a91c36ac5cae77fcbd2102da250cbb474..a07441695025567a9230d06aac4b4fffa4469346 100644 (file)
  */
 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<Object, Object> 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
+       }
+
 }
index 5203372494aee3835a749563fbbe45e1d87ed024..d5d598d6d042ff5c81b3bc4d7ab117a04abb00e4 100644 (file)
@@ -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.
+        * <p>
+        * @param emailWrapper Deliverable mail wrapper
+        * @throws MessagingException If something bad happened
+        */
+       void sendDeliverableMail (final WrapableEmailDelivery emailWrapper) throws MessagingException;
+
 }