From 82edf57e1b93d5a5ca34294dc62e3620889c2341 Mon Sep 17 00:00:00 2001 From: Abyss777 Date: Wed, 26 Jul 2017 15:20:22 +0500 Subject: Override getById in all managers --- src/org/traccar/BaseProtocolDecoder.java | 2 +- src/org/traccar/database/AttributesManager.java | 7 ++++++- src/org/traccar/database/BaseObjectManager.java | 2 +- src/org/traccar/database/CalendarManager.java | 5 +++++ src/org/traccar/database/DeviceManager.java | 14 +++++++------- src/org/traccar/database/DriversManager.java | 9 +++++++-- src/org/traccar/database/GeofenceManager.java | 7 ++++++- src/org/traccar/database/GroupsManager.java | 5 +++++ src/org/traccar/database/UsersManager.java | 11 ++++++++--- src/org/traccar/reports/Events.java | 2 +- src/org/traccar/reports/Route.java | 2 +- src/org/traccar/reports/Stops.java | 2 +- src/org/traccar/reports/Trips.java | 2 +- 13 files changed, 50 insertions(+), 20 deletions(-) diff --git a/src/org/traccar/BaseProtocolDecoder.java b/src/org/traccar/BaseProtocolDecoder.java index 1aff4564f..b7ceabb5c 100644 --- a/src/org/traccar/BaseProtocolDecoder.java +++ b/src/org/traccar/BaseProtocolDecoder.java @@ -51,7 +51,7 @@ public abstract class BaseProtocolDecoder extends ExtendedObjectDecoder { Log.info("Automatically registered device " + uniqueId); if (defaultGroupId != 0) { - Context.getPermissionsManager().refreshPermissions(); + Context.getPermissionsManager().refreshDeviceAndGroupPermissions(); Context.getPermissionsManager().refreshAllExtendedPermissions(); } diff --git a/src/org/traccar/database/AttributesManager.java b/src/org/traccar/database/AttributesManager.java index 4dc70eeb7..e80d57c4e 100644 --- a/src/org/traccar/database/AttributesManager.java +++ b/src/org/traccar/database/AttributesManager.java @@ -25,10 +25,15 @@ public class AttributesManager extends ExtendedObjectManager { super(dataManager, Attribute.class); } + @Override + public Attribute getById(long calendarId) { + return (Attribute) super.getById(calendarId); + } + @Override public void updateCachedItem(BaseModel item) { Attribute attribute = (Attribute) item; - Attribute cachedAttribute = (Attribute) getById(item.getId()); + Attribute cachedAttribute = getById(item.getId()); cachedAttribute.setDescription(attribute.getDescription()); cachedAttribute.setAttribute(attribute.getAttribute()); cachedAttribute.setExpression(attribute.getExpression()); diff --git a/src/org/traccar/database/BaseObjectManager.java b/src/org/traccar/database/BaseObjectManager.java index d515fc2a9..92cfd9b6b 100644 --- a/src/org/traccar/database/BaseObjectManager.java +++ b/src/org/traccar/database/BaseObjectManager.java @@ -48,7 +48,7 @@ public class BaseObjectManager { return baseClass; } - public final BaseModel getById(long itemId) { + public BaseModel getById(long itemId) { return items.get(itemId); } diff --git a/src/org/traccar/database/CalendarManager.java b/src/org/traccar/database/CalendarManager.java index 6874020ec..b8d0914ee 100644 --- a/src/org/traccar/database/CalendarManager.java +++ b/src/org/traccar/database/CalendarManager.java @@ -24,4 +24,9 @@ public class CalendarManager extends SimpleObjectManager { super(dataManager, Calendar.class); } + @Override + public Calendar getById(long calendarId) { + return (Calendar) super.getById(calendarId); + } + } diff --git a/src/org/traccar/database/DeviceManager.java b/src/org/traccar/database/DeviceManager.java index ac66f1bb8..1fe7ddfa5 100644 --- a/src/org/traccar/database/DeviceManager.java +++ b/src/org/traccar/database/DeviceManager.java @@ -143,8 +143,8 @@ public class DeviceManager extends BaseObjectManager implements IdentityManager, @Override protected void addNewItem(BaseModel item) { - Device device = (Device) item; super.addNewItem(item); + Device device = (Device) item; putUniqueDeviceId(device); if (device.getPhone() != null && !device.getPhone().isEmpty()) { putPhone(device); @@ -160,7 +160,7 @@ public class DeviceManager extends BaseObjectManager implements IdentityManager, @Override protected void updateCachedItem(BaseModel item) { Device device = (Device) item; - Device cachedDevice = (Device) getById(device.getId()); + Device cachedDevice = getDeviceById(device.getId()); cachedDevice.setName(device.getName()); cachedDevice.setGroupId(device.getGroupId()); cachedDevice.setCategory(device.getCategory()); @@ -182,7 +182,7 @@ public class DeviceManager extends BaseObjectManager implements IdentityManager, @Override protected void removeCachedItem(long deviceId) { - Device cachedDevice = (Device) getById(deviceId); + Device cachedDevice = getDeviceById(deviceId); if (cachedDevice != null) { String deviceUniqueId = cachedDevice.getUniqueId(); String phone = cachedDevice.getPhone(); @@ -197,7 +197,7 @@ public class DeviceManager extends BaseObjectManager implements IdentityManager, public void updateDeviceStatus(Device device) throws SQLException { getDataManager().updateDeviceStatus(device); - Device cachedDevice = (Device) getById(device.getId()); + Device cachedDevice = getDeviceById(device.getId()); if (cachedDevice != null) { cachedDevice.setStatus(device.getStatus()); } @@ -226,7 +226,7 @@ public class DeviceManager extends BaseObjectManager implements IdentityManager, getDataManager().updateLatestPosition(position); - Device device = (Device) getById(position.getDeviceId()); + Device device = getDeviceById(position.getDeviceId()); if (device != null) { device.setPositionId(position.getId()); } @@ -311,7 +311,7 @@ public class DeviceManager extends BaseObjectManager implements IdentityManager, if (result == null && lookupGroupsAttribute) { long groupId = device.getGroupId(); while (groupId != 0) { - Group group = (Group) Context.getGroupsManager().getById(groupId); + Group group = Context.getGroupsManager().getById(groupId); if (group != null) { result = group.getString(attributeName); if (result != null) { @@ -352,7 +352,7 @@ public class DeviceManager extends BaseObjectManager implements IdentityManager, Position lastPosition = getLastPosition(deviceId); if (lastPosition != null) { BaseProtocol protocol = Context.getServerManager().getProtocol(lastPosition.getProtocol()); - protocol.sendTextCommand(((Device) getById(deviceId)).getPhone(), command); + protocol.sendTextCommand(getDeviceById(deviceId).getPhone(), command); } else if (command.getType().equals(Command.TYPE_CUSTOM)) { Context.getSmppManager().sendMessageSync(((Device) getById(deviceId)).getPhone(), command.getString(Command.KEY_DATA), true); diff --git a/src/org/traccar/database/DriversManager.java b/src/org/traccar/database/DriversManager.java index 14b74bab1..420824dbb 100644 --- a/src/org/traccar/database/DriversManager.java +++ b/src/org/traccar/database/DriversManager.java @@ -30,6 +30,11 @@ public class DriversManager extends ExtendedObjectManager { super(dataManager, Driver.class); } + @Override + public Driver getById(long driverId) { + return (Driver) super.getById(driverId); + } + private void putUniqueDriverId(Driver driver) { if (driversByUniqueId == null) { driversByUniqueId = new ConcurrentHashMap<>(); @@ -46,7 +51,7 @@ public class DriversManager extends ExtendedObjectManager { @Override protected void updateCachedItem(BaseModel item) { Driver driver = (Driver) item; - Driver cachedDriver = (Driver) getById(driver.getId()); + Driver cachedDriver = getById(driver.getId()); cachedDriver.setName(driver.getName()); if (!driver.getUniqueId().equals(cachedDriver.getUniqueId())) { driversByUniqueId.remove(cachedDriver.getUniqueId()); @@ -58,7 +63,7 @@ public class DriversManager extends ExtendedObjectManager { @Override protected void removeCachedItem(long driverId) { - Driver cachedDriver = (Driver) getById(driverId); + Driver cachedDriver = getById(driverId); if (cachedDriver != null) { String driverUniqueId = cachedDriver.getUniqueId(); super.removeCachedItem(driverId); diff --git a/src/org/traccar/database/GeofenceManager.java b/src/org/traccar/database/GeofenceManager.java index 2c3c32c56..4bc3909cd 100644 --- a/src/org/traccar/database/GeofenceManager.java +++ b/src/org/traccar/database/GeofenceManager.java @@ -29,6 +29,11 @@ public class GeofenceManager extends ExtendedObjectManager { super(dataManager, Geofence.class); } + @Override + public Geofence getById(long geofenceId) { + return (Geofence) super.getById(geofenceId); + } + @Override public final void refreshExtendedPermissions() { super.refreshExtendedPermissions(); @@ -38,7 +43,7 @@ public class GeofenceManager extends ExtendedObjectManager { public List getCurrentDeviceGeofences(Position position) { List result = new ArrayList<>(); for (long geofenceId : getAllDeviceItems(position.getDeviceId())) { - Geofence geofence = (Geofence) getById(geofenceId); + Geofence geofence = getById(geofenceId); if (geofence != null && geofence.getGeometry() .containsPoint(position.getLatitude(), position.getLongitude())) { result.add(geofenceId); diff --git a/src/org/traccar/database/GroupsManager.java b/src/org/traccar/database/GroupsManager.java index e92158fd6..095e7a22c 100644 --- a/src/org/traccar/database/GroupsManager.java +++ b/src/org/traccar/database/GroupsManager.java @@ -37,6 +37,11 @@ public class GroupsManager extends BaseObjectManager implements ManagableObjects DeviceManager.DEFAULT_REFRESH_DELAY) * 1000; } + @Override + public Group getById(long groupId) { + return (Group) super.getById(groupId); + } + private void checkGroupCycles(BaseModel group) { Set groups = new HashSet<>(); while (group != null) { diff --git a/src/org/traccar/database/UsersManager.java b/src/org/traccar/database/UsersManager.java index cb4ea623f..e53409b4f 100644 --- a/src/org/traccar/database/UsersManager.java +++ b/src/org/traccar/database/UsersManager.java @@ -32,6 +32,11 @@ public class UsersManager extends SimpleObjectManager { super(dataManager, User.class); } + @Override + public User getById(long userId) { + return (User) super.getById(userId); + } + private void putToken(User user) { if (usersTokens == null) { usersTokens = new ConcurrentHashMap<>(); @@ -50,7 +55,7 @@ public class UsersManager extends SimpleObjectManager { @Override protected void updateCachedItem(BaseModel item) { User user = (User) item; - User cachedUser = (User) getById(item.getId()); + User cachedUser = getById(item.getId()); super.updateCachedItem(item); if (user.getToken() != null) { usersTokens.put(user.getToken(), user.getId()); @@ -62,7 +67,7 @@ public class UsersManager extends SimpleObjectManager { @Override protected void removeCachedItem(long userId) { - User cachedUser = (User) getById(userId); + User cachedUser = getById(userId); if (cachedUser != null) { String userToken = cachedUser.getToken(); super.removeCachedItem(userId); @@ -81,7 +86,7 @@ public class UsersManager extends SimpleObjectManager { } public User getUserByToken(String token) { - return (User) getById(usersTokens.get(token)); + return getById(usersTokens.get(token)); } } diff --git a/src/org/traccar/reports/Events.java b/src/org/traccar/reports/Events.java index 9d3acb250..348e6077f 100644 --- a/src/org/traccar/reports/Events.java +++ b/src/org/traccar/reports/Events.java @@ -92,7 +92,7 @@ public final class Events { deviceEvents.setDeviceName(device.getName()); sheetNames.add(WorkbookUtil.createSafeSheetName(deviceEvents.getDeviceName())); if (device.getGroupId() != 0) { - Group group = (Group) Context.getGroupsManager().getById(device.getGroupId()); + Group group = Context.getGroupsManager().getById(device.getGroupId()); if (group != null) { deviceEvents.setGroupName(group.getName()); } diff --git a/src/org/traccar/reports/Route.java b/src/org/traccar/reports/Route.java index 9f0696085..69be55155 100644 --- a/src/org/traccar/reports/Route.java +++ b/src/org/traccar/reports/Route.java @@ -61,7 +61,7 @@ public final class Route { deviceRoutes.setDeviceName(device.getName()); sheetNames.add(WorkbookUtil.createSafeSheetName(deviceRoutes.getDeviceName())); if (device.getGroupId() != 0) { - Group group = (Group) Context.getGroupsManager().getById(device.getGroupId()); + Group group = Context.getGroupsManager().getById(device.getGroupId()); if (group != null) { deviceRoutes.setGroupName(group.getName()); } diff --git a/src/org/traccar/reports/Stops.java b/src/org/traccar/reports/Stops.java index 1c631a0c2..20a6c1ce3 100644 --- a/src/org/traccar/reports/Stops.java +++ b/src/org/traccar/reports/Stops.java @@ -78,7 +78,7 @@ public final class Stops { deviceStops.setDeviceName(device.getName()); sheetNames.add(WorkbookUtil.createSafeSheetName(deviceStops.getDeviceName())); if (device.getGroupId() != 0) { - Group group = (Group) Context.getGroupsManager().getById(device.getGroupId()); + Group group = Context.getGroupsManager().getById(device.getGroupId()); if (group != null) { deviceStops.setGroupName(group.getName()); } diff --git a/src/org/traccar/reports/Trips.java b/src/org/traccar/reports/Trips.java index 51689bfd3..7b5df0248 100644 --- a/src/org/traccar/reports/Trips.java +++ b/src/org/traccar/reports/Trips.java @@ -77,7 +77,7 @@ public final class Trips { deviceTrips.setDeviceName(device.getName()); sheetNames.add(WorkbookUtil.createSafeSheetName(deviceTrips.getDeviceName())); if (device.getGroupId() != 0) { - Group group = (Group) Context.getGroupsManager().getById(device.getGroupId()); + Group group = Context.getGroupsManager().getById(device.getGroupId()); if (group != null) { deviceTrips.setGroupName(group.getName()); } -- cgit v1.2.3