Skip to content

Commit

Permalink
For #148 - added possibility to save notification templates
Browse files Browse the repository at this point in the history
  • Loading branch information
vitalidze committed Apr 25, 2015
1 parent 1364246 commit 54f9803
Show file tree
Hide file tree
Showing 8 changed files with 221 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.traccar.web.client.view.*;
import org.traccar.web.shared.model.ApplicationSettings;
import org.traccar.web.shared.model.NotificationSettings;
import org.traccar.web.shared.model.NotificationTemplate;
import org.traccar.web.shared.model.User;

import com.google.gwt.core.client.GWT;
Expand Down Expand Up @@ -236,8 +237,8 @@ public void onSuccess(Void aVoid) {
}

@Override
public void onTestMessageTemplate(String subject, String body) {
service.checkTemplate(subject, body, new AsyncCallback<String>() {
public void onTestMessageTemplate(NotificationTemplate template) {
service.checkTemplate(template, new AsyncCallback<String>() {
@Override
public void onFailure(Throwable caught) {
new AlertMessageBox(i18n.notificationSettings(), i18n.testFailed() + "<br><br>" + caught.getLocalizedMessage()).show();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@
import com.google.gwt.user.client.rpc.RemoteService;
import com.google.gwt.user.client.rpc.RemoteServiceRelativePath;
import org.traccar.web.shared.model.NotificationSettings;
import org.traccar.web.shared.model.NotificationTemplate;

@RemoteServiceRelativePath("notificationService")
public interface NotificationService extends RemoteService {
void checkEmailSettings(NotificationSettings settings);
void checkPushbulletSettings(NotificationSettings settings);
String checkTemplate(String subject, String body);
String checkTemplate(NotificationTemplate template);
NotificationSettings getSettings();
void saveSettings(NotificationSettings settings);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.rpc.RemoteService;
import org.traccar.web.shared.model.NotificationSettings;
import org.traccar.web.shared.model.NotificationTemplate;

public interface NotificationServiceAsync {
void checkEmailSettings(NotificationSettings settings, AsyncCallback<Void> async);
Expand All @@ -13,5 +14,5 @@ public interface NotificationServiceAsync {

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

void checkTemplate(String subject, String body, AsyncCallback<String> async);
void checkTemplate(NotificationTemplate template, AsyncCallback<String> async);
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,7 @@
import org.traccar.web.client.i18n.Messages;
import org.traccar.web.client.model.EnumKeyProvider;
import org.traccar.web.client.model.NotificationSettingsProperties;
import org.traccar.web.shared.model.DeviceEventType;
import org.traccar.web.shared.model.MessagePlaceholder;
import org.traccar.web.shared.model.NotificationSettings;
import org.traccar.web.shared.model.UserSettings;
import org.traccar.web.shared.model.*;

import java.util.Arrays;
import java.util.LinkedList;
Expand All @@ -65,10 +62,11 @@ public interface NotificationSettingsHandler {
void onSave(NotificationSettings notificationSettings);
void onTestEmail(NotificationSettings notificationSettings);
void onTestPushbullet(NotificationSettings notificationSettings);
void onTestMessageTemplate(String subject, String body);
void onTestMessageTemplate(NotificationTemplate template);
}

private NotificationSettingsHandler notificationSettingsHandler;
private final NotificationSettings settings;
private final NotificationSettingsHandler notificationSettingsHandler;

@UiField
Window window;
Expand Down Expand Up @@ -100,6 +98,8 @@ public interface NotificationSettingsHandler {
@UiField
TextField pushbulletAccessToken;

NotificationTemplate messageTemplate;

@Ignore
@UiField(provided = true)
ComboBox<DeviceEventType> eventType;
Expand All @@ -123,6 +123,7 @@ public interface NotificationSettingsHandler {
Messages i18n = GWT.create(Messages.class);

public NotificationSettingsDialog(NotificationSettings notificationSettings, NotificationSettingsHandler notificationSettingsHandler) {
this.settings = notificationSettings;
this.notificationSettingsHandler = notificationSettingsHandler;

ListStore<NotificationSettings.SecureConnectionType> secureConnectionTypeStore = new ListStore<NotificationSettings.SecureConnectionType>(new EnumKeyProvider<NotificationSettings.SecureConnectionType>());
Expand Down Expand Up @@ -187,6 +188,7 @@ public void hide() {
@UiHandler("saveButton")
public void onSaveClicked(SelectEvent event) {
window.hide();
flushTemplate();
notificationSettingsHandler.onSave(driver.flush());
}

Expand All @@ -207,11 +209,33 @@ public void onCancelClicked(SelectEvent event) {

@UiHandler("eventType")
public void onEventTypeChanged(SelectionEvent<DeviceEventType> event) {
messageBody.setText(i18n.defaultNotificationTemplate(event.getSelectedItem(), "${deviceName}", "${geoFenceName}", "${eventTime}", "${positionTime}"));
// save previously edited template
flushTemplate();
messageTemplate = settings.getTransferTemplates().get(event.getSelectedItem());
if (messageTemplate == null) {
messageTemplate = new NotificationTemplate();
messageTemplate.setType(event.getSelectedItem());
messageTemplate.setBody(i18n.defaultNotificationTemplate(event.getSelectedItem(), "${deviceName}", "${geoFenceName}", "${eventTime}", "${positionTime}"));
settings.getTransferTemplates().put(event.getSelectedItem(), messageTemplate);
}
messageSubject.setText(messageTemplate.getSubject());
messageBody.setText(messageTemplate.getBody());
messageContentType.setText(messageTemplate.getContentType());
}

@UiHandler("testTemplateButton")
public void onTestTemplateClicked(SelectEvent event) {
notificationSettingsHandler.onTestMessageTemplate(messageSubject.getText(), messageBody.getText());
flushTemplate();
if (messageTemplate != null) {
notificationSettingsHandler.onTestMessageTemplate(messageTemplate);
}
}

private void flushTemplate() {
if (messageTemplate != null) {
messageTemplate.setSubject(messageSubject.getText());
messageTemplate.setBody(messageBody.getText());
messageTemplate.setContentType(messageContentType.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" text="[traccar-web] Notification" />
<form:TextField ui:field="messageSubject" />
</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" text="text/plain; charset=utf-8" />
<form:TextField ui:field="messageContentType" />
</form:widget>
</form:FieldLabel>
</container:child>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,7 @@ public void checkPushbulletSettings(NotificationSettings settings) {
@Transactional
@RequireUser(roles = { Role.ADMIN, Role.MANAGER })
@Override
public String checkTemplate(String subject, String body) {
public String checkTemplate(NotificationTemplate template) {
List<Device> devices = dataService.getDevices();
Device testDevice;
if (devices.isEmpty()) {
Expand Down Expand Up @@ -530,8 +530,8 @@ public Class<?>[] getSupportedClasses() {
return new Class<?>[] { Date.class };
}
});
String transformedSubject = engine.transform(subject, model);
String transformedBody = engine.transform(body, model);
String transformedSubject = engine.transform(template.getSubject(), model);
String transformedBody = engine.transform(template.getBody(), model);

return "<div style=\"background-color: #ffffff;\">" +
"<table style=\"border-collapse: collapse;\">" +
Expand All @@ -548,7 +548,16 @@ public NotificationSettings getSettings() {
List<NotificationSettings> settings = entityManager.get().createQuery("SELECT n FROM NotificationSettings n WHERE n.user = :user", NotificationSettings.class)
.setParameter("user", sessionUser.get())
.getResultList();
return settings.isEmpty() ? null : settings.get(0);
if (settings.isEmpty()) {
return null;
} else {
NotificationSettings s = settings.get(0);
s.setTransferTemplates(new HashMap<DeviceEventType, NotificationTemplate>(s.getTemplates().size()));
for (NotificationTemplate t : s.getTemplates()) {
s.getTransferTemplates().put(t.getType(), t);
}
return s;
}
}

@Transactional
Expand All @@ -560,9 +569,25 @@ public void saveSettings(NotificationSettings settings) {
if (currentSettings == null) {
currentSettings = settings;
settings.setUser(sessionUser.get());
entityManager.get().persist(currentSettings);
for (NotificationTemplate newTemplate : currentSettings.getTransferTemplates().values()) {
newTemplate.setSettings(currentSettings);
entityManager.get().persist(newTemplate);
}
} else {
currentSettings.copyFrom(settings);
for (NotificationTemplate existingTemplate : currentSettings.getTemplates()) {
NotificationTemplate updatedTemplate = settings.getTransferTemplates().remove(existingTemplate.getType());
if (updatedTemplate == null) {
entityManager.get().remove(existingTemplate);
} else {
existingTemplate.copyFrom(updatedTemplate);
}
}
for (NotificationTemplate newTemplate : settings.getTransferTemplates().values()) {
newTemplate.setSettings(currentSettings);
entityManager.get().persist(newTemplate);
}
}
entityManager.get().persist(currentSettings);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@

import javax.persistence.*;
import java.io.Serializable;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

@Entity
@Table(name = "notification_settings")
Expand All @@ -44,6 +48,8 @@ public NotificationSettings() {
port = 465;
useAuthorization = true;
secureConnectionType = SecureConnectionType.SSL_TLS;
templates = new HashSet<NotificationTemplate>();
transferTemplates = new HashMap<DeviceEventType, NotificationTemplate>();
}

@Id
Expand Down Expand Up @@ -97,6 +103,13 @@ public boolean equals(Object object) {
@Enumerated(EnumType.STRING)
private SecureConnectionType secureConnectionType;

@GwtTransient
@OneToMany(mappedBy = "settings", fetch = FetchType.LAZY)
private Set<NotificationTemplate> templates;

@Transient
private Map<DeviceEventType, NotificationTemplate> transferTemplates;

public String getFromAddress() {
return fromAddress;
}
Expand Down Expand Up @@ -163,6 +176,22 @@ public void setPushbulletAccessToken(String pushbulletApiKey) {
this.pushbulletAccessToken = pushbulletApiKey;
}

public Set<NotificationTemplate> getTemplates() {
return templates;
}

public void setTemplates(Set<NotificationTemplate> templates) {
this.templates = templates;
}

public Map<DeviceEventType, NotificationTemplate> getTransferTemplates() {
return transferTemplates;
}

public void setTransferTemplates(Map<DeviceEventType, NotificationTemplate> transferTemplates) {
this.transferTemplates = transferTemplates;
}

public void copyFrom(NotificationSettings s) {
setFromAddress(s.getFromAddress());
setServer(s.getServer());
Expand Down
121 changes: 121 additions & 0 deletions src/main/java/org/traccar/web/shared/model/NotificationTemplate.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
/*
* Copyright 2015 Vitaly Litvak ([email protected])
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.traccar.web.shared.model;

import com.google.gwt.user.client.rpc.GwtTransient;

import javax.persistence.*;
import java.io.Serializable;

@Entity
@Table(name = "notification_templates")
public class NotificationTemplate implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", nullable = false, updatable = false, unique = true)
private long id;

@GwtTransient
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(foreignKey = @ForeignKey(name = "ntemplates_fkey_settings_id"))
private NotificationSettings settings;

@Enumerated(EnumType.STRING)
private DeviceEventType type;

public NotificationTemplate() {
subject = "[traccar-web] Notification";
contentType = "text/plain; charset=utf-8";
}

public long getId() {
return id;
}

public void setId(long id) {
this.id = id;
}

public NotificationSettings getSettings() {
return settings;
}

public void setSettings(NotificationSettings settings) {
this.settings = settings;
}

public DeviceEventType getType() {
return type;
}

public void setType(DeviceEventType type) {
this.type = type;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

NotificationTemplate that = (NotificationTemplate) o;

if (!settings.equals(that.settings)) return false;
return type == that.type;

}

@Override
public int hashCode() {
int result = settings.hashCode();
result = 31 * result + type.hashCode();
return result;
}

private String subject;
@Column(length = 4096)
private String body;
private String contentType;

public String getSubject() {
return subject;
}

public void setSubject(String subject) {
this.subject = subject;
}

public String getBody() {
return body;
}

public void setBody(String body) {
this.body = body;
}

public String getContentType() {
return contentType;
}

public void setContentType(String contentType) {
this.contentType = contentType;
}

public void copyFrom(NotificationTemplate t) {
setSubject(t.getSubject());
setBody(t.getBody());
setContentType(t.getContentType());
}
}

0 comments on commit 54f9803

Please sign in to comment.