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