Skip to content

Commit

Permalink
For #75 - added possibility to disable event recording
Browse files Browse the repository at this point in the history
  • Loading branch information
vitalidze committed Jan 29, 2015
1 parent f4ee730 commit e4ca8cb
Show file tree
Hide file tree
Showing 14 changed files with 84 additions and 6 deletions.
2 changes: 2 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 @@ -243,4 +243,6 @@ public interface Messages extends com.google.gwt.i18n.client.Messages {
String exportData();

String errNoDeviceNameOrId();

String eventRecordingEnabled();
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ public interface ApplicationSettingsProperties extends PropertyAccess<Applicatio

ValueProvider<ApplicationSettings, Boolean> disallowDeviceManagementByUsers();

ValueProvider<ApplicationSettings, Boolean> eventRecordingEnabled();

public static class PasswordHashMethodLabelProvider implements LabelProvider<PasswordHashMethod> {
@Override
public String getLabel(PasswordHashMethod item) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@

@RemoteServiceRelativePath("eventService")
public interface EventService extends RemoteService {
void applicationSettingsChanged();
}
Original file line number Diff line number Diff line change
@@ -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<Void> async);
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ public interface ApplicationSettingsHandler {
@UiField
CheckBox disallowDeviceManagementByUsers;

@UiField
CheckBox eventRecordingEnabled;

@UiField(provided = true)
NumberPropertyEditor<Short> shortPropertyEditor = new NumberPropertyEditor.ShortPropertyEditor();

Expand All @@ -92,8 +95,8 @@ public ApplicationSettingsDialog(ApplicationSettings applicationSettings, Applic

uiBinder.createAndBindUi(this);

updateInterval.addValidator(new MinNumberValidator<Short>(Short.valueOf((short) 100)));
updateInterval.addValidator(new MaxNumberValidator<Short>(Short.valueOf((short) 30000)));
updateInterval.addValidator(new MinNumberValidator<Short>((short) 100));
updateInterval.addValidator(new MaxNumberValidator<Short>((short) 30000));

driver.initialize(this);
driver.edit(applicationSettings);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

<ui:with field='i18n' type='org.traccar.web.client.i18n.Messages' />

<gxt:Window ui:field="window" pixelSize="300, 170" modal="true" headingText="{i18n.globalSettings}" focusWidget="{saveButton}">
<gxt:Window ui:field="window" pixelSize="300, 190" modal="true" headingText="{i18n.globalSettings}" focusWidget="{saveButton}">
<container:VerticalLayoutContainer>
<container:child layoutData="{verticalLayoutData}">
<form:FieldLabel text="{i18n.registration}" labelWidth="210">
Expand All @@ -31,6 +31,13 @@
</form:widget>
</form:FieldLabel>
</container:child>
<container:child layoutData="{verticalLayoutData}">
<form:FieldLabel text="{i18n.eventRecordingEnabled}" labelWidth="210">
<form:widget>
<form:CheckBox ui:field="eventRecordingEnabled" />
</form:widget>
</form:FieldLabel>
</container:child>
<container:child layoutData="{verticalLayoutData}">
<form:FieldLabel text="{i18n.updateInterval}" labelWidth="210">
<form:widget>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ public class ApplicationSettingsProvider implements Provider<ApplicationSettings
public ApplicationSettings get() {
TypedQuery<ApplicationSettings> query = entityManager.get().createQuery("SELECT x FROM ApplicationSettings x", ApplicationSettings.class);
List<ApplicationSettings> resultList = query.getResultList();
return resultList.isEmpty() ? null : resultList.get(0);
return resultList.isEmpty() ? new ApplicationSettings() : resultList.get(0);
}
}
9 changes: 9 additions & 0 deletions src/main/java/org/traccar/web/server/model/DBMigrations.java
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down Expand Up @@ -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();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -52,6 +53,9 @@ public class DataServiceImpl extends RemoteServiceServlet implements DataService
@Inject
private Provider<HttpServletRequest> request;

@Inject
private EventService eventService;

@Override
public void init() throws ServletException {
super.init();
Expand Down Expand Up @@ -503,6 +507,7 @@ public ApplicationSettings getApplicationSettings() {
@Override
public void updateApplicationSettings(ApplicationSettings applicationSettings) {
getSessionEntityManager().merge(applicationSettings);
eventService.applicationSettingsChanged();
}

@Transactional
Expand Down
33 changes: 32 additions & 1 deletion src/main/java/org/traccar/web/server/model/EventServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -72,13 +74,42 @@ public void doWork() {

@Inject
private OfflineDetector offlineDetector;
private ScheduledFuture<?> detectorFuture;
@Inject
private Provider<ApplicationSettings> applicationSettings;

private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);

@Override
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();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
}
Expand Down Expand Up @@ -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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ globalSettings=Глобальные настройки
registration=Регистрация
updateInterval=Интервал обновления
disallowDeviceManagementByUsers = Запретить пользователям управлять устройствами
eventRecordingEnabled = Запись событий
defaultHashImplementation = Хэш-функция паролей
# archive view
archive=Архив
Expand Down

0 comments on commit e4ca8cb

Please sign in to comment.