]> git.mxchange.org Git - jjobs-mailer-lib.git/blob - src/org/mxchange/addressbook/mailer/model/delivery/AddressbookMailer.java
Please cherry-pick:
[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.mail.MessagingException;
22 import javax.mail.Session;
23 import javax.naming.Context;
24 import javax.naming.InitialContext;
25 import javax.naming.NamingException;
26 import org.apache.velocity.Template;
27 import org.apache.velocity.VelocityContext;
28 import org.mxchange.jmailee.model.delivery.BaseMailer;
29 import org.mxchange.jmailee.model.delivery.wrapper.WrapableEmailDelivery;
30
31 /**
32  * A mailer for JJobs project
33  * <p>
34  * @author Roland Häder<roland@mxchange.org>
35  */
36 public class AddressbookMailer extends BaseMailer implements DeliverableAddressbookEmail {
37
38         /**
39          * Serial number
40          */
41         private static final long serialVersionUID = 17_857_816_596_030_918L;
42
43         /**
44          * Email session
45          */
46         private final Session mailSession;
47
48         /**
49          * Default constructor
50          * <p>
51          * @throws javax.naming.NamingException If the JNDI name is not found
52          */
53         public AddressbookMailer () throws NamingException {
54                 // Get initial context
55                 Context context = new InitialContext();
56
57                 // Try to lookup
58                 this.mailSession = (Session) context.lookup("java:global/jfinancials-mailer/smtpSession"); //NOI18N
59         }
60
61         @Override
62         public void sendDeliverableMail (final WrapableEmailDelivery emailWrapper) throws MessagingException {
63                 // Log trace message
64                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("sendDeliverableMail: emailWrapper={0} - CALLED!", emailWrapper)); //NOI18N
65
66                 // The parameter must be valid
67                 if (null == emailWrapper) {
68                         // Throw NPE
69                         throw new NullPointerException("emailWrapper is null"); //NOI18N
70                 } else if (emailWrapper.getRecipientAddress() == null) {
71                         // Throw NPE again
72                         throw new NullPointerException("emailWrapper.recipientAddress is null"); //NOI18N
73                 } else if (emailWrapper.getSubjectLine() == null) {
74                         // ... and again
75                         throw new NullPointerException("emailWrapper.subjectLine is null"); //NOI18N
76                 } else if (emailWrapper.getSubjectLine().isEmpty()) {
77                         // Is empty
78                         throw new IllegalArgumentException("emailWrapper.subjectLine is empty"); //NOI18N
79                 } else if (emailWrapper.getTemplateName() == null) {
80                         // ... and again
81                         throw new NullPointerException("emailWrapper.templateName is null"); //NOI18N
82                 } else if (emailWrapper.getTemplateName().isEmpty()) {
83                         // Is empty
84                         throw new IllegalArgumentException("emailWrapper.templateName is empty"); //NOI18N
85                 } else if (emailWrapper.getLocale() == null) {
86                         // Throw NPE again
87                         throw new NullPointerException("emailWrapper.locale is null"); //NOI18N
88                 } else if (!emailWrapper.getTemplateVariables().containsKey("baseUrl")) { //NOI18N
89                         // Not set
90                         throw new IllegalArgumentException("emailWrapper.templateVariables.baseUrl is not set"); //NOI18N
91                 }
92
93                 // All required data is set, load template
94                 Template template = this.getTemplateEngine().getTemplate(String.format("templates/%s/%s.vm", emailWrapper.getLocale().getLanguage().toLowerCase(), emailWrapper.getTemplateName())); //NOI18N
95
96                 // Init context
97                 VelocityContext context = new VelocityContext();
98
99                 // Are some variables set?
100                 if ((emailWrapper.getTemplateVariables() != null) && (!emailWrapper.getTemplateVariables().isEmpty())) {
101                         // Add all variables
102                         for (Map.Entry<Object, Object> entry : emailWrapper.getTemplateVariables().entrySet()) {
103                                 // Get key/value
104                                 String key = (String) entry.getKey();
105                                 String value = (String) entry.getValue();
106
107                                 // Both should not be empty
108                                 if (null == key) {
109                                         // Throw NPE
110                                         throw new NullPointerException("key is null"); //NOI18N
111                                 } else if (null == value) {
112                                         // Throw NPE again
113                                         throw new NullPointerException(MessageFormat.format("value for key={0} is null", key)); //NOI18N
114                                 }
115
116                                 // Set it
117                                 context.put(key, value);
118                         }
119                 }
120
121                 // Send the email
122                 this.sendMailTemplate(template, context, emailWrapper, this.mailSession);
123
124                 // Trace message
125                 this.getLoggerBeanLocal().logTrace("sendDeliverableMail: EXIT!"); //NOI18N
126         }
127
128 }