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