diff options
Diffstat (limited to 'src/org/traccar/notification/NotificationFormatter.java')
-rw-r--r-- | src/org/traccar/notification/NotificationFormatter.java | 33 |
1 files changed, 29 insertions, 4 deletions
diff --git a/src/org/traccar/notification/NotificationFormatter.java b/src/org/traccar/notification/NotificationFormatter.java index b68d53ee0..1e3072d53 100644 --- a/src/org/traccar/notification/NotificationFormatter.java +++ b/src/org/traccar/notification/NotificationFormatter.java @@ -1,5 +1,6 @@ /* - * Copyright 2016 Anton Tananaev (anton@traccar.org) + * Copyright 2016 - 2017 Anton Tananaev (anton@traccar.org) + * Copyright 2017 Andrey Kunitsyn (andrey@traccar.org) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -33,7 +34,7 @@ public final class NotificationFormatter { private NotificationFormatter() { } - public static MailMessage formatMessage(long userId, Event event, Position position) { + private static VelocityContext prepareContext(long userId, Event event, Position position) { Device device = Context.getIdentityManager().getDeviceById(event.getDeviceId()); VelocityContext velocityContext = new VelocityContext(); @@ -47,13 +48,20 @@ public final class NotificationFormatter { velocityContext.put("geofence", Context.getGeofenceManager().getGeofence(event.getGeofenceId())); } velocityContext.put("webUrl", Context.getVelocityEngine().getProperty("web.url")); + return velocityContext; + } + public static MailMessage formatMailMessage(long userId, Event event, Position position) { + VelocityContext velocityContext = prepareContext(userId, event, position); + String mailTemplatesPath = Context.getConfig().getString("mail.templatesPath", "mail") + "/"; Template template = null; try { - template = Context.getVelocityEngine().getTemplate(event.getType() + ".vm", StandardCharsets.UTF_8.name()); + template = Context.getVelocityEngine().getTemplate(mailTemplatesPath + event.getType() + ".vm", + StandardCharsets.UTF_8.name()); } catch (ResourceNotFoundException error) { Log.warning(error); - template = Context.getVelocityEngine().getTemplate("unknown.vm", StandardCharsets.UTF_8.name()); + template = Context.getVelocityEngine().getTemplate(mailTemplatesPath + "unknown.vm", + StandardCharsets.UTF_8.name()); } StringWriter writer = new StringWriter(); @@ -61,4 +69,21 @@ public final class NotificationFormatter { String subject = (String) velocityContext.get("subject"); return new MailMessage(subject, writer.toString()); } + + public static String formatSmsMessage(long userId, Event event, Position position) { + VelocityContext velocityContext = prepareContext(userId, event, position); + String smsTemplatesPath = Context.getConfig().getString("sms.templatesPath", "sms") + "/"; + Template template = null; + try { + template = Context.getVelocityEngine().getTemplate(smsTemplatesPath + event.getType() + ".vm", + StandardCharsets.UTF_8.name()); + } catch (ResourceNotFoundException error) { + Log.warning(error); + template = Context.getVelocityEngine().getTemplate(smsTemplatesPath + "unknown.vm", + StandardCharsets.UTF_8.name()); + } + StringWriter writer = new StringWriter(); + template.merge(velocityContext, writer); + return writer.toString(); + } } |