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.app.verify;
35
36 import info.magnolia.context.MgnlContext;
37 import info.magnolia.dam.DamManager;
38 import info.magnolia.dam.asset.Asset;
39 import info.magnolia.i18nsystem.SimpleTranslator;
40 import info.magnolia.jcr.util.PropertyUtil;
41 import info.magnolia.jcr.util.SessionUtil;
42 import info.magnolia.module.mail.MailModule;
43 import info.magnolia.module.mail.MailTemplate;
44 import info.magnolia.module.mail.app.MailSubAppDecriptor;
45 import info.magnolia.module.mail.templates.MailAttachment;
46 import info.magnolia.module.mail.templates.MgnlEmail;
47 import info.magnolia.module.mail.util.MailUtil;
48 import info.magnolia.objectfactory.ComponentProvider;
49 import info.magnolia.objectfactory.Components;
50 import info.magnolia.repository.RepositoryConstants;
51 import info.magnolia.ui.api.app.SubAppContext;
52 import info.magnolia.ui.api.context.UiContext;
53 import info.magnolia.ui.dialog.formdialog.FormBuilder;
54 import info.magnolia.ui.form.definition.FormDefinition;
55 import info.magnolia.ui.vaadin.form.FormViewReduced;
56 import info.magnolia.ui.vaadin.overlay.MessageStyleTypeEnum;
57
58 import java.io.File;
59 import java.util.HashMap;
60 import java.util.Iterator;
61 import java.util.List;
62 import java.util.Map;
63
64 import javax.jcr.Node;
65 import javax.mail.Message.RecipientType;
66 import javax.mail.internet.InternetAddress;
67
68 import org.apache.commons.io.IOUtils;
69 import org.apache.commons.lang.StringUtils;
70 import org.slf4j.Logger;
71 import org.slf4j.LoggerFactory;
72
73 import com.google.inject.Inject;
74 import com.mycila.inject.internal.guava.io.Files;
75 import com.vaadin.data.util.PropertysetItem;
76
77
78
79
80 public class MailVerifyPresenter implements MailVerifyView.Listener {
81
82 private MailVerifyView view;
83 private ComponentProvider componentProvider;
84 private MailModule mailModule;
85 private SubAppContext subAppContext;
86 private UiContext context;
87 private SimpleTranslator translator;
88 private FormBuilder builder;
89 protected Map<String, PropertysetItem> items = new HashMap<String, PropertysetItem>();
90
91 private static final String PATH_SEPARATOR = "/";
92 private static final String SIMPLE_RENDERER = "simple";
93
94 private static final String SIMPLE_MAIL_FORM_DEFINITION = "simple";
95 private static final String TEMPLATE_MAIL_FORM_DEFINITION = "template";
96 private static final String SIMPLE_CONFIGURATION_NODE_PATH = "/modules/mail/config/simpleConfiguration";
97
98 private static final String PROPERTY_CONTENT_TYPE = "contentType";
99 private static final String PROPERTY_ATTACHMENT = "attachment";
100 private static final String PROPERTY_EMAIL = "email";
101 private static final String PROPERTY_TEMPLATE = "template";
102 private static final String PROPERTY_DATA = "data";
103
104 private static final String BUTTON_SIMPLE_MAIL_SUCCESS = "mail.app.verify.button.simple.success";
105 private static final String BUTTON_SIMPLE_MAIL_ERROR = "mail.app.verify.button.simple.error";
106 private static final String BUTTON_TEMPLATE_MAIL_SUCCESS = "mail.app.verify.button.template.success";
107 private static final String BUTTON_TEMPLATE_MAIL_ERROR = "mail.app.verify.button.template.error";
108
109 private static final Logger log = LoggerFactory.getLogger(MailVerifyPresenter.class);
110
111 @Inject
112 public MailVerifyPresenter(MailVerifyView view, FormBuilder builder, ComponentProvider componentProvider, MailModule mailModule, SubAppContext subAppContext, UiContext context, SimpleTranslator translator) {
113 this.view = view;
114 this.builder = builder;
115 this.componentProvider = componentProvider;
116 this.mailModule = mailModule;
117 this.subAppContext = subAppContext;
118 this.context = context;
119 this.translator = translator;
120 }
121
122 public MailVerifyView start() {
123 FormDefinition formDefinition = ((MailSubAppDecriptor) subAppContext.getSubAppDescriptor()).getFormDefinitions().get(SIMPLE_MAIL_FORM_DEFINITION);
124 FormViewReduced formView = componentProvider.getComponent(FormViewReduced.class);
125 PropertysetItem item = new PropertysetItem();
126 items.put(SIMPLE_MAIL_FORM_DEFINITION, item);
127 builder.buildReducedForm(formDefinition, formView, item, null);
128 view.addFormViewReduced(SIMPLE_MAIL_FORM_DEFINITION, formView);
129
130 formDefinition = ((MailSubAppDecriptor) subAppContext.getSubAppDescriptor()).getFormDefinitions().get(TEMPLATE_MAIL_FORM_DEFINITION);
131 formView = componentProvider.getComponent(FormViewReduced.class);
132 item = new PropertysetItem();
133 items.put(TEMPLATE_MAIL_FORM_DEFINITION, item);
134 builder.buildReducedForm(formDefinition, formView, item, null);
135 view.addFormViewReduced(TEMPLATE_MAIL_FORM_DEFINITION, formView);
136
137 view.setListner(this);
138 view.build();
139 return view;
140 }
141
142 private Node getMailConfigurationNode() {
143 return SessionUtil.getNode(RepositoryConstants.CONFIG, SIMPLE_CONFIGURATION_NODE_PATH);
144 }
145
146 @Override
147 public void sendSimpleMail() {
148 File tmp = null;
149 try {
150 Node mailNode = getMailConfigurationNode();
151 Map<String, Object> params = new HashMap<String, Object>();
152
153 String userEmailAddress = PropertyUtil.getString(SessionUtil.getNodeByIdentifier(RepositoryConstants.USERS, MgnlContext.getUser().getIdentifier()), PROPERTY_EMAIL);
154 if (StringUtils.isBlank(userEmailAddress)) {
155 context.openNotification(MessageStyleTypeEnum.ERROR, true, translator.translate("mail.app.verify.button.simple.error.no.email", MgnlContext.getUser().getName()));
156 return;
157 }
158 String contentType = (String) items.get(SIMPLE_MAIL_FORM_DEFINITION).getItemProperty(PROPERTY_CONTENT_TYPE).getValue();
159 String attachment = (String) items.get(SIMPLE_MAIL_FORM_DEFINITION).getItemProperty(PROPERTY_ATTACHMENT).getValue();
160
161
162 params.put(MailTemplate.MAIL_TO, userEmailAddress);
163 params.put(MailTemplate.MAIL_FROM, PropertyUtil.getString(mailNode, MailTemplate.MAIL_FROM));
164 params.put(MailTemplate.MAIL_SUBJECT, PropertyUtil.getString(mailNode, MailTemplate.MAIL_SUBJECT));
165 params.put(MailTemplate.MAIL_BODY, PropertyUtil.getString(mailNode, MailTemplate.MAIL_BODY));
166
167
168 List<MailAttachment> attachments = MailUtil.createAttachmentList();
169
170
171 if (StringUtils.isNotEmpty(attachment)) {
172 Asset asset = Components.getComponent(DamManager.class).getAssetForPath(attachment);
173 if (asset != null) {
174 tmp = new File(asset.getName() + "." + asset.getFileExtension());
175 Files.write(IOUtils.toByteArray(asset.getContentStream()), tmp);
176 MailAttachment a = new MailAttachment(tmp, asset.getName() + "." + asset.getFileExtension(), asset.getDescription(), MailAttachment.DISPOSITION_ATTACHMENT);
177 attachments.add(a);
178 }
179 }
180
181 MgnlEmail email = mailModule.getFactory().getEmailFromType(params, SIMPLE_RENDERER, contentType, attachments);
182 mailModule.getHandler().prepareAndSendMail(email);
183 context.openNotification(MessageStyleTypeEnum.INFO, true, translator.translate(BUTTON_SIMPLE_MAIL_SUCCESS, userEmailAddress));
184 } catch (Exception e) {
185 log.error("Unable to send email.", e);
186 context.openNotification(MessageStyleTypeEnum.ERROR, true, translator.translate(BUTTON_SIMPLE_MAIL_ERROR, e.getMessage()));
187 } finally {
188 if (tmp != null) {
189 tmp.delete();
190 }
191 }
192 }
193
194 @Override
195 public void sendTemplateMail() {
196 try {
197 String templateName = StringUtils.substringAfterLast((String) items.get(TEMPLATE_MAIL_FORM_DEFINITION).getItemProperty(PROPERTY_TEMPLATE).getValue(), PATH_SEPARATOR);
198 String paramStr = (String) items.get(TEMPLATE_MAIL_FORM_DEFINITION).getItemProperty(PROPERTY_DATA).getValue();
199
200 Iterator<MailTemplate> iter = mailModule.getTemplatesConfiguration().iterator();
201 MailTemplate template = null;
202 while (iter.hasNext()) {
203 template = iter.next();
204 if (!template.getName().equals(templateName)) {
205 template = null;
206 } else {
207 break;
208 }
209 }
210 if (template != null) {
211 Map<String, Object> params = new HashMap<String, Object>();
212
213 String userEmailAddress = PropertyUtil.getString(SessionUtil.getNodeByIdentifier(RepositoryConstants.USERS, MgnlContext.getUser().getIdentifier()), PROPERTY_EMAIL);
214 if (StringUtils.isBlank(userEmailAddress)) {
215 context.openNotification(MessageStyleTypeEnum.ERROR, true, translator.translate("mail.app.verify.button.simple.error.no.email", MgnlContext.getUser().getName()));
216 return;
217 }
218 params.put(MailTemplate.MAIL_TO, userEmailAddress);
219
220 if (StringUtils.isNotEmpty(paramStr)) {
221 params = template.getParameters();
222 String[] parameters = StringUtils.split(paramStr, "\n");
223 for (String parameter : parameters) {
224 String[] value = StringUtils.split(parameter, "=");
225 if (value.length == 2) {
226 params.put(value[0], value[1]);
227 } else {
228 log.warn("Template mail parameter {} has no value specified.", value[0]);
229 params.put(value[0], "");
230 }
231 }
232 }
233
234 MgnlEmail email = mailModule.getFactory().getEmailFromTemplate(template.getName(), template.getAttachments(), params);
235 email.addRecipient(RecipientType.TO, new InternetAddress(userEmailAddress));
236 email.setBodyFromResourceFile();
237 mailModule.getHandler().sendMail(email);
238 context.openNotification(MessageStyleTypeEnum.INFO, true, translator.translate(BUTTON_TEMPLATE_MAIL_SUCCESS, template.getName(), userEmailAddress));
239 }
240 } catch (Exception e) {
241 log.error("Unable to send email.", e);
242 context.openNotification(MessageStyleTypeEnum.ERROR, true, translator.translate(BUTTON_TEMPLATE_MAIL_ERROR, e.getMessage()));
243 }
244 }
245
246 }