Skip to content

Commit

Permalink
For #364 - added notification message placeholders for more fields of…
Browse files Browse the repository at this point in the history
… position entity
  • Loading branch information
vitalidze committed Dec 1, 2015
1 parent 982827a commit 4e13831
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 18 deletions.
8 changes: 7 additions & 1 deletion src/main/java/org/traccar/web/client/i18n/Messages.java
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,13 @@ String defaultNotificationTemplate(@Select DeviceEventType type,
@Optional String geoFenceName,
@Optional String eventTime,
@Optional String positionTime,
@Optional String maintenanceName);
@Optional String maintenanceName,
@Optional String positionAddress,
@Optional String positionLat,
@Optional String positionLon,
@Optional String positionAlt,
@Optional String positionSpeed,
@Optional String positionCourse);

String noMarkers();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,7 @@
import com.sencha.gxt.widget.core.client.form.*;
import com.sencha.gxt.widget.core.client.form.validator.MaxNumberValidator;
import com.sencha.gxt.widget.core.client.form.validator.MinNumberValidator;
import com.sencha.gxt.widget.core.client.grid.ColumnConfig;
import com.sencha.gxt.widget.core.client.grid.ColumnModel;
import com.sencha.gxt.widget.core.client.grid.Grid;
import com.sencha.gxt.widget.core.client.grid.GridView;
import com.sencha.gxt.widget.core.client.grid.*;
import org.traccar.web.client.i18n.Messages;
import org.traccar.web.client.model.EnumKeyProvider;
import org.traccar.web.client.model.NotificationSettingsProperties;
Expand Down Expand Up @@ -159,12 +156,32 @@ public String getValue(MessagePlaceholder ph) {
return i18n.placeholderDescription(ph);
}
}));
ColumnConfig<MessagePlaceholder, String> colGroup = new ColumnConfig<>(new ToStringValueProvider<MessagePlaceholder>() {
@Override
public String getValue(MessagePlaceholder ph) {
switch (ph) {
case positionCourse:
case positionTime:
case positionAddress:
case positionAlt:
case positionLat:
case positionLon:
case positionSpeed:
return i18n.eventPosition();
default:
return "";
}
}
});
placeholderColumns.get(placeholderColumns.size() - 1).setHeader(i18n.description());
ListStore<MessagePlaceholder> placeholderListStore = new ListStore<>(new EnumKeyProvider<MessagePlaceholder>());
placeholderListStore.addAll(Arrays.asList(MessagePlaceholder.values()));
GridView<MessagePlaceholder> placeholderGridView = new GridView<>();
GroupingView<MessagePlaceholder> placeholderGridView = new GroupingView<>();
placeholderGridView.setStripeRows(true);
placeholderGridView.setAutoFill(true);
placeholderGridView.setEnableNoGroups(false);
placeholderGridView.setEnableGroupingMenu(false);
placeholderGridView.groupBy(colGroup);
placeholderGrid = new Grid<>(placeholderListStore, new ColumnModel<>(placeholderColumns), placeholderGridView);

uiBinder.createAndBindUi(this);
Expand Down Expand Up @@ -214,7 +231,18 @@ public void onEventTypeChanged(SelectionEvent<DeviceEventType> event) {
if (messageTemplate == null) {
messageTemplate = new NotificationTemplate();
messageTemplate.setType(event.getSelectedItem());
messageTemplate.setBody(i18n.defaultNotificationTemplate(event.getSelectedItem(), "${deviceName}", "${geoFenceName}", "${eventTime}", "${positionTime}", "${maintenanceName}"));
messageTemplate.setBody(i18n.defaultNotificationTemplate(event.getSelectedItem(),
"${deviceName}",
"${geoFenceName}",
"${eventTime}",
"${positionTime}",
"${maintenanceName}",
"${positionAddress}",
"${positionLat}",
"${positionLon}",
"${positionAlt}",
"${positionSpeed}",
"${positionCourse}"));
settings.getTransferTemplates().put(event.getSelectedItem(), messageTemplate);
}
messageSubject.setText(messageTemplate.getSubject());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
}
</ui:style>

<gxt:Window ui:field="window" pixelSize="450, 500" modal="true" headingText="{i18n.notificationSettings}" focusWidget="{saveButton}">
<gxt:Window ui:field="window" pixelSize="450, 550" modal="true" headingText="{i18n.notificationSettings}" focusWidget="{saveButton}">
<container:VerticalLayoutContainer>
<container:child layoutData="{verticalLayoutData}">
<gxt:PlainTabPanel>
Expand Down Expand Up @@ -143,7 +143,7 @@
</form:FieldLabel>
</container:child>
<container:child layoutData="{verticalLayoutData}">
<grid:Grid ui:field="placeholderGrid" />
<grid:Grid ui:field="placeholderGrid" height="210" />
</container:child>
<container:child layoutData="{horizontalLayoutData}">
<button:TextButton ui:field="testTemplateButton" text="{i18n.test}" width="96" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package org.traccar.web.server.model;

import static org.traccar.web.shared.model.MessagePlaceholder.*;

import com.fasterxml.jackson.core.JsonEncoding;
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonGenerator;
Expand Down Expand Up @@ -153,7 +155,7 @@ public void doWork() throws Exception {
}

Engine engine = getTemplateEngine(getTimeZone(user));
Map<String, Object> model = getTemplateModel(deviceEvent);
Map<String, Object> model = getTemplateModel(user, deviceEvent);
String subject = engine.transform(template.getSubject(), model);
String body = engine.transform(template.getBody(), model);

Expand Down Expand Up @@ -454,9 +456,15 @@ public String checkTemplate(NotificationTemplate template) {
c.set(Calendar.HOUR, c.get(Calendar.HOUR) - 1);
c.set(Calendar.MINUTE, c.get(Calendar.MINUTE) - 15);
testPosition.setTime(c.getTime());
testPosition.setAddress("New York, Times Square");
testPosition.setLatitude(40.758899);
testPosition.setLongitude(-73.987325);
testPosition.setAltitude(100d);
testPosition.setSpeed(UserSettings.SpeedUnit.kilometersPerHour.toKnots(5d));
testPosition.setCourse(30d);

Engine engine = getTemplateEngine(getTimeZone(sessionUser.get()));
Map<String, Object> model = getTemplateModel(new DeviceEvent(new Date(), testDevice, testPosition, testGeoFence, testMaintenance));
Map<String, Object> model = getTemplateModel(sessionUser.get(), new DeviceEvent(new Date(), testDevice, testPosition, testGeoFence, testMaintenance));

String transformedSubject = engine.transform(template.getSubject(), model);
String transformedBody = engine.transform(template.getBody(), model);
Expand All @@ -469,13 +477,22 @@ public String checkTemplate(NotificationTemplate template) {
"</div>";
}

private static Map<String, Object> getTemplateModel(DeviceEvent event) {
private static Map<String, Object> getTemplateModel(User user, DeviceEvent event) {
Map<String, Object> model = new HashMap<>();
model.put(MessagePlaceholder.deviceName.name(), event.getDevice() == null ? "N/A" : event.getDevice().getName());
model.put(MessagePlaceholder.geoFenceName.name(), event.getGeoFence() == null ? "N/A" : event.getGeoFence().getName());
model.put(MessagePlaceholder.eventTime.name(), event.getTime());
model.put(MessagePlaceholder.positionTime.name(), event.getPosition() == null ? null : event.getPosition().getTime());
model.put(MessagePlaceholder.maintenanceName.name(), event.getMaintenance() == null ? "N/A" : event.getMaintenance().getName());
model.put(deviceName.name(), event.getDevice() == null ? "N/A" : event.getDevice().getName());
model.put(geoFenceName.name(), event.getGeoFence() == null ? "N/A" : event.getGeoFence().getName());
model.put(eventTime.name(), event.getTime());
if (event.getPosition() != null) {
Position p = event.getPosition();
model.put(positionTime.name(), p.getTime());
model.put(positionAddress.name(), p.getAddress());
model.put(positionLat.name(), p.getLatitude());
model.put(positionLon.name(), p.getLongitude());
model.put(positionAlt.name(), p.getAltitude());
model.put(positionSpeed.name(), p.getSpeed() == null ? null : (p.getSpeed() * user.getUserSettings().getSpeedUnit().getFactor()));
model.put(positionCourse.name(), p.getCourse());
}
model.put(maintenanceName.name(), event.getMaintenance() == null ? "N/A" : event.getMaintenance().getName());
return model;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,15 @@
package org.traccar.web.shared.model;

public enum MessagePlaceholder {
deviceName, geoFenceName, eventTime, positionTime, maintenanceName;
deviceName,
geoFenceName,
eventTime,
positionTime,
positionAddress,
positionLat,
positionLon,
positionAlt,
positionSpeed,
positionCourse,
maintenanceName;
}
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,12 @@ 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
placeholderDescription[positionAddress] = Address of associated position
placeholderDescription[positionLat] = Latitude of associated position
placeholderDescription[positionLon] = Longitude of associated position
placeholderDescription[positionAlt] = Altitude of associated position
placeholderDescription[positionSpeed] = Speed of associated position
placeholderDescription[positionCourse] = Course of associated position
placeholderDescription[maintenanceName] = Name of maintenance service
defaultNotificationTemplate = Unknown message
defaultNotificationTemplate[OFFLINE] = Device ''{1}'' went offline at {3}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,12 @@ placeholderDescription[deviceName] = Имя устройств
placeholderDescription[geoFenceName] = Имя геозоны
placeholderDescription[eventTime] = Дата и время события
placeholderDescription[positionTime] = Дата и время последней позиции
placeholderDescription[positionAddress] = Адрес последней позиции
placeholderDescription[positionLat] = Широта последней позиции
placeholderDescription[positionLon] = Долгота последней позиции
placeholderDescription[positionAlt] = Высота последней позиции
placeholderDescription[positionSpeed] = Скорость последней позиции
placeholderDescription[positionCourse] = Курс последней позиции
placeholderDescription[maintenanceName] = Имя техобслуживания
defaultNotificationTemplate = Неизвестное сообщение
defaultNotificationTemplate[OFFLINE] = Потеряна связь с устройством ''{1}'' в {3}
Expand Down

0 comments on commit 4e13831

Please sign in to comment.