Search in sources :

Example 1 with DateTool

use of org.apache.velocity.tools.generic.DateTool in project CzechIdMng by bcvsolutions.

the class DefaultIdmNotificationTemplateService method buildMessage.

@Override
public IdmMessageDto buildMessage(IdmMessageDto message, boolean showGuardedString) {
    StringWriter bodyHtml = new StringWriter();
    StringWriter bodyText = new StringWriter();
    StringWriter subject = new StringWriter();
    IdmNotificationTemplateDto template = message.getTemplate() == null ? null : get(message.getTemplate().getId());
    // 
    if (template == null) {
        return message;
    }
    // get parameters from messages
    Map<String, Object> model = message.getParameters();
    // 
    // create copy of parameters
    Map<String, Object> parameters = new HashMap<>();
    // templates, but no parameters
    if (model != null) {
        for (Entry<String, Object> entry : model.entrySet()) {
            if (entry.getValue() instanceof GuardedString && showGuardedString) {
                parameters.put(entry.getKey(), ((GuardedString) entry.getValue()).asString());
            } else if (entry.getValue() instanceof GuardedString) {
                parameters.put(entry.getKey(), ((GuardedString) entry.getValue()).toString());
            } else {
                parameters.put(entry.getKey(), entry.getValue());
            }
        }
    }
    // prepare html, text, subject
    String html = template.getBodyHtml();
    String text = template.getBodyText();
    String subjectString = template.getSubject();
    // Same parameters for all (html, txt, subject)
    VelocityContext velocityContext = getContext(parameters);
    // include some tools from Apache velocity -
    // http://velocity.apache.org/tools/devel/generic.html#tools
    velocityContext.put("display", new DisplayTool());
    velocityContext.put("date", new DateTool());
    // 
    velocityEngine.evaluate(velocityContext, bodyHtml, template.getCode(), html);
    velocityEngine.evaluate(velocityContext, bodyText, template.getCode(), text);
    velocityEngine.evaluate(velocityContext, subject, template.getCode(), subjectString);
    // 
    IdmMessageDto newMessage;
    // if is set model from message build with them
    if (message.getModel() != null) {
        newMessage = new IdmMessageDto.Builder().setHtmlMessage(bodyHtml.toString()).setTextMessage(bodyText.toString()).setSubject(StringUtils.isNotEmpty(subject.toString()) ? subject.toString() : message.getModel().getStatusEnum()).setLevel(// level get from old message
        message.getLevel()).setTemplate(template).setParameters(model).setModel(message.getModel()).build();
    } else {
        // Build IdmMessage
        newMessage = new IdmMessageDto.Builder().setHtmlMessage(bodyHtml.toString()).setTextMessage(bodyText.toString()).setSubject(subject.toString()).setLevel(// level
        message.getLevel()).setTemplate(template).setParameters(model).build();
    }
    // 
    return newMessage;
}
Also used : DisplayTool(org.apache.velocity.tools.generic.DisplayTool) HashMap(java.util.HashMap) VelocityContext(org.apache.velocity.VelocityContext) IdmMessageDto(eu.bcvsolutions.idm.core.notification.api.dto.IdmMessageDto) GuardedString(eu.bcvsolutions.idm.core.security.api.domain.GuardedString) GuardedString(eu.bcvsolutions.idm.core.security.api.domain.GuardedString) IdmNotificationTemplateDto(eu.bcvsolutions.idm.core.notification.api.dto.IdmNotificationTemplateDto) DateTool(org.apache.velocity.tools.generic.DateTool) StringWriter(java.io.StringWriter)

Example 2 with DateTool

use of org.apache.velocity.tools.generic.DateTool in project traccar by tananaev.

the class NotificationFormatter method prepareContext.

public static VelocityContext prepareContext(long userId, Event event, Position position) {
    User user = Context.getPermissionsManager().getUser(userId);
    Device device = Context.getIdentityManager().getById(event.getDeviceId());
    VelocityContext velocityContext = new VelocityContext();
    velocityContext.put("user", user);
    velocityContext.put("device", device);
    velocityContext.put("event", event);
    if (position != null) {
        velocityContext.put("position", position);
        velocityContext.put("speedUnit", ReportUtils.getSpeedUnit(userId));
        velocityContext.put("distanceUnit", ReportUtils.getDistanceUnit(userId));
        velocityContext.put("volumeUnit", ReportUtils.getVolumeUnit(userId));
    }
    if (event.getGeofenceId() != 0) {
        velocityContext.put("geofence", Context.getGeofenceManager().getById(event.getGeofenceId()));
    }
    String driverUniqueId = event.getString(Position.KEY_DRIVER_UNIQUE_ID);
    if (driverUniqueId != null) {
        velocityContext.put("driver", Context.getDriversManager().getDriverByUniqueId(driverUniqueId));
    }
    velocityContext.put("webUrl", Context.getVelocityEngine().getProperty("web.url"));
    velocityContext.put("dateTool", new DateTool());
    velocityContext.put("numberTool", new NumberTool());
    velocityContext.put("timezone", ReportUtils.getTimezone(userId));
    velocityContext.put("locale", Locale.getDefault());
    return velocityContext;
}
Also used : NumberTool(org.apache.velocity.tools.generic.NumberTool) User(org.traccar.model.User) DateTool(org.apache.velocity.tools.generic.DateTool) Device(org.traccar.model.Device) VelocityContext(org.apache.velocity.VelocityContext)

Example 3 with DateTool

use of org.apache.velocity.tools.generic.DateTool in project traccar by tananaev.

the class ReportUtils method initializeContext.

public static org.jxls.common.Context initializeContext(long userId) {
    org.jxls.common.Context jxlsContext = PoiTransformer.createInitialContext();
    jxlsContext.putVar("distanceUnit", getDistanceUnit(userId));
    jxlsContext.putVar("speedUnit", getSpeedUnit(userId));
    jxlsContext.putVar("volumeUnit", getVolumeUnit(userId));
    jxlsContext.putVar("webUrl", Context.getVelocityEngine().getProperty("web.url"));
    jxlsContext.putVar("dateTool", new DateTool());
    jxlsContext.putVar("numberTool", new NumberTool());
    jxlsContext.putVar("timezone", getTimezone(userId));
    jxlsContext.putVar("locale", Locale.getDefault());
    jxlsContext.putVar("bracketsRegex", "[\\{\\}\"]");
    return jxlsContext;
}
Also used : NumberTool(org.apache.velocity.tools.generic.NumberTool) DateTool(org.apache.velocity.tools.generic.DateTool)

Aggregations

DateTool (org.apache.velocity.tools.generic.DateTool)3 VelocityContext (org.apache.velocity.VelocityContext)2 NumberTool (org.apache.velocity.tools.generic.NumberTool)2 IdmMessageDto (eu.bcvsolutions.idm.core.notification.api.dto.IdmMessageDto)1 IdmNotificationTemplateDto (eu.bcvsolutions.idm.core.notification.api.dto.IdmNotificationTemplateDto)1 GuardedString (eu.bcvsolutions.idm.core.security.api.domain.GuardedString)1 StringWriter (java.io.StringWriter)1 HashMap (java.util.HashMap)1 DisplayTool (org.apache.velocity.tools.generic.DisplayTool)1 Device (org.traccar.model.Device)1 User (org.traccar.model.User)1