From 6e438776f1e2a87ae30fff3d432ab71afb811089 Mon Sep 17 00:00:00 2001 From: Vitaly Litvak Date: Thu, 15 Sep 2016 09:11:57 +0300 Subject: [PATCH] For #830 - preparing to implement OSRM v5 API for snap-to-roads, added new setting - 'MatchServiceType' --- .../org/traccar/web/client/i18n/Messages.java | 2 + .../model/ApplicationSettingsProperties.java | 10 ++++- .../view/ApplicationSettingsDialog.java | 19 +++++++++- .../view/ApplicationSettingsDialog.ui.xml | 7 ++++ .../web/server/model/DBMigrations.java | 14 +++++-- .../web/shared/model/ApplicationSettings.java | 15 +++++++- .../web/shared/model/MatchServiceType.java | 37 +++++++++++++++++++ .../web/shared/model/PasswordHashMethod.java | 2 - .../web/client/i18n/Messages.properties | 1 + .../web/client/i18n/Messages_ru.properties | 1 + 10 files changed, 96 insertions(+), 12 deletions(-) create mode 100644 src/main/java/org/traccar/web/shared/model/MatchServiceType.java diff --git a/src/main/java/org/traccar/web/client/i18n/Messages.java b/src/main/java/org/traccar/web/client/i18n/Messages.java index 07a2e223..00413269 100644 --- a/src/main/java/org/traccar/web/client/i18n/Messages.java +++ b/src/main/java/org/traccar/web/client/i18n/Messages.java @@ -558,4 +558,6 @@ String defaultNotificationTemplate(@Select DeviceEventType type, String allowCommandsOnlyForAdmins(); String defaultPreferences(); + + String matchServiceType(); } diff --git a/src/main/java/org/traccar/web/client/model/ApplicationSettingsProperties.java b/src/main/java/org/traccar/web/client/model/ApplicationSettingsProperties.java index 45979b16..a610ba5d 100644 --- a/src/main/java/org/traccar/web/client/model/ApplicationSettingsProperties.java +++ b/src/main/java/org/traccar/web/client/model/ApplicationSettingsProperties.java @@ -15,8 +15,7 @@ */ package org.traccar.web.client.model; -import org.traccar.web.shared.model.ApplicationSettings; -import org.traccar.web.shared.model.PasswordHashMethod; +import org.traccar.web.shared.model.*; import com.sencha.gxt.core.client.ValueProvider; import com.sencha.gxt.data.shared.LabelProvider; @@ -45,4 +44,11 @@ public String getLabel(PasswordHashMethod item) { return item.getName(); } } + + class MatchServiceTypeLabelProvider implements LabelProvider { + @Override + public String getLabel(MatchServiceType item) { + return item.getName(); + } + } } diff --git a/src/main/java/org/traccar/web/client/view/ApplicationSettingsDialog.java b/src/main/java/org/traccar/web/client/view/ApplicationSettingsDialog.java index 20e5a5e9..c0adf0b1 100644 --- a/src/main/java/org/traccar/web/client/view/ApplicationSettingsDialog.java +++ b/src/main/java/org/traccar/web/client/view/ApplicationSettingsDialog.java @@ -15,6 +15,7 @@ */ package org.traccar.web.client.view; +import com.google.gwt.event.logical.shared.*; import com.sencha.gxt.cell.core.client.form.ComboBoxCell; import com.sencha.gxt.data.shared.ListStore; import com.sencha.gxt.widget.core.client.form.*; @@ -23,7 +24,7 @@ import org.traccar.web.client.model.ApplicationSettingsProperties; import org.traccar.web.client.model.EnumKeyProvider; import org.traccar.web.client.widget.LanguageComboBox; -import org.traccar.web.shared.model.ApplicationSettings; +import org.traccar.web.shared.model.*; import com.google.gwt.core.client.GWT; import com.google.gwt.editor.client.Editor; @@ -34,7 +35,6 @@ import com.google.gwt.user.client.ui.Widget; import com.sencha.gxt.widget.core.client.Window; import com.sencha.gxt.widget.core.client.event.SelectEvent; -import org.traccar.web.shared.model.PasswordHashMethod; import java.util.Arrays; @@ -92,6 +92,9 @@ public interface ApplicationSettingsHandler { @UiField TextField bingMapsKey; + @UiField(provided = true) + ComboBox matchServiceType; + @UiField TextField matchServiceURL; @@ -107,6 +110,18 @@ public ApplicationSettingsDialog(ApplicationSettings applicationSettings, Applic defaultHashImplementation.setForceSelection(true); defaultHashImplementation.setTriggerAction(ComboBoxCell.TriggerAction.ALL); + ListStore mstStore = new ListStore<>(new EnumKeyProvider()); + mstStore.addAll(Arrays.asList(MatchServiceType.values())); + matchServiceType = new ComboBox<>(mstStore, new ApplicationSettingsProperties.MatchServiceTypeLabelProvider()); + matchServiceType.setForceSelection(true); + matchServiceType.setTriggerAction(ComboBoxCell.TriggerAction.ALL); + matchServiceType.addSelectionHandler(new SelectionHandler() { + @Override + public void onSelection(SelectionEvent event) { + matchServiceURL.setValue(event.getSelectedItem().getDefaultURL()); + } + }); + language = new LanguageComboBox(); uiBinder.createAndBindUi(this); diff --git a/src/main/java/org/traccar/web/client/view/ApplicationSettingsDialog.ui.xml b/src/main/java/org/traccar/web/client/view/ApplicationSettingsDialog.ui.xml index cac0157b..22018477 100644 --- a/src/main/java/org/traccar/web/client/view/ApplicationSettingsDialog.ui.xml +++ b/src/main/java/org/traccar/web/client/view/ApplicationSettingsDialog.ui.xml @@ -92,6 +92,13 @@ + + + + + + + diff --git a/src/main/java/org/traccar/web/server/model/DBMigrations.java b/src/main/java/org/traccar/web/server/model/DBMigrations.java index 5567c97f..997fced8 100644 --- a/src/main/java/org/traccar/web/server/model/DBMigrations.java +++ b/src/main/java/org/traccar/web/server/model/DBMigrations.java @@ -65,7 +65,7 @@ public void migrate(EntityManager em) throws Exception { new SetGeoFenceAllDevicesFlag(), new SetReportsFilterAndPreview(), new SetDefaultExpiredFlagForEvents(), - new SetDefaultMatchServiceURL(), + new SetDefaultMatchServiceSettings(), new RemoveMapQuest() }) { em.getTransaction().begin(); @@ -448,11 +448,17 @@ public void migrate(EntityManager em) throws Exception { } } - static class SetDefaultMatchServiceURL implements Migration { + static class SetDefaultMatchServiceSettings implements Migration { @Override public void migrate(EntityManager em) throws Exception { - em.createQuery("UPDATE " + ApplicationSettings.class.getName() + " S SET S.matchServiceURL = :url WHERE S.matchServiceURL IS NULL") - .setParameter("url", ApplicationSettings.DEFAULT_MATCH_SERVICE_URL) + // for existing installations, which are using v4 and has no 'matchServiceType' set yet + em.createQuery("UPDATE " + ApplicationSettings.class.getName() + " S SET S.matchServiceType = :msType WHERE S.matchServiceURL IS NOT NULL") + .setParameter("msType", MatchServiceType.OSRM_V4) + .executeUpdate(); + // for new installations without match service type set + em.createQuery("UPDATE " + ApplicationSettings.class.getName() + " S SET S.matchServiceType = :msType, S.matchServiceURL = :url WHERE S.matchServiceType IS NULL") + .setParameter("msType", MatchServiceType.OSRM_V5) + .setParameter("url", MatchServiceType.OSRM_V5.getDefaultURL()) .executeUpdate(); } } diff --git a/src/main/java/org/traccar/web/shared/model/ApplicationSettings.java b/src/main/java/org/traccar/web/shared/model/ApplicationSettings.java index f0511b21..0b5cacb8 100644 --- a/src/main/java/org/traccar/web/shared/model/ApplicationSettings.java +++ b/src/main/java/org/traccar/web/shared/model/ApplicationSettings.java @@ -13,7 +13,6 @@ public class ApplicationSettings implements IsSerializable { private static final long serialVersionUID = 1; public static final short DEFAULT_UPDATE_INTERVAL = 15000; public static final short DEFAULT_NOTIFICATION_EXPIRATION_PERIOD = 12 * 60; - public static final String DEFAULT_MATCH_SERVICE_URL = "https://router.project-osrm.org/match"; @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @@ -28,7 +27,8 @@ public ApplicationSettings() { eventRecordingEnabled = true; language = "default"; notificationExpirationPeriod = DEFAULT_NOTIFICATION_EXPIRATION_PERIOD; - matchServiceURL = DEFAULT_MATCH_SERVICE_URL; + matchServiceType = MatchServiceType.OSRM_V4; + matchServiceURL = matchServiceType.getDefaultURL(); } private boolean registrationEnabled; @@ -129,6 +129,17 @@ public void setNotificationExpirationPeriod(int notificationExpirationPeriod) { this.notificationExpirationPeriod = notificationExpirationPeriod; } + @Enumerated(EnumType.STRING) + private MatchServiceType matchServiceType; + + public MatchServiceType getMatchServiceType() { + return matchServiceType; + } + + public void setMatchServiceType(MatchServiceType matchServiceType) { + this.matchServiceType = matchServiceType; + } + private String matchServiceURL; public String getMatchServiceURL() { diff --git a/src/main/java/org/traccar/web/shared/model/MatchServiceType.java b/src/main/java/org/traccar/web/shared/model/MatchServiceType.java new file mode 100644 index 00000000..ded8828d --- /dev/null +++ b/src/main/java/org/traccar/web/shared/model/MatchServiceType.java @@ -0,0 +1,37 @@ +/* + * Copyright 2016 Vitaly Litvak (vitavaque@gmail.com) + * + * 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; + +public enum MatchServiceType { + OSRM_V4("OSRM v4", "https://router.project-osrm.org/match"), + OSRM_V5("OSRM v5", "https://router.project-osrm.org/match/v1/"); + + private final String name; + private final String defaultURL; + + MatchServiceType(String name, String defaultURL) { + this.name = name; + this.defaultURL = defaultURL; + } + + public String getName() { + return name; + } + + public String getDefaultURL() { + return defaultURL; + } +} diff --git a/src/main/java/org/traccar/web/shared/model/PasswordHashMethod.java b/src/main/java/org/traccar/web/shared/model/PasswordHashMethod.java index b8d9d7a2..f30d7377 100644 --- a/src/main/java/org/traccar/web/shared/model/PasswordHashMethod.java +++ b/src/main/java/org/traccar/web/shared/model/PasswordHashMethod.java @@ -19,8 +19,6 @@ import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; -import java.security.SecureRandom; -import java.util.Random; public enum PasswordHashMethod implements IsSerializable { PLAIN("plain") { diff --git a/src/main/resources/org/traccar/web/client/i18n/Messages.properties b/src/main/resources/org/traccar/web/client/i18n/Messages.properties index 9a3c4418..6f90c528 100644 --- a/src/main/resources/org/traccar/web/client/i18n/Messages.properties +++ b/src/main/resources/org/traccar/web/client/i18n/Messages.properties @@ -30,6 +30,7 @@ eventRecordingEnabled = Event recording defaultHashImplementation = Default password hash bingMapsKey = Bing Maps key notificationExpiryPeriod = Notification expiry period +matchServiceType = OSRM service type matchServiceURL = OSRM service URL # archive view archive = Archive diff --git a/src/main/resources/org/traccar/web/client/i18n/Messages_ru.properties b/src/main/resources/org/traccar/web/client/i18n/Messages_ru.properties index a6ebc3e7..3d0c4c73 100644 --- a/src/main/resources/org/traccar/web/client/i18n/Messages_ru.properties +++ b/src/main/resources/org/traccar/web/client/i18n/Messages_ru.properties @@ -30,6 +30,7 @@ eventRecordingEnabled = Запись событий defaultHashImplementation = Хэш-функция паролей bingMapsKey = Ключ Bing Maps notificationExpiryPeriod = Срок годности уведомлений +matchServiceType = Тип сервера OSRM matchServiceURL = Адрес сервера OSRM # archive view archive = Архив