From 137c89bc9a870c4018f4c000494c89af9ceea67a Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Wed, 9 Jan 2013 23:34:39 +1300 Subject: Finished archive panel --- src/org/traccar/web/client/Application.java | 16 +--- .../web/client/controller/ArchiveController.java | 43 +++++++++-- .../web/client/controller/DeviceController.java | 21 ++++-- .../web/client/controller/MapController.java | 9 ++- .../web/client/model/BaseStoreHandlers.java | 58 ++++++++++++++ .../traccar/web/client/model/DeviceProperties.java | 5 ++ src/org/traccar/web/client/view/ArchiveView.java | 88 ++++++++++++++++++++-- src/org/traccar/web/client/view/ArchiveView.ui.xml | 20 ++--- src/org/traccar/web/client/view/DeviceView.java | 27 +------ src/org/traccar/web/client/view/DeviceView.ui.xml | 4 +- .../traccar/web/server/model/DataServiceImpl.java | 16 +++- src/org/traccar/web/shared/model/Position.java | 2 + 12 files changed, 237 insertions(+), 72 deletions(-) create mode 100644 src/org/traccar/web/client/model/BaseStoreHandlers.java (limited to 'src') diff --git a/src/org/traccar/web/client/Application.java b/src/org/traccar/web/client/Application.java index 9b14d421..22f20d8c 100644 --- a/src/org/traccar/web/client/Application.java +++ b/src/org/traccar/web/client/Application.java @@ -1,8 +1,6 @@ package org.traccar.web.client; -import java.util.HashMap; import java.util.List; -import java.util.Map; import org.traccar.web.client.controller.ArchiveController; import org.traccar.web.client.controller.DeviceController; @@ -33,7 +31,7 @@ public class Application { public Application() { deviceController = new DeviceController(deviceHandler); mapController = new MapController(); - archiveController = new ArchiveController(archiveHanlder); + archiveController = new ArchiveController(archiveHanlder, deviceController.getDeviceStore()); view = new ApplicationView( deviceController.getView(), mapController.getView(), archiveController.getView()); @@ -47,16 +45,10 @@ public class Application { archiveController.run(); } - private Map devices = new HashMap(); - private DeviceController.DeviceHandler deviceHandler = new DeviceController.DeviceHandler() { @Override public void onLoad(List devices) { - Application.this.devices.clear(); - for (Device device : devices) { - Application.this.devices.put(device.getId(), device); - } } @Override @@ -66,21 +58,15 @@ public class Application { @Override public void onAdd(Device device) { - devices.put(device.getId(), device); - archiveController.updateDevices(devices.values()); mapController.update(); } @Override public void onUpdate(Device device) { - devices.put(device.getId(), device); - archiveController.updateDevices(devices.values()); } @Override public void onRemove(Device device) { - devices.remove(device.getId()); - archiveController.updateDevices(devices.values()); mapController.update(); } diff --git a/src/org/traccar/web/client/controller/ArchiveController.java b/src/org/traccar/web/client/controller/ArchiveController.java index 598a744d..9f865b65 100644 --- a/src/org/traccar/web/client/controller/ArchiveController.java +++ b/src/org/traccar/web/client/controller/ArchiveController.java @@ -1,12 +1,19 @@ package org.traccar.web.client.controller; -import java.util.Collection; +import java.util.Date; +import java.util.List; +import org.traccar.web.client.Application; +import org.traccar.web.client.model.BaseAsyncCallback; +import org.traccar.web.client.model.PositionProperties; import org.traccar.web.client.view.ArchiveView; import org.traccar.web.shared.model.Device; import org.traccar.web.shared.model.Position; +import com.google.gwt.core.client.GWT; +import com.sencha.gxt.data.shared.ListStore; import com.sencha.gxt.widget.core.client.ContentPanel; +import com.sencha.gxt.widget.core.client.box.AlertMessageBox; public class ArchiveController implements ContentController, ArchiveView.ArchiveHandler { @@ -16,11 +23,19 @@ public class ArchiveController implements ContentController, ArchiveView.Archive private ArchiveHandler archiveHandler; + private ListStore positionStore; + private ArchiveView archiveView; - public ArchiveController(ArchiveHandler archiveHandler) { + public ArchiveController(ArchiveHandler archiveHandler, ListStore deviceStore) { this.archiveHandler = archiveHandler; - archiveView = new ArchiveView(this); + PositionProperties positionProperties = GWT.create(PositionProperties.class); + positionStore = new ListStore(positionProperties.id()); + archiveView = new ArchiveView(this, positionStore, deviceStore); + } + + public ListStore getPositionStore() { + return positionStore; } @Override @@ -32,13 +47,29 @@ public class ArchiveController implements ContentController, ArchiveView.Archive public void run() { } - public void updateDevices(Collection devices) { + @Override + public void onSelected(Position position) { + archiveHandler.onSelected(position); + } + @Override + public void onLoad(Device device, Date from, Date to) { + if (device != null && from != null && to != null) { + Application.getDataService().getPositions(device, from, to, new BaseAsyncCallback>() { + @Override + public void onSuccess(List result) { + positionStore.clear(); + positionStore.addAll(result); + } + }); + } else { + new AlertMessageBox("Error", "All form fields must be filled first").show(); + } } @Override - public void onSelected(Position position) { - archiveHandler.onSelected(position); + public void onClear() { + positionStore.clear(); } } diff --git a/src/org/traccar/web/client/controller/DeviceController.java b/src/org/traccar/web/client/controller/DeviceController.java index b24c205e..865aaeae 100644 --- a/src/org/traccar/web/client/controller/DeviceController.java +++ b/src/org/traccar/web/client/controller/DeviceController.java @@ -4,10 +4,13 @@ import java.util.List; import org.traccar.web.client.Application; import org.traccar.web.client.model.BaseAsyncCallback; +import org.traccar.web.client.model.DeviceProperties; import org.traccar.web.client.view.DeviceDialog; import org.traccar.web.client.view.DeviceView; import org.traccar.web.shared.model.Device; +import com.google.gwt.core.client.GWT; +import com.sencha.gxt.data.shared.ListStore; import com.sencha.gxt.widget.core.client.ContentPanel; import com.sencha.gxt.widget.core.client.Dialog.PredefinedButton; import com.sencha.gxt.widget.core.client.box.ConfirmMessageBox; @@ -26,11 +29,19 @@ public class DeviceController implements ContentController, DeviceView.DeviceHan private DeviceHandler deviceHandler; + private ListStore deviceStore; + private DeviceView deviceView; public DeviceController(DeviceHandler deviceHandler) { this.deviceHandler = deviceHandler; - deviceView = new DeviceView(this); + DeviceProperties deviceProperties = GWT.create(DeviceProperties.class); + deviceStore = new ListStore(deviceProperties.id()); + deviceView = new DeviceView(this, deviceStore); + } + + public ListStore getDeviceStore() { + return deviceStore; } @Override @@ -43,7 +54,7 @@ public class DeviceController implements ContentController, DeviceView.DeviceHan Application.getDataService().getDevices(new BaseAsyncCallback>() { @Override public void onSuccess(List result) { - deviceView.load(result); + deviceStore.addAll(result); deviceHandler.onLoad(result); } }); @@ -62,7 +73,7 @@ public class DeviceController implements ContentController, DeviceView.DeviceHan Application.getDataService().addDevice(device, new BaseAsyncCallback() { @Override public void onSuccess(Device result) { - deviceView.add(result); + deviceStore.add(result); deviceHandler.onAdd(result); } }); @@ -78,7 +89,7 @@ public class DeviceController implements ContentController, DeviceView.DeviceHan Application.getDataService().updateDevice(device, new BaseAsyncCallback() { @Override public void onSuccess(Device result) { - deviceView.update(result); + deviceStore.update(result); deviceHandler.onUpdate(result); } }); @@ -96,7 +107,7 @@ public class DeviceController implements ContentController, DeviceView.DeviceHan Application.getDataService().removeDevice(device, new BaseAsyncCallback() { @Override public void onSuccess(Device result) { - deviceView.remove(device); + deviceStore.remove(device); deviceHandler.onRemove(device); } }); diff --git a/src/org/traccar/web/client/controller/MapController.java b/src/org/traccar/web/client/controller/MapController.java index 5a53cf22..5ab5bbcf 100644 --- a/src/org/traccar/web/client/controller/MapController.java +++ b/src/org/traccar/web/client/controller/MapController.java @@ -1,9 +1,14 @@ package org.traccar.web.client.controller; +import java.util.List; + +import org.traccar.web.client.Application; import org.traccar.web.client.view.MapView; import org.traccar.web.shared.model.Device; +import org.traccar.web.shared.model.Position; import com.google.gwt.user.client.Timer; +import com.google.gwt.user.client.rpc.AsyncCallback; import com.sencha.gxt.widget.core.client.ContentPanel; public class MapController implements ContentController { @@ -36,7 +41,7 @@ public class MapController implements ContentController { public void update() { updateTimer.cancel(); - /*Application.getDataService().getLatestPositions(new AsyncCallback>() { + Application.getDataService().getLatestPositions(new AsyncCallback>() { @Override public void onSuccess(List result) { mapView.showPositions(result); @@ -46,7 +51,7 @@ public class MapController implements ContentController { public void onFailure(Throwable caught) { updateTimer.schedule(UPDATE_INTERVAL); } - });*/ + }); } public void select(Device device) { diff --git a/src/org/traccar/web/client/model/BaseStoreHandlers.java b/src/org/traccar/web/client/model/BaseStoreHandlers.java new file mode 100644 index 00000000..ed4e5419 --- /dev/null +++ b/src/org/traccar/web/client/model/BaseStoreHandlers.java @@ -0,0 +1,58 @@ +package org.traccar.web.client.model; + +import com.sencha.gxt.data.shared.event.StoreAddEvent; +import com.sencha.gxt.data.shared.event.StoreClearEvent; +import com.sencha.gxt.data.shared.event.StoreDataChangeEvent; +import com.sencha.gxt.data.shared.event.StoreFilterEvent; +import com.sencha.gxt.data.shared.event.StoreHandlers; +import com.sencha.gxt.data.shared.event.StoreRecordChangeEvent; +import com.sencha.gxt.data.shared.event.StoreRemoveEvent; +import com.sencha.gxt.data.shared.event.StoreSortEvent; +import com.sencha.gxt.data.shared.event.StoreUpdateEvent; + +public class BaseStoreHandlers implements StoreHandlers { + + @Override + public void onAdd(StoreAddEvent event) { + onAnything(); + } + + @Override + public void onRemove(StoreRemoveEvent event) { + onAnything(); + } + + @Override + public void onFilter(StoreFilterEvent event) { + onAnything(); + } + + @Override + public void onClear(StoreClearEvent event) { + onAnything(); + } + + @Override + public void onUpdate(StoreUpdateEvent event) { + onAnything(); + } + + @Override + public void onDataChange(StoreDataChangeEvent event) { + onAnything(); + } + + @Override + public void onRecordChange(StoreRecordChangeEvent event) { + onAnything(); + } + + @Override + public void onSort(StoreSortEvent event) { + onAnything(); + } + + public void onAnything() { + } + +} diff --git a/src/org/traccar/web/client/model/DeviceProperties.java b/src/org/traccar/web/client/model/DeviceProperties.java index 2c56ff6d..2ba4d47f 100644 --- a/src/org/traccar/web/client/model/DeviceProperties.java +++ b/src/org/traccar/web/client/model/DeviceProperties.java @@ -2,7 +2,9 @@ package org.traccar.web.client.model; import org.traccar.web.shared.model.Device; +import com.google.gwt.editor.client.Editor.Path; import com.sencha.gxt.core.client.ValueProvider; +import com.sencha.gxt.data.shared.LabelProvider; import com.sencha.gxt.data.shared.ModelKeyProvider; import com.sencha.gxt.data.shared.PropertyAccess; @@ -14,4 +16,7 @@ public interface DeviceProperties extends PropertyAccess { ValueProvider name(); + @Path("name") + LabelProvider label(); + } diff --git a/src/org/traccar/web/client/view/ArchiveView.java b/src/org/traccar/web/client/view/ArchiveView.java index 79621ba9..f61ffccc 100644 --- a/src/org/traccar/web/client/view/ArchiveView.java +++ b/src/org/traccar/web/client/view/ArchiveView.java @@ -4,7 +4,10 @@ import java.util.Date; import java.util.LinkedList; import java.util.List; +import org.traccar.web.client.model.BaseStoreHandlers; +import org.traccar.web.client.model.DeviceProperties; import org.traccar.web.client.model.PositionProperties; +import org.traccar.web.shared.model.Device; import org.traccar.web.shared.model.Position; import com.google.gwt.cell.client.DateCell; @@ -12,15 +15,21 @@ import com.google.gwt.core.client.GWT; import com.google.gwt.i18n.client.DateTimeFormat; import com.google.gwt.uibinder.client.UiBinder; import com.google.gwt.uibinder.client.UiField; -import com.google.gwt.user.client.ui.Composite; +import com.google.gwt.uibinder.client.UiHandler; import com.google.gwt.user.client.ui.Widget; +import com.sencha.gxt.core.client.Style.SelectionMode; import com.sencha.gxt.data.shared.ListStore; +import com.sencha.gxt.data.shared.event.StoreHandlers; import com.sencha.gxt.widget.core.client.ContentPanel; +import com.sencha.gxt.widget.core.client.event.SelectEvent; +import com.sencha.gxt.widget.core.client.form.ComboBox; +import com.sencha.gxt.widget.core.client.form.DateField; +import com.sencha.gxt.widget.core.client.form.TimeField; 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; -public class ArchiveView extends Composite { +public class ArchiveView { private static ArchiveViewUiBinder uiBinder = GWT.create(ArchiveViewUiBinder.class); @@ -29,6 +38,8 @@ public class ArchiveView extends Composite { public interface ArchiveHandler { public void onSelected(Position position); + public void onLoad(Device device, Date from, Date to); + public void onClear(); } private ArchiveHandler archiveHandler; @@ -40,17 +51,40 @@ public class ArchiveView extends Composite { return contentPanel; } + ListStore deviceStore; + + @UiField + DateField fromDate; + + @UiField + TimeField fromTime; + + @UiField + DateField toDate; + + @UiField + TimeField toTime; + + @UiField(provided = true) + ComboBox deviceCombo; + @UiField(provided = true) ColumnModel columnModel; @UiField(provided = true) - ListStore store; + ListStore positionStore; @UiField Grid grid; - public ArchiveView(ArchiveHandler archiveHandler) { + public ArchiveView(ArchiveHandler archiveHandler, ListStore positionStore, ListStore deviceStore) { this.archiveHandler = archiveHandler; + this.positionStore = positionStore; + deviceStore.addStoreHandlers(deviceStoreHandlers); + this.deviceStore = deviceStore; + + DeviceProperties deviceProperties = GWT.create(DeviceProperties.class); + deviceCombo = new ComboBox(deviceStore, deviceProperties.label()); PositionProperties positionProperties = GWT.create(PositionProperties.class); @@ -71,9 +105,51 @@ public class ArchiveView extends Composite { columnModel = new ColumnModel(columnConfigList); - store = new ListStore(positionProperties.id()); - uiBinder.createAndBindUi(this); + + grid.getSelectionModel().setSelectionMode(SelectionMode.SINGLE); + + // Initialize with current time + Date now = new Date(); + fromDate.setValue(now); + fromTime.setValue(now); + toDate.setValue(now); + toTime.setValue(now); + } + + @SuppressWarnings("deprecation") + private static Date getCombineDate(DateField dateField, TimeField timeField) { + Date result = null; + Date date = dateField.getValue(); + Date time = timeField.getValue(); + if (date != null && time != null) { + result = new Date( + date.getYear(), date.getMonth(), date.getDate(), + time.getHours(), time.getMinutes(), time.getSeconds()); + } + return result; + } + + @UiHandler("loadButton") + public void onLoadClicked(SelectEvent event) { + archiveHandler.onLoad( + deviceCombo.getValue(), + getCombineDate(fromDate, fromTime), + getCombineDate(toDate, toTime)); + } + + @UiHandler("clearButton") + public void onClearClicked(SelectEvent event) { + archiveHandler.onClear(); } + private StoreHandlers deviceStoreHandlers = new BaseStoreHandlers() { + + @Override + public void onAnything() { + deviceCombo.setValue(deviceStore.findModel(deviceCombo.getValue())); + } + + }; + } diff --git a/src/org/traccar/web/client/view/ArchiveView.ui.xml b/src/org/traccar/web/client/view/ArchiveView.ui.xml index 370eb024..f27d38ff 100644 --- a/src/org/traccar/web/client/view/ArchiveView.ui.xml +++ b/src/org/traccar/web/client/view/ArchiveView.ui.xml @@ -16,7 +16,7 @@ - + @@ -30,28 +30,30 @@ - + - + - + - + - + - + - + + + - + diff --git a/src/org/traccar/web/client/view/DeviceView.java b/src/org/traccar/web/client/view/DeviceView.java index 157973e0..1213e5f0 100644 --- a/src/org/traccar/web/client/view/DeviceView.java +++ b/src/org/traccar/web/client/view/DeviceView.java @@ -1,6 +1,5 @@ package org.traccar.web.client.view; -import java.util.Collection; import java.util.LinkedList; import java.util.List; @@ -11,7 +10,6 @@ import com.google.gwt.core.client.GWT; import com.google.gwt.uibinder.client.UiBinder; import com.google.gwt.uibinder.client.UiField; import com.google.gwt.uibinder.client.UiHandler; -import com.google.gwt.user.client.ui.Composite; import com.google.gwt.user.client.ui.Widget; import com.sencha.gxt.core.client.Style.SelectionMode; import com.sencha.gxt.data.shared.ListStore; @@ -23,7 +21,7 @@ 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.selection.SelectionChangedEvent; -public class DeviceView extends Composite implements SelectionChangedEvent.SelectionChangedHandler { +public class DeviceView implements SelectionChangedEvent.SelectionChangedHandler { private static DeviceViewUiBinder uiBinder = GWT.create(DeviceViewUiBinder.class); @@ -59,13 +57,14 @@ public class DeviceView extends Composite implements SelectionChangedEvent.Selec ColumnModel columnModel; @UiField(provided = true) - ListStore store; + ListStore deviceStore; @UiField Grid grid; - public DeviceView(DeviceHandler deviceHandler) { + public DeviceView(DeviceHandler deviceHandler, ListStore deviceStore) { this.deviceHandler = deviceHandler; + this.deviceStore = deviceStore; DeviceProperties deviceProperties = GWT.create(DeviceProperties.class); @@ -74,8 +73,6 @@ public class DeviceView extends Composite implements SelectionChangedEvent.Selec columnConfigList.add(new ColumnConfig(deviceProperties.uniqueId(), 0, "Unique Identifier")); columnModel = new ColumnModel(columnConfigList); - store = new ListStore(deviceProperties.id()); - uiBinder.createAndBindUi(this); grid.getSelectionModel().addSelectionChangedHandler(this); @@ -94,35 +91,19 @@ public class DeviceView extends Composite implements SelectionChangedEvent.Selec } } - public void load(Collection devices) { - store.addAll(devices); - } - @UiHandler("addButton") public void onAddClicked(SelectEvent event) { deviceHandler.onAdd(); } - public void add(Device device) { - store.add(store.size(), device); - } - @UiHandler("editButton") public void onEditClicked(SelectEvent event) { deviceHandler.onEdit(grid.getSelectionModel().getSelectedItem()); } - public void update(Device device) { - store.update(device); - } - @UiHandler("removeButton") public void onRemoveClicked(SelectEvent event) { deviceHandler.onRemove(grid.getSelectionModel().getSelectedItem()); } - public void remove(Device device) { - store.remove(device); - } - } diff --git a/src/org/traccar/web/client/view/DeviceView.ui.xml b/src/org/traccar/web/client/view/DeviceView.ui.xml index d9988d5e..af1b33cd 100644 --- a/src/org/traccar/web/client/view/DeviceView.ui.xml +++ b/src/org/traccar/web/client/view/DeviceView.ui.xml @@ -15,7 +15,7 @@ - + @@ -37,7 +37,7 @@ - + diff --git a/src/org/traccar/web/server/model/DataServiceImpl.java b/src/org/traccar/web/server/model/DataServiceImpl.java index cfcc6226..f9d0e7e3 100644 --- a/src/org/traccar/web/server/model/DataServiceImpl.java +++ b/src/org/traccar/web/server/model/DataServiceImpl.java @@ -90,8 +90,10 @@ public class DataServiceImpl extends RemoteServiceServlet implements DataService entityManager.getTransaction().begin(); try { entityManager.persist(user); - } finally { entityManager.getTransaction().commit(); + } catch (Exception e) { + entityManager.getTransaction().rollback(); + throw e; } setUser(user); return true; @@ -112,8 +114,10 @@ public class DataServiceImpl extends RemoteServiceServlet implements DataService try { entityManager.persist(device); user.getDevices().add(device); - } finally { entityManager.getTransaction().commit(); + } catch (Exception e) { + entityManager.getTransaction().rollback(); + throw e; } return device; } @@ -123,8 +127,10 @@ public class DataServiceImpl extends RemoteServiceServlet implements DataService entityManager.getTransaction().begin(); try { device = entityManager.merge(device); - } finally { entityManager.getTransaction().commit(); + } catch (Exception e) { + entityManager.getTransaction().rollback(); + throw e; } return device; } @@ -137,8 +143,10 @@ public class DataServiceImpl extends RemoteServiceServlet implements DataService device = entityManager.merge(device); user.getDevices().remove(device); entityManager.remove(device); - } finally { entityManager.getTransaction().commit(); + } catch (Exception e) { + entityManager.getTransaction().rollback(); + throw e; } return device; } diff --git a/src/org/traccar/web/shared/model/Position.java b/src/org/traccar/web/shared/model/Position.java index 13827301..2980860a 100644 --- a/src/org/traccar/web/shared/model/Position.java +++ b/src/org/traccar/web/shared/model/Position.java @@ -11,6 +11,8 @@ import javax.persistence.Table; import org.hibernate.annotations.Index; +//INSERT INTO positions (device_id, valid, time, latitude, longitude, altitude, speed, course) VALUES (11, 1, NOW(), 60, 30, 0, 0, 0); + @Entity @Table(name = "positions") public class Position implements Serializable, Cloneable { -- cgit v1.2.3