From a9478ded48de140d47d17def1ee5329267fe6088 Mon Sep 17 00:00:00 2001 From: Abyss777 Date: Wed, 19 Jul 2017 15:24:55 +0500 Subject: Refactored four managers --- .../api/resource/AttributePermissionResource.java | 8 +- .../traccar/api/resource/AttributeResource.java | 26 +- .../api/resource/CalendarPermissionResource.java | 8 +- src/org/traccar/api/resource/CalendarResource.java | 20 +- .../api/resource/DeviceAttributeResource.java | 4 +- .../traccar/api/resource/DeviceDriverResource.java | 4 +- .../api/resource/DeviceGeofenceResource.java | 4 +- .../api/resource/DriverPermissionResource.java | 8 +- src/org/traccar/api/resource/DriverResource.java | 26 +- src/org/traccar/api/resource/EventResource.java | 2 +- .../api/resource/GeofencePermissionResource.java | 8 +- src/org/traccar/api/resource/GeofenceResource.java | 27 +- .../api/resource/GroupAttributeResource.java | 4 +- .../traccar/api/resource/GroupDriverResource.java | 4 +- .../api/resource/GroupGeofenceResource.java | 4 +- src/org/traccar/api/resource/UserResource.java | 2 +- src/org/traccar/database/AttributesManager.java | 171 +------------ src/org/traccar/database/CalendarManager.java | 99 +------ src/org/traccar/database/DataManager.java | 183 ++----------- src/org/traccar/database/DriversManager.java | 179 +++---------- .../traccar/database/ExtendedObjectManager.java | 129 ++++++++++ src/org/traccar/database/GeofenceManager.java | 284 ++------------------- src/org/traccar/database/NotificationManager.java | 2 +- src/org/traccar/database/PermissionsManager.java | 73 +++--- src/org/traccar/database/SimpleObjectManager.java | 164 ++++++++++++ src/org/traccar/events/GeofenceEventHandler.java | 16 +- src/org/traccar/model/Attribute.java | 12 +- src/org/traccar/model/AttributeAlias.java | 12 +- src/org/traccar/model/AttributePermission.java | 17 +- src/org/traccar/model/BaseDevicePermission.java | 31 +++ src/org/traccar/model/BaseGroupPermission.java | 31 +++ src/org/traccar/model/BasePermission.java | 23 ++ src/org/traccar/model/BaseUserPermission.java | 31 +++ src/org/traccar/model/CalendarPermission.java | 17 +- src/org/traccar/model/DeviceAttribute.java | 17 +- src/org/traccar/model/DeviceDriver.java | 17 +- src/org/traccar/model/DeviceGeofence.java | 17 +- src/org/traccar/model/DriverPermission.java | 17 +- src/org/traccar/model/Extensible.java | 12 +- src/org/traccar/model/GeofencePermission.java | 17 +- src/org/traccar/model/GroupAttribute.java | 17 +- src/org/traccar/model/GroupDriver.java | 17 +- src/org/traccar/model/GroupGeofence.java | 17 +- src/org/traccar/model/Identifiable.java | 31 +++ src/org/traccar/notification/EventForwarder.java | 2 +- .../notification/NotificationFormatter.java | 2 +- .../processing/ComputedAttributesHandler.java | 4 +- src/org/traccar/reports/Events.java | 6 +- 48 files changed, 723 insertions(+), 1103 deletions(-) create mode 100644 src/org/traccar/database/ExtendedObjectManager.java create mode 100644 src/org/traccar/database/SimpleObjectManager.java create mode 100644 src/org/traccar/model/BaseDevicePermission.java create mode 100644 src/org/traccar/model/BaseGroupPermission.java create mode 100644 src/org/traccar/model/BasePermission.java create mode 100644 src/org/traccar/model/BaseUserPermission.java create mode 100644 src/org/traccar/model/Identifiable.java (limited to 'src/org') diff --git a/src/org/traccar/api/resource/AttributePermissionResource.java b/src/org/traccar/api/resource/AttributePermissionResource.java index 1924bcdf1..37e50e371 100644 --- a/src/org/traccar/api/resource/AttributePermissionResource.java +++ b/src/org/traccar/api/resource/AttributePermissionResource.java @@ -39,9 +39,9 @@ public class AttributePermissionResource extends BaseResource { public Response add(AttributePermission entity) throws SQLException { Context.getPermissionsManager().checkReadonly(getUserId()); Context.getPermissionsManager().checkUser(getUserId(), entity.getUserId()); - Context.getPermissionsManager().checkAttribute(getUserId(), entity.getAttributeId()); + Context.getPermissionsManager().checkPermission("attribute", getUserId(), entity.getAttributeId()); Context.getDataManager().linkAttribute(entity.getUserId(), entity.getAttributeId()); - Context.getAttributesManager().refreshUserAttributes(); + Context.getAttributesManager().refreshUserItems(); return Response.ok(entity).build(); } @@ -49,9 +49,9 @@ public class AttributePermissionResource extends BaseResource { public Response remove(AttributePermission entity) throws SQLException { Context.getPermissionsManager().checkReadonly(getUserId()); Context.getPermissionsManager().checkUser(getUserId(), entity.getUserId()); - Context.getPermissionsManager().checkAttribute(getUserId(), entity.getAttributeId()); + Context.getPermissionsManager().checkPermission("attribute", getUserId(), entity.getAttributeId()); Context.getDataManager().unlinkAttribute(entity.getUserId(), entity.getAttributeId()); - Context.getAttributesManager().refreshUserAttributes(); + Context.getAttributesManager().refreshUserItems(); return Response.noContent().build(); } diff --git a/src/org/traccar/api/resource/AttributeResource.java b/src/org/traccar/api/resource/AttributeResource.java index 4d326779b..c67d9cafe 100644 --- a/src/org/traccar/api/resource/AttributeResource.java +++ b/src/org/traccar/api/resource/AttributeResource.java @@ -52,42 +52,42 @@ public class AttributeResource extends BaseResource { AttributesManager attributesManager = Context.getAttributesManager(); if (refresh) { - attributesManager.refreshAttributes(); + attributesManager.refreshItems(); } Set result = new HashSet<>(); if (all) { if (Context.getPermissionsManager().isAdmin(getUserId())) { - result.addAll(attributesManager.getAllAttributes()); + result.addAll(attributesManager.getAllItems()); } else { Context.getPermissionsManager().checkManager(getUserId()); - result.addAll(attributesManager.getManagedAttributes(getUserId())); + result.addAll(attributesManager.getManagedItems(getUserId())); } } else { if (userId == 0) { userId = getUserId(); } Context.getPermissionsManager().checkUser(getUserId(), userId); - result.addAll(attributesManager.getUserAttributes(userId)); + result.addAll(attributesManager.getUserItems(userId)); } if (groupId != 0) { Context.getPermissionsManager().checkGroup(getUserId(), groupId); - result.retainAll(attributesManager.getGroupAttributes(groupId)); + result.retainAll(attributesManager.getGroupItems(groupId)); } if (deviceId != 0) { Context.getPermissionsManager().checkDevice(getUserId(), deviceId); - result.retainAll(attributesManager.getDeviceAttributes(deviceId)); + result.retainAll(attributesManager.getDeviceItems(deviceId)); } - return attributesManager.getAttributes(result); + return attributesManager.getItems(Attribute.class, result); } private Response add(Attribute entity) throws SQLException { - Context.getAttributesManager().addAttribute(entity); + Context.getAttributesManager().addItem(entity); Context.getDataManager().linkAttribute(getUserId(), entity.getId()); - Context.getAttributesManager().refreshUserAttributes(); + Context.getAttributesManager().refreshUserItems(); return Response.ok(entity).build(); } @@ -127,8 +127,8 @@ public class AttributeResource extends BaseResource { @PUT public Response update(Attribute entity) throws SQLException { Context.getPermissionsManager().checkReadonly(getUserId()); - Context.getPermissionsManager().checkAttribute(getUserId(), entity.getId()); - Context.getAttributesManager().updateAttribute(entity); + Context.getPermissionsManager().checkPermission("attribute", getUserId(), entity.getId()); + Context.getAttributesManager().updateItem(entity); return Response.ok(entity).build(); } @@ -136,8 +136,8 @@ public class AttributeResource extends BaseResource { @DELETE public Response remove(@PathParam("id") long id) throws SQLException { Context.getPermissionsManager().checkReadonly(getUserId()); - Context.getPermissionsManager().checkAttribute(getUserId(), id); - Context.getAttributesManager().removeAttribute(id); + Context.getPermissionsManager().checkPermission("attribute", getUserId(), id); + Context.getAttributesManager().removeItem(id); return Response.noContent().build(); } diff --git a/src/org/traccar/api/resource/CalendarPermissionResource.java b/src/org/traccar/api/resource/CalendarPermissionResource.java index a49254b6b..3936bcaf3 100644 --- a/src/org/traccar/api/resource/CalendarPermissionResource.java +++ b/src/org/traccar/api/resource/CalendarPermissionResource.java @@ -39,9 +39,9 @@ public class CalendarPermissionResource extends BaseResource { public Response add(CalendarPermission entity) throws SQLException { Context.getPermissionsManager().checkReadonly(getUserId()); Context.getPermissionsManager().checkUser(getUserId(), entity.getUserId()); - Context.getPermissionsManager().checkCalendar(getUserId(), entity.getCalendarId()); + Context.getPermissionsManager().checkPermission("calendar", getUserId(), entity.getCalendarId()); Context.getDataManager().linkCalendar(entity.getUserId(), entity.getCalendarId()); - Context.getCalendarManager().refreshUserCalendars(); + Context.getCalendarManager().refreshUserItems(); return Response.ok(entity).build(); } @@ -49,9 +49,9 @@ public class CalendarPermissionResource extends BaseResource { public Response remove(CalendarPermission entity) throws SQLException { Context.getPermissionsManager().checkReadonly(getUserId()); Context.getPermissionsManager().checkUser(getUserId(), entity.getUserId()); - Context.getPermissionsManager().checkCalendar(getUserId(), entity.getCalendarId()); + Context.getPermissionsManager().checkPermission("calendar", getUserId(), entity.getCalendarId()); Context.getDataManager().unlinkCalendar(entity.getUserId(), entity.getCalendarId()); - Context.getCalendarManager().refreshUserCalendars(); + Context.getCalendarManager().refreshUserItems(); return Response.noContent().build(); } } diff --git a/src/org/traccar/api/resource/CalendarResource.java b/src/org/traccar/api/resource/CalendarResource.java index 641d3b4b5..be675fc79 100644 --- a/src/org/traccar/api/resource/CalendarResource.java +++ b/src/org/traccar/api/resource/CalendarResource.java @@ -33,6 +33,7 @@ import javax.ws.rs.core.Response; import org.traccar.Context; import org.traccar.api.BaseResource; +import org.traccar.database.CalendarManager; import org.traccar.model.Calendar; @Path("calendars") @@ -44,28 +45,29 @@ public class CalendarResource extends BaseResource { public Collection get( @QueryParam("all") boolean all, @QueryParam("userId") long userId) throws SQLException { + CalendarManager calendarManager = Context.getCalendarManager(); if (all) { if (Context.getPermissionsManager().isAdmin(getUserId())) { - return Context.getCalendarManager().getAllCalendars(); + return calendarManager.getItems(Calendar.class, calendarManager.getAllItems()); } else { Context.getPermissionsManager().checkManager(getUserId()); - return Context.getCalendarManager().getManagedCalendars(getUserId()); + return calendarManager.getItems(Calendar.class, calendarManager.getManagedItems(getUserId())); } } else { if (userId == 0) { userId = getUserId(); } Context.getPermissionsManager().checkUser(getUserId(), userId); - return Context.getCalendarManager().getUserCalendars(userId); + return calendarManager.getItems(Calendar.class, calendarManager.getUserItems(userId)); } } @POST public Response add(Calendar entity) throws SQLException { Context.getPermissionsManager().checkReadonly(getUserId()); - Context.getCalendarManager().addCalendar(entity); + Context.getCalendarManager().addItem(entity); Context.getDataManager().linkCalendar(getUserId(), entity.getId()); - Context.getCalendarManager().refreshUserCalendars(); + Context.getCalendarManager().refreshUserItems(); return Response.ok(entity).build(); } @@ -73,8 +75,8 @@ public class CalendarResource extends BaseResource { @PUT public Response update(Calendar entity) throws SQLException { Context.getPermissionsManager().checkReadonly(getUserId()); - Context.getPermissionsManager().checkCalendar(getUserId(), entity.getId()); - Context.getCalendarManager().updateCalendar(entity); + Context.getPermissionsManager().checkPermission("calendar", getUserId(), entity.getId()); + Context.getCalendarManager().updateItem(entity); return Response.ok(entity).build(); } @@ -82,8 +84,8 @@ public class CalendarResource extends BaseResource { @DELETE public Response remove(@PathParam("id") long id) throws SQLException { Context.getPermissionsManager().checkReadonly(getUserId()); - Context.getPermissionsManager().checkCalendar(getUserId(), id); - Context.getCalendarManager().removeCalendar(id); + Context.getPermissionsManager().checkPermission("calendar", getUserId(), id); + Context.getCalendarManager().removeItem(id); return Response.noContent().build(); } } diff --git a/src/org/traccar/api/resource/DeviceAttributeResource.java b/src/org/traccar/api/resource/DeviceAttributeResource.java index 82d17bcc6..bb833d4db 100644 --- a/src/org/traccar/api/resource/DeviceAttributeResource.java +++ b/src/org/traccar/api/resource/DeviceAttributeResource.java @@ -39,7 +39,7 @@ public class DeviceAttributeResource extends BaseResource { public Response add(DeviceAttribute entity) throws SQLException { Context.getPermissionsManager().checkReadonly(getUserId()); Context.getPermissionsManager().checkDevice(getUserId(), entity.getDeviceId()); - Context.getPermissionsManager().checkAttribute(getUserId(), entity.getAttributeId()); + Context.getPermissionsManager().checkPermission("attribute", getUserId(), entity.getAttributeId()); Context.getDataManager().linkDeviceAttribute(entity.getDeviceId(), entity.getAttributeId()); Context.getAttributesManager().refresh(); return Response.ok(entity).build(); @@ -49,7 +49,7 @@ public class DeviceAttributeResource extends BaseResource { public Response remove(DeviceAttribute entity) throws SQLException { Context.getPermissionsManager().checkReadonly(getUserId()); Context.getPermissionsManager().checkDevice(getUserId(), entity.getDeviceId()); - Context.getPermissionsManager().checkAttribute(getUserId(), entity.getAttributeId()); + Context.getPermissionsManager().checkPermission("attribute", getUserId(), entity.getAttributeId()); Context.getDataManager().unlinkDeviceAttribute(entity.getDeviceId(), entity.getAttributeId()); Context.getAttributesManager().refresh(); return Response.noContent().build(); diff --git a/src/org/traccar/api/resource/DeviceDriverResource.java b/src/org/traccar/api/resource/DeviceDriverResource.java index 341c50909..286f2223f 100644 --- a/src/org/traccar/api/resource/DeviceDriverResource.java +++ b/src/org/traccar/api/resource/DeviceDriverResource.java @@ -39,7 +39,7 @@ public class DeviceDriverResource extends BaseResource { public Response add(DeviceDriver entity) throws SQLException { Context.getPermissionsManager().checkReadonly(getUserId()); Context.getPermissionsManager().checkDevice(getUserId(), entity.getDeviceId()); - Context.getPermissionsManager().checkDriver(getUserId(), entity.getDriverId()); + Context.getPermissionsManager().checkPermission("driver", getUserId(), entity.getDriverId()); Context.getDataManager().linkDeviceDriver(entity.getDeviceId(), entity.getDriverId()); Context.getDriversManager().refresh(); return Response.ok(entity).build(); @@ -49,7 +49,7 @@ public class DeviceDriverResource extends BaseResource { public Response remove(DeviceDriver entity) throws SQLException { Context.getPermissionsManager().checkReadonly(getUserId()); Context.getPermissionsManager().checkDevice(getUserId(), entity.getDeviceId()); - Context.getPermissionsManager().checkDriver(getUserId(), entity.getDriverId()); + Context.getPermissionsManager().checkPermission("driver", getUserId(), entity.getDriverId()); Context.getDataManager().unlinkDeviceDriver(entity.getDeviceId(), entity.getDriverId()); Context.getDriversManager().refresh(); return Response.noContent().build(); diff --git a/src/org/traccar/api/resource/DeviceGeofenceResource.java b/src/org/traccar/api/resource/DeviceGeofenceResource.java index 6254fe3cf..8034e2168 100644 --- a/src/org/traccar/api/resource/DeviceGeofenceResource.java +++ b/src/org/traccar/api/resource/DeviceGeofenceResource.java @@ -38,7 +38,7 @@ public class DeviceGeofenceResource extends BaseResource { public Response add(DeviceGeofence entity) throws SQLException { Context.getPermissionsManager().checkReadonly(getUserId()); Context.getPermissionsManager().checkDevice(getUserId(), entity.getDeviceId()); - Context.getPermissionsManager().checkGeofence(getUserId(), entity.getGeofenceId()); + Context.getPermissionsManager().checkPermission("geofence", getUserId(), entity.getGeofenceId()); Context.getDataManager().linkDeviceGeofence(entity.getDeviceId(), entity.getGeofenceId()); Context.getGeofenceManager().refresh(); return Response.ok(entity).build(); @@ -48,7 +48,7 @@ public class DeviceGeofenceResource extends BaseResource { public Response remove(DeviceGeofence entity) throws SQLException { Context.getPermissionsManager().checkReadonly(getUserId()); Context.getPermissionsManager().checkDevice(getUserId(), entity.getDeviceId()); - Context.getPermissionsManager().checkGeofence(getUserId(), entity.getGeofenceId()); + Context.getPermissionsManager().checkPermission("geofence", getUserId(), entity.getGeofenceId()); Context.getDataManager().unlinkDeviceGeofence(entity.getDeviceId(), entity.getGeofenceId()); Context.getGeofenceManager().refresh(); return Response.noContent().build(); diff --git a/src/org/traccar/api/resource/DriverPermissionResource.java b/src/org/traccar/api/resource/DriverPermissionResource.java index fd1ca7c6d..4d890cae9 100644 --- a/src/org/traccar/api/resource/DriverPermissionResource.java +++ b/src/org/traccar/api/resource/DriverPermissionResource.java @@ -40,9 +40,9 @@ public class DriverPermissionResource extends BaseResource { public Response add(DriverPermission entity) throws SQLException { Context.getPermissionsManager().checkReadonly(getUserId()); Context.getPermissionsManager().checkUser(getUserId(), entity.getUserId()); - Context.getPermissionsManager().checkDriver(getUserId(), entity.getDriverId()); + Context.getPermissionsManager().checkPermission("driver", getUserId(), entity.getDriverId()); Context.getDataManager().linkDriver(entity.getUserId(), entity.getDriverId()); - Context.getDriversManager().refreshUserDrivers(); + Context.getDriversManager().refreshUserItems(); return Response.ok(entity).build(); } @@ -50,9 +50,9 @@ public class DriverPermissionResource extends BaseResource { public Response remove(DriverPermission entity) throws SQLException { Context.getPermissionsManager().checkReadonly(getUserId()); Context.getPermissionsManager().checkUser(getUserId(), entity.getUserId()); - Context.getPermissionsManager().checkDriver(getUserId(), entity.getDriverId()); + Context.getPermissionsManager().checkPermission("driver", getUserId(), entity.getDriverId()); Context.getDataManager().unlinkDriver(entity.getUserId(), entity.getDriverId()); - Context.getDriversManager().refreshUserDrivers(); + Context.getDriversManager().refreshUserItems(); return Response.noContent().build(); } diff --git a/src/org/traccar/api/resource/DriverResource.java b/src/org/traccar/api/resource/DriverResource.java index 7fe0af473..3d6a12795 100644 --- a/src/org/traccar/api/resource/DriverResource.java +++ b/src/org/traccar/api/resource/DriverResource.java @@ -50,44 +50,44 @@ public class DriverResource extends BaseResource { DriversManager driversManager = Context.getDriversManager(); if (refresh) { - driversManager.refreshDrivers(); + driversManager.refreshItems(); } Set result = new HashSet<>(); if (all) { if (Context.getPermissionsManager().isAdmin(getUserId())) { - result.addAll(driversManager.getAllDrivers()); + result.addAll(driversManager.getAllItems()); } else { Context.getPermissionsManager().checkManager(getUserId()); - result.addAll(driversManager.getManagedDrivers(getUserId())); + result.addAll(driversManager.getManagedItems(getUserId())); } } else { if (userId == 0) { userId = getUserId(); } Context.getPermissionsManager().checkUser(getUserId(), userId); - result.addAll(driversManager.getUserDrivers(userId)); + result.addAll(driversManager.getUserItems(userId)); } if (groupId != 0) { Context.getPermissionsManager().checkGroup(getUserId(), groupId); - result.retainAll(driversManager.getGroupDrivers(groupId)); + result.retainAll(driversManager.getGroupItems(groupId)); } if (deviceId != 0) { Context.getPermissionsManager().checkDevice(getUserId(), deviceId); - result.retainAll(driversManager.getDeviceDrivers(deviceId)); + result.retainAll(driversManager.getDeviceItems(deviceId)); } - return driversManager.getDrivers(result); + return driversManager.getItems(Driver.class, result); } @POST public Response add(Driver entity) throws SQLException { Context.getPermissionsManager().checkReadonly(getUserId()); - Context.getDriversManager().addDriver(entity); + Context.getDriversManager().addItem(entity); Context.getDataManager().linkDriver(getUserId(), entity.getId()); - Context.getDriversManager().refreshUserDrivers(); + Context.getDriversManager().refreshUserItems(); return Response.ok(entity).build(); } @@ -95,8 +95,8 @@ public class DriverResource extends BaseResource { @PUT public Response update(Driver entity) throws SQLException { Context.getPermissionsManager().checkReadonly(getUserId()); - Context.getPermissionsManager().checkDriver(getUserId(), entity.getId()); - Context.getDriversManager().updateDriver(entity); + Context.getPermissionsManager().checkPermission("driver", getUserId(), entity.getId()); + Context.getDriversManager().updateItem(entity); return Response.ok(entity).build(); } @@ -104,8 +104,8 @@ public class DriverResource extends BaseResource { @DELETE public Response remove(@PathParam("id") long id) throws SQLException { Context.getPermissionsManager().checkReadonly(getUserId()); - Context.getPermissionsManager().checkDriver(getUserId(), id); - Context.getDriversManager().removeDriver(id); + Context.getPermissionsManager().checkPermission("driver", getUserId(), id); + Context.getDriversManager().removeItem(id); return Response.noContent().build(); } diff --git a/src/org/traccar/api/resource/EventResource.java b/src/org/traccar/api/resource/EventResource.java index 0ef5456af..9b43bf4b0 100644 --- a/src/org/traccar/api/resource/EventResource.java +++ b/src/org/traccar/api/resource/EventResource.java @@ -25,7 +25,7 @@ public class EventResource extends BaseResource { Event event = Context.getDataManager().getEvent(id); Context.getPermissionsManager().checkDevice(getUserId(), event.getDeviceId()); if (event.getGeofenceId() != 0) { - Context.getPermissionsManager().checkGeofence(getUserId(), event.getGeofenceId()); + Context.getPermissionsManager().checkPermission("geofence", getUserId(), event.getGeofenceId()); } return event; } diff --git a/src/org/traccar/api/resource/GeofencePermissionResource.java b/src/org/traccar/api/resource/GeofencePermissionResource.java index 8faa63d85..a569e56fd 100644 --- a/src/org/traccar/api/resource/GeofencePermissionResource.java +++ b/src/org/traccar/api/resource/GeofencePermissionResource.java @@ -37,9 +37,9 @@ public class GeofencePermissionResource extends BaseResource { public Response add(GeofencePermission entity) throws SQLException { Context.getPermissionsManager().checkReadonly(getUserId()); Context.getPermissionsManager().checkUser(getUserId(), entity.getUserId()); - Context.getPermissionsManager().checkGeofence(getUserId(), entity.getGeofenceId()); + Context.getPermissionsManager().checkPermission("geofence", getUserId(), entity.getGeofenceId()); Context.getDataManager().linkGeofence(entity.getUserId(), entity.getGeofenceId()); - Context.getGeofenceManager().refreshUserGeofences(); + Context.getGeofenceManager().refreshUserItems(); return Response.ok(entity).build(); } @@ -47,9 +47,9 @@ public class GeofencePermissionResource extends BaseResource { public Response remove(GeofencePermission entity) throws SQLException { Context.getPermissionsManager().checkReadonly(getUserId()); Context.getPermissionsManager().checkUser(getUserId(), entity.getUserId()); - Context.getPermissionsManager().checkGeofence(getUserId(), entity.getGeofenceId()); + Context.getPermissionsManager().checkPermission("geofence", getUserId(), entity.getGeofenceId()); Context.getDataManager().unlinkGeofence(entity.getUserId(), entity.getGeofenceId()); - Context.getGeofenceManager().refreshUserGeofences(); + Context.getGeofenceManager().refreshUserItems(); return Response.noContent().build(); } diff --git a/src/org/traccar/api/resource/GeofenceResource.java b/src/org/traccar/api/resource/GeofenceResource.java index d5acf106a..4a52822cc 100644 --- a/src/org/traccar/api/resource/GeofenceResource.java +++ b/src/org/traccar/api/resource/GeofenceResource.java @@ -49,44 +49,44 @@ public class GeofenceResource extends BaseResource { GeofenceManager geofenceManager = Context.getGeofenceManager(); if (refresh) { - geofenceManager.refreshGeofences(); + geofenceManager.refreshItems(); } Set result = new HashSet<>(); if (all) { if (Context.getPermissionsManager().isAdmin(getUserId())) { - result.addAll(geofenceManager.getAllGeofencesIds()); + result.addAll(geofenceManager.getAllItems()); } else { Context.getPermissionsManager().checkManager(getUserId()); - result.addAll(geofenceManager.getManagedGeofencesIds(getUserId())); + result.addAll(geofenceManager.getManagedItems(getUserId())); } } else { if (userId == 0) { userId = getUserId(); } Context.getPermissionsManager().checkUser(getUserId(), userId); - result.addAll(geofenceManager.getUserGeofencesIds(userId)); + result.addAll(geofenceManager.getUserItems(userId)); } if (groupId != 0) { Context.getPermissionsManager().checkGroup(getUserId(), groupId); - result.retainAll(geofenceManager.getGroupGeofencesIds(groupId)); + result.retainAll(geofenceManager.getGroupItems(groupId)); } if (deviceId != 0) { Context.getPermissionsManager().checkDevice(getUserId(), deviceId); - result.retainAll(geofenceManager.getDeviceGeofencesIds(deviceId)); + result.retainAll(geofenceManager.getDeviceItems(deviceId)); } - return geofenceManager.getGeofences(result); + return geofenceManager.getItems(Geofence.class, result); } @POST public Response add(Geofence entity) throws SQLException { Context.getPermissionsManager().checkReadonly(getUserId()); - Context.getDataManager().addGeofence(entity); + Context.getGeofenceManager().addItem(entity); Context.getDataManager().linkGeofence(getUserId(), entity.getId()); - Context.getGeofenceManager().refreshGeofences(); + Context.getGeofenceManager().refreshUserItems(); return Response.ok(entity).build(); } @@ -94,8 +94,8 @@ public class GeofenceResource extends BaseResource { @PUT public Response update(Geofence entity) throws SQLException { Context.getPermissionsManager().checkReadonly(getUserId()); - Context.getPermissionsManager().checkGeofence(getUserId(), entity.getId()); - Context.getGeofenceManager().updateGeofence(entity); + Context.getPermissionsManager().checkPermission("geofence", getUserId(), entity.getId()); + Context.getGeofenceManager().updateItem(entity); return Response.ok(entity).build(); } @@ -103,9 +103,8 @@ public class GeofenceResource extends BaseResource { @DELETE public Response remove(@PathParam("id") long id) throws SQLException { Context.getPermissionsManager().checkReadonly(getUserId()); - Context.getPermissionsManager().checkGeofence(getUserId(), id); - Context.getDataManager().removeGeofence(id); - Context.getGeofenceManager().refreshGeofences(); + Context.getPermissionsManager().checkPermission("geofence", getUserId(), id); + Context.getGeofenceManager().removeItem(id); return Response.noContent().build(); } diff --git a/src/org/traccar/api/resource/GroupAttributeResource.java b/src/org/traccar/api/resource/GroupAttributeResource.java index d8b8b58c9..5df63194e 100644 --- a/src/org/traccar/api/resource/GroupAttributeResource.java +++ b/src/org/traccar/api/resource/GroupAttributeResource.java @@ -39,7 +39,7 @@ public class GroupAttributeResource extends BaseResource { public Response add(GroupAttribute entity) throws SQLException { Context.getPermissionsManager().checkReadonly(getUserId()); Context.getPermissionsManager().checkGroup(getUserId(), entity.getGroupId()); - Context.getPermissionsManager().checkAttribute(getUserId(), entity.getAttributeId()); + Context.getPermissionsManager().checkPermission("attribute", getUserId(), entity.getAttributeId()); Context.getDataManager().linkGroupAttribute(entity.getGroupId(), entity.getAttributeId()); Context.getAttributesManager().refresh(); return Response.ok(entity).build(); @@ -49,7 +49,7 @@ public class GroupAttributeResource extends BaseResource { public Response remove(GroupAttribute entity) throws SQLException { Context.getPermissionsManager().checkReadonly(getUserId()); Context.getPermissionsManager().checkGroup(getUserId(), entity.getGroupId()); - Context.getPermissionsManager().checkAttribute(getUserId(), entity.getAttributeId()); + Context.getPermissionsManager().checkPermission("attribute", getUserId(), entity.getAttributeId()); Context.getDataManager().unlinkGroupAttribute(entity.getGroupId(), entity.getAttributeId()); Context.getAttributesManager().refresh(); return Response.noContent().build(); diff --git a/src/org/traccar/api/resource/GroupDriverResource.java b/src/org/traccar/api/resource/GroupDriverResource.java index 76fc2892c..71c1aca29 100644 --- a/src/org/traccar/api/resource/GroupDriverResource.java +++ b/src/org/traccar/api/resource/GroupDriverResource.java @@ -39,7 +39,7 @@ public class GroupDriverResource extends BaseResource { public Response add(GroupDriver entity) throws SQLException { Context.getPermissionsManager().checkReadonly(getUserId()); Context.getPermissionsManager().checkGroup(getUserId(), entity.getGroupId()); - Context.getPermissionsManager().checkDriver(getUserId(), entity.getDriverId()); + Context.getPermissionsManager().checkPermission("driver", getUserId(), entity.getDriverId()); Context.getDataManager().linkGroupDriver(entity.getGroupId(), entity.getDriverId()); Context.getDriversManager().refresh(); return Response.ok(entity).build(); @@ -49,7 +49,7 @@ public class GroupDriverResource extends BaseResource { public Response remove(GroupDriver entity) throws SQLException { Context.getPermissionsManager().checkReadonly(getUserId()); Context.getPermissionsManager().checkGroup(getUserId(), entity.getGroupId()); - Context.getPermissionsManager().checkDriver(getUserId(), entity.getDriverId()); + Context.getPermissionsManager().checkPermission("driver", getUserId(), entity.getDriverId()); Context.getDataManager().unlinkGroupDriver(entity.getGroupId(), entity.getDriverId()); Context.getDriversManager().refresh(); return Response.noContent().build(); diff --git a/src/org/traccar/api/resource/GroupGeofenceResource.java b/src/org/traccar/api/resource/GroupGeofenceResource.java index 81fd4e45f..14000c4b5 100644 --- a/src/org/traccar/api/resource/GroupGeofenceResource.java +++ b/src/org/traccar/api/resource/GroupGeofenceResource.java @@ -37,7 +37,7 @@ public class GroupGeofenceResource extends BaseResource { public Response add(GroupGeofence entity) throws SQLException { Context.getPermissionsManager().checkReadonly(getUserId()); Context.getPermissionsManager().checkGroup(getUserId(), entity.getGroupId()); - Context.getPermissionsManager().checkGeofence(getUserId(), entity.getGeofenceId()); + Context.getPermissionsManager().checkPermission("geofence", getUserId(), entity.getGeofenceId()); Context.getDataManager().linkGroupGeofence(entity.getGroupId(), entity.getGeofenceId()); Context.getGeofenceManager().refresh(); return Response.ok(entity).build(); @@ -47,7 +47,7 @@ public class GroupGeofenceResource extends BaseResource { public Response remove(GroupGeofence entity) throws SQLException { Context.getPermissionsManager().checkReadonly(getUserId()); Context.getPermissionsManager().checkGroup(getUserId(), entity.getGroupId()); - Context.getPermissionsManager().checkGeofence(getUserId(), entity.getGeofenceId()); + Context.getPermissionsManager().checkPermission("geofence", getUserId(), entity.getGeofenceId()); Context.getDataManager().unlinkGroupGeofence(entity.getGroupId(), entity.getGeofenceId()); Context.getGeofenceManager().refresh(); return Response.noContent().build(); diff --git a/src/org/traccar/api/resource/UserResource.java b/src/org/traccar/api/resource/UserResource.java index 4d8a8b3a4..b9187fdbb 100644 --- a/src/org/traccar/api/resource/UserResource.java +++ b/src/org/traccar/api/resource/UserResource.java @@ -104,7 +104,7 @@ public class UserResource extends BaseResource { Context.getPermissionsManager().checkUser(getUserId(), id); Context.getPermissionsManager().removeUser(id); if (Context.getGeofenceManager() != null) { - Context.getGeofenceManager().refreshUserGeofences(); + Context.getGeofenceManager().refreshUserItems(); } if (Context.getNotificationManager() != null) { Context.getNotificationManager().refresh(); diff --git a/src/org/traccar/database/AttributesManager.java b/src/org/traccar/database/AttributesManager.java index 362d6130f..88bedfba4 100644 --- a/src/org/traccar/database/AttributesManager.java +++ b/src/org/traccar/database/AttributesManager.java @@ -17,183 +17,30 @@ package org.traccar.database; import java.sql.SQLException; -import java.util.Collection; -import java.util.HashSet; -import java.util.LinkedList; -import java.util.Map; -import java.util.Set; -import java.util.concurrent.ConcurrentHashMap; -import org.traccar.Context; -import org.traccar.helper.Log; import org.traccar.model.AttributePermission; import org.traccar.model.Attribute; -import org.traccar.model.Device; import org.traccar.model.DeviceAttribute; import org.traccar.model.GroupAttribute; +import org.traccar.model.Identifiable; -public class AttributesManager { - - private final DataManager dataManager; - - private final Map attributes = new ConcurrentHashMap<>(); - private final Map> deviceAttributes = new ConcurrentHashMap<>(); - private final Map> deviceAttributesWithGroups = new ConcurrentHashMap<>(); - private final Map> groupAttributes = new ConcurrentHashMap<>(); - private final Map> userAttributes = new ConcurrentHashMap<>(); +public class AttributesManager extends ExtendedObjectManager { public AttributesManager(DataManager dataManager) { - this.dataManager = dataManager; - refreshAttributes(); - } - - public Set getUserAttributes(long userId) { - if (!userAttributes.containsKey(userId)) { - userAttributes.put(userId, new HashSet()); - } - return userAttributes.get(userId); - } - - public Set getGroupAttributes(long groupId) { - if (!groupAttributes.containsKey(groupId)) { - groupAttributes.put(groupId, new HashSet()); - } - return groupAttributes.get(groupId); - } - - public Set getDeviceAttributes(long deviceId) { - return getDeviceAttributes(deviceAttributes, deviceId); - } - - public Set getAllDeviceAttributes(long deviceId) { - return getDeviceAttributes(deviceAttributesWithGroups, deviceId); - } - - private Set getDeviceAttributes(Map> deviceAttributes, long deviceId) { - if (!deviceAttributes.containsKey(deviceId)) { - deviceAttributes.put(deviceId, new HashSet()); - } - return deviceAttributes.get(deviceId); - } - - public final void refreshAttributes() { - if (dataManager != null) { - try { - attributes.clear(); - for (Attribute attribute : dataManager.getAttributes()) { - attributes.put(attribute.getId(), attribute); - } - } catch (SQLException error) { - Log.warning(error); - } - } - refreshUserAttributes(); + super(dataManager, Attribute.class, AttributePermission.class, DeviceAttribute.class, GroupAttribute.class); + refreshItems(); refresh(); } - public final void refreshUserAttributes() { - if (dataManager != null) { - try { - userAttributes.clear(); - for (AttributePermission attributePermission : dataManager.getAttributePermissions()) { - getUserAttributes(attributePermission.getUserId()).add(attributePermission.getAttributeId()); - } - } catch (SQLException error) { - Log.warning(error); - } - } - } - - public final void refresh() { - if (dataManager != null) { - try { - - Collection databaseGroupAttributes = dataManager.getGroupAttributes(); - - groupAttributes.clear(); - for (GroupAttribute groupAttribute : databaseGroupAttributes) { - getGroupAttributes(groupAttribute.getGroupId()).add(groupAttribute.getAttributeId()); - } - - Collection databaseDeviceAttributes = dataManager.getDeviceAttributes(); - Collection allDevices = Context.getDeviceManager().getAllDevices(); - - deviceAttributes.clear(); - deviceAttributesWithGroups.clear(); - - for (DeviceAttribute deviceAttribute : databaseDeviceAttributes) { - getDeviceAttributes(deviceAttribute.getDeviceId()) - .add(deviceAttribute.getAttributeId()); - getAllDeviceAttributes(deviceAttribute.getDeviceId()) - .add(deviceAttribute.getAttributeId()); - } - - for (Device device : allDevices) { - long groupId = device.getGroupId(); - while (groupId != 0) { - getAllDeviceAttributes(device.getId()).addAll(getGroupAttributes(groupId)); - if (Context.getDeviceManager().getGroupById(groupId) != null) { - groupId = Context.getDeviceManager().getGroupById(groupId).getGroupId(); - } else { - groupId = 0; - } - } - } - - } catch (SQLException error) { - Log.warning(error); - } - } - } - - public void addAttribute(Attribute attribute) throws SQLException { - dataManager.addAttribute(attribute); - attributes.put(attribute.getId(), attribute); - } - - public void updateAttribute(Attribute attribute) throws SQLException { - dataManager.updateAttribute(attribute); - Attribute cachedAttribute = attributes.get(attribute.getId()); + @Override + public void updateItem(Identifiable item) throws SQLException { + Attribute attribute = (Attribute) item; + getDataManager().updateObject(attribute); + Attribute cachedAttribute = (Attribute) getById(item.getId()); cachedAttribute.setDescription(attribute.getDescription()); cachedAttribute.setAttribute(attribute.getAttribute()); cachedAttribute.setExpression(attribute.getExpression()); cachedAttribute.setType(attribute.getType()); } - public void removeAttribute(long computedAttributeId) throws SQLException { - dataManager.removeAttribute(computedAttributeId); - attributes.remove(computedAttributeId); - refreshUserAttributes(); - refresh(); - } - - public boolean checkAttribute(long userId, long attributeId) { - return getUserAttributes(userId).contains(attributeId); - } - - public Attribute getAttribute(long id) { - return attributes.get(id); - } - - public final Collection getAttributes(Set attributeIds) { - Collection result = new LinkedList<>(); - for (long attributeId : attributeIds) { - result.add(getAttribute(attributeId)); - } - return result; - } - - public final Set getAllAttributes() { - return attributes.keySet(); - } - - public final Set getManagedAttributes(long userId) { - Set attributes = new HashSet<>(); - attributes.addAll(getUserAttributes(userId)); - for (long managedUserId : Context.getPermissionsManager().getUserPermissions(userId)) { - attributes.addAll(getUserAttributes(managedUserId)); - } - return attributes; - } - } diff --git a/src/org/traccar/database/CalendarManager.java b/src/org/traccar/database/CalendarManager.java index 31d484327..e00b4faa6 100644 --- a/src/org/traccar/database/CalendarManager.java +++ b/src/org/traccar/database/CalendarManager.java @@ -16,107 +16,14 @@ */ package org.traccar.database; -import java.sql.SQLException; -import java.util.ArrayList; -import java.util.Collection; -import java.util.HashSet; -import java.util.Map; -import java.util.Set; -import java.util.concurrent.ConcurrentHashMap; - -import org.traccar.Context; -import org.traccar.helper.Log; import org.traccar.model.Calendar; import org.traccar.model.CalendarPermission; -public class CalendarManager { - - private final DataManager dataManager; - - private final Map calendars = new ConcurrentHashMap<>(); - private final Map> userCalendars = new ConcurrentHashMap<>(); +public class CalendarManager extends SimpleObjectManager { public CalendarManager(DataManager dataManager) { - this.dataManager = dataManager; - refreshCalendars(); - } - - public final void refreshCalendars() { - if (dataManager != null) { - try { - calendars.clear(); - for (Calendar calendar : dataManager.getCalendars()) { - calendars.put(calendar.getId(), calendar); - } - } catch (SQLException error) { - Log.warning(error); - } - } - refreshUserCalendars(); - } - - private Set getUserCalendarIds(long userId) { - if (!userCalendars.containsKey(userId)) { - userCalendars.put(userId, new HashSet()); - } - return userCalendars.get(userId); - } - - public Collection getUserCalendars(long userId) { - ArrayList result = new ArrayList<>(); - for (long calendarId : getUserCalendarIds(userId)) { - result.add(calendars.get(calendarId)); - } - return result; - } - - public Collection getManagedCalendars(long userId) { - ArrayList result = new ArrayList<>(); - result.addAll(getUserCalendars(userId)); - for (long managedUserId : Context.getPermissionsManager().getUserPermissions(userId)) { - result.addAll(getUserCalendars(managedUserId)); - } - return result; - } - - public final void refreshUserCalendars() { - if (dataManager != null) { - try { - userCalendars.clear(); - for (CalendarPermission calendarsPermission : dataManager.getCalendarPermissions()) { - getUserCalendarIds(calendarsPermission.getUserId()).add(calendarsPermission.getCalendarId()); - } - } catch (SQLException error) { - Log.warning(error); - } - } - } - - public Calendar getCalendar(long calendarId) { - return calendars.get(calendarId); - } - - public final void addCalendar(Calendar calendar) throws SQLException { - dataManager.addCalendar(calendar); - calendars.put(calendar.getId(), calendar); - } - - public final void updateCalendar(Calendar calendar) throws SQLException { - dataManager.updateCalendar(calendar); - calendars.put(calendar.getId(), calendar); - } - - public final void removeCalendar(long calendarId) throws SQLException { - dataManager.removeCalendar(calendarId); - calendars.remove(calendarId); - refreshUserCalendars(); + super(dataManager, Calendar.class, CalendarPermission.class); + refreshItems(); } - public Collection getAllCalendars() { - return calendars.values(); - } - - public boolean checkCalendar(long userId, long calendarId) { - return getUserCalendarIds(userId).contains(calendarId); - } } diff --git a/src/org/traccar/database/DataManager.java b/src/org/traccar/database/DataManager.java index 1c2a66bcf..4bf4b1b9a 100644 --- a/src/org/traccar/database/DataManager.java +++ b/src/org/traccar/database/DataManager.java @@ -38,31 +38,18 @@ import liquibase.resource.ResourceAccessor; import org.traccar.Config; import org.traccar.helper.Log; import org.traccar.model.AttributeAlias; -import org.traccar.model.AttributePermission; -import org.traccar.model.Calendar; -import org.traccar.model.CalendarPermission; -import org.traccar.model.Attribute; import org.traccar.model.Device; -import org.traccar.model.DeviceAttribute; -import org.traccar.model.DeviceDriver; import org.traccar.model.DevicePermission; -import org.traccar.model.Driver; -import org.traccar.model.DriverPermission; import org.traccar.model.Event; -import org.traccar.model.Geofence; import org.traccar.model.Group; -import org.traccar.model.GroupAttribute; -import org.traccar.model.GroupDriver; -import org.traccar.model.GroupGeofence; import org.traccar.model.GroupPermission; +import org.traccar.model.Identifiable; import org.traccar.model.Notification; import org.traccar.model.Position; import org.traccar.model.Server; import org.traccar.model.Statistics; import org.traccar.model.User; import org.traccar.model.UserPermission; -import org.traccar.model.DeviceGeofence; -import org.traccar.model.GeofencePermission; import com.zaxxer.hikari.HikariConfig; import com.zaxxer.hikari.HikariDataSource; @@ -362,34 +349,6 @@ public class DataManager { .executeQuery(Event.class); } - public Collection getGeofences() throws SQLException { - return QueryBuilder.create(dataSource, getQuery("database.selectGeofencesAll")) - .executeQuery(Geofence.class); - } - - public void addGeofence(Geofence geofence) throws SQLException { - geofence.setId(QueryBuilder.create(dataSource, getQuery("database.insertGeofence"), true) - .setObject(geofence) - .executeUpdate()); - } - - public void updateGeofence(Geofence geofence) throws SQLException { - QueryBuilder.create(dataSource, getQuery("database.updateGeofence")) - .setObject(geofence) - .executeUpdate(); - } - - public void removeGeofence(long geofenceId) throws SQLException { - QueryBuilder.create(dataSource, getQuery("database.deleteGeofence")) - .setLong("id", geofenceId) - .executeUpdate(); - } - - public Collection getGeofencePermissions() throws SQLException { - return QueryBuilder.create(dataSource, getQuery("database.selectGeofencePermissions")) - .executeQuery(GeofencePermission.class); - } - public void linkGeofence(long userId, long geofenceId) throws SQLException { QueryBuilder.create(dataSource, getQuery("database.linkGeofence")) .setLong("userId", userId) @@ -404,11 +363,6 @@ public class DataManager { .executeUpdate(); } - public Collection getGroupGeofences() throws SQLException { - return QueryBuilder.create(dataSource, getQuery("database.selectGroupGeofences")) - .executeQuery(GroupGeofence.class); - } - public void linkGroupGeofence(long groupId, long geofenceId) throws SQLException { QueryBuilder.create(dataSource, getQuery("database.linkGroupGeofence")) .setLong("groupId", groupId) @@ -423,11 +377,6 @@ public class DataManager { .executeUpdate(); } - public Collection getDeviceGeofences() throws SQLException { - return QueryBuilder.create(dataSource, getQuery("database.selectDeviceGeofences")) - .executeQuery(DeviceGeofence.class); - } - public void linkDeviceGeofence(long deviceId, long geofenceId) throws SQLException { QueryBuilder.create(dataSource, getQuery("database.linkDeviceGeofence")) .setLong("deviceId", deviceId) @@ -501,34 +450,6 @@ public class DataManager { .executeUpdate()); } - public Collection getCalendars() throws SQLException { - return QueryBuilder.create(dataSource, getQuery("database.selectCalendarsAll")) - .executeQuery(Calendar.class); - } - - public void addCalendar(Calendar calendar) throws SQLException { - calendar.setId(QueryBuilder.create(dataSource, getQuery("database.insertCalendar"), true) - .setObject(calendar) - .executeUpdate()); - } - - public void updateCalendar(Calendar calendar) throws SQLException { - QueryBuilder.create(dataSource, getQuery("database.updateCalendar")) - .setObject(calendar) - .executeUpdate(); - } - - public void removeCalendar(long calendarId) throws SQLException { - QueryBuilder.create(dataSource, getQuery("database.deleteCalendar")) - .setLong("id", calendarId) - .executeUpdate(); - } - - public Collection getCalendarPermissions() throws SQLException { - return QueryBuilder.create(dataSource, getQuery("database.selectCalendarPermissions")) - .executeQuery(CalendarPermission.class); - } - public void linkCalendar(long userId, long calendarId) throws SQLException { QueryBuilder.create(dataSource, getQuery("database.linkCalendar")) .setLong("userId", userId) @@ -562,34 +483,6 @@ public class DataManager { .executeUpdate(); } - public Collection getAttributes() throws SQLException { - return QueryBuilder.create(dataSource, getQuery("database.selectAttributes")) - .executeQuery(Attribute.class); - } - - public void addAttribute(Attribute attribute) throws SQLException { - attribute.setId(QueryBuilder.create(dataSource, getQuery("database.insertAttribute"), true) - .setObject(attribute) - .executeUpdate()); - } - - public void updateAttribute(Attribute attribute) throws SQLException { - QueryBuilder.create(dataSource, getQuery("database.updateAttribute")) - .setObject(attribute) - .executeUpdate(); - } - - public void removeAttribute(long computedAttributeId) throws SQLException { - QueryBuilder.create(dataSource, getQuery("database.deleteAttribute")) - .setLong("id", computedAttributeId) - .executeUpdate(); - } - - public Collection getAttributePermissions() throws SQLException { - return QueryBuilder.create(dataSource, getQuery("database.selectAttributePermissions")) - .executeQuery(AttributePermission.class); - } - public void linkAttribute(long userId, long attributeId) throws SQLException { QueryBuilder.create(dataSource, getQuery("database.linkAttribute")) .setLong("userId", userId) @@ -604,11 +497,6 @@ public class DataManager { .executeUpdate(); } - public Collection getGroupAttributes() throws SQLException { - return QueryBuilder.create(dataSource, getQuery("database.selectGroupAttributes")) - .executeQuery(GroupAttribute.class); - } - public void linkGroupAttribute(long groupId, long attributeId) throws SQLException { QueryBuilder.create(dataSource, getQuery("database.linkGroupAttribute")) .setLong("groupId", groupId) @@ -623,11 +511,6 @@ public class DataManager { .executeUpdate(); } - public Collection getDeviceAttributes() throws SQLException { - return QueryBuilder.create(dataSource, getQuery("database.selectDeviceAttributes")) - .executeQuery(DeviceAttribute.class); - } - public void linkDeviceAttribute(long deviceId, long attributeId) throws SQLException { QueryBuilder.create(dataSource, getQuery("database.linkDeviceAttribute")) .setLong("deviceId", deviceId) @@ -642,34 +525,6 @@ public class DataManager { .executeUpdate(); } - public Collection getDrivers() throws SQLException { - return QueryBuilder.create(dataSource, getQuery("database.selectDrivers")) - .executeQuery(Driver.class); - } - - public void addDriver(Driver driver) throws SQLException { - driver.setId(QueryBuilder.create(dataSource, getQuery("database.insertDriver"), true) - .setObject(driver) - .executeUpdate()); - } - - public void updateDriver(Driver driver) throws SQLException { - QueryBuilder.create(dataSource, getQuery("database.updateDriver")) - .setObject(driver) - .executeUpdate(); - } - - public void removeDriver(long driverId) throws SQLException { - QueryBuilder.create(dataSource, getQuery("database.deleteDriver")) - .setLong("id", driverId) - .executeUpdate(); - } - - public Collection getDriverPermissions() throws SQLException { - return QueryBuilder.create(dataSource, getQuery("database.selectDriverPermissions")) - .executeQuery(DriverPermission.class); - } - public void linkDriver(long userId, long driverId) throws SQLException { QueryBuilder.create(dataSource, getQuery("database.linkDriver")) .setLong("userId", userId) @@ -684,11 +539,6 @@ public class DataManager { .executeUpdate(); } - public Collection getGroupDrivers() throws SQLException { - return QueryBuilder.create(dataSource, getQuery("database.selectGroupDrivers")) - .executeQuery(GroupDriver.class); - } - public void linkGroupDriver(long groupId, long driverId) throws SQLException { QueryBuilder.create(dataSource, getQuery("database.linkGroupDriver")) .setLong("groupId", groupId) @@ -703,11 +553,6 @@ public class DataManager { .executeUpdate(); } - public Collection getDeviceDrivers() throws SQLException { - return QueryBuilder.create(dataSource, getQuery("database.selectDeviceDrivers")) - .executeQuery(DeviceDriver.class); - } - public void linkDeviceDriver(long deviceId, long driverId) throws SQLException { QueryBuilder.create(dataSource, getQuery("database.linkDeviceDriver")) .setLong("deviceId", deviceId) @@ -722,4 +567,30 @@ public class DataManager { .executeUpdate(); } + public Collection getObjects(Class clazz) throws SQLException { + String query = "database.select" + clazz.getSimpleName() + "s"; + return QueryBuilder.create(dataSource, getQuery(query)).executeQuery(clazz); + } + + public void addObject(Identifiable entity) throws SQLException { + String query = "database.insert" + entity.getClass().getSimpleName(); + entity.setId(QueryBuilder.create(dataSource, getQuery(query), true) + .setObject(entity) + .executeUpdate()); + } + + public void updateObject(Identifiable entity) throws SQLException { + String query = "database.update" + entity.getClass().getSimpleName(); + QueryBuilder.create(dataSource, getQuery(query)) + .setObject(entity) + .executeUpdate(); + } + + public void removeObject(Class clazz, long entityId) throws SQLException { + String query = "database.delete" + clazz.getSimpleName(); + QueryBuilder.create(dataSource, getQuery(query)) + .setLong("id", entityId) + .executeUpdate(); + } + } diff --git a/src/org/traccar/database/DriversManager.java b/src/org/traccar/database/DriversManager.java index e89d59311..391708f64 100644 --- a/src/org/traccar/database/DriversManager.java +++ b/src/org/traccar/database/DriversManager.java @@ -17,147 +17,53 @@ package org.traccar.database; import java.sql.SQLException; -import java.util.Collection; -import java.util.HashSet; -import java.util.LinkedList; import java.util.Map; -import java.util.Set; import java.util.concurrent.ConcurrentHashMap; -import org.traccar.Context; import org.traccar.helper.Log; -import org.traccar.model.Device; import org.traccar.model.DeviceDriver; import org.traccar.model.Driver; import org.traccar.model.DriverPermission; import org.traccar.model.GroupDriver; +import org.traccar.model.Identifiable; -public class DriversManager { +public class DriversManager extends ExtendedObjectManager { - private final DataManager dataManager; - - private final Map drivers = new ConcurrentHashMap<>(); private final Map driversByUniqueId = new ConcurrentHashMap<>(); - private final Map> deviceDrivers = new ConcurrentHashMap<>(); - private final Map> deviceDriversWithGroups = new ConcurrentHashMap<>(); - private final Map> groupDrivers = new ConcurrentHashMap<>(); - private final Map> userDrivers = new ConcurrentHashMap<>(); public DriversManager(DataManager dataManager) { - this.dataManager = dataManager; - refreshDrivers(); - } - - public Set getUserDrivers(long userId) { - if (!userDrivers.containsKey(userId)) { - userDrivers.put(userId, new HashSet()); - } - return userDrivers.get(userId); - } - - public Set getGroupDrivers(long groupId) { - if (!groupDrivers.containsKey(groupId)) { - groupDrivers.put(groupId, new HashSet()); - } - return groupDrivers.get(groupId); - } - - public Set getDeviceDrivers(long deviceId) { - return getDeviceDrivers(deviceDrivers, deviceId); - } - - public Set getAllDeviceDrivers(long deviceId) { - return getDeviceDrivers(deviceDriversWithGroups, deviceId); - } - - private Set getDeviceDrivers(Map> deviceDrivers, long deviceId) { - if (!deviceDrivers.containsKey(deviceId)) { - deviceDrivers.put(deviceId, new HashSet()); - } - return deviceDrivers.get(deviceId); - } - - public final void refreshDrivers() { - if (dataManager != null) { - try { - drivers.clear(); - driversByUniqueId.clear(); - for (Driver driver : dataManager.getDrivers()) { - drivers.put(driver.getId(), driver); - driversByUniqueId.put(driver.getUniqueId(), driver); - } - } catch (SQLException error) { - Log.warning(error); - } - } - refreshUserDrivers(); + super(dataManager, Driver.class, DriverPermission.class, DeviceDriver.class, GroupDriver.class); + refreshItems(); refresh(); } - public final void refreshUserDrivers() { - if (dataManager != null) { + @Override + public void refreshItems() { + if (getDataManager() != null) { try { - userDrivers.clear(); - for (DriverPermission driverPermission : dataManager.getDriverPermissions()) { - getUserDrivers(driverPermission.getUserId()).add(driverPermission.getDriverId()); + clearItems(); + for (Identifiable item : getDataManager().getObjects(getBaseClass())) { + putItem(item.getId(), item); + driversByUniqueId.put(((Driver) item).getUniqueId(), (Driver) item); } } catch (SQLException error) { Log.warning(error); } } + refreshUserItems(); } - public final void refresh() { - if (dataManager != null) { - try { - - Collection databaseGroupDrivers = dataManager.getGroupDrivers(); - - groupDrivers.clear(); - for (GroupDriver groupDriver : databaseGroupDrivers) { - getGroupDrivers(groupDriver.getGroupId()).add(groupDriver.getDriverId()); - } - - Collection databaseDeviceDrivers = dataManager.getDeviceDrivers(); - Collection allDevices = Context.getDeviceManager().getAllDevices(); - - deviceDrivers.clear(); - deviceDriversWithGroups.clear(); - - for (DeviceDriver deviceAttribute : databaseDeviceDrivers) { - getDeviceDrivers(deviceAttribute.getDeviceId()) - .add(deviceAttribute.getDriverId()); - getAllDeviceDrivers(deviceAttribute.getDeviceId()) - .add(deviceAttribute.getDriverId()); - } - - for (Device device : allDevices) { - long groupId = device.getGroupId(); - while (groupId != 0) { - getAllDeviceDrivers(device.getId()).addAll(getGroupDrivers(groupId)); - if (Context.getDeviceManager().getGroupById(groupId) != null) { - groupId = Context.getDeviceManager().getGroupById(groupId).getGroupId(); - } else { - groupId = 0; - } - } - } - - } catch (SQLException error) { - Log.warning(error); - } - } - } - - public void addDriver(Driver driver) throws SQLException { - dataManager.addDriver(driver); - drivers.put(driver.getId(), driver); - driversByUniqueId.put(driver.getUniqueId(), driver); + @Override + public void addItem(Identifiable item) throws SQLException { + super.addItem(item); + driversByUniqueId.put(((Driver) item).getUniqueId(), (Driver) item); } - public void updateDriver(Driver driver) throws SQLException { - dataManager.updateDriver(driver); - Driver cachedDriver = drivers.get(driver.getId()); + @Override + public void updateItem(Identifiable item) throws SQLException { + Driver driver = (Driver) item; + getDataManager().updateObject(driver); + Driver cachedDriver = (Driver) getById(driver.getId()); cachedDriver.setName(driver.getName()); if (!driver.getUniqueId().equals(cachedDriver.getUniqueId())) { driversByUniqueId.remove(cachedDriver.getUniqueId()); @@ -167,47 +73,20 @@ public class DriversManager { cachedDriver.setAttributes(driver.getAttributes()); } - public void removeDriver(long driverId) throws SQLException { - dataManager.removeDriver(driverId); - if (drivers.containsKey(driverId)) { - String driverUniqueId = drivers.get(driverId).getUniqueId(); - drivers.remove(driverId); + @Override + public void removeItem(long driverId) throws SQLException { + Driver cachedDriver = (Driver) getById(driverId); + getDataManager().removeObject(cachedDriver.getClass(), driverId); + if (cachedDriver != null) { + String driverUniqueId = cachedDriver.getUniqueId(); + removeCachedItem(driverId); driversByUniqueId.remove(driverUniqueId); } - refreshUserDrivers(); + refreshUserItems(); refresh(); } - public boolean checkDriver(long userId, long driverId) { - return getUserDrivers(userId).contains(driverId); - } - - public Driver getDriver(long id) { - return drivers.get(id); - } - public Driver getDriverByUniqueId(String uniqueId) { return driversByUniqueId.get(uniqueId); } - - public final Collection getDrivers(Set driverIds) { - Collection result = new LinkedList<>(); - for (long driverId : driverIds) { - result.add(getDriver(driverId)); - } - return result; - } - - public final Set getAllDrivers() { - return drivers.keySet(); - } - - public final Set getManagedDrivers(long userId) { - Set drivers = new HashSet<>(); - drivers.addAll(getUserDrivers(userId)); - for (long managedUserId : Context.getPermissionsManager().getUserPermissions(userId)) { - drivers.addAll(getUserDrivers(managedUserId)); - } - return drivers; - } } diff --git a/src/org/traccar/database/ExtendedObjectManager.java b/src/org/traccar/database/ExtendedObjectManager.java new file mode 100644 index 000000000..5147b9f9f --- /dev/null +++ b/src/org/traccar/database/ExtendedObjectManager.java @@ -0,0 +1,129 @@ +/* + * Copyright 2017 Anton Tananaev (anton@traccar.org) + * Copyright 2017 Andrey Kunitsyn (andrey@traccar.org) + * + * 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.database; + +import java.sql.SQLException; +import java.util.Collection; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; + +import org.traccar.Context; +import org.traccar.helper.Log; +import org.traccar.model.BaseDevicePermission; +import org.traccar.model.BaseGroupPermission; +import org.traccar.model.BaseUserPermission; +import org.traccar.model.Device; +import org.traccar.model.Identifiable; + +public abstract class ExtendedObjectManager extends SimpleObjectManager { + + private final Map> deviceItems = new ConcurrentHashMap<>(); + private final Map> deviceItemsWithGroups = new ConcurrentHashMap<>(); + private final Map> groupItems = new ConcurrentHashMap<>(); + + private Class devicePermissionClass; + private Class groupPermissionClass; + + protected ExtendedObjectManager(DataManager dataManager, + Class baseClass, + Class permissionClass, + Class devicePermissionClass, + Class groupPermissionClass) { + super(dataManager, baseClass, permissionClass); + this.devicePermissionClass = devicePermissionClass; + this.groupPermissionClass = groupPermissionClass; + } + + public final Set getGroupItems(long groupId) { + if (!groupItems.containsKey(groupId)) { + groupItems.put(groupId, new HashSet()); + } + return groupItems.get(groupId); + } + + protected final void clearGroupItems() { + groupItems.clear(); + } + + public final Set getDeviceItems(long deviceId) { + if (!deviceItems.containsKey(deviceId)) { + deviceItems.put(deviceId, new HashSet()); + } + return deviceItems.get(deviceId); + } + + protected final void clearDeviceItems() { + deviceItems.clear(); + } + + public Set getAllDeviceItems(long deviceId) { + if (!deviceItemsWithGroups.containsKey(deviceId)) { + deviceItemsWithGroups.put(deviceId, new HashSet()); + } + return deviceItemsWithGroups.get(deviceId); + } + + @Override + public void removeItem(long itemId) throws SQLException { + super.removeItem(itemId); + refresh(); + } + + public void refresh() { + if (getDataManager() != null) { + try { + + Collection databaseGroupPermissions = + getDataManager().getObjects(groupPermissionClass); + + clearGroupItems(); + for (BaseGroupPermission groupPermission : databaseGroupPermissions) { + getGroupItems(groupPermission.getGroupId()).add(groupPermission.getSlaveId()); + } + + Collection databaseDevicePermissions = + getDataManager().getObjects(devicePermissionClass); + Collection allDevices = Context.getDeviceManager().getAllDevices(); + + clearDeviceItems(); + deviceItemsWithGroups.clear(); + + for (BaseDevicePermission devicePermission : databaseDevicePermissions) { + getDeviceItems(devicePermission.getDeviceId()).add(devicePermission.getSlaveId()); + getAllDeviceItems(devicePermission.getDeviceId()).add(devicePermission.getSlaveId()); + } + + for (Device device : allDevices) { + long groupId = device.getGroupId(); + while (groupId != 0) { + getAllDeviceItems(device.getId()).addAll(getGroupItems(groupId)); + if (Context.getDeviceManager().getGroupById(groupId) != null) { + groupId = Context.getDeviceManager().getGroupById(groupId).getGroupId(); + } else { + groupId = 0; + } + } + } + + } catch (SQLException error) { + Log.warning(error); + } + } + } +} diff --git a/src/org/traccar/database/GeofenceManager.java b/src/org/traccar/database/GeofenceManager.java index b8e6a5d73..868804d8f 100644 --- a/src/org/traccar/database/GeofenceManager.java +++ b/src/org/traccar/database/GeofenceManager.java @@ -15,20 +15,10 @@ */ package org.traccar.database; -import java.sql.SQLException; import java.util.ArrayList; -import java.util.Collection; -import java.util.HashMap; -import java.util.HashSet; -import java.util.LinkedList; import java.util.List; -import java.util.Map; -import java.util.Set; -import java.util.concurrent.locks.ReadWriteLock; -import java.util.concurrent.locks.ReentrantReadWriteLock; import org.traccar.Context; -import org.traccar.helper.Log; import org.traccar.model.Device; import org.traccar.model.DeviceGeofence; import org.traccar.model.Geofence; @@ -36,269 +26,45 @@ import org.traccar.model.GeofencePermission; import org.traccar.model.GroupGeofence; import org.traccar.model.Position; -public class GeofenceManager { - - private final DataManager dataManager; - - private final Map geofences = new HashMap<>(); - private final Map> userGeofences = new HashMap<>(); - private final Map> groupGeofences = new HashMap<>(); - - private final Map> deviceGeofencesWithGroups = new HashMap<>(); - private final Map> deviceGeofences = new HashMap<>(); - - private final ReadWriteLock deviceGeofencesLock = new ReentrantReadWriteLock(); - private final ReadWriteLock geofencesLock = new ReentrantReadWriteLock(); - private final ReadWriteLock groupGeofencesLock = new ReentrantReadWriteLock(); - private final ReadWriteLock userGeofencesLock = new ReentrantReadWriteLock(); +public class GeofenceManager extends ExtendedObjectManager { public GeofenceManager(DataManager dataManager) { - this.dataManager = dataManager; - refreshGeofences(); - } - - private Set getUserGeofences(long userId) { - if (!userGeofences.containsKey(userId)) { - userGeofences.put(userId, new HashSet()); - } - return userGeofences.get(userId); - } - - public Set getUserGeofencesIds(long userId) { - userGeofencesLock.readLock().lock(); - try { - return getUserGeofences(userId); - } finally { - userGeofencesLock.readLock().unlock(); - } - } - - private Set getGroupGeofences(long groupId) { - if (!groupGeofences.containsKey(groupId)) { - groupGeofences.put(groupId, new HashSet()); - } - return groupGeofences.get(groupId); - } - - public Set getGroupGeofencesIds(long groupId) { - groupGeofencesLock.readLock().lock(); - try { - return getGroupGeofences(groupId); - } finally { - groupGeofencesLock.readLock().unlock(); - } - } - - public Set getAllDeviceGeofences(long deviceId) { - deviceGeofencesLock.readLock().lock(); - try { - return getDeviceGeofences(deviceGeofencesWithGroups, deviceId); - } finally { - deviceGeofencesLock.readLock().unlock(); - } - } - - public Set getDeviceGeofencesIds(long deviceId) { - deviceGeofencesLock.readLock().lock(); - try { - return getDeviceGeofences(deviceGeofences, deviceId); - } finally { - deviceGeofencesLock.readLock().unlock(); - } - } - - private Set getDeviceGeofences(Map> deviceGeofences, long deviceId) { - if (!deviceGeofences.containsKey(deviceId)) { - deviceGeofences.put(deviceId, new HashSet()); - } - return deviceGeofences.get(deviceId); - } - - public final void refreshGeofences() { - if (dataManager != null) { - try { - geofencesLock.writeLock().lock(); - try { - geofences.clear(); - for (Geofence geofence : dataManager.getGeofences()) { - geofences.put(geofence.getId(), geofence); - } - } finally { - geofencesLock.writeLock().unlock(); - } - } catch (SQLException error) { - Log.warning(error); - } - } - refreshUserGeofences(); + super(dataManager, Geofence.class, GeofencePermission.class, DeviceGeofence.class, GroupGeofence.class); + refreshItems(); refresh(); } - public final void refreshUserGeofences() { - if (dataManager != null) { - try { - userGeofencesLock.writeLock().lock(); - try { - userGeofences.clear(); - for (GeofencePermission geofencePermission : dataManager.getGeofencePermissions()) { - getUserGeofences(geofencePermission.getUserId()).add(geofencePermission.getGeofenceId()); - } - } finally { - userGeofencesLock.writeLock().unlock(); - } - } catch (SQLException error) { - Log.warning(error); - } - } - } - public final void refresh() { - if (dataManager != null) { - try { - - Collection databaseGroupGeofences = dataManager.getGroupGeofences(); - groupGeofencesLock.writeLock().lock(); - try { - groupGeofences.clear(); - for (GroupGeofence groupGeofence : databaseGroupGeofences) { - getGroupGeofences(groupGeofence.getGroupId()).add(groupGeofence.getGeofenceId()); - } - } finally { - groupGeofencesLock.writeLock().unlock(); - } - - Collection databaseDeviceGeofences = dataManager.getDeviceGeofences(); - Collection allDevices = Context.getDeviceManager().getAllDevices(); - - groupGeofencesLock.readLock().lock(); - deviceGeofencesLock.writeLock().lock(); - try { - deviceGeofences.clear(); - deviceGeofencesWithGroups.clear(); - - for (DeviceGeofence deviceGeofence : databaseDeviceGeofences) { - getDeviceGeofences(deviceGeofences, deviceGeofence.getDeviceId()) - .add(deviceGeofence.getGeofenceId()); - getDeviceGeofences(deviceGeofencesWithGroups, deviceGeofence.getDeviceId()) - .add(deviceGeofence.getGeofenceId()); - } - - for (Device device : allDevices) { - long groupId = device.getGroupId(); - while (groupId != 0) { - getDeviceGeofences(deviceGeofencesWithGroups, - device.getId()).addAll(getGroupGeofences(groupId)); - if (Context.getDeviceManager().getGroupById(groupId) != null) { - groupId = Context.getDeviceManager().getGroupById(groupId).getGroupId(); - } else { - groupId = 0; - } - } - List deviceGeofenceIds = device.getGeofenceIds(); - if (deviceGeofenceIds == null) { - deviceGeofenceIds = new ArrayList<>(); - } else { - deviceGeofenceIds.clear(); - } - Position lastPosition = Context.getIdentityManager().getLastPosition(device.getId()); - if (lastPosition != null && deviceGeofencesWithGroups.containsKey(device.getId())) { - for (long geofenceId : deviceGeofencesWithGroups.get(device.getId())) { - Geofence geofence = getGeofence(geofenceId); - if (geofence != null && geofence.getGeometry() - .containsPoint(lastPosition.getLatitude(), lastPosition.getLongitude())) { - deviceGeofenceIds.add(geofenceId); - } - } - } - device.setGeofenceIds(deviceGeofenceIds); - } - - } finally { - deviceGeofencesLock.writeLock().unlock(); - groupGeofencesLock.readLock().unlock(); - } - - } catch (SQLException error) { - Log.warning(error); - } - } - } - - public final Collection getAllGeofences() { - geofencesLock.readLock().lock(); - try { - return geofences.values(); - } finally { - geofencesLock.readLock().unlock(); - } - } - - public final Set getAllGeofencesIds() { - geofencesLock.readLock().lock(); - try { - return geofences.keySet(); - } finally { - geofencesLock.readLock().unlock(); - } - } - - public final Set getManagedGeofencesIds(long userId) { - Set geofences = new HashSet<>(); - geofences.addAll(getUserGeofencesIds(userId)); - for (long managedUserId : Context.getPermissionsManager().getUserPermissions(userId)) { - geofences.addAll(getUserGeofencesIds(managedUserId)); - } - return geofences; - } - - public final Collection getGeofences(Set geofencesIds) { - geofencesLock.readLock().lock(); - try { - Collection result = new LinkedList<>(); - for (long geofenceId : geofencesIds) { - result.add(getGeofence(geofenceId)); - } - return result; - } finally { - geofencesLock.readLock().unlock(); - } - } - - public final Geofence getGeofence(long geofenceId) { - geofencesLock.readLock().lock(); - try { - return geofences.get(geofenceId); - } finally { - geofencesLock.readLock().unlock(); - } - } - - public final void updateGeofence(Geofence geofence) { - geofencesLock.writeLock().lock(); - try { - geofences.put(geofence.getId(), geofence); - } finally { - geofencesLock.writeLock().unlock(); - } - try { - dataManager.updateGeofence(geofence); - } catch (SQLException error) { - Log.warning(error); - } - } - - public boolean checkGeofence(long userId, long geofenceId) { - return getUserGeofencesIds(userId).contains(geofenceId); + super.refresh(); + recalculateDevicesGeofences(); } public List getCurrentDeviceGeofences(Position position) { List result = new ArrayList<>(); - for (long geofenceId : getAllDeviceGeofences(position.getDeviceId())) { - if (getGeofence(geofenceId).getGeometry().containsPoint(position.getLatitude(), position.getLongitude())) { + for (long geofenceId : getAllDeviceItems(position.getDeviceId())) { + Geofence geofence = (Geofence) getById(geofenceId); + if (geofence != null && geofence.getGeometry() + .containsPoint(position.getLatitude(), position.getLongitude())) { result.add(geofenceId); } } return result; } + public void recalculateDevicesGeofences() { + for (Device device : Context.getDeviceManager().getAllDevices()) { + List deviceGeofenceIds = device.getGeofenceIds(); + if (deviceGeofenceIds == null) { + deviceGeofenceIds = new ArrayList<>(); + } else { + deviceGeofenceIds.clear(); + } + Position lastPosition = Context.getIdentityManager().getLastPosition(device.getId()); + if (lastPosition != null && getAllDeviceItems(device.getId()) != null) { + deviceGeofenceIds.addAll(getCurrentDeviceGeofences(lastPosition)); + } + device.setGeofenceIds(deviceGeofenceIds); + } + } + } diff --git a/src/org/traccar/database/NotificationManager.java b/src/org/traccar/database/NotificationManager.java index 48caa615c..11d5a9aca 100644 --- a/src/org/traccar/database/NotificationManager.java +++ b/src/org/traccar/database/NotificationManager.java @@ -57,7 +57,7 @@ public class NotificationManager { Set users = Context.getPermissionsManager().getDeviceUsers(event.getDeviceId()); for (long userId : users) { if (event.getGeofenceId() == 0 || Context.getGeofenceManager() != null - && Context.getGeofenceManager().checkGeofence(userId, event.getGeofenceId())) { + && Context.getGeofenceManager().checkItemPermission(userId, event.getGeofenceId())) { Notification notification = getUserNotificationByType(userId, event.getType()); if (notification != null) { if (notification.getWeb()) { diff --git a/src/org/traccar/database/PermissionsManager.java b/src/org/traccar/database/PermissionsManager.java index 82ca6af49..f88e14bfe 100644 --- a/src/org/traccar/database/PermissionsManager.java +++ b/src/org/traccar/database/PermissionsManager.java @@ -298,52 +298,37 @@ public class PermissionsManager { } } - public void checkGeofence(long userId, long geofenceId) throws SecurityException { - if (!Context.getGeofenceManager().checkGeofence(userId, geofenceId) && !isAdmin(userId)) { - checkManager(userId); - for (long managedUserId : getUserPermissions(userId)) { - if (Context.getGeofenceManager().checkGeofence(managedUserId, geofenceId)) { - return; - } - } - throw new SecurityException("Geofence access denied"); - } - } - - public void checkAttribute(long userId, long attributeId) throws SecurityException { - if (!Context.getAttributesManager().checkAttribute(userId, attributeId) && !isAdmin(userId)) { - checkManager(userId); - for (long managedUserId : getUserPermissions(userId)) { - if (Context.getAttributesManager().checkAttribute(managedUserId, attributeId)) { - return; - } - } - throw new SecurityException("Attribute access denied"); - } - } - - public void checkDriver(long userId, long driverId) throws SecurityException { - if (!Context.getDriversManager().checkDriver(userId, driverId) && !isAdmin(userId)) { - checkManager(userId); - for (long managedUserId : getUserPermissions(userId)) { - if (Context.getDriversManager().checkDriver(managedUserId, driverId)) { - return; - } - } - throw new SecurityException("Driver access denied"); - } - } - - - public void checkCalendar(long userId, long calendarId) throws SecurityException { - if (!Context.getCalendarManager().checkCalendar(userId, calendarId) && !isAdmin(userId)) { - checkManager(userId); - for (long managedUserId : getUserPermissions(userId)) { - if (Context.getCalendarManager().checkCalendar(managedUserId, calendarId)) { - return; + public void checkPermission(String object, long userId, long objectId) throws SecurityException { + SimpleObjectManager manager = null; + + switch (object) { + case "geofence": + manager = Context.getGeofenceManager(); + break; + case "attribute": + manager = Context.getAttributesManager(); + break; + case "driver": + manager = Context.getDriversManager(); + break; + case "calendar": + manager = Context.getCalendarManager(); + break; + default: + throw new IllegalArgumentException("Unknown object type"); + } + + if (manager != null) { + if (manager.checkItemPermission(userId, objectId) && !isAdmin(userId)) { + checkManager(userId); + for (long managedUserId : getUserPermissions(userId)) { + if (manager.checkItemPermission(managedUserId, objectId)) { + return; + } } + throw new SecurityException(object.substring(0, 1).toUpperCase() + object.substring(1) + + " access denied"); } - throw new SecurityException("Calendar access denied"); } } diff --git a/src/org/traccar/database/SimpleObjectManager.java b/src/org/traccar/database/SimpleObjectManager.java new file mode 100644 index 000000000..84d41a84f --- /dev/null +++ b/src/org/traccar/database/SimpleObjectManager.java @@ -0,0 +1,164 @@ +/* + * Copyright 2017 Anton Tananaev (anton@traccar.org) + * Copyright 2017 Andrey Kunitsyn (andrey@traccar.org) + * + * 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.database; + +import java.sql.SQLException; +import java.util.Collection; +import java.util.HashSet; +import java.util.LinkedList; +import java.util.Map; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; + +import org.traccar.Context; +import org.traccar.helper.Log; +import org.traccar.model.BaseUserPermission; +import org.traccar.model.Identifiable; + +public abstract class SimpleObjectManager { + + private final DataManager dataManager; + + private final Map items = new ConcurrentHashMap<>(); + private final Map> userItems = new ConcurrentHashMap<>(); + + private Class baseClass; + private Class permissionClass; + + protected SimpleObjectManager(DataManager dataManager, + Class baseClass, + Class permissionClass) { + this.dataManager = dataManager; + this.baseClass = baseClass; + this.permissionClass = permissionClass; + } + + protected final DataManager getDataManager() { + return dataManager; + } + + protected final Class getBaseClass() { + return baseClass; + } + + public final Identifiable getById(long itemId) { + return items.get(itemId); + } + + protected final void clearItems() { + items.clear(); + } + + protected final void putItem(long itemId, Identifiable item) { + items.put(itemId, item); + } + + protected final void removeCachedItem(long itemId) { + items.remove(itemId); + } + + public final Set getUserItems(long userId) { + if (!userItems.containsKey(userId)) { + userItems.put(userId, new HashSet()); + } + return userItems.get(userId); + } + + protected final void clearUserItems() { + userItems.clear(); + } + + public final boolean checkItemPermission(long userId, long itemId) { + return getUserItems(userId).contains(itemId); + } + + public void refreshItems() { + if (dataManager != null) { + try { + clearItems(); + for (Identifiable item : dataManager.getObjects(this.baseClass)) { + putItem(item.getId(), item); + } + } catch (SQLException error) { + Log.warning(error); + } + } + refreshUserItems(); + } + + public final void refreshUserItems() { + if (dataManager != null) { + try { + clearUserItems(); + for (BaseUserPermission permission : dataManager.getObjects(this.permissionClass)) { + getUserItems(permission.getUserId()).add(permission.getSlaveId()); + } + } catch (SQLException error) { + Log.warning(error); + } + } + } + + public void addItem(Identifiable item) throws SQLException { + dataManager.addObject(item); + putItem(item.getId(), item); + } + + public void updateItem(Identifiable item) throws SQLException { + dataManager.updateObject(item); + putItem(item.getId(), item); + } + + public void removeItem(long itemId) throws SQLException { + Identifiable item = getById(itemId); + if (item != null) { + dataManager.removeObject(item.getClass(), itemId); + removeCachedItem(itemId); + } + refreshUserItems(); + } + +// public final Collection getItems(Set itemIds) { +// Collection result = new LinkedList<>(); +// for (long itemId : itemIds) { +// result.add(getById(itemId)); +// } +// return result; +// } + + public final Collection getItems(Class clazz, Set itemIds) { + Collection result = new LinkedList<>(); + for (long itemId : itemIds) { + result.add((T) getById(itemId)); + } + return result; + } + + public final Set getAllItems() { + return items.keySet(); + } + + public final Set getManagedItems(long userId) { + Set result = new HashSet<>(); + result.addAll(getUserItems(userId)); + for (long managedUserId : Context.getPermissionsManager().getUserPermissions(userId)) { + result.addAll(getUserItems(managedUserId)); + } + return result; + } + +} diff --git a/src/org/traccar/events/GeofenceEventHandler.java b/src/org/traccar/events/GeofenceEventHandler.java index fbec932b1..cdac7993f 100644 --- a/src/org/traccar/events/GeofenceEventHandler.java +++ b/src/org/traccar/events/GeofenceEventHandler.java @@ -22,8 +22,10 @@ import java.util.List; import org.traccar.BaseEventHandler; import org.traccar.Context; import org.traccar.database.GeofenceManager; +import org.traccar.model.Calendar; import org.traccar.model.Device; import org.traccar.model.Event; +import org.traccar.model.Geofence; import org.traccar.model.Position; public class GeofenceEventHandler extends BaseEventHandler { @@ -57,18 +59,20 @@ public class GeofenceEventHandler extends BaseEventHandler { Collection events = new ArrayList<>(); for (long geofenceId : newGeofences) { - long calendarId = geofenceManager.getGeofence(geofenceId).getCalendarId(); - if (calendarId == 0 || Context.getCalendarManager().getCalendar(calendarId) == null - || Context.getCalendarManager().getCalendar(calendarId).checkMoment(position.getFixTime())) { + long calendarId = ((Geofence) geofenceManager.getById(geofenceId)).getCalendarId(); + if (calendarId == 0 || Context.getCalendarManager().getById(calendarId) == null + || ((Calendar) Context.getCalendarManager().getById(calendarId)) + .checkMoment(position.getFixTime())) { Event event = new Event(Event.TYPE_GEOFENCE_ENTER, position.getDeviceId(), position.getId()); event.setGeofenceId(geofenceId); events.add(event); } } for (long geofenceId : oldGeofences) { - long calendarId = geofenceManager.getGeofence(geofenceId).getCalendarId(); - if (calendarId == 0 || Context.getCalendarManager().getCalendar(calendarId) == null - || Context.getCalendarManager().getCalendar(calendarId).checkMoment(position.getFixTime())) { + long calendarId = ((Geofence) geofenceManager.getById(geofenceId)).getCalendarId(); + if (calendarId == 0 || Context.getCalendarManager().getById(calendarId) == null + || ((Calendar) Context.getCalendarManager().getById(calendarId)) + .checkMoment(position.getFixTime())) { Event event = new Event(Event.TYPE_GEOFENCE_EXIT, position.getDeviceId(), position.getId()); event.setGeofenceId(geofenceId); events.add(event); diff --git a/src/org/traccar/model/Attribute.java b/src/org/traccar/model/Attribute.java index 9c3b5e43b..ab12cd2c4 100644 --- a/src/org/traccar/model/Attribute.java +++ b/src/org/traccar/model/Attribute.java @@ -16,17 +16,7 @@ */ package org.traccar.model; -public class Attribute { - - private long id; - - public long getId() { - return id; - } - - public void setId(long id) { - this.id = id; - } +public class Attribute extends Identifiable { private String description; diff --git a/src/org/traccar/model/AttributeAlias.java b/src/org/traccar/model/AttributeAlias.java index 2835c0558..5aa8a5122 100644 --- a/src/org/traccar/model/AttributeAlias.java +++ b/src/org/traccar/model/AttributeAlias.java @@ -16,17 +16,7 @@ */ package org.traccar.model; -public class AttributeAlias { - - private long id; - - public long getId() { - return id; - } - - public void setId(long id) { - this.id = id; - } +public class AttributeAlias extends Identifiable { private long deviceId; diff --git a/src/org/traccar/model/AttributePermission.java b/src/org/traccar/model/AttributePermission.java index fe2fe7b6e..e6bbb0f91 100644 --- a/src/org/traccar/model/AttributePermission.java +++ b/src/org/traccar/model/AttributePermission.java @@ -16,17 +16,7 @@ */ package org.traccar.model; -public class AttributePermission { - - private long userId; - - public long getUserId() { - return userId; - } - - public void setUserId(long userId) { - this.userId = userId; - } +public class AttributePermission extends BaseUserPermission { private long attributeId; @@ -38,4 +28,9 @@ public class AttributePermission { this.attributeId = attributeId; } + @Override + public long getSlaveId() { + return attributeId; + } + } diff --git a/src/org/traccar/model/BaseDevicePermission.java b/src/org/traccar/model/BaseDevicePermission.java new file mode 100644 index 000000000..6eda15d0e --- /dev/null +++ b/src/org/traccar/model/BaseDevicePermission.java @@ -0,0 +1,31 @@ +/* + * Copyright 2017 Anton Tananaev (anton@traccar.org) + * Copyright 2017 Andrey Kunitsyn (andrey@traccar.org) + * + * 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.model; + +public abstract class BaseDevicePermission implements BasePermission { + + private long deviceId; + + public long getDeviceId() { + return deviceId; + } + + public void setDeviceId(long deviceId) { + this.deviceId = deviceId; + } + +} diff --git a/src/org/traccar/model/BaseGroupPermission.java b/src/org/traccar/model/BaseGroupPermission.java new file mode 100644 index 000000000..5a289cad2 --- /dev/null +++ b/src/org/traccar/model/BaseGroupPermission.java @@ -0,0 +1,31 @@ +/* + * Copyright 2017 Anton Tananaev (anton@traccar.org) + * Copyright 2017 Andrey Kunitsyn (andrey@traccar.org) + * + * 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.model; + +public abstract class BaseGroupPermission implements BasePermission { + + private long groupId; + + public long getGroupId() { + return groupId; + } + + public void setGroupId(long groupId) { + this.groupId = groupId; + } + +} diff --git a/src/org/traccar/model/BasePermission.java b/src/org/traccar/model/BasePermission.java new file mode 100644 index 000000000..6f1baffdc --- /dev/null +++ b/src/org/traccar/model/BasePermission.java @@ -0,0 +1,23 @@ +/* + * Copyright 2017 Anton Tananaev (anton@traccar.org) + * Copyright 2017 Andrey Kunitsyn (andrey@traccar.org) + * + * 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.model; + +public interface BasePermission { + + long getSlaveId(); + +} diff --git a/src/org/traccar/model/BaseUserPermission.java b/src/org/traccar/model/BaseUserPermission.java new file mode 100644 index 000000000..64a7be82a --- /dev/null +++ b/src/org/traccar/model/BaseUserPermission.java @@ -0,0 +1,31 @@ +/* + * Copyright 2017 Anton Tananaev (anton@traccar.org) + * Copyright 2017 Andrey Kunitsyn (andrey@traccar.org) + * + * 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.model; + +public abstract class BaseUserPermission implements BasePermission { + + private long userId; + + public long getUserId() { + return userId; + } + + public void setUserId(long userId) { + this.userId = userId; + } + +} diff --git a/src/org/traccar/model/CalendarPermission.java b/src/org/traccar/model/CalendarPermission.java index 59f54e07b..d95e51b0e 100644 --- a/src/org/traccar/model/CalendarPermission.java +++ b/src/org/traccar/model/CalendarPermission.java @@ -16,17 +16,7 @@ */ package org.traccar.model; -public class CalendarPermission { - - private long userId; - - public long getUserId() { - return userId; - } - - public void setUserId(long userId) { - this.userId = userId; - } +public class CalendarPermission extends BaseUserPermission { private long calendarId; @@ -37,4 +27,9 @@ public class CalendarPermission { public void setCalendarId(long calendarId) { this.calendarId = calendarId; } + + @Override + public long getSlaveId() { + return calendarId; + } } diff --git a/src/org/traccar/model/DeviceAttribute.java b/src/org/traccar/model/DeviceAttribute.java index e0ac6dd98..bfcd873fa 100644 --- a/src/org/traccar/model/DeviceAttribute.java +++ b/src/org/traccar/model/DeviceAttribute.java @@ -16,17 +16,7 @@ */ package org.traccar.model; -public class DeviceAttribute { - - private long deviceId; - - public long getDeviceId() { - return deviceId; - } - - public void setDeviceId(long deviceId) { - this.deviceId = deviceId; - } +public class DeviceAttribute extends BaseDevicePermission { private long attributeId; @@ -37,4 +27,9 @@ public class DeviceAttribute { public void setAttributeId(long attributeId) { this.attributeId = attributeId; } + + @Override + public long getSlaveId() { + return attributeId; + } } diff --git a/src/org/traccar/model/DeviceDriver.java b/src/org/traccar/model/DeviceDriver.java index 2008aaddf..f5257531d 100644 --- a/src/org/traccar/model/DeviceDriver.java +++ b/src/org/traccar/model/DeviceDriver.java @@ -16,17 +16,7 @@ */ package org.traccar.model; -public class DeviceDriver { - - private long deviceId; - - public long getDeviceId() { - return deviceId; - } - - public void setDeviceId(long deviceId) { - this.deviceId = deviceId; - } +public class DeviceDriver extends BaseDevicePermission { private long driverId; @@ -38,4 +28,9 @@ public class DeviceDriver { this.driverId = driverId; } + @Override + public long getSlaveId() { + return driverId; + } + } diff --git a/src/org/traccar/model/DeviceGeofence.java b/src/org/traccar/model/DeviceGeofence.java index 00c99add6..7ea4d3bea 100644 --- a/src/org/traccar/model/DeviceGeofence.java +++ b/src/org/traccar/model/DeviceGeofence.java @@ -15,17 +15,7 @@ */ package org.traccar.model; -public class DeviceGeofence { - - private long deviceId; - - public long getDeviceId() { - return deviceId; - } - - public void setDeviceId(long deviceId) { - this.deviceId = deviceId; - } +public class DeviceGeofence extends BaseDevicePermission { private long geofenceId; @@ -37,4 +27,9 @@ public class DeviceGeofence { this.geofenceId = geofenceId; } + @Override + public long getSlaveId() { + return geofenceId; + } + } diff --git a/src/org/traccar/model/DriverPermission.java b/src/org/traccar/model/DriverPermission.java index cdbcc4a50..44f34c8e6 100644 --- a/src/org/traccar/model/DriverPermission.java +++ b/src/org/traccar/model/DriverPermission.java @@ -16,17 +16,7 @@ */ package org.traccar.model; -public class DriverPermission { - - private long userId; - - public long getUserId() { - return userId; - } - - public void setUserId(long userId) { - this.userId = userId; - } +public class DriverPermission extends BaseUserPermission { private long driverId; @@ -37,4 +27,9 @@ public class DriverPermission { public void setDriverId(long driverId) { this.driverId = driverId; } + + @Override + public long getSlaveId() { + return driverId; + } } diff --git a/src/org/traccar/model/Extensible.java b/src/org/traccar/model/Extensible.java index b7953d8a6..3b382838e 100644 --- a/src/org/traccar/model/Extensible.java +++ b/src/org/traccar/model/Extensible.java @@ -18,17 +18,7 @@ package org.traccar.model; import java.util.LinkedHashMap; import java.util.Map; -public class Extensible { - - private long id; - - public long getId() { - return id; - } - - public void setId(long id) { - this.id = id; - } +public class Extensible extends Identifiable { private Map attributes = new LinkedHashMap<>(); diff --git a/src/org/traccar/model/GeofencePermission.java b/src/org/traccar/model/GeofencePermission.java index 464f4e9eb..8606cc3e7 100644 --- a/src/org/traccar/model/GeofencePermission.java +++ b/src/org/traccar/model/GeofencePermission.java @@ -15,17 +15,7 @@ */ package org.traccar.model; -public class GeofencePermission { - - private long userId; - - public long getUserId() { - return userId; - } - - public void setUserId(long userId) { - this.userId = userId; - } +public class GeofencePermission extends BaseUserPermission { private long geofenceId; @@ -37,4 +27,9 @@ public class GeofencePermission { this.geofenceId = geofenceId; } + @Override + public long getSlaveId() { + return geofenceId; + } + } diff --git a/src/org/traccar/model/GroupAttribute.java b/src/org/traccar/model/GroupAttribute.java index a7e8a80bc..cfaf9cc2f 100644 --- a/src/org/traccar/model/GroupAttribute.java +++ b/src/org/traccar/model/GroupAttribute.java @@ -16,17 +16,7 @@ */ package org.traccar.model; -public class GroupAttribute { - - private long groupId; - - public long getGroupId() { - return groupId; - } - - public void setGroupId(long groupId) { - this.groupId = groupId; - } +public class GroupAttribute extends BaseGroupPermission { private long attributeId; @@ -37,4 +27,9 @@ public class GroupAttribute { public void setAttributeId(long attributeId) { this.attributeId = attributeId; } + + @Override + public long getSlaveId() { + return attributeId; + } } diff --git a/src/org/traccar/model/GroupDriver.java b/src/org/traccar/model/GroupDriver.java index 91b205923..9bf74c214 100644 --- a/src/org/traccar/model/GroupDriver.java +++ b/src/org/traccar/model/GroupDriver.java @@ -16,17 +16,7 @@ */ package org.traccar.model; -public class GroupDriver { - - private long groupId; - - public long getGroupId() { - return groupId; - } - - public void setGroupId(long groupId) { - this.groupId = groupId; - } +public class GroupDriver extends BaseGroupPermission { private long driverId; @@ -38,4 +28,9 @@ public class GroupDriver { this.driverId = driverId; } + @Override + public long getSlaveId() { + return driverId; + } + } diff --git a/src/org/traccar/model/GroupGeofence.java b/src/org/traccar/model/GroupGeofence.java index 736e6c704..89830324d 100644 --- a/src/org/traccar/model/GroupGeofence.java +++ b/src/org/traccar/model/GroupGeofence.java @@ -15,17 +15,7 @@ */ package org.traccar.model; -public class GroupGeofence { - - private long groupId; - - public long getGroupId() { - return groupId; - } - - public void setGroupId(long groupId) { - this.groupId = groupId; - } +public class GroupGeofence extends BaseGroupPermission { private long geofenceId; @@ -37,4 +27,9 @@ public class GroupGeofence { this.geofenceId = geofenceId; } + @Override + public long getSlaveId() { + return geofenceId; + } + } diff --git a/src/org/traccar/model/Identifiable.java b/src/org/traccar/model/Identifiable.java new file mode 100644 index 000000000..f22cfe800 --- /dev/null +++ b/src/org/traccar/model/Identifiable.java @@ -0,0 +1,31 @@ +/* + * Copyright 2017 Anton Tananaev (anton@traccar.org) + * Copyright 2017 Andrey Kunitsyn (andrey@traccar.org) + * + * 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.model; + +public class Identifiable { + + private long id; + + public final long getId() { + return id; + } + + public final void setId(long id) { + this.id = id; + } + +} diff --git a/src/org/traccar/notification/EventForwarder.java b/src/org/traccar/notification/EventForwarder.java index bd7cfc0c5..08f55df75 100644 --- a/src/org/traccar/notification/EventForwarder.java +++ b/src/org/traccar/notification/EventForwarder.java @@ -75,7 +75,7 @@ public final class EventForwarder { } } if (event.getGeofenceId() != 0) { - Geofence geofence = Context.getGeofenceManager().getGeofence(event.getGeofenceId()); + Geofence geofence = (Geofence) Context.getGeofenceManager().getById(event.getGeofenceId()); if (geofence != null) { data.put(KEY_GEOFENCE, geofence); } diff --git a/src/org/traccar/notification/NotificationFormatter.java b/src/org/traccar/notification/NotificationFormatter.java index a30023fdc..cd9959671 100644 --- a/src/org/traccar/notification/NotificationFormatter.java +++ b/src/org/traccar/notification/NotificationFormatter.java @@ -51,7 +51,7 @@ public final class NotificationFormatter { velocityContext.put("speedUnits", ReportUtils.getSpeedUnit(userId)); } if (event.getGeofenceId() != 0) { - velocityContext.put("geofence", Context.getGeofenceManager().getGeofence(event.getGeofenceId())); + velocityContext.put("geofence", Context.getGeofenceManager().getById(event.getGeofenceId())); } String driverUniqueId = event.getString(Position.KEY_DRIVER_UNIQUE_ID); if (driverUniqueId != null) { diff --git a/src/org/traccar/processing/ComputedAttributesHandler.java b/src/org/traccar/processing/ComputedAttributesHandler.java index 536e39ec4..d6a762535 100644 --- a/src/org/traccar/processing/ComputedAttributesHandler.java +++ b/src/org/traccar/processing/ComputedAttributesHandler.java @@ -86,8 +86,8 @@ public class ComputedAttributesHandler extends BaseDataHandler { @Override protected Position handlePosition(Position position) { - Collection attributes = Context.getAttributesManager().getAttributes( - Context.getAttributesManager().getAllDeviceAttributes(position.getDeviceId())); + Collection attributes = Context.getAttributesManager().getItems(Attribute.class, + Context.getAttributesManager().getAllDeviceItems(position.getDeviceId())); for (Attribute attribute : attributes) { if (attribute.getAttribute() != null) { Object result = null; diff --git a/src/org/traccar/reports/Events.java b/src/org/traccar/reports/Events.java index 0706f1382..3a111944c 100644 --- a/src/org/traccar/reports/Events.java +++ b/src/org/traccar/reports/Events.java @@ -50,7 +50,7 @@ public final class Events { for (Event event : events) { if (all || types.contains(event.getType())) { long geofenceId = event.getGeofenceId(); - if (geofenceId == 0 || Context.getGeofenceManager().checkGeofence(userId, geofenceId)) { + if (geofenceId == 0 || Context.getGeofenceManager().checkItemPermission(userId, geofenceId)) { result.add(event); } } @@ -74,8 +74,8 @@ public final class Events { if (all || types.contains(event.getType())) { long geofenceId = event.getGeofenceId(); if (geofenceId != 0) { - if (Context.getGeofenceManager().checkGeofence(userId, geofenceId)) { - Geofence geofence = Context.getGeofenceManager().getGeofence(geofenceId); + if (Context.getGeofenceManager().checkItemPermission(userId, geofenceId)) { + Geofence geofence = (Geofence) Context.getGeofenceManager().getById(geofenceId); if (geofence != null) { geofenceNames.put(geofenceId, geofence.getName()); } -- cgit v1.2.3