1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package info.magnolia.module.mail;
35
36 import info.magnolia.module.ModuleRegistry;
37 import info.magnolia.module.mail.handlers.MgnlMailHandler;
38 import info.magnolia.module.mail.templates.MailAttachment;
39 import info.magnolia.module.mail.templates.MgnlEmail;
40 import info.magnolia.module.mail.templates.impl.SimpleEmail;
41 import info.magnolia.objectfactory.Classes;
42 import info.magnolia.objectfactory.Components;
43
44 import java.util.ArrayList;
45 import java.util.HashMap;
46 import java.util.List;
47 import java.util.Map;
48
49 import javax.inject.Inject;
50
51 import org.apache.commons.beanutils.BeanUtils;
52 import org.apache.commons.collections4.CollectionUtils;
53 import org.apache.commons.lang3.StringUtils;
54 import org.slf4j.Logger;
55 import org.slf4j.LoggerFactory;
56
57
58
59
60
61
62
63 public class MgnlMailFactory {
64 private final static Logger log = LoggerFactory.getLogger(MgnlMailFactory.class);
65
66 private Map<String, String> renderers = new HashMap<>();
67
68 private final MailModule mailModule;
69
70 @Inject
71 public MgnlMailFactory(MailModule mailModule) {
72 this.mailModule = mailModule;
73 }
74
75
76
77
78 public MgnlMailFactory(ModuleRegistry moduleRegistry) {
79 this(Components.getComponent(MailModule.class));
80 }
81
82 public MgnlMailHandler getEmailHandler() {
83 return mailModule.getHandler();
84 }
85
86
87
88
89 public MgnlEmail getEmail(Map<String, Object> params) {
90 return getEmail(params, null);
91 }
92
93
94
95
96 public MgnlEmail getEmailFromType(Map<String, Object> params, String type) {
97 return getEmailFromType(params, type, null);
98 }
99
100 public MgnlEmail getEmailFromType(Map<String, Object> params, String type, String contentType, List<MailAttachment> attachments) {
101 Map<String, Object> newParams = new HashMap<>();
102 newParams.putAll(params);
103 if (!StringUtils.isEmpty(type)) {
104 newParams.put(MailTemplate.MAIL_TYPE, type);
105 }
106 if (!StringUtils.isEmpty(contentType)) {
107 newParams.put(MailTemplate.MAIL_CONTENT_TYPE, contentType);
108 }
109 return getEmail(newParams, attachments);
110 }
111
112
113
114
115 public MgnlEmail getEmailFromType(Map<String, Object> params, String type, List<MailAttachment> attachments) {
116 return getEmailFromType(params, type, null, attachments);
117 }
118
119
120
121
122 public MgnlEmail getEmail(Map<String, Object> params, List<MailAttachment> attachments) {
123 MailTemplate template = new MailTemplate();
124 return getEmail(params, attachments, template);
125 }
126
127
128
129
130 public MgnlEmail getEmailFromTemplate(String id, List<MailAttachment> attachments, Map<String, Object> params) throws Exception {
131 MailTemplate template = getTemplate(id);
132 if (template == null) {
133 log.error("Template {} can't be found", id);
134 return null;
135 }
136 if (!CollectionUtils.isEmpty(attachments)) {
137
138 attachments.addAll(template.getAttachments());
139 }
140 return getEmail(params, attachments, template);
141 }
142
143
144
145
146 public MgnlEmail getEmailFromTemplate(String id, Map<String, Object> params) throws Exception {
147 return getEmailFromTemplate(id, null, params);
148 }
149
150 protected MgnlEmail getEmail(Map<String, Object> params, List<MailAttachment> attachments, MailTemplate template) {
151 template.setValues(params, attachments);
152
153 try {
154 return getEmailFromType(template);
155
156 } catch (Exception e) {
157 log.error("Couln't instantiate email type: " + template.getType(), e);
158 return null;
159 }
160 }
161
162 protected MgnlEmail getEmailFromType(MailTemplate template) throws Exception {
163 MgnlEmail mail;
164 if (StringUtils.isEmpty(template.getType()) || !renderers.containsKey(template.getType().toLowerCase())) {
165 mail = new SimpleEmail(template);
166
167 } else {
168 String rendererClass = renderers.get(template.getType().toLowerCase());
169 mail = Classes.newInstance(rendererClass, template);
170 }
171
172
173 if (!StringUtils.isEmpty(template.getFrom())) {
174 mail.setFrom(template.getFrom());
175 }
176 if (!StringUtils.isEmpty(template.getTo())) {
177 mail.setToList(template.getTo());
178 }
179 if (!StringUtils.isEmpty(template.getCc())) {
180 mail.setCcList(template.getCc());
181 }
182 if (!StringUtils.isEmpty(template.getBcc())) {
183 mail.setBccList(template.getBcc());
184 }
185 if (!StringUtils.isEmpty(template.getReplyTo())) {
186 mail.setReplyToList(template.getReplyTo());
187 }
188 if (!StringUtils.isEmpty(template.getSubject())) {
189 mail.setSubject(template.getSubject());
190 }
191
192 return mail;
193 }
194
195
196 protected MailTemplate getTemplate(String templateName) throws Exception {
197 final List<MailTemplate> configuration = mailModule.getTemplatesConfiguration();
198 for (MailTemplate mailTemplate : configuration) {
199 if (StringUtils.equals(mailTemplate.getName(), templateName)) {
200 MailTemplate cloned = (MailTemplate) BeanUtils.cloneBean(mailTemplate);
201 cloned.setParameters(new HashMap<>(mailTemplate.getParameters()));
202 cloned.setAttachments(new ArrayList<>(mailTemplate.getAttachments()));
203 return cloned;
204 }
205 }
206 return null;
207 }
208
209
210 public Map<String, String> getRenderers() {
211 return renderers;
212 }
213
214 public void setRenderers(Map<String, String> renderers) {
215 this.renderers = renderers;
216 }
217
218 }