Example usage for org.apache.commons.lang.text StrSubstitutor setVariableResolver

List of usage examples for org.apache.commons.lang.text StrSubstitutor setVariableResolver

Introduction

In this page you can find the example usage for org.apache.commons.lang.text StrSubstitutor setVariableResolver.

Prototype

public void setVariableResolver(StrLookup variableResolver) 

Source Link

Document

Sets the VariableResolver that is used to lookup variables.

Usage

From source file:org.eurekastreams.server.action.execution.notification.TemplateEmailBuilder.java

/**
 * Builds the email message from the notification and initial properties.
 * //  ww  w. j av  a 2  s . c o  m
 * @param notif
 *            Notification for which to build message.
 * @param invocationProperties
 *            Initial properties to use.
 * @param inMessage
 *            Email message.
 * @throws Exception
 *             On error.
 */
public void build(final NotificationDTO notif, final Map<String, String> invocationProperties,
        final MimeMessage inMessage) throws Exception {
    // -- build properties --

    Map<String, String> properties = new HashMap<String, String>();

    // from system settings
    SystemSettings systemSettings = systemSettingsMapper.execute(null);
    properties.put("settings.sitelabel", systemSettings.getSiteLabel());
    properties.put("settings.support.email", systemSettings.getSupportEmailAddress());
    properties.put("settings.support.phone", systemSettings.getSupportPhoneNumber());
    properties.put("settings.support.name", systemSettings.getSupportStreamGroupDisplayName());
    properties.put("settings.support.uniqueid", systemSettings.getSupportStreamGroupShortName());

    // from upstream builders
    if (invocationProperties != null) {
        properties.putAll(invocationProperties);
    }

    // from system configuration
    if (extraProperties != null) {
        properties.putAll(extraProperties);
    }

    // actor
    if (notif.getActorId() > 0) {
        properties.put("actor.id", Long.toString(notif.getActorId()));
        properties.put("actor.accountid", notif.getActorAccountId());
        properties.put("actor.name", notif.getActorName());
    }

    // activity
    if (notif.getActivityId() > 0) {
        properties.put("activity.id", Long.toString(notif.getActivityId()));
        String type = activityTypeDisplayNameOverrides.get(notif.getActivityType());
        if (type == null) {
            type = notif.getActivityType().name().toLowerCase();
        }
        properties.put("activity.type", type);
    }

    // destination
    if (notif.getDestinationId() > 0) {
        properties.put("dest.id", Long.toString(notif.getDestinationId()));
        properties.put("dest.type", notif.getDestinationType().name());
        properties.put("dest.uniqueid", notif.getDestinationUniqueId());
        properties.put("dest.name", notif.getDestinationName());
        properties.put("dest.page", entityPageNames.get(notif.getDestinationType()));
    }

    // auxiliary
    if (notif.getAuxiliaryType() != null) {
        properties.put("aux.type", notif.getAuxiliaryType().name());
        properties.put("aux.uniqueid", notif.getAuxiliaryUniqueId());
        properties.put("aux.name", notif.getAuxiliaryName());
        properties.put("aux.page", entityPageNames.get(notif.getAuxiliaryType()));
    }

    // -- build email --

    // build and set the email parts
    StrSubstitutor transform = new StrSubstitutor(properties, "$(", ")");
    emailer.setSubject(inMessage, transform.replace(subjectTemplate));
    emailer.setTextBody(inMessage, transform.replace(textBodyTemplate));

    transform.setVariableResolver(new HtmlEncodingLookup(transform.getVariableResolver()));
    emailer.setHtmlBody(inMessage, transform.replace(htmlBodyTemplate));

    // look up recipients and put as email recipients
    List<PersonModelView> recipients = peopleMapper.execute(notif.getRecipientIds());
    if (recipients.size() == 1) {
        emailer.setTo(inMessage, recipients.get(0).getEmail());
    } else {
        emailer.setBcc(inMessage, EmailerFactory.buildEmailList(recipients));
    }
}