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 ++++++++++++++++++++++++ src/org/traccar/reports/ReportUtils.java | 29 +++-------------------- 2 files changed, 33 insertions(+), 26 deletions(-) (limited to 'src/org/traccar') 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; + } + } diff --git a/src/org/traccar/reports/ReportUtils.java b/src/org/traccar/reports/ReportUtils.java index 9614ecf71..ed97c6857 100644 --- a/src/org/traccar/reports/ReportUtils.java +++ b/src/org/traccar/reports/ReportUtils.java @@ -32,38 +32,15 @@ public final class ReportUtils { } public static String getDistanceUnit(long userId) { - String unit; - String userUnit = Context.getPermissionsManager().getUser(userId).getDistanceUnit(); - String serverUnit = Context.getPermissionsManager().getServer().getDistanceUnit(); - if (Context.getPermissionsManager().getServer().getForceSettings()) { - unit = serverUnit != null ? serverUnit : userUnit; - } else { - unit = userUnit != null ? userUnit : serverUnit; - } - return unit != null ? unit : "km"; + return (String) Context.getPermissionsManager().lookupPreference(userId, "distanceUnit", "km"); } public static String getSpeedUnit(long userId) { - String unit; - String userUnit = Context.getPermissionsManager().getUser(userId).getSpeedUnit(); - String serverUnit = Context.getPermissionsManager().getServer().getSpeedUnit(); - if (Context.getPermissionsManager().getServer().getForceSettings()) { - unit = serverUnit != null ? serverUnit : userUnit; - } else { - unit = userUnit != null ? userUnit : serverUnit; - } - return unit != null ? unit : "kn"; + return (String) Context.getPermissionsManager().lookupPreference(userId, "speedUnit", "kn"); } public static TimeZone getTimezone(long userId) { - String timezone; - String userTimezone = Context.getPermissionsManager().getUser(userId).getTimezone(); - String serverTimezone = Context.getPermissionsManager().getServer().getTimezone(); - if (Context.getPermissionsManager().getServer().getForceSettings()) { - timezone = serverTimezone != null ? serverTimezone : userTimezone; - } else { - timezone = userTimezone != null ? userTimezone : serverTimezone; - } + String timezone = (String) Context.getPermissionsManager().lookupPreference(userId, "timezone", null); return timezone != null ? TimeZone.getTimeZone(timezone) : TimeZone.getDefault(); } -- cgit v1.2.3