From f5c44298e1232802da1fc0f05d6a19d93bbf409f Mon Sep 17 00:00:00 2001 From: ryounes Date: Tue, 14 Jun 2011 13:26:52 +0000 Subject: [PATCH] NIHVIVO-2693 Freemarker email messaging code refactoring --- .../accounts/admin/UserAccountsEditPage.java | 2 +- .../admin/UserAccountsEditPageStrategy.java | 25 ++++++++--------- .../freemarker/FreemarkerHttpServlet.java | 3 --- .../webapp/email/FreemarkerEmailMessage.java | 18 ++++++++----- .../webapp/web/directives/EmailDirective.java | 4 +-- .../userAccounts-resetPasswordEmail.ftl | 27 +++++++++++++------ 6 files changed, 46 insertions(+), 33 deletions(-) diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/accounts/admin/UserAccountsEditPage.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/accounts/admin/UserAccountsEditPage.java index 6151d7d32..c939dfe24 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/accounts/admin/UserAccountsEditPage.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/accounts/admin/UserAccountsEditPage.java @@ -200,7 +200,7 @@ public class UserAccountsEditPage extends UserAccountsPage { userAccountsDao.updateUserAccount(userAccount); - strategy.notifyUser(vreq); + strategy.notifyUser(); } public boolean wasPasswordEmailSent() { diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/accounts/admin/UserAccountsEditPageStrategy.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/accounts/admin/UserAccountsEditPageStrategy.java index d06105c6b..1c1a3f383 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/accounts/admin/UserAccountsEditPageStrategy.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/accounts/admin/UserAccountsEditPageStrategy.java @@ -9,8 +9,8 @@ import java.net.URL; import java.util.HashMap; import java.util.Map; +import edu.cornell.mannlib.vitro.webapp.beans.ApplicationBean; import edu.cornell.mannlib.vitro.webapp.beans.UserAccount; -import edu.cornell.mannlib.vitro.webapp.beans.UserAccount.Status; import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest; import edu.cornell.mannlib.vitro.webapp.controller.accounts.UserAccountsPage; import edu.cornell.mannlib.vitro.webapp.controller.authenticate.Authenticator; @@ -47,7 +47,7 @@ public abstract class UserAccountsEditPageStrategy extends UserAccountsPage { protected abstract void setAdditionalProperties(UserAccount u); - protected abstract void notifyUser(VitroRequest vreq); + protected abstract void notifyUser(); protected abstract boolean wasPasswordEmailSent(); @@ -95,7 +95,7 @@ public abstract class UserAccountsEditPageStrategy extends UserAccountsPage { } @Override - protected void notifyUser(VitroRequest vreq) { + protected void notifyUser() { if (!resetPassword) { return; } @@ -103,24 +103,25 @@ public abstract class UserAccountsEditPageStrategy extends UserAccountsPage { Map body = new HashMap(); body.put("userAccount", page.getUpdatedAccount()); body.put("passwordLink", buildResetPasswordLink()); - //body.put("subjectLine", "Reset password request"); + body.put("siteName", getSiteName()); FreemarkerEmailMessage email = FreemarkerEmailFactory .createNewMessage(vreq); email.addRecipient(TO, page.getUpdatedAccount().getEmailAddress()); + email.setTemplate(EMAIL_TEMPLATE); + email.setBodyMap(body); vreq.setAttribute("email", email); - email.processTemplate(vreq, EMAIL_TEMPLATE, body); - - //email.setSubject("Reset password request"); - //email.setHtmlTemplate("userAccounts-resetPasswordEmail-html.ftl"); - //email.setTextTemplate("userAccounts-resetPasswordEmail-text.ftl"); - //email.setBodyMap(body); - //email.send(); + email.processTemplate(vreq); sentEmail = true; } + + private String getSiteName() { + ApplicationBean appBean = vreq.getAppBean(); + return appBean.getApplicationName(); + } private String buildResetPasswordLink() { try { @@ -199,7 +200,7 @@ public abstract class UserAccountsEditPageStrategy extends UserAccountsPage { } @Override - protected void notifyUser(VitroRequest vreq) { + protected void notifyUser() { // Do nothing. } diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/freemarker/FreemarkerHttpServlet.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/freemarker/FreemarkerHttpServlet.java index 6371bc0e6..f0430dc64 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/freemarker/FreemarkerHttpServlet.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/freemarker/FreemarkerHttpServlet.java @@ -337,9 +337,6 @@ public class FreemarkerHttpServlet extends VitroHttpServlet { Map map = new HashMap(); ApplicationBean appBean = vreq.getAppBean(); - // Ideally, templates wouldn't need portal id. Currently used as a hidden input value - // in the site search box, so needed for now. - String siteName = appBean.getApplicationName(); map.put("siteName", siteName); diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/email/FreemarkerEmailMessage.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/email/FreemarkerEmailMessage.java index 72fb702d5..53e0b0c8e 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/email/FreemarkerEmailMessage.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/email/FreemarkerEmailMessage.java @@ -61,6 +61,7 @@ public class FreemarkerEmailMessage { private InternetAddress fromAddress = null; private String subject = ""; + private String templateName; private String htmlTemplateName; private String textTemplateName; private Map bodyMap = Collections.emptyMap(); @@ -144,27 +145,30 @@ public class FreemarkerEmailMessage { public void setTextTemplate(String templateName) { this.textTemplateName = nonNull(templateName, ""); } + + public void setTemplate(String templateName) { + this.templateName = nonNull(templateName, ""); + } public void setBodyMap(Map body) { if (body == null) { this.bodyMap = Collections.emptyMap(); } else { - this.bodyMap = Collections - .unmodifiableMap(new HashMap(body)); + this.bodyMap = new HashMap(body); } } - public void processTemplate(VitroRequest vreq, String templateName, Map map) { + public void processTemplate(VitroRequest vreq) { - vreq.setAttribute("email", this); + vreq.setAttribute("emailMessage", this); - map.putAll(FreemarkerHttpServlet.getDirectivesForAllEnvironments()); - map.put("email", new edu.cornell.mannlib.vitro.webapp.web.directives.EmailDirective()); + bodyMap.putAll(FreemarkerHttpServlet.getDirectivesForAllEnvironments()); + bodyMap.put("email", new edu.cornell.mannlib.vitro.webapp.web.directives.EmailDirective()); try { Template template = config.getTemplate(templateName); StringWriter writer = new StringWriter(); - Environment env = template.createProcessingEnvironment(map, writer); + Environment env = template.createProcessingEnvironment(bodyMap, writer); env.setCustomAttribute("request", vreq); env.process(); } catch (TemplateException e) { diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/web/directives/EmailDirective.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/web/directives/EmailDirective.java index f3d75523b..515acf72e 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/web/directives/EmailDirective.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/web/directives/EmailDirective.java @@ -65,10 +65,10 @@ public class EmailDirective extends BaseTemplateDirectiveModel { HttpServletRequest request = (HttpServletRequest) env.getCustomAttribute("request"); - o = (FreemarkerEmailMessage) request.getAttribute("email"); + o = (FreemarkerEmailMessage) request.getAttribute("emailMessage"); if ( o == null) { throw new TemplateModelException( - "No email object found in the request."); + "No email message object found in the request."); } if ( ! (o instanceof FreemarkerEmailMessage)) { throw new TemplateModelException( diff --git a/webapp/web/templates/freemarker/body/accounts/userAccounts-resetPasswordEmail.ftl b/webapp/web/templates/freemarker/body/accounts/userAccounts-resetPasswordEmail.ftl index ca1e84abc..d6fcc59a2 100644 --- a/webapp/web/templates/freemarker/body/accounts/userAccounts-resetPasswordEmail.ftl +++ b/webapp/web/templates/freemarker/body/accounts/userAccounts-resetPasswordEmail.ftl @@ -2,7 +2,7 @@ <#-- Confirmation email for user account password reset --> -<#assign subject = "Reset password request" /> +<#assign subject = "${siteName} reset password request" /> <#assign html> @@ -11,33 +11,44 @@

- ${userAccount.firstName} ${userAccount.lastName} + Dear ${userAccount.firstName} ${userAccount.lastName}:

- Password successfully changed. + We have received a request to reset the password for your ${siteName} account (${userAccount.emailAddress}).

- Your new password associated with ${userAccount.emailAddress} has been changed. + Please follow the instructions below to proceed with your password reset.

- Thank you. + If you did not request this new account you can safely ignore this email. + This request will expire if not acted upon within 30 days.

+ +

+ Click on the link below or paste it into your browser's address bar to reset your password + using our secure server. +

+ +

${passwordLink}

+ +

Thank you!

<#assign text> -${userAccount.firstName} ${userAccount.lastName} +Dear ${userAccount.firstName} ${userAccount.lastName}: -We received a request to reset the password for your account +We have received a request to reset the password for your ${siteName} account (${userAccount.emailAddress}). + Please follow the instructions below to proceed with your password reset. If you did not request this new account you can safely ignore this email. -This request will expire if not acted upon for 30 days. +This request will expire if not acted upon within 30 days. Paste the link below into your browser's address bar to reset your password using our secure server.