From 9ebd2a934a5ca3a61863f839733bff321ba30296 Mon Sep 17 00:00:00 2001 From: Abyss777 Date: Thu, 7 Sep 2017 11:33:22 +0500 Subject: Handle correct and incorrect attribute types --- src/org/traccar/database/DeviceManager.java | 45 ++++++++++------------------- 1 file changed, 15 insertions(+), 30 deletions(-) (limited to 'src/org/traccar/database') diff --git a/src/org/traccar/database/DeviceManager.java b/src/org/traccar/database/DeviceManager.java index a485d6dc6..9bc221aa3 100644 --- a/src/org/traccar/database/DeviceManager.java +++ b/src/org/traccar/database/DeviceManager.java @@ -262,59 +262,44 @@ public class DeviceManager extends BaseObjectManager implements Identity public boolean lookupAttributeBoolean( long deviceId, String attributeName, boolean defaultValue, boolean lookupConfig) { - String result = lookupAttribute(deviceId, attributeName, lookupConfig); - if (result != null) { - return Boolean.parseBoolean(result); - } - return defaultValue; + Object result = lookupAttribute(deviceId, attributeName, lookupConfig); + return result != null ? Boolean.parseBoolean(result.toString()) : defaultValue; } public String lookupAttributeString( long deviceId, String attributeName, String defaultValue, boolean lookupConfig) { - String result = lookupAttribute(deviceId, attributeName, lookupConfig); - if (result != null) { - return result; - } - return defaultValue; + Object result = lookupAttribute(deviceId, attributeName, lookupConfig); + return result != null ? result.toString() : defaultValue; } public int lookupAttributeInteger(long deviceId, String attributeName, int defaultValue, boolean lookupConfig) { - String result = lookupAttribute(deviceId, attributeName, lookupConfig); - if (result != null) { - return Integer.parseInt(result); - } - return defaultValue; + Object result = lookupAttribute(deviceId, attributeName, lookupConfig); + return result != null ? Integer.parseInt(result.toString()) : defaultValue; } public long lookupAttributeLong( long deviceId, String attributeName, long defaultValue, boolean lookupConfig) { - String result = lookupAttribute(deviceId, attributeName, lookupConfig); - if (result != null) { - return Long.parseLong(result); - } - return defaultValue; + Object result = lookupAttribute(deviceId, attributeName, lookupConfig); + return result != null ? Long.parseLong(result.toString()) : defaultValue; } public double lookupAttributeDouble( long deviceId, String attributeName, double defaultValue, boolean lookupConfig) { - String result = lookupAttribute(deviceId, attributeName, lookupConfig); - if (result != null) { - return Double.parseDouble(result); - } - return defaultValue; + Object result = lookupAttribute(deviceId, attributeName, lookupConfig); + return result != null ? Double.parseDouble(result.toString()) : defaultValue; } - private String lookupAttribute(long deviceId, String attributeName, boolean lookupConfig) { - String result = null; + private Object lookupAttribute(long deviceId, String attributeName, boolean lookupConfig) { + Object result = null; Device device = getById(deviceId); if (device != null) { - result = device.getString(attributeName); + result = device.getAttributes().get(attributeName); if (result == null && lookupGroupsAttribute) { long groupId = device.getGroupId(); while (groupId != 0) { Group group = Context.getGroupsManager().getById(groupId); if (group != null) { - result = group.getString(attributeName); + result = group.getAttributes().get(attributeName); if (result != null) { break; } @@ -329,7 +314,7 @@ public class DeviceManager extends BaseObjectManager implements Identity result = Context.getConfig().getString(attributeName); } else { Server server = Context.getPermissionsManager().getServer(); - result = server.getString(attributeName); + result = server.getAttributes().get(attributeName); } } } -- cgit v1.2.3 From ee28d68243892623df7f718d50cd3ea5791f35ee Mon Sep 17 00:00:00 2001 From: Abyss777 Date: Thu, 7 Sep 2017 15:31:28 +0500 Subject: Optimize values parsing --- src/org/traccar/database/DeviceManager.java | 22 +++++++++++++++++----- .../traccar/notification/PropertiesProvider.java | 9 ++++++--- 2 files changed, 23 insertions(+), 8 deletions(-) (limited to 'src/org/traccar/database') diff --git a/src/org/traccar/database/DeviceManager.java b/src/org/traccar/database/DeviceManager.java index 9bc221aa3..2157e738d 100644 --- a/src/org/traccar/database/DeviceManager.java +++ b/src/org/traccar/database/DeviceManager.java @@ -263,30 +263,42 @@ public class DeviceManager extends BaseObjectManager implements Identity public boolean lookupAttributeBoolean( long deviceId, String attributeName, boolean defaultValue, boolean lookupConfig) { Object result = lookupAttribute(deviceId, attributeName, lookupConfig); - return result != null ? Boolean.parseBoolean(result.toString()) : defaultValue; + if (result != null) { + return result instanceof String ? Boolean.parseBoolean((String) result) : (Boolean) result; + } + return defaultValue; } public String lookupAttributeString( long deviceId, String attributeName, String defaultValue, boolean lookupConfig) { Object result = lookupAttribute(deviceId, attributeName, lookupConfig); - return result != null ? result.toString() : defaultValue; + return result != null ? (String) result : defaultValue; } public int lookupAttributeInteger(long deviceId, String attributeName, int defaultValue, boolean lookupConfig) { Object result = lookupAttribute(deviceId, attributeName, lookupConfig); - return result != null ? Integer.parseInt(result.toString()) : defaultValue; + if (result != null) { + return result instanceof String ? Integer.parseInt((String) result) : ((Number) result).intValue(); + } + return defaultValue; } public long lookupAttributeLong( long deviceId, String attributeName, long defaultValue, boolean lookupConfig) { Object result = lookupAttribute(deviceId, attributeName, lookupConfig); - return result != null ? Long.parseLong(result.toString()) : defaultValue; + if (result != null) { + return result instanceof String ? Long.parseLong((String) result) : ((Number) result).longValue(); + } + return defaultValue; } public double lookupAttributeDouble( long deviceId, String attributeName, double defaultValue, boolean lookupConfig) { Object result = lookupAttribute(deviceId, attributeName, lookupConfig); - return result != null ? Double.parseDouble(result.toString()) : defaultValue; + if (result != null) { + return result instanceof String ? Double.parseDouble((String) result) : ((Number) result).doubleValue(); + } + return defaultValue; } private Object lookupAttribute(long deviceId, String attributeName, boolean lookupConfig) { diff --git a/src/org/traccar/notification/PropertiesProvider.java b/src/org/traccar/notification/PropertiesProvider.java index 1a4104d2b..c5ba688e8 100644 --- a/src/org/traccar/notification/PropertiesProvider.java +++ b/src/org/traccar/notification/PropertiesProvider.java @@ -51,10 +51,13 @@ public class PropertiesProvider { public int getInteger(String key, int defaultValue) { if (config != null) { return config.getInteger(key, defaultValue); - } else if (extendedModel.getAttributes().containsKey(key)) { - return Integer.parseInt(extendedModel.getAttributes().get(key).toString()); } else { - return defaultValue; + Object result = extendedModel.getAttributes().get(key); + if (result != null) { + return result instanceof String ? Integer.parseInt((String) result) : (Integer) result; + } else { + return defaultValue; + } } } -- cgit v1.2.3