/*
- * Copyright (C) 2016 quix0r
+ * Copyright (C) 2016, 2017 Roland Häder
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
*/
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
* <p>
- * @author Roland Haeder<roland@mxchange.org>
+ * @author Roland Häder<roland@mxchange.org>
*/
public class AddressbookMailer extends BaseMailer implements DeliverableAddressbookEmail {
*/
private static final long serialVersionUID = 17_857_816_596_030_918L;
+ /**
+ * Email session
+ */
+ @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
+ } else if (!emailWrapper.getTemplateVariables().containsKey("baseUrl")) { //NOI18N
+ // Not set
+ throw new IllegalArgumentException("emailWrapper.templateVariables.baseUrl is not set"); //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
+ }
+
}