]> git.mxchange.org Git - jjobs-mailer-lib.git/blob - src/org/mxchange/addressbook/mailer/model/delivery/AddressbookMailer.java
e0e9a5b42e7bfcaa6a99424484467d9d0ef6ed2d
[jjobs-mailer-lib.git] / src / org / mxchange / addressbook / mailer / model / delivery / AddressbookMailer.java
1 /*
2  * Copyright (C) 2016, 2017 Roland Häder
3  *
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.
8  *
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.
13  *
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/>.
16  */
17 package org.mxchange.addressbook.mailer.model.delivery;
18
19 import java.text.MessageFormat;
20 import java.util.Map;
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;
28
29 /**
30  * A mailer for JJobs project
31  * <p>
32  * @author Roland Häder<roland@mxchange.org>
33  */
34 public class AddressbookMailer extends BaseMailer implements DeliverableAddressbookEmail {
35
36         /**
37          * Serial number
38          */
39         private static final long serialVersionUID = 17_857_816_596_030_918L;
40
41         /**
42          * Email session
43          */
44         @Resource (name = "jmail/addressbook", description = "A JavaMail resource")
45         private Session mailSession;
46
47         @Override
48         public void sendDeliverableMail (final WrapableEmailDelivery emailWrapper) throws MessagingException {
49                 // Log trace message
50                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("sendDeliverableMail: emailWrapper={0} - CALLED!", emailWrapper)); //NOI18N
51
52                 // The parameter must be valid
53                 if (null == emailWrapper) {
54                         // Throw NPE
55                         throw new NullPointerException("emailWrapper is null"); //NOI18N
56                 } else if (emailWrapper.getRecipient() == null) {
57                         // Throw NPE again
58                         throw new NullPointerException("emailWrapper.recipient is null"); //NOI18N
59                 } else if (emailWrapper.getSubjectLine() == null) {
60                         // ... and again
61                         throw new NullPointerException("emailWrapper.subjectLine is null"); //NOI18N
62                 } else if (emailWrapper.getSubjectLine().isEmpty()) {
63                         // Is empty
64                         throw new IllegalArgumentException("emailWrapper.subjectLine is empty"); //NOI18N
65                 } else if (emailWrapper.getTemplateName() == null) {
66                         // ... and again
67                         throw new NullPointerException("emailWrapper.templateName is null"); //NOI18N
68                 } else if (emailWrapper.getTemplateName().isEmpty()) {
69                         // Is empty
70                         throw new IllegalArgumentException("emailWrapper.templateName is empty"); //NOI18N
71                 } else if (emailWrapper.getLocale() == null) {
72                         // Throw NPE again
73                         throw new NullPointerException("emailWrapper.locale is null"); //NOI18N
74                 } else if (!emailWrapper.getTemplateVariables().containsKey("baseUrl")) { //NOI18N
75                         // Not set
76                         throw new IllegalArgumentException("emailWrapper.templateVariables.baseUrl is not set"); //NOI18N
77                 }
78
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
81
82                 // Init context
83                 VelocityContext context = new VelocityContext();
84
85                 // Are some variables set?
86                 if ((emailWrapper.getTemplateVariables() != null) && (!emailWrapper.getTemplateVariables().isEmpty())) {
87                         // Add all variables
88                         for (Map.Entry<Object, Object> entry : emailWrapper.getTemplateVariables().entrySet()) {
89                                 // Get key/value
90                                 String key = (String) entry.getKey();
91                                 String value = (String) entry.getValue();
92
93                                 // Both should not be empty
94                                 if (null == key) {
95                                         // Throw NPE
96                                         throw new NullPointerException("key is null"); //NOI18N
97                                 } else if (null == value) {
98                                         // Throw NPE again
99                                         throw new NullPointerException(MessageFormat.format("value for key={0} is null", key)); //NOI18N
100                                 }
101
102                                 // Set it
103                                 context.put(key, value);
104                         }
105                 }
106
107                 // Send the email
108                 this.sendMailTemplate(template, context, emailWrapper, this.mailSession);
109
110                 // Trace message
111                 this.getLoggerBeanLocal().logTrace("sendDeliverableMail: EXIT!"); //NOI18N
112         }
113
114 }