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;
}
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;
}
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;
}
Aggregations