From fd93d48040f7e04eea31ee918bce11269fcac08c Mon Sep 17 00:00:00 2001 From: "DESKTOP-EUNOBNI\\Avi" Date: Sat, 6 Oct 2018 13:16:45 +0300 Subject: [PATCH 1/3] Email template will now allow params to be interpolated in body string (not from file) and subject. --- .../com/haulmont/cuba/core/app/Emailer.java | 31 ++++++++++++++----- .../haulmont/cuba/core/app/EmailerTest.java | 22 +++++++++++-- 2 files changed, 43 insertions(+), 10 deletions(-) diff --git a/modules/core/src/com/haulmont/cuba/core/app/Emailer.java b/modules/core/src/com/haulmont/cuba/core/app/Emailer.java index f93f44a9f9..70a5c41bc3 100644 --- a/modules/core/src/com/haulmont/cuba/core/app/Emailer.java +++ b/modules/core/src/com/haulmont/cuba/core/app/Emailer.java @@ -128,6 +128,7 @@ public List sendEmailAsync(EmailInfo info, Integer attemptsCount protected void prepareEmailInfo(EmailInfo emailInfo) { processBodyTemplate(emailInfo); + processCationTemplate(emailInfo); if (emailInfo.getFrom() == null) { String defaultFromAddress = config.getFromAddress(); @@ -138,23 +139,37 @@ protected void prepareEmailInfo(EmailInfo emailInfo) { } } + protected void processCationTemplate(EmailInfo emailInfo) { + String caption = buildStringWithTemplateParams(emailInfo, emailInfo.getCaption()); + emailInfo.setCaption(caption); + } + protected void processBodyTemplate(EmailInfo info) { String templatePath = info.getTemplatePath(); - if (templatePath == null) { - return; + String templateContents; + if (templatePath != null) { + templateContents = resources.getResourceAsString(templatePath); + if (templateContents == null) { + throw new IllegalArgumentException("Could not find template by path: " + templatePath); + } + }else{ + templateContents = info.getBody(); } + templateContents = buildStringWithTemplateParams(info, templateContents); + info.setBody(templateContents); + } + + private String buildStringWithTemplateParams(EmailInfo info, String templateContents) { Map params = info.getTemplateParameters() == null ? Collections.emptyMap() : info.getTemplateParameters(); - String templateContents = resources.getResourceAsString(templatePath); - if (templateContents == null) { - throw new IllegalArgumentException("Could not find template by path: " + templatePath); - } - String body = TemplateHelper.processTemplate(templateContents, params); - info.setBody(body); + + return TemplateHelper.processTemplate(templateContents, params); + } + protected List splitEmail(EmailInfo info, @Nullable Integer attemptsCount, @Nullable Date deadline) { List sendingMessageList = new ArrayList<>(); String[] splitAddresses = info.getAddresses().split("[,;]"); diff --git a/modules/core/test/com/haulmont/cuba/core/app/EmailerTest.java b/modules/core/test/com/haulmont/cuba/core/app/EmailerTest.java index 99788b9de2..5523d614bd 100644 --- a/modules/core/test/com/haulmont/cuba/core/app/EmailerTest.java +++ b/modules/core/test/com/haulmont/cuba/core/app/EmailerTest.java @@ -407,16 +407,30 @@ public void testEmailTemplate() throws Exception { Map params = new HashMap<>(); params.put("userName", "Bob"); + params.put("greeting", "Hello"); params.put("dateParam", new SimpleDateFormat("dd/MM/yyyy").parse("01/05/2013")); - EmailInfo info = new EmailInfo("bob@example.com", "Test", null, templateFileName, params); + EmailInfo info = new EmailInfo("bob@example.com", "${greeting} Test", null, templateFileName, params); emailer.sendEmailAsync(info); emailer.processQueuedEmails(); - String body = getBody(testMailSender.fetchSentEmail()); + MimeMessage sent = testMailSender.fetchSentEmail(); + String body = getBody(sent); assertEquals("Greetings, Bob! 01-05-2013", body.trim()); + + String caption = getCaption(sent); + assertEquals("Hello Test", caption.trim()); + + info = new EmailInfo("bob@example.com", "${greeting} Test", null, null, params); + info.setBody("Greetings, ${userName}!"); + emailer.sendEmailAsync(info); + emailer.processQueuedEmails(); + sent = testMailSender.fetchSentEmail(); + body = getBody(sent); + assertEquals("Greetings, Bob!", body.trim()); } + @Test public void testTextAttachment() throws Exception { doTestTextAttachment(false); @@ -609,6 +623,10 @@ private String getBody(MimeMessage msg) throws Exception { return (String) textPart.getContent(); } + private String getCaption(MimeMessage msg) throws Exception { + return msg.getSubject(); + } + private String getBodyContentType(MimeMessage msg) throws Exception { MimeBodyPart textPart = getTextPart(msg); return textPart.getContentType(); From ffa6149ff140b4201135ef0d2032142433b18cd5 Mon Sep 17 00:00:00 2001 From: "DESKTOP-EUNOBNI\\Avi" Date: Sat, 6 Oct 2018 14:25:16 +0300 Subject: [PATCH 2/3] typo --- modules/core/src/com/haulmont/cuba/core/app/Emailer.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/modules/core/src/com/haulmont/cuba/core/app/Emailer.java b/modules/core/src/com/haulmont/cuba/core/app/Emailer.java index 70a5c41bc3..6340558d19 100644 --- a/modules/core/src/com/haulmont/cuba/core/app/Emailer.java +++ b/modules/core/src/com/haulmont/cuba/core/app/Emailer.java @@ -128,7 +128,7 @@ public List sendEmailAsync(EmailInfo info, Integer attemptsCount protected void prepareEmailInfo(EmailInfo emailInfo) { processBodyTemplate(emailInfo); - processCationTemplate(emailInfo); + processCaptionTemplate(emailInfo); if (emailInfo.getFrom() == null) { String defaultFromAddress = config.getFromAddress(); @@ -139,7 +139,7 @@ protected void prepareEmailInfo(EmailInfo emailInfo) { } } - protected void processCationTemplate(EmailInfo emailInfo) { + protected void processCaptionTemplate(EmailInfo emailInfo) { String caption = buildStringWithTemplateParams(emailInfo, emailInfo.getCaption()); emailInfo.setCaption(caption); } From 742539ada7551179a802d448f8f0c2e0fab7de9f Mon Sep 17 00:00:00 2001 From: "DESKTOP-EUNOBNI\\Avi" Date: Sat, 6 Oct 2018 16:42:33 +0300 Subject: [PATCH 3/3] safer execution, if there are no params, just return the value. + relevant tests --- .../core/src/com/haulmont/cuba/core/app/Emailer.java | 11 +++++------ .../test/com/haulmont/cuba/core/app/EmailerTest.java | 12 ++++++++++-- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/modules/core/src/com/haulmont/cuba/core/app/Emailer.java b/modules/core/src/com/haulmont/cuba/core/app/Emailer.java index 6340558d19..080e85d489 100644 --- a/modules/core/src/com/haulmont/cuba/core/app/Emailer.java +++ b/modules/core/src/com/haulmont/cuba/core/app/Emailer.java @@ -161,12 +161,11 @@ protected void processBodyTemplate(EmailInfo info) { } private String buildStringWithTemplateParams(EmailInfo info, String templateContents) { - Map params = info.getTemplateParameters() == null - ? Collections.emptyMap() - : info.getTemplateParameters(); - - return TemplateHelper.processTemplate(templateContents, params); - + String content = templateContents; + if(info.getTemplateParameters() != null && info.getTemplateParameters().size() > 0) { + content = TemplateHelper.processTemplate(templateContents, info.getTemplateParameters()); + } + return content; } diff --git a/modules/core/test/com/haulmont/cuba/core/app/EmailerTest.java b/modules/core/test/com/haulmont/cuba/core/app/EmailerTest.java index 5523d614bd..f5948b812f 100644 --- a/modules/core/test/com/haulmont/cuba/core/app/EmailerTest.java +++ b/modules/core/test/com/haulmont/cuba/core/app/EmailerTest.java @@ -426,8 +426,16 @@ public void testEmailTemplate() throws Exception { emailer.sendEmailAsync(info); emailer.processQueuedEmails(); sent = testMailSender.fetchSentEmail(); - body = getBody(sent); - assertEquals("Greetings, Bob!", body.trim()); + assertEquals("Greetings, Bob!", getBody(sent).trim()); + + info = new EmailInfo("bob@example.com", "${greeting} Test", null, null); + info.setBody("Greetings, ${userName}!"); + emailer.sendEmailAsync(info); + emailer.processQueuedEmails(); + sent = testMailSender.fetchSentEmail(); + + assertEquals("Greetings, ${userName}!", getBody(sent).trim()); + assertEquals("${greeting} Test", getCaption(sent).trim()); }