NIHVIVO-2693 Add option to set default subject, html, and text attributes of email object in case template doesn't supply them.
This commit is contained in:
parent
3c1cbff0e0
commit
c94ad4511b
3 changed files with 106 additions and 66 deletions
|
@ -103,13 +103,18 @@ public abstract class UserAccountsEditPageStrategy extends UserAccountsPage {
|
|||
Map<String, Object> body = new HashMap<String, Object>();
|
||||
body.put("userAccount", page.getUpdatedAccount());
|
||||
body.put("passwordLink", buildResetPasswordLink());
|
||||
body.put("siteName", getSiteName());
|
||||
|
||||
String siteName = getSiteName();
|
||||
body.put("siteName", siteName);
|
||||
|
||||
FreemarkerEmailMessage email = FreemarkerEmailFactory
|
||||
.createNewMessage(vreq);
|
||||
email.addRecipient(TO, page.getUpdatedAccount().getEmailAddress());
|
||||
email.setTemplate(EMAIL_TEMPLATE);
|
||||
email.setBodyMap(body);
|
||||
email.setDefaultSubject(getDefaultSubject(siteName));
|
||||
email.setDefaultHtml(getDefaultHtml());
|
||||
email.setDefaultText(getDefaultText());
|
||||
|
||||
vreq.setAttribute("email", email);
|
||||
|
||||
|
@ -119,8 +124,20 @@ public abstract class UserAccountsEditPageStrategy extends UserAccountsPage {
|
|||
}
|
||||
|
||||
private String getSiteName() {
|
||||
ApplicationBean appBean = vreq.getAppBean();
|
||||
return appBean.getApplicationName();
|
||||
ApplicationBean appBean = vreq.getAppBean();
|
||||
return appBean.getApplicationName();
|
||||
}
|
||||
|
||||
private String getDefaultSubject(String siteName) {
|
||||
return siteName + " reset password request";
|
||||
}
|
||||
|
||||
private String getDefaultHtml() {
|
||||
return "";
|
||||
}
|
||||
|
||||
private String getDefaultText() {
|
||||
return "Default text for user accounts edit page";
|
||||
}
|
||||
|
||||
private String buildResetPasswordLink() {
|
||||
|
|
|
@ -26,6 +26,7 @@ import javax.mail.internet.MimeMultipart;
|
|||
import javax.servlet.ServletContext;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
|
@ -61,11 +62,16 @@ public class FreemarkerEmailMessage {
|
|||
|
||||
private InternetAddress fromAddress = null;
|
||||
private String subject = "";
|
||||
private String defaultSubject = "";
|
||||
private String defaultHtml = "";
|
||||
private String defaultText = "";
|
||||
private String templateName;
|
||||
private String htmlTemplateName;
|
||||
private String textTemplateName;
|
||||
private Map<String, Object> bodyMap = Collections.emptyMap();
|
||||
|
||||
// TO BE REMOVED
|
||||
private String htmlTemplateName;
|
||||
private String textTemplateName;
|
||||
|
||||
/**
|
||||
* Package access - should only be created by the factory.
|
||||
*/
|
||||
|
@ -134,22 +140,36 @@ public class FreemarkerEmailMessage {
|
|||
}
|
||||
}
|
||||
|
||||
public void setSubject(String subject) {
|
||||
this.subject = nonNull(subject, "");
|
||||
}
|
||||
|
||||
// TO BE REMOVED
|
||||
public void setHtmlTemplate(String templateName) {
|
||||
this.htmlTemplateName = nonNull(templateName, "");
|
||||
}
|
||||
|
||||
// TO BE REMOVED
|
||||
public void setTextTemplate(String templateName) {
|
||||
this.textTemplateName = nonNull(templateName, "");
|
||||
}
|
||||
|
||||
public void setSubject(String subject) {
|
||||
this.subject = nonNull(subject, "");
|
||||
}
|
||||
|
||||
public void setTemplate(String templateName) {
|
||||
this.templateName = nonNull(templateName, "");
|
||||
}
|
||||
|
||||
public void setDefaultSubject(String defaultSubject) {
|
||||
this.defaultSubject = nonNull(defaultSubject, "");
|
||||
}
|
||||
|
||||
public void setDefaultHtml(String defaultHtml) {
|
||||
this.defaultHtml = nonNull(defaultHtml, "");
|
||||
}
|
||||
|
||||
public void setDefaultText(String defaultText) {
|
||||
this.defaultText = nonNull(defaultText, "");
|
||||
}
|
||||
|
||||
public void setBodyMap(Map<String, Object> body) {
|
||||
if (body == null) {
|
||||
this.bodyMap = Collections.emptyMap();
|
||||
|
@ -193,23 +213,35 @@ public class FreemarkerEmailMessage {
|
|||
msg.addRecipient(recipient.type, recipient.address);
|
||||
}
|
||||
|
||||
if (subject == null) {
|
||||
log.debug("No email subject specified in template. Using default subject.");
|
||||
subject = defaultSubject;
|
||||
}
|
||||
msg.setSubject(subject);
|
||||
|
||||
if (html.isEmpty()) {
|
||||
if (html.isEmpty()) {
|
||||
if (html == null) {
|
||||
log.debug("No html email specified in template. Using default html.");
|
||||
html = defaultHtml;
|
||||
}
|
||||
|
||||
if (text == null) {
|
||||
log.debug("No plain text email specified in template. Using default html.");
|
||||
text = defaultText;
|
||||
}
|
||||
|
||||
if (StringUtils.isEmpty(text)) {
|
||||
if (StringUtils.isEmpty(html)) {
|
||||
log.error("Message has neither text body nor HTML body");
|
||||
} else {
|
||||
msg.setContent(html, "text/html");
|
||||
}
|
||||
} else if (StringUtils.isEmpty(html)) {
|
||||
msg.setContent(text, "text/plain");
|
||||
} else {
|
||||
if (html.isEmpty()) {
|
||||
msg.setContent(text, "text/plain");
|
||||
} else {
|
||||
MimeMultipart content = new MimeMultipart("alternative");
|
||||
addBodyPart(content, text, "text/plain");
|
||||
addBodyPart(content, html, "text/html");
|
||||
msg.setContent(content);
|
||||
}
|
||||
MimeMultipart content = new MimeMultipart("alternative");
|
||||
addBodyPart(content, text, "text/plain");
|
||||
addBodyPart(content, html, "text/html");
|
||||
msg.setContent(content);
|
||||
}
|
||||
|
||||
msg.setSentDate(new Date());
|
||||
|
|
|
@ -30,53 +30,42 @@ public class EmailDirective extends BaseTemplateDirectiveModel {
|
|||
public void execute(Environment env, Map params, TemplateModel[] loopVars,
|
||||
TemplateDirectiveBody body) throws TemplateException, IOException {
|
||||
|
||||
Object o = params.get("subject");
|
||||
if (o == null) {
|
||||
throw new TemplateModelException(
|
||||
"The email directive requires a value for parameter 'subject'.");
|
||||
}
|
||||
if (! ( o instanceof SimpleScalar)) {
|
||||
throw new TemplateModelException(
|
||||
"The email directive requires a string value for parameter 'subject'.");
|
||||
}
|
||||
String subject = o.toString();
|
||||
|
||||
o = params.get("html");
|
||||
if (o == null) {
|
||||
throw new TemplateModelException(
|
||||
"The email directive requires a value for parameter 'html'.");
|
||||
}
|
||||
if (! ( o instanceof SimpleScalar)) {
|
||||
throw new TemplateModelException(
|
||||
"The email directive requires a string value for parameter 'html'.");
|
||||
}
|
||||
String html = o.toString();
|
||||
|
||||
o = params.get("text");
|
||||
if (o == null) {
|
||||
throw new TemplateModelException(
|
||||
"The email directive requires a value for parameter 'text'.");
|
||||
}
|
||||
if (! ( o instanceof SimpleScalar)) {
|
||||
throw new TemplateModelException(
|
||||
"The email directive requires a string value for parameter 'text'.");
|
||||
}
|
||||
String text = o.toString();
|
||||
|
||||
HttpServletRequest request = (HttpServletRequest) env.getCustomAttribute("request");
|
||||
FreemarkerEmailMessage email = null;
|
||||
|
||||
o = (FreemarkerEmailMessage) request.getAttribute("emailMessage");
|
||||
if ( o == null) {
|
||||
Object paramValue = (FreemarkerEmailMessage) request.getAttribute("emailMessage");
|
||||
if ( paramValue == null) {
|
||||
throw new TemplateModelException(
|
||||
"No email message object found in the request.");
|
||||
}
|
||||
if ( ! (o instanceof FreemarkerEmailMessage)) {
|
||||
if ( ! (paramValue instanceof FreemarkerEmailMessage)) {
|
||||
throw new TemplateModelException(
|
||||
"Invalid value for request email attribute");
|
||||
}
|
||||
FreemarkerEmailMessage email = (FreemarkerEmailMessage) o;
|
||||
email.send(subject, html, text);
|
||||
email = (FreemarkerEmailMessage) paramValue;
|
||||
|
||||
|
||||
// Read in parameter values. If a value is undefined by the template, the
|
||||
// default values defined by the email object will be used.
|
||||
String subject = null;
|
||||
paramValue = params.get("subject");
|
||||
if (paramValue != null && paramValue instanceof SimpleScalar) {
|
||||
subject = paramValue.toString();
|
||||
}
|
||||
|
||||
String html = null;
|
||||
paramValue = params.get("html");
|
||||
if (paramValue != null && paramValue instanceof SimpleScalar) {
|
||||
html = paramValue.toString();
|
||||
}
|
||||
|
||||
String text = null;
|
||||
paramValue = params.get("text");
|
||||
if (paramValue != null && paramValue instanceof SimpleScalar) {
|
||||
text = paramValue.toString();
|
||||
}
|
||||
|
||||
email.send(subject, html, text);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -84,15 +73,17 @@ public class EmailDirective extends BaseTemplateDirectiveModel {
|
|||
Map<String, Object> map = new LinkedHashMap<String, Object>();
|
||||
|
||||
map.put("effect", "Create an email message from the parameters set in the invoking template.");
|
||||
map.put("comment", "Parameter values undefined by the template will be provided by controller default values.");
|
||||
|
||||
Map<String, String> params = new HashMap<String, String>();
|
||||
params.put("subject", "email subject");
|
||||
params.put("html", "HTML version of email message");
|
||||
params.put("text", "Plain text version of email message");
|
||||
params.put("subject", "email subject (optional)");
|
||||
params.put("html", "HTML version of email message (optional)");
|
||||
params.put("text", "Plain text version of email message (optional)");
|
||||
map.put("parameters", params);
|
||||
|
||||
List<String> examples = new ArrayList<String>();
|
||||
examples.add("<email subject=\"Password reset confirmation\" html=html text=text>");
|
||||
examples.add("<email html=html text=text>");
|
||||
map.put("examples", examples);
|
||||
|
||||
return map;
|
||||
|
|
Loading…
Add table
Reference in a new issue