From 69c46399ee0f6b3dd2b0589cb412e9e17f040369 Mon Sep 17 00:00:00 2001 From: Abyss777 Date: Fri, 21 Jul 2017 18:41:49 +0500 Subject: Use classes instead of constants everywhere --- src/org/traccar/database/DataManager.java | 21 +++-- src/org/traccar/database/DriversManager.java | 2 +- .../traccar/database/ExtendedObjectManager.java | 14 ++-- src/org/traccar/database/PermissionsManager.java | 96 +++++++++++----------- src/org/traccar/database/SimpleObjectManager.java | 16 ++-- 5 files changed, 78 insertions(+), 71 deletions(-) (limited to 'src/org/traccar/database') diff --git a/src/org/traccar/database/DataManager.java b/src/org/traccar/database/DataManager.java index 3bb367ece..80b9f98e9 100644 --- a/src/org/traccar/database/DataManager.java +++ b/src/org/traccar/database/DataManager.java @@ -268,17 +268,19 @@ public class DataManager { .executeUpdate()); } - public static String makeName(String object) { - return object.substring(0, 1).toUpperCase() + object.replace("Id", "").substring(1); + public static Class getClassByName(String name) throws ClassNotFoundException { + return Class.forName("org.traccar.model." + + name.substring(0, 1).toUpperCase() + name.replace("Id", "").substring(1)); } - public static String makeNameId(String object) { - return object.substring(0, 1).toLowerCase() + object.substring(1) + (object.indexOf("Id") == -1 ? "Id" : ""); + public static String makeNameId(Class clazz) { + String name = clazz.getSimpleName(); + return name.substring(0, 1).toLowerCase() + name.substring(1) + (name.indexOf("Id") == -1 ? "Id" : ""); } - public void linkObject(String owner, long ownerId, String property, long propertyId, - boolean link) throws SQLException { - String query = "database." + (!link ? "un" : "") + "link" + makeName(owner) + makeName(property); + public void linkObject(Class owner, long ownerId, Class property, long propertyId, boolean link) + throws SQLException { + String query = "database." + (link ? "link" : "unlink") + owner.getSimpleName() + property.getSimpleName(); QueryBuilder queryBuilder = QueryBuilder.create(dataSource, getQuery(query)); queryBuilder.setLong(makeNameId(owner), ownerId); @@ -291,8 +293,9 @@ public class DataManager { return QueryBuilder.create(dataSource, getQuery(query)).executeQuery(clazz); } - public Collection> getPermissions(String owner, String property) throws SQLException { - String query = "database.select" + makeName(owner) + makeName(property) + "s"; + public Collection> getPermissions(Class owner, + Class property) throws SQLException { + String query = "database.select" + owner.getSimpleName() + property.getSimpleName() + "s"; return QueryBuilder.create(dataSource, getQuery(query)).executeMapQuery(Long.class); } diff --git a/src/org/traccar/database/DriversManager.java b/src/org/traccar/database/DriversManager.java index 8c35f3ddf..0dc2b102d 100644 --- a/src/org/traccar/database/DriversManager.java +++ b/src/org/traccar/database/DriversManager.java @@ -73,7 +73,7 @@ public class DriversManager extends ExtendedObjectManager { @Override public void removeItem(long driverId) throws SQLException { Driver cachedDriver = (Driver) getById(driverId); - getDataManager().removeObject(cachedDriver.getClass(), driverId); + getDataManager().removeObject(Driver.class, driverId); if (cachedDriver != null) { String driverUniqueId = cachedDriver.getUniqueId(); removeCachedItem(driverId); diff --git a/src/org/traccar/database/ExtendedObjectManager.java b/src/org/traccar/database/ExtendedObjectManager.java index fce32fb41..8380b9da2 100644 --- a/src/org/traccar/database/ExtendedObjectManager.java +++ b/src/org/traccar/database/ExtendedObjectManager.java @@ -26,6 +26,7 @@ import java.util.concurrent.ConcurrentHashMap; import org.traccar.Context; import org.traccar.helper.Log; import org.traccar.model.Device; +import org.traccar.model.Group; import org.traccar.model.BaseModel; public abstract class ExtendedObjectManager extends SimpleObjectManager { @@ -79,23 +80,26 @@ public abstract class ExtendedObjectManager extends SimpleObjectManager { try { Collection> databaseGroupPermissions = - getDataManager().getPermissions("Group", getBaseClassName()); + getDataManager().getPermissions(Group.class, getBaseClass()); clearGroupItems(); for (Map groupPermission : databaseGroupPermissions) { - getGroupItems(groupPermission.get("groupId")).add(groupPermission.get(getBaseClassIdName())); + getGroupItems(groupPermission.get(DataManager.makeNameId(Group.class))) + .add(groupPermission.get(getBaseClassIdName())); } Collection> databaseDevicePermissions = - getDataManager().getPermissions("Device", getBaseClassName()); + getDataManager().getPermissions(Device.class, getBaseClass()); Collection allDevices = Context.getDeviceManager().getAllDevices(); clearDeviceItems(); deviceItemsWithGroups.clear(); for (Map devicePermission : databaseDevicePermissions) { - getDeviceItems(devicePermission.get("deviceId")).add(devicePermission.get(getBaseClassIdName())); - getAllDeviceItems(devicePermission.get("deviceId")).add(devicePermission.get(getBaseClassIdName())); + getDeviceItems(devicePermission.get(DataManager.makeNameId(Device.class))) + .add(devicePermission.get(getBaseClassIdName())); + getAllDeviceItems(devicePermission.get(DataManager.makeNameId(Device.class))) + .add(devicePermission.get(getBaseClassIdName())); } for (Device device : allDevices) { diff --git a/src/org/traccar/database/PermissionsManager.java b/src/org/traccar/database/PermissionsManager.java index 56a12e941..4008134ec 100644 --- a/src/org/traccar/database/PermissionsManager.java +++ b/src/org/traccar/database/PermissionsManager.java @@ -17,8 +17,13 @@ package org.traccar.database; import org.traccar.Context; import org.traccar.helper.Log; +import org.traccar.model.Attribute; +import org.traccar.model.Calendar; import org.traccar.model.Device; +import org.traccar.model.Driver; +import org.traccar.model.Geofence; import org.traccar.model.Group; +import org.traccar.model.ManagedUser; import org.traccar.model.Server; import org.traccar.model.User; @@ -109,8 +114,9 @@ public class PermissionsManager { public final void refreshUserPermissions() { userPermissions.clear(); try { - for (Map permission : dataManager.getPermissions("User", "Permission")) { - getUserPermissions(permission.get("userId")).add(permission.get("managedUserId")); + for (Map permission : dataManager.getPermissions(User.class, User.class)) { + getUserPermissions(permission.get(DataManager.makeNameId(User.class))) + .add(permission.get(DataManager.makeNameId(ManagedUser.class))); } } catch (SQLException error) { Log.warning(error); @@ -123,20 +129,23 @@ public class PermissionsManager { try { GroupTree groupTree = new GroupTree(Context.getDeviceManager().getAllGroups(), Context.getDeviceManager().getAllDevices()); - for (Map groupPermission : dataManager.getPermissions("Group", "Permission")) { - Set userGroupPermissions = getGroupPermissions(groupPermission.get("userId")); - Set userDevicePermissions = getDevicePermissions(groupPermission.get("userId")); - userGroupPermissions.add(groupPermission.get("groupId")); - for (Group group : groupTree.getGroups(groupPermission.get("groupId"))) { + for (Map groupPermission : dataManager.getPermissions(User.class, Group.class)) { + Set userGroupPermissions = getGroupPermissions(groupPermission + .get(DataManager.makeNameId(User.class))); + Set userDevicePermissions = getDevicePermissions(groupPermission + .get(DataManager.makeNameId(User.class))); + userGroupPermissions.add(groupPermission.get(DataManager.makeNameId(Group.class))); + for (Group group : groupTree.getGroups(groupPermission.get(DataManager.makeNameId(Group.class)))) { userGroupPermissions.add(group.getId()); } - for (Device device : groupTree.getDevices(groupPermission.get("groupId"))) { + for (Device device : groupTree.getDevices(groupPermission.get(DataManager.makeNameId(Group.class)))) { userDevicePermissions.add(device.getId()); } } - for (Map devicePermission : dataManager.getPermissions("Device", "Permission")) { - getDevicePermissions(devicePermission.get("userId")).add(devicePermission.get("deviceId")); + for (Map devicePermission : dataManager.getPermissions(User.class, Device.class)) { + getDevicePermissions(devicePermission.get(DataManager.makeNameId(User.class))) + .add(devicePermission.get(DataManager.makeNameId(Device.class))); } groupDevices.clear(); @@ -296,33 +305,26 @@ public class PermissionsManager { } } - public void checkPermission(String object, long userId, long objectId) throws SecurityException { + public void checkPermission(Class object, long userId, long objectId) + throws SecurityException { SimpleObjectManager manager = null; - switch (DataManager.makeName(object)) { - case Context.TYPE_DEVICE: - checkDevice(userId, objectId); - break; - case Context.TYPE_GROUP: - checkGroup(userId, objectId); - break; - case Context.TYPE_USER: - checkUser(userId, objectId); - break; - case Context.TYPE_GEOFENCE: - manager = Context.getGeofenceManager(); - break; - case Context.TYPE_ATTRIBUTE: - manager = Context.getAttributesManager(); - break; - case Context.TYPE_DRIVER: - manager = Context.getDriversManager(); - break; - case Context.TYPE_CALENDAR: - manager = Context.getCalendarManager(); - break; - default: - throw new IllegalArgumentException("Unknown object type"); + if (object.equals(Device.class)) { + checkDevice(userId, objectId); + } else if (object.equals(Group.class)) { + checkGroup(userId, objectId); + } else if (object.equals(User.class) || object.equals(ManagedUser.class)) { + checkUser(userId, objectId); + } else if (object.equals(Geofence.class)) { + manager = Context.getGeofenceManager(); + } else if (object.equals(Attribute.class)) { + manager = Context.getAttributesManager(); + } else if (object.equals(Driver.class)) { + manager = Context.getDriversManager(); + } else if (object.equals(Calendar.class)) { + manager = Context.getCalendarManager(); + } else { + throw new IllegalArgumentException("Unknown object type"); } if (manager != null) { @@ -347,31 +349,33 @@ public class PermissionsManager { } public void refreshPermissions(Map entity) { - if (entity.containsKey("userId")) { - if (entity.containsKey("deviceId") || entity.containsKey("groupId")) { + if (entity.containsKey(DataManager.makeNameId(User.class))) { + if (entity.containsKey(DataManager.makeNameId(Device.class)) + || entity.containsKey(DataManager.makeNameId(Group.class))) { refreshPermissions(); refreshAllExtendedPermissions(); - } else if (entity.containsKey("managedUserId")) { + } else if (entity.containsKey(DataManager.makeNameId(ManagedUser.class))) { refreshUserPermissions(); - } else if (entity.containsKey("geofenceId")) { + } else if (entity.containsKey(DataManager.makeNameId(Geofence.class))) { if (Context.getGeofenceManager() != null) { Context.getGeofenceManager().refreshUserItems(); } - } else if (entity.containsKey("driverId")) { + } else if (entity.containsKey(DataManager.makeNameId(Driver.class))) { Context.getDriversManager().refreshUserItems(); - } else if (entity.containsKey("attributeId")) { + } else if (entity.containsKey(DataManager.makeNameId(Attribute.class))) { Context.getAttributesManager().refreshUserItems(); - } else if (entity.containsKey("calendarId")) { + } else if (entity.containsKey(DataManager.makeNameId(Calendar.class))) { Context.getCalendarManager().refreshUserItems(); } - } else if (entity.containsKey("deviceId") || entity.containsKey("groupId")) { - if (entity.containsKey("geofenceId")) { + } else if (entity.containsKey(DataManager.makeNameId(Device.class)) + || entity.containsKey(DataManager.makeNameId(Group.class))) { + if (entity.containsKey(DataManager.makeNameId(Geofence.class))) { if (Context.getGeofenceManager() != null) { Context.getGeofenceManager().refreshExtendedPermissions(); } - } else if (entity.containsKey("driverId")) { + } else if (entity.containsKey(DataManager.makeNameId(Driver.class))) { Context.getDriversManager().refreshExtendedPermissions(); - } else if (entity.containsKey("attributeId")) { + } else if (entity.containsKey(DataManager.makeNameId(Attribute.class))) { Context.getAttributesManager().refreshExtendedPermissions(); } } diff --git a/src/org/traccar/database/SimpleObjectManager.java b/src/org/traccar/database/SimpleObjectManager.java index c4fc0e97a..0db8af658 100644 --- a/src/org/traccar/database/SimpleObjectManager.java +++ b/src/org/traccar/database/SimpleObjectManager.java @@ -27,6 +27,7 @@ import java.util.concurrent.ConcurrentHashMap; import org.traccar.Context; import org.traccar.helper.Log; import org.traccar.model.BaseModel; +import org.traccar.model.User; public abstract class SimpleObjectManager { @@ -36,14 +37,12 @@ public abstract class SimpleObjectManager { private final Map> userItems = new ConcurrentHashMap<>(); private Class baseClass; - private String baseClassName; private String baseClassIdName; protected SimpleObjectManager(DataManager dataManager, Class baseClass) { this.dataManager = dataManager; this.baseClass = baseClass; - baseClassName = baseClass.getSimpleName(); - baseClassIdName = baseClassName.substring(0, 1).toLowerCase() + baseClassName.substring(1) + "Id"; + baseClassIdName = DataManager.makeNameId(baseClass); } protected final DataManager getDataManager() { @@ -54,10 +53,6 @@ public abstract class SimpleObjectManager { return baseClass; } - protected final String getBaseClassName() { - return baseClassName; - } - protected final String getBaseClassIdName() { return baseClassIdName; } @@ -111,8 +106,9 @@ public abstract class SimpleObjectManager { if (dataManager != null) { try { clearUserItems(); - for (Map permission : dataManager.getPermissions(baseClassName, "Permission")) { - getUserItems(permission.get("userId")).add(permission.get(baseClassIdName)); + for (Map permission : dataManager.getPermissions(User.class, baseClass)) { + getUserItems(permission.get(DataManager.makeNameId(User.class))) + .add(permission.get(baseClassIdName)); } } catch (SQLException error) { Log.warning(error); @@ -133,7 +129,7 @@ public abstract class SimpleObjectManager { public void removeItem(long itemId) throws SQLException { BaseModel item = getById(itemId); if (item != null) { - dataManager.removeObject(item.getClass(), itemId); + dataManager.removeObject(baseClass, itemId); removeCachedItem(itemId); } refreshUserItems(); -- cgit v1.2.3