Skip to content

Commit

Permalink
For #148 - added default notification templates, added JMTE ultra-lig…
Browse files Browse the repository at this point in the history
…htweight template engine, implemented templates testing
  • Loading branch information
vitalidze committed Apr 25, 2015
1 parent 2d14e18 commit 1364246
Show file tree
Hide file tree
Showing 10 changed files with 139 additions and 6 deletions.
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,11 @@
<artifactId>commons-lang3</artifactId>
<version>3.3.2</version>
</dependency>
<dependency>
<groupId>com.floreysoft</groupId>
<artifactId>jmte</artifactId>
<version>3.1.1</version>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,8 @@ public void onSave(NotificationSettings notificationSettings) {
public void onTestEmail(NotificationSettings notificationSettings) {
service.checkEmailSettings(notificationSettings, new AsyncCallback<Void>() {
@Override
public void onFailure(Throwable throwable) {
new AlertMessageBox(i18n.notificationSettings(), i18n.testFailed()).show();
public void onFailure(Throwable caught) {
new AlertMessageBox(i18n.notificationSettings(), i18n.testFailed() + "<br><br>" + caught.getLocalizedMessage()).show();
}

@Override
Expand All @@ -222,8 +222,8 @@ public void onSuccess(Void aVoid) {
public void onTestPushbullet(NotificationSettings notificationSettings) {
service.checkPushbulletSettings(notificationSettings, new AsyncCallback<Void>() {
@Override
public void onFailure(Throwable throwable) {
new AlertMessageBox(i18n.notificationSettings(), i18n.testFailed()).show();
public void onFailure(Throwable caught) {
new AlertMessageBox(i18n.notificationSettings(), i18n.testFailed() + "<br><br>" + caught.getLocalizedMessage()).show();
}

@Override
Expand All @@ -234,6 +234,21 @@ public void onSuccess(Void aVoid) {
}
});
}

@Override
public void onTestMessageTemplate(String subject, String body) {
service.checkTemplate(subject, body, new AsyncCallback<String>() {
@Override
public void onFailure(Throwable caught) {
new AlertMessageBox(i18n.notificationSettings(), i18n.testFailed() + "<br><br>" + caught.getLocalizedMessage()).show();
}

@Override
public void onSuccess(String result) {
new MessageBox(i18n.notificationSettings(), result).show();
}
});
}
}).show();
}
});
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/org/traccar/web/client/i18n/Messages.java
Original file line number Diff line number Diff line change
Expand Up @@ -299,4 +299,10 @@ public interface Messages extends com.google.gwt.i18n.client.Messages {
String contentType();

String placeholderDescription(@Select MessagePlaceholder placeholder);

String defaultNotificationTemplate(@Select DeviceEventType type,
@Optional String deviceName,
@Optional String geoFenceName,
@Optional String eventTime,
@Optional String positionTime);
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
public interface NotificationService extends RemoteService {
void checkEmailSettings(NotificationSettings settings);
void checkPushbulletSettings(NotificationSettings settings);
String checkTemplate(String subject, String body);
NotificationSettings getSettings();
void saveSettings(NotificationSettings settings);
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@ public interface NotificationServiceAsync {
void saveSettings(NotificationSettings settings, AsyncCallback<Void> async);

void checkPushbulletSettings(NotificationSettings settings, AsyncCallback<Void> async);

void checkTemplate(String subject, String body, AsyncCallback<String> async);
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.google.gwt.core.client.GWT;
import com.google.gwt.editor.client.Editor;
import com.google.gwt.editor.client.SimpleBeanEditorDriver;
import com.google.gwt.event.logical.shared.SelectionEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
Expand Down Expand Up @@ -64,6 +65,7 @@ public interface NotificationSettingsHandler {
void onSave(NotificationSettings notificationSettings);
void onTestEmail(NotificationSettings notificationSettings);
void onTestPushbullet(NotificationSettings notificationSettings);
void onTestMessageTemplate(String subject, String body);
}

private NotificationSettingsHandler notificationSettingsHandler;
Expand Down Expand Up @@ -102,6 +104,18 @@ public interface NotificationSettingsHandler {
@UiField(provided = true)
ComboBox<DeviceEventType> eventType;

@Ignore
@UiField
TextField messageSubject;

@Ignore
@UiField
TextArea messageBody;

@Ignore
@UiField
TextField messageContentType;

@UiField(provided = true)
Grid<MessagePlaceholder> placeholderGrid;

Expand Down Expand Up @@ -190,4 +204,14 @@ public void onTestPushbulletClicked(SelectEvent event) {
public void onCancelClicked(SelectEvent event) {
window.hide();
}

@UiHandler("eventType")
public void onEventTypeChanged(SelectionEvent<DeviceEventType> event) {
messageBody.setText(i18n.defaultNotificationTemplate(event.getSelectedItem(), "${deviceName}", "${geoFenceName}", "${eventTime}", "${positionTime}"));
}

@UiHandler("testTemplateButton")
public void onTestTemplateClicked(SelectEvent event) {
notificationSettingsHandler.onTestMessageTemplate(messageSubject.getText(), messageBody.getText());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@
<container:child layoutData="{verticalLayoutData}">
<form:FieldLabel text="{i18n.subject}" labelWidth="86">
<form:widget>
<form:TextField ui:field="messageSubject" />
<form:TextField ui:field="messageSubject" text="[traccar-web] Notification" />
</form:widget>
</form:FieldLabel>
</container:child>
Expand All @@ -138,7 +138,7 @@
<container:child layoutData="{verticalLayoutData}">
<form:FieldLabel text="{i18n.contentType}" labelWidth="86">
<form:widget>
<form:TextField ui:field="messageContentType" />
<form:TextField ui:field="messageContentType" text="text/plain; charset=utf-8" />
</form:widget>
</form:FieldLabel>
</container:child>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,13 @@
*/
package org.traccar.web.server.model;

import com.floreysoft.jmte.Engine;
import com.floreysoft.jmte.NamedRenderer;
import com.floreysoft.jmte.RenderFormatInfo;
import com.google.gson.stream.JsonWriter;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.google.inject.persist.Transactional;
import org.traccar.web.client.model.DataService;
import org.traccar.web.client.model.NotificationService;
import org.traccar.web.shared.model.*;

Expand All @@ -39,6 +43,7 @@
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
Expand All @@ -57,6 +62,9 @@ public class NotificationServiceImpl extends RemoteServiceServlet implements Not
@Inject
protected Logger logger;

@Inject
private DataService dataService;

static class DeviceEvents implements Comparator<DeviceEvent> {
Set<DeviceEvent> offlineEvents;
Set<DeviceEvent> geoFenceEvents;
Expand Down Expand Up @@ -469,6 +477,70 @@ public void checkPushbulletSettings(NotificationSettings settings) {
}
}

@Transactional
@RequireUser(roles = { Role.ADMIN, Role.MANAGER })
@Override
public String checkTemplate(String subject, String body) {
List<Device> devices = dataService.getDevices();
Device testDevice;
if (devices.isEmpty()) {
testDevice = new Device();
testDevice.setName("Test-Device");
testDevice.setUniqueId("123");
} else {
testDevice = devices.get(0);
}
List<GeoFence> geoFences = dataService.getGeoFences();
GeoFence testGeoFence;
if (geoFences.isEmpty()) {
testGeoFence = new GeoFence(0l, "Some-GeoFence");
} else {
testGeoFence = geoFences.get(0);
}

Map<String, Object> model = new HashMap<String, Object>();
model.put(MessagePlaceholder.deviceName.name(), testDevice.getName());
model.put(MessagePlaceholder.geoFenceName.name(), testGeoFence.getName());
model.put(MessagePlaceholder.eventTime.name(), new Date());
Calendar c = Calendar.getInstance();
c.set(Calendar.HOUR, c.get(Calendar.HOUR) - 1);
c.set(Calendar.MINUTE, c.get(Calendar.MINUTE) - 15);
model.put(MessagePlaceholder.positionTime.name(), c.getTime());

Engine engine = new Engine();
engine.registerNamedRenderer(new NamedRenderer() {
@Override
public String render(Object o, String format, Locale locale) {
SimpleDateFormat dateFormat = new SimpleDateFormat(format, locale);
return dateFormat.format((Date) o);
}

@Override
public String getName() {
return "date";
}

@Override
public RenderFormatInfo getFormatInfo() {
return null;
}

@Override
public Class<?>[] getSupportedClasses() {
return new Class<?>[] { Date.class };
}
});
String transformedSubject = engine.transform(subject, model);
String transformedBody = engine.transform(body, model);

return "<div style=\"background-color: #ffffff;\">" +
"<table style=\"border-collapse: collapse;\">" +
"<tr><td style=\"border: 1px solid black; padding: 2px;\">" + transformedSubject + "</td></tr>" +
"<tr><td style=\"border: 1px solid black; padding: 4px;\">" + transformedBody + "</td></tr>" +
"</table>" +
"</div>";
}

@Transactional
@RequireUser(roles = { Role.ADMIN, Role.MANAGER })
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,10 @@ placeholderDescription[deviceName] = Name of device
placeholderDescription[geoFenceName] = Name of geo-fence
placeholderDescription[eventTime] = Date and time when event was recorded
placeholderDescription[positionTime] = Date and time of associated position
defaultNotificationTemplate = Unknown message
defaultNotificationTemplate[OFFLINE] = Device ''{1}'' went offline at {3}
defaultNotificationTemplate[GEO_FENCE_ENTER] = Device ''{1}'' entered geo-fence ''{2}'' at {4}
defaultNotificationTemplate[GEO_FENCE_EXIT] = Device ''{1}'' exited geo-fence ''{2}'' at {4}
# Archive style menu
style = Style
fullPalette = Full palette
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,10 @@ placeholderDescription[deviceName] = Имя устройств
placeholderDescription[geoFenceName] = Имя геозоны
placeholderDescription[eventTime] = Дата и время события
placeholderDescription[positionTime] = Дата и время последней позиции
defaultNotificationTemplate = Неизвестное сообщение
defaultNotificationTemplate[OFFLINE] = Потеряна связь с устройством ''{1}'' в {3}
defaultNotificationTemplate[GEO_FENCE_ENTER] = Устройство ''{1}'' вошло в геозону ''{2}'' в {4}
defaultNotificationTemplate[GEO_FENCE_EXIT] = Устройство ''{1}'' покинуло геозону ''{2}'' в {4}
# Archive style menu
style = Стиль
fullPalette = Вся палитра
Expand Down

0 comments on commit 1364246

Please sign in to comment.