From e1deb7ae138a8821fd91cf85321b68c5a73675c7 Mon Sep 17 00:00:00 2001 From: Abyss777 Date: Tue, 14 Mar 2017 12:05:25 +0500 Subject: Implemented helper lookupPreference --- src/org/traccar/database/PermissionsManager.java | 30 ++++++++++++++++++++++++ 1 file changed, 30 insertions(+) (limited to 'src/org/traccar/database') diff --git a/src/org/traccar/database/PermissionsManager.java b/src/org/traccar/database/PermissionsManager.java index 5313859df..0607a2141 100644 --- a/src/org/traccar/database/PermissionsManager.java +++ b/src/org/traccar/database/PermissionsManager.java @@ -25,6 +25,8 @@ import org.traccar.model.Server; import org.traccar.model.User; import org.traccar.model.UserPermission; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; import java.sql.SQLException; import java.util.ArrayList; import java.util.Collection; @@ -395,4 +397,32 @@ public class PermissionsManager { return users.get(usersTokens.get(token)); } + public Object lookupPreference(long userId, String key, Object defaultValue) { + String methodName = "get" + key.substring(0, 1).toUpperCase() + key.substring(1); + Object preference; + Object serverPreference = null; + Object userPreference = null; + try { + Method method = null; + method = User.class.getMethod(methodName, (Class[]) null); + if (method != null) { + userPreference = method.invoke(users.get(userId), (Object[]) null); + } + method = null; + method = Server.class.getMethod(methodName, (Class[]) null); + if (method != null) { + serverPreference = method.invoke(server, (Object[]) null); + } + } catch (NoSuchMethodException | SecurityException | IllegalAccessException + | IllegalArgumentException | InvocationTargetException exception) { + return defaultValue; + } + if (server.getForceSettings()) { + preference = serverPreference != null ? serverPreference : userPreference; + } else { + preference = userPreference != null ? userPreference : serverPreference; + } + return preference != null ? preference : defaultValue; + } + } -- cgit v1.2.3 From 0e2e23f0d1bfef546992c399a7f62aaa4d6f7ad6 Mon Sep 17 00:00:00 2001 From: Abyss777 Date: Tue, 14 Mar 2017 12:46:09 +0500 Subject: Fix indentation --- src/org/traccar/database/PermissionsManager.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/org/traccar/database') diff --git a/src/org/traccar/database/PermissionsManager.java b/src/org/traccar/database/PermissionsManager.java index 0607a2141..5814a1203 100644 --- a/src/org/traccar/database/PermissionsManager.java +++ b/src/org/traccar/database/PermissionsManager.java @@ -415,7 +415,7 @@ public class PermissionsManager { } } catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException exception) { - return defaultValue; + return defaultValue; } if (server.getForceSettings()) { preference = serverPreference != null ? serverPreference : userPreference; -- cgit v1.2.3 From 1b499620810fe6d376a4a72c5ab66b6be2f4af5c Mon Sep 17 00:00:00 2001 From: Abyss777 Date: Tue, 14 Mar 2017 14:21:18 +0500 Subject: Combine some exceptions to ReflectiveOperationException --- src/org/traccar/database/PermissionsManager.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) (limited to 'src/org/traccar/database') diff --git a/src/org/traccar/database/PermissionsManager.java b/src/org/traccar/database/PermissionsManager.java index 5814a1203..e4bd6f5db 100644 --- a/src/org/traccar/database/PermissionsManager.java +++ b/src/org/traccar/database/PermissionsManager.java @@ -25,7 +25,6 @@ import org.traccar.model.Server; import org.traccar.model.User; import org.traccar.model.UserPermission; -import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.sql.SQLException; import java.util.ArrayList; @@ -413,8 +412,7 @@ public class PermissionsManager { if (method != null) { serverPreference = method.invoke(server, (Object[]) null); } - } catch (NoSuchMethodException | SecurityException | IllegalAccessException - | IllegalArgumentException | InvocationTargetException exception) { + } catch (ReflectiveOperationException | SecurityException | IllegalArgumentException exception) { return defaultValue; } if (server.getForceSettings()) { -- cgit v1.2.3