From e4ca8cb51fd219c31e90b1b63464a326ba0fc287 Mon Sep 17 00:00:00 2001 From: Vitaly Litvak Date: Thu, 29 Jan 2015 22:54:16 +0300 Subject: [PATCH] For #75 - added possibility to disable event recording --- .../org/traccar/web/client/i18n/Messages.java | 2 ++ .../model/ApplicationSettingsProperties.java | 2 ++ .../web/client/model/EventService.java | 1 + .../web/client/model/EventServiceAsync.java | 2 ++ .../view/ApplicationSettingsDialog.java | 7 ++-- .../view/ApplicationSettingsDialog.ui.xml | 9 ++++- .../model/ApplicationSettingsProvider.java | 2 +- .../web/server/model/DBMigrations.java | 9 +++++ .../web/server/model/DataServiceImpl.java | 5 +++ .../web/server/model/EventServiceImpl.java | 33 ++++++++++++++++++- .../web/server/model/GuiceServletConfig.java | 2 ++ .../web/shared/model/ApplicationSettings.java | 14 +++++++- .../web/client/i18n/Messages.properties | 1 + .../web/client/i18n/Messages_ru.properties | 1 + 14 files changed, 84 insertions(+), 6 deletions(-) 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 ea4fa83a..182aebe6 100644 --- a/src/main/java/org/traccar/web/client/i18n/Messages.java +++ b/src/main/java/org/traccar/web/client/i18n/Messages.java @@ -243,4 +243,6 @@ public interface Messages extends com.google.gwt.i18n.client.Messages { String exportData(); String errNoDeviceNameOrId(); + + String eventRecordingEnabled(); } 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 34447509..8d126f1d 100644 --- a/src/main/java/org/traccar/web/client/model/ApplicationSettingsProperties.java +++ b/src/main/java/org/traccar/web/client/model/ApplicationSettingsProperties.java @@ -33,6 +33,8 @@ public interface ApplicationSettingsProperties extends PropertyAccess disallowDeviceManagementByUsers(); + ValueProvider eventRecordingEnabled(); + public static class PasswordHashMethodLabelProvider implements LabelProvider { @Override public String getLabel(PasswordHashMethod item) { diff --git a/src/main/java/org/traccar/web/client/model/EventService.java b/src/main/java/org/traccar/web/client/model/EventService.java index 3bc299c8..38590bae 100644 --- a/src/main/java/org/traccar/web/client/model/EventService.java +++ b/src/main/java/org/traccar/web/client/model/EventService.java @@ -20,4 +20,5 @@ @RemoteServiceRelativePath("eventService") public interface EventService extends RemoteService { + void applicationSettingsChanged(); } diff --git a/src/main/java/org/traccar/web/client/model/EventServiceAsync.java b/src/main/java/org/traccar/web/client/model/EventServiceAsync.java index 3c648c2b..e966a233 100644 --- a/src/main/java/org/traccar/web/client/model/EventServiceAsync.java +++ b/src/main/java/org/traccar/web/client/model/EventServiceAsync.java @@ -1,7 +1,9 @@ package org.traccar.web.client.model; +import com.google.gwt.user.client.rpc.AsyncCallback; import com.google.gwt.user.client.rpc.RemoteService; import com.google.gwt.user.client.rpc.RemoteServiceRelativePath; public interface EventServiceAsync { + void applicationSettingsChanged(AsyncCallback async); } 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 77ed46bf..c03a7372 100644 --- a/src/main/java/org/traccar/web/client/view/ApplicationSettingsDialog.java +++ b/src/main/java/org/traccar/web/client/view/ApplicationSettingsDialog.java @@ -69,6 +69,9 @@ public interface ApplicationSettingsHandler { @UiField CheckBox disallowDeviceManagementByUsers; + @UiField + CheckBox eventRecordingEnabled; + @UiField(provided = true) NumberPropertyEditor shortPropertyEditor = new NumberPropertyEditor.ShortPropertyEditor(); @@ -92,8 +95,8 @@ public ApplicationSettingsDialog(ApplicationSettings applicationSettings, Applic uiBinder.createAndBindUi(this); - updateInterval.addValidator(new MinNumberValidator(Short.valueOf((short) 100))); - updateInterval.addValidator(new MaxNumberValidator(Short.valueOf((short) 30000))); + updateInterval.addValidator(new MinNumberValidator((short) 100)); + updateInterval.addValidator(new MaxNumberValidator((short) 30000)); driver.initialize(this); driver.edit(applicationSettings); 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 94e8c355..fed3c699 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 @@ -15,7 +15,7 @@ - + @@ -31,6 +31,13 @@ + + + + + + + diff --git a/src/main/java/org/traccar/web/server/model/ApplicationSettingsProvider.java b/src/main/java/org/traccar/web/server/model/ApplicationSettingsProvider.java index 98d14e0b..32684b7c 100644 --- a/src/main/java/org/traccar/web/server/model/ApplicationSettingsProvider.java +++ b/src/main/java/org/traccar/web/server/model/ApplicationSettingsProvider.java @@ -16,6 +16,6 @@ public class ApplicationSettingsProvider implements Provider query = entityManager.get().createQuery("SELECT x FROM ApplicationSettings x", ApplicationSettings.class); List resultList = query.getResultList(); - return resultList.isEmpty() ? null : resultList.get(0); + return resultList.isEmpty() ? new ApplicationSettings() : resultList.get(0); } } 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 7d0f9f55..1840f683 100644 --- a/src/main/java/org/traccar/web/server/model/DBMigrations.java +++ b/src/main/java/org/traccar/web/server/model/DBMigrations.java @@ -33,6 +33,7 @@ public void migrate(EntityManager em) throws Exception { new SetDefaultDeviceTimeout(), new SetDefaultIdleSpeedThreshold(), new SetDefaultDisallowDeviceManagementByUsers(), + new SetDefaultEventRecordingEnabled(), new SetDefaultMapType(), new CreateAdmin(), new SetDefaultDeviceIconType(), @@ -225,6 +226,14 @@ public void migrate(EntityManager em) throws Exception { .setParameter("n", Boolean.FALSE) .executeUpdate(); } + } + static class SetDefaultEventRecordingEnabled implements Migration { + @Override + public void migrate(EntityManager em) throws Exception { + em.createQuery("UPDATE " + ApplicationSettings.class.getName() + " S SET S.eventRecordingEnabled = :b WHERE S.eventRecordingEnabled IS NULL") + .setParameter("b", Boolean.TRUE) + .executeUpdate(); + } } } diff --git a/src/main/java/org/traccar/web/server/model/DataServiceImpl.java b/src/main/java/org/traccar/web/server/model/DataServiceImpl.java index e8cbae1d..f366df7d 100644 --- a/src/main/java/org/traccar/web/server/model/DataServiceImpl.java +++ b/src/main/java/org/traccar/web/server/model/DataServiceImpl.java @@ -34,6 +34,7 @@ import com.google.inject.persist.Transactional; import org.traccar.web.client.model.DataService; +import org.traccar.web.client.model.EventService; import org.traccar.web.shared.model.*; @Singleton @@ -52,6 +53,9 @@ public class DataServiceImpl extends RemoteServiceServlet implements DataService @Inject private Provider request; + @Inject + private EventService eventService; + @Override public void init() throws ServletException { super.init(); @@ -503,6 +507,7 @@ public ApplicationSettings getApplicationSettings() { @Override public void updateApplicationSettings(ApplicationSettings applicationSettings) { getSessionEntityManager().merge(applicationSettings); + eventService.applicationSettingsChanged(); } @Transactional diff --git a/src/main/java/org/traccar/web/server/model/EventServiceImpl.java b/src/main/java/org/traccar/web/server/model/EventServiceImpl.java index d9d9573e..d9dbf423 100644 --- a/src/main/java/org/traccar/web/server/model/EventServiceImpl.java +++ b/src/main/java/org/traccar/web/server/model/EventServiceImpl.java @@ -18,6 +18,7 @@ import com.google.gwt.user.server.rpc.RemoteServiceServlet; import com.google.inject.persist.Transactional; import org.traccar.web.client.model.EventService; +import org.traccar.web.shared.model.ApplicationSettings; import org.traccar.web.shared.model.Device; import org.traccar.web.shared.model.DeviceEvent; import org.traccar.web.shared.model.DeviceEventType; @@ -31,6 +32,7 @@ import java.util.List; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.ScheduledFuture; import java.util.concurrent.TimeUnit; @Singleton @@ -72,6 +74,9 @@ public void doWork() { @Inject private OfflineDetector offlineDetector; + private ScheduledFuture detectorFuture; + @Inject + private Provider applicationSettings; private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1); @@ -79,6 +84,32 @@ public void doWork() { public void init() throws ServletException { super.init(); - scheduler.scheduleAtFixedRate(offlineDetector, 0, 1, TimeUnit.MINUTES); + if (applicationSettings.get().isEventRecordingEnabled()) { + startOfflineDetector(); + } + } + + private synchronized void startOfflineDetector() { + if (detectorFuture == null) { + detectorFuture = scheduler.scheduleAtFixedRate(offlineDetector, 0, 1, TimeUnit.MINUTES); + } + } + + private synchronized void stopOfflineDetector() { + if (detectorFuture != null) { + detectorFuture.cancel(true); + detectorFuture = null; + } + } + + @Transactional + @RequireUser(roles = { Role.ADMIN }) + @Override + public void applicationSettingsChanged() { + if (applicationSettings.get().isEventRecordingEnabled()) { + startOfflineDetector(); + } else { + stopOfflineDetector(); + } } } diff --git a/src/main/java/org/traccar/web/server/model/GuiceServletConfig.java b/src/main/java/org/traccar/web/server/model/GuiceServletConfig.java index 261c9411..78dc93d7 100644 --- a/src/main/java/org/traccar/web/server/model/GuiceServletConfig.java +++ b/src/main/java/org/traccar/web/server/model/GuiceServletConfig.java @@ -23,6 +23,7 @@ import com.google.inject.servlet.GuiceServletContextListener; import com.google.inject.servlet.ServletModule; import org.traccar.web.client.model.DataService; +import org.traccar.web.client.model.EventService; import org.traccar.web.shared.model.ApplicationSettings; import org.traccar.web.shared.model.User; @@ -72,6 +73,7 @@ protected void configureServlets() { bind(User.class).toProvider(CurrentUserProvider.class); bind(ApplicationSettings.class).toProvider(ApplicationSettingsProvider.class); bind(DataService.class).to(DataServiceImpl.class); + bind(EventService.class).to(EventServiceImpl.class); } }); } 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 4ceba290..7fac98e9 100644 --- a/src/main/java/org/traccar/web/shared/model/ApplicationSettings.java +++ b/src/main/java/org/traccar/web/shared/model/ApplicationSettings.java @@ -29,8 +29,9 @@ public class ApplicationSettings implements Serializable { public ApplicationSettings() { registrationEnabled = true; - updateInterval = Short.valueOf(DEFAULT_UPDATE_INTERVAL); + updateInterval = DEFAULT_UPDATE_INTERVAL; defaultPasswordHash = PasswordHashMethod.PLAIN; + eventRecordingEnabled = true; } @Expose @@ -47,6 +48,9 @@ public ApplicationSettings() { @Column(nullable = true) private boolean disallowDeviceManagementByUsers; + @Column(nullable = true) + private boolean eventRecordingEnabled; + public void setRegistrationEnabled(boolean registrationEnabled) { this.registrationEnabled = registrationEnabled; } @@ -79,6 +83,14 @@ public void setDefaultHashImplementation(PasswordHashMethod hash) { this.defaultPasswordHash = hash; } + public boolean isEventRecordingEnabled() { + return eventRecordingEnabled; + } + + public void setEventRecordingEnabled(boolean eventRecordingEnabled) { + this.eventRecordingEnabled = eventRecordingEnabled; + } + @Override public int hashCode() { return (int) (id ^ (id >>> 32)); 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 8f6686b1..af646c70 100644 --- a/src/main/resources/org/traccar/web/client/i18n/Messages.properties +++ b/src/main/resources/org/traccar/web/client/i18n/Messages.properties @@ -19,6 +19,7 @@ globalSettings = Global Settings registration = Registration updateInterval = Update interval disallowDeviceManagementByUsers = Disallow users to manage devices +eventRecordingEnabled = Event recording defaultHashImplementation = Default password hash # 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 3d957e1d..3f4a9bc6 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 @@ -19,6 +19,7 @@ globalSettings=Глобальные настройки registration=Регистрация updateInterval=Интервал обновления disallowDeviceManagementByUsers = Запретить пользователям управлять устройствами +eventRecordingEnabled = Запись событий defaultHashImplementation = Хэш-функция паролей # archive view archive=Архив