}
List<UserActionRequest> requests = new ArrayList<UserActionRequest>(emailCount);
// -- prepare the email --
NotificationEmailDTO email = new NotificationEmailDTO();
// Note: The doubly-nested context is to prevent the code here and Velocity templates from updating
// inProperties. The inner context uses inProperties as its backing store, so anything added to the context --
// such as the values added here and any SET calls in templates -- would be added to inProperties, which we
// don't want.
Context velocityContext = new VelocityContext(new VelocityContext(inProperties, velocityGlobalContext));
velocityContext.put("context", velocityContext);
velocityContext.put("type", inType);
if (addressesWithTokens.size() + addresses.size() == 1)
{
velocityContext.put("recipient", inRecipientIndex.get(inRecipients.iterator().next()));
}
// build the subject
StringWriter writer = new StringWriter();
velocityEngine.evaluate(velocityContext, writer, "EmailSubject-" + inType, template.getSubjectTemplate());
email.setSubject(subjectPrefix + writer.toString());
// set the priority
email.setHighPriority(Boolean.TRUE.equals(inProperties.get(NotificationPropertyKeys.HIGH_PRIORITY)));
// render the body
String noReplyTextBody = null;
String replyTextBody = null;
String noReplyHtmlBody = null;
String replyHtmlBody = null;
// build the text body
Template vt = velocityEngine.getTemplate(template.getTextBodyTemplateResourcePath());
if (!addresses.isEmpty())
{
velocityContext.put("hasReplyAddress", false);
writer.getBuffer().setLength(0);
vt.merge(velocityContext, writer);
noReplyTextBody = writer.toString();
}
if (!addressesWithTokens.isEmpty())
{
velocityContext.put("hasReplyAddress", true);
writer.getBuffer().setLength(0);
vt.merge(velocityContext, writer);
replyTextBody = writer.toString();
}
// build the HTML body
if (sendHtml)
{
final String htmlBodyTemplateResourcePath = template.getHtmlBodyTemplateResourcePath();
if (htmlBodyTemplateResourcePath != null)
{
vt = velocityEngine.getTemplate(htmlBodyTemplateResourcePath);
// HTML-escape all content inserted
EventCartridge ec = new EventCartridge();
ec.addEventHandler(new EscapeHtmlReference());
ec.attachToContext(velocityContext);
if (!addresses.isEmpty())
{
velocityContext.put("hasReplyAddress", false);
writer.getBuffer().setLength(0);
vt.merge(velocityContext, writer);
noReplyHtmlBody = writer.toString();
}
if (!addressesWithTokens.isEmpty())
{
velocityContext.put("hasReplyAddress", true);
writer.getBuffer().setLength(0);
vt.merge(velocityContext, writer);
replyHtmlBody = writer.toString();
}
}
}
// -- create requests to send emails --
if (!addressesWithTokens.isEmpty())
{
email.setTextBody(replyTextBody);
if (replyHtmlBody != null)
{
email.setHtmlBody(replyHtmlBody);
}
for (Entry<String, String> entry : addressesWithTokens.entrySet())
{
NotificationEmailDTO userEmail = email.clone();
userEmail.setReplyTo(entry.getValue());
String address = entry.getKey();
userEmail.setToRecipient(address);
// set the description (for logging / debugging)
userEmail.setDescription(inType + " with token to " + address);
requests.add(new UserActionRequest("sendEmailNotificationAction", null, userEmail));
}
}
if (!addresses.isEmpty())