2 * Copyright (C) 2016, 2017 Roland Häder
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 package org.mxchange.addressbook.mailer.model.delivery;
19 import java.text.MessageFormat;
21 import javax.annotation.Resource;
22 import javax.mail.MessagingException;
23 import javax.mail.Session;
24 import org.apache.velocity.Template;
25 import org.apache.velocity.VelocityContext;
26 import org.mxchange.jmailee.model.delivery.BaseMailer;
27 import org.mxchange.jmailee.model.delivery.wrapper.WrapableEmailDelivery;
30 * A mailer for JJobs project
32 * @author Roland Häder<roland@mxchange.org>
34 public class AddressbookMailer extends BaseMailer implements DeliverableAddressbookEmail {
39 private static final long serialVersionUID = 17_857_816_596_030_918L;
44 @Resource (name = "jmail/addressbook")
45 private Session mailSession;
48 public void sendDeliverableMail (final WrapableEmailDelivery emailWrapper) throws MessagingException {
50 this.getLoggerBeanLocal().logTrace(MessageFormat.format("sendDeliverableMail: emailWrapper={0} - CALLED!", emailWrapper)); //NOI18N
52 // The parameter must be valid
53 if (null == emailWrapper) {
55 throw new NullPointerException("emailWrapper is null"); //NOI18N
56 } else if (emailWrapper.getRecipient() == null) {
58 throw new NullPointerException("emailWrapper.recipient is null"); //NOI18N
59 } else if (emailWrapper.getSubjectLine() == null) {
61 throw new NullPointerException("emailWrapper.subjectLine is null"); //NOI18N
62 } else if (emailWrapper.getSubjectLine().isEmpty()) {
64 throw new IllegalArgumentException("emailWrapper.subjectLine is empty"); //NOI18N
65 } else if (emailWrapper.getTemplateName() == null) {
67 throw new NullPointerException("emailWrapper.templateName is null"); //NOI18N
68 } else if (emailWrapper.getTemplateName().isEmpty()) {
70 throw new IllegalArgumentException("emailWrapper.templateName is empty"); //NOI18N
71 } else if (emailWrapper.getLocale() == null) {
73 throw new NullPointerException("emailWrapper.locale is null"); //NOI18N
74 } else if (!emailWrapper.getTemplateVariables().containsKey("baseUrl")) { //NOI18N
76 throw new IllegalArgumentException("emailWrapper.templateVariables.baseUrl is not set"); //NOI18N
79 // All required data is set, load template
80 Template template = this.getTemplateEngine().getTemplate(String.format("templates/%s/%s.vm", emailWrapper.getLocale().getLanguage().toLowerCase(), emailWrapper.getTemplateName())); //NOI18N
83 VelocityContext context = new VelocityContext();
85 // Are some variables set?
86 if ((emailWrapper.getTemplateVariables() != null) && (!emailWrapper.getTemplateVariables().isEmpty())) {
88 for (Map.Entry<Object, Object> entry : emailWrapper.getTemplateVariables().entrySet()) {
90 String key = (String) entry.getKey();
91 String value = (String) entry.getValue();
93 // Both should not be empty
96 throw new NullPointerException("key is null"); //NOI18N
97 } else if (null == value) {
99 throw new NullPointerException(MessageFormat.format("value for key={0} is null", key)); //NOI18N
103 context.put(key, value);
108 this.sendMailTemplate(template, context, emailWrapper, this.mailSession);
111 this.getLoggerBeanLocal().logTrace("sendDeliverableMail: EXIT!"); //NOI18N