diff options
author | Anton Tananaev <anton.tananaev@gmail.com> | 2017-03-14 22:24:17 +1300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-03-14 22:24:17 +1300 |
commit | e85f2a54420aa1299ea70b0bcf4a0c870620a31a (patch) | |
tree | 6a0ce5ded2fd24e5cf98f5e5f04095a03593105d | |
parent | b3e2f211c3a3c64c8fa704ba1942970bb97f8eae (diff) | |
parent | 1b499620810fe6d376a4a72c5ab66b6be2f4af5c (diff) | |
download | trackermap-server-e85f2a54420aa1299ea70b0bcf4a0c870620a31a.tar.gz trackermap-server-e85f2a54420aa1299ea70b0bcf4a0c870620a31a.tar.bz2 trackermap-server-e85f2a54420aa1299ea70b0bcf4a0c870620a31a.zip |
Merge pull request #3001 from Abyss777/timezone
Timezone field to User and Server model
34 files changed, 106 insertions, 39 deletions
@@ -144,6 +144,11 @@ <version>1.7</version> </dependency> <dependency> + <groupId>org.apache.velocity</groupId> + <artifactId>velocity-tools</artifactId> + <version>2.0</version> + </dependency> + <dependency> <groupId>org.mnode.ical4j</groupId> <artifactId>ical4j</artifactId> <version>2.0.0</version> diff --git a/schema/changelog-3.11.xml b/schema/changelog-3.11.xml index c169e1146..1fdaa3f17 100644 --- a/schema/changelog-3.11.xml +++ b/schema/changelog-3.11.xml @@ -11,7 +11,7 @@ <addColumn tableName="users"> <column name="phone" type="VARCHAR(128)" /> </addColumn> - + <addColumn tableName="notifications"> <column name="sms" type="BOOLEAN" defaultValueBoolean="false" /> </addColumn> @@ -20,6 +20,14 @@ <column name="devicereadonly" type="BOOLEAN" defaultValueBoolean="false" /> </addColumn> + <addColumn tableName="server"> + <column name="timezone" type="VARCHAR(128)" /> + </addColumn> + + <addColumn tableName="users"> + <column name="timezone" type="VARCHAR(128)" /> + </addColumn> + </changeSet> </databaseChangeLog> diff --git a/setup/default.xml b/setup/default.xml index 9fe49edbe..90bdc1973 100644 --- a/setup/default.xml +++ b/setup/default.xml @@ -51,6 +51,7 @@ longitude = :longitude, zoom = :zoom, twelveHourFormat = :twelveHourFormat, + timezone = :timezone, attributes = :attributes WHERE id = :id </entry> @@ -65,8 +66,8 @@ </entry> <entry key='database.insertUser'> - INSERT INTO users (name, email, phone, hashedPassword, salt, readonly, admin, map, distanceUnit, speedUnit, latitude, longitude, zoom, twelveHourFormat, coordinateFormat, disabled, expirationTime, deviceLimit, userLimit, deviceReadonly, token, attributes) - VALUES (:name, :email, :phone, :hashedPassword, :salt, :readonly, :admin, :map, :distanceUnit, :speedUnit, :latitude, :longitude, :zoom, :twelveHourFormat, :coordinateFormat, :disabled, :expirationTime, :deviceLimit, :userLimit, :deviceReadonly, :token, :attributes) + INSERT INTO users (name, email, phone, hashedPassword, salt, readonly, admin, map, distanceUnit, speedUnit, latitude, longitude, zoom, twelveHourFormat, coordinateFormat, disabled, expirationTime, deviceLimit, userLimit, deviceReadonly, token, timezone, attributes) + VALUES (:name, :email, :phone, :hashedPassword, :salt, :readonly, :admin, :map, :distanceUnit, :speedUnit, :latitude, :longitude, :zoom, :twelveHourFormat, :coordinateFormat, :disabled, :expirationTime, :deviceLimit, :userLimit, :deviceReadonly, :token, :timezone, :attributes) </entry> <entry key='database.updateUser'> @@ -90,6 +91,7 @@ userLimit = :userLimit, deviceReadonly = :deviceReadonly, token = :token, + timezone = :timezone, attributes = :attributes WHERE id = :id </entry> diff --git a/src/org/traccar/database/PermissionsManager.java b/src/org/traccar/database/PermissionsManager.java index 5313859df..e4bd6f5db 100644 --- a/src/org/traccar/database/PermissionsManager.java +++ b/src/org/traccar/database/PermissionsManager.java @@ -25,6 +25,7 @@ import org.traccar.model.Server; import org.traccar.model.User; import org.traccar.model.UserPermission; +import java.lang.reflect.Method; import java.sql.SQLException; import java.util.ArrayList; import java.util.Collection; @@ -395,4 +396,31 @@ 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 (ReflectiveOperationException | SecurityException | IllegalArgumentException 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/model/Server.java b/src/org/traccar/model/Server.java index b588a4de0..4ded65204 100644 --- a/src/org/traccar/model/Server.java +++ b/src/org/traccar/model/Server.java @@ -15,6 +15,8 @@ */ package org.traccar.model; +import java.util.TimeZone; + import org.traccar.helper.Log; public class Server extends Extensible { @@ -166,4 +168,13 @@ public class Server extends Extensible { this.coordinateFormat = coordinateFormat; } + private String timezone; + + public void setTimezone(String timezone) { + this.timezone = timezone != null ? TimeZone.getTimeZone(timezone).getID() : null; + } + + public String getTimezone() { + return timezone; + } } diff --git a/src/org/traccar/model/User.java b/src/org/traccar/model/User.java index 274f2b2a2..366ced503 100644 --- a/src/org/traccar/model/User.java +++ b/src/org/traccar/model/User.java @@ -19,6 +19,7 @@ import com.fasterxml.jackson.annotation.JsonIgnore; import org.traccar.helper.Hashing; import java.util.Date; +import java.util.TimeZone; public class User extends Extensible { @@ -265,4 +266,13 @@ public class User extends Extensible { return Hashing.validatePassword(password, hashedPassword, salt); } + private String timezone; + + public void setTimezone(String timezone) { + this.timezone = timezone != null ? TimeZone.getTimeZone(timezone).getID() : null; + } + + public String getTimezone() { + return timezone; + } } diff --git a/src/org/traccar/notification/NotificationFormatter.java b/src/org/traccar/notification/NotificationFormatter.java index cec238548..eae2681c9 100644 --- a/src/org/traccar/notification/NotificationFormatter.java +++ b/src/org/traccar/notification/NotificationFormatter.java @@ -18,10 +18,12 @@ package org.traccar.notification; import java.io.StringWriter; import java.nio.charset.StandardCharsets; +import java.util.Locale; import org.apache.velocity.Template; import org.apache.velocity.VelocityContext; import org.apache.velocity.exception.ResourceNotFoundException; +import org.apache.velocity.tools.generic.DateTool; import org.traccar.Context; import org.traccar.helper.Log; import org.traccar.model.Device; @@ -48,6 +50,9 @@ public final class NotificationFormatter { velocityContext.put("geofence", Context.getGeofenceManager().getGeofence(event.getGeofenceId())); } velocityContext.put("webUrl", Context.getVelocityEngine().getProperty("web.url")); + velocityContext.put("dateTool", new DateTool()); + velocityContext.put("timezone", ReportUtils.getTimezone(userId)); + velocityContext.put("locale", Locale.getDefault()); return velocityContext; } diff --git a/src/org/traccar/reports/ReportUtils.java b/src/org/traccar/reports/ReportUtils.java index 1402e10d4..ed97c6857 100644 --- a/src/org/traccar/reports/ReportUtils.java +++ b/src/org/traccar/reports/ReportUtils.java @@ -24,6 +24,7 @@ import java.math.BigDecimal; import java.math.RoundingMode; import java.util.ArrayList; import java.util.Collection; +import java.util.TimeZone; public final class ReportUtils { @@ -31,19 +32,16 @@ public final class ReportUtils { } public static String getDistanceUnit(long userId) { - String unit = Context.getPermissionsManager().getUser(userId).getDistanceUnit(); - if (unit == null) { - unit = Context.getPermissionsManager().getServer().getDistanceUnit(); - } - return unit != null ? unit : "km"; + return (String) Context.getPermissionsManager().lookupPreference(userId, "distanceUnit", "km"); } public static String getSpeedUnit(long userId) { - String unit = Context.getPermissionsManager().getUser(userId).getSpeedUnit(); - if (unit == null) { - unit = Context.getPermissionsManager().getServer().getSpeedUnit(); - } - return unit != null ? unit : "kn"; + return (String) Context.getPermissionsManager().lookupPreference(userId, "speedUnit", "kn"); + } + + public static TimeZone getTimezone(long userId) { + String timezone = (String) Context.getPermissionsManager().lookupPreference(userId, "timezone", null); + return timezone != null ? TimeZone.getTimeZone(timezone) : TimeZone.getDefault(); } public static Collection<Long> getDeviceList(Collection<Long> deviceIds, Collection<Long> groupIds) { diff --git a/templates/mail/alarm.vm b/templates/mail/alarm.vm index b64b2126a..8f1164291 100644 --- a/templates/mail/alarm.vm +++ b/templates/mail/alarm.vm @@ -4,7 +4,7 @@ <body> Device: $device.name<br> Alarm: $position.getString("alarm")<br> -Time: $event.serverTime<br> +Time: $dateTool.format("YYYY-MM-dd HH:mm:ss", $event.serverTime, $locale, $timezone)<br> Point: <a href="$webUrl?eventId=$event.id">#{if}($position.address)$position.address#{else}$position.latitude°, $position.longitude°#{end}</a><br> </body> </html> diff --git a/templates/mail/commandResult.vm b/templates/mail/commandResult.vm index 4c330584d..5b6d8ef3e 100644 --- a/templates/mail/commandResult.vm +++ b/templates/mail/commandResult.vm @@ -4,7 +4,7 @@ <body> Device: $device.name<br> Result: $position.getString("result")<br> -Time: $event.serverTime<br> +Time: $dateTool.format("YYYY-MM-dd HH:mm:ss", $event.serverTime, $locale, $timezone)<br> Link: <a href="$webUrl?eventId=$event.id">$webUrl?eventId=$event.id</a> </body> </html> diff --git a/templates/mail/deviceMoving.vm b/templates/mail/deviceMoving.vm index 9ad2d8bdc..6e98af1de 100644 --- a/templates/mail/deviceMoving.vm +++ b/templates/mail/deviceMoving.vm @@ -4,7 +4,7 @@ <body> Device: $device.name<br> Moving<br> -Time: $event.serverTime<br> +Time: $dateTool.format("YYYY-MM-dd HH:mm:ss", $event.serverTime, $locale, $timezone)<br> Point: <a href="$webUrl?eventId=$event.id">#{if}($position.address)$position.address#{else}$position.latitude°, $position.longitude°#{end}</a><br> </body> </html> diff --git a/templates/mail/deviceOffline.vm b/templates/mail/deviceOffline.vm index ee7e96f05..1f6d02fb2 100644 --- a/templates/mail/deviceOffline.vm +++ b/templates/mail/deviceOffline.vm @@ -4,7 +4,7 @@ <body> Device: $device.name<br> Offline<br> -Time: $event.serverTime<br> +Time: $dateTool.format("YYYY-MM-dd HH:mm:ss", $event.serverTime, $locale, $timezone)<br> Link: <a href="$webUrl?eventId=$event.id">$webUrl?eventId=$event.id</a> </body> </html> diff --git a/templates/mail/deviceOnline.vm b/templates/mail/deviceOnline.vm index 379b5cc80..9211eff11 100644 --- a/templates/mail/deviceOnline.vm +++ b/templates/mail/deviceOnline.vm @@ -4,7 +4,7 @@ <body> Device: $device.name<br> Online<br> -Time: $event.serverTime<br> +Time: $dateTool.format("YYYY-MM-dd HH:mm:ss", $event.serverTime, $locale, $timezone)<br> Link: <a href="$webUrl?eventId=$event.id">$webUrl?eventId=$event.id</a> </body> </html> diff --git a/templates/mail/deviceOverspeed.vm b/templates/mail/deviceOverspeed.vm index a8a58a181..3b203ddcd 100644 --- a/templates/mail/deviceOverspeed.vm +++ b/templates/mail/deviceOverspeed.vm @@ -11,7 +11,7 @@ <body> Device: $device.name<br> Exceeds the speed: $speedString<br> -Time: $event.serverTime<br> +Time: $dateTool.format("YYYY-MM-dd HH:mm:ss", $event.serverTime, $locale, $timezone)<br> Point: <a href="$webUrl?eventId=$event.id">#{if}($position.address)$position.address#{else}$position.latitude°, $position.longitude°#{end}</a><br> </body> </html> diff --git a/templates/mail/deviceStopped.vm b/templates/mail/deviceStopped.vm index 273e1c988..2ec0f8db9 100644 --- a/templates/mail/deviceStopped.vm +++ b/templates/mail/deviceStopped.vm @@ -4,7 +4,7 @@ <body> Device: $device.name<br> Stopped<br> -Time: $event.serverTime<br> +Time: $dateTool.format("YYYY-MM-dd HH:mm:ss", $event.serverTime, $locale, $timezone)<br> Point: <a href="$webUrl?eventId=$event.id">#{if}($position.address)$position.address#{else}$position.latitude°, $position.longitude°#{end}</a><br> </body> </html> diff --git a/templates/mail/deviceUnknown.vm b/templates/mail/deviceUnknown.vm index 40b8fbfa7..34fa01a8a 100644 --- a/templates/mail/deviceUnknown.vm +++ b/templates/mail/deviceUnknown.vm @@ -4,7 +4,7 @@ <body> Device: $device.name<br> Status is unknown<br> -Time: $event.serverTime<br> +Time: $dateTool.format("YYYY-MM-dd HH:mm:ss", $event.serverTime, $locale, $timezone)<br> Link: <a href="$webUrl?eventId=$event.id">$webUrl?eventId=$event.id</a> </body> </html> diff --git a/templates/mail/geofenceEnter.vm b/templates/mail/geofenceEnter.vm index 75d16617f..e83a0de62 100644 --- a/templates/mail/geofenceEnter.vm +++ b/templates/mail/geofenceEnter.vm @@ -4,7 +4,7 @@ <body> Device: $device.name<br> Has entered geofence: $geofence.name<br> -Time: $event.serverTime<br> +Time: $dateTool.format("YYYY-MM-dd HH:mm:ss", $event.serverTime, $locale, $timezone)<br> Point: <a href="$webUrl?eventId=$event.id">#{if}($position.address)$position.address#{else}$position.latitude°, $position.longitude°#{end}</a><br> </body> </html> diff --git a/templates/mail/geofenceExit.vm b/templates/mail/geofenceExit.vm index 6d4d1639d..3557f6eb2 100644 --- a/templates/mail/geofenceExit.vm +++ b/templates/mail/geofenceExit.vm @@ -4,7 +4,7 @@ <body> Device: $device.name<br> Has exited geofence: $geofence.name<br> -Time: $event.serverTime<br> +Time: $dateTool.format("YYYY-MM-dd HH:mm:ss", $event.serverTime, $locale, $timezone)<br> Point: <a href="$webUrl?eventId=$event.id">#{if}($position.address)$position.address#{else}$position.latitude°, $position.longitude°#{end}</a><br> </body> </html> diff --git a/templates/mail/ignitionOff.vm b/templates/mail/ignitionOff.vm index 3a3212b12..0281eef02 100644 --- a/templates/mail/ignitionOff.vm +++ b/templates/mail/ignitionOff.vm @@ -4,7 +4,7 @@ <body> Device: $device.name<br> Ignition OFF<br> -Time: $event.serverTime<br> +Time: $dateTool.format("YYYY-MM-dd HH:mm:ss", $event.serverTime, $locale, $timezone)<br> Point: <a href="$webUrl?eventId=$event.id">#{if}($position.address)$position.address#{else}$position.latitude°, $position.longitude°#{end}</a><br> </body> </html> diff --git a/templates/mail/ignitionOn.vm b/templates/mail/ignitionOn.vm index bbe6c40f6..27135a7f0 100644 --- a/templates/mail/ignitionOn.vm +++ b/templates/mail/ignitionOn.vm @@ -4,7 +4,7 @@ <body> Device: $device.name<br> Ignition ON<br> -Time: $event.serverTime<br> +Time: $dateTool.format("YYYY-MM-dd HH:mm:ss", $event.serverTime, $locale, $timezone)<br> Point: <a href="$webUrl?eventId=$event.id">#{if}($position.address)$position.address#{else}$position.latitude°, $position.longitude°#{end}</a><br> </body> </html> diff --git a/templates/mail/maintenance.vm b/templates/mail/maintenance.vm index c94c35cc4..7f69b6c0d 100644 --- a/templates/mail/maintenance.vm +++ b/templates/mail/maintenance.vm @@ -4,7 +4,7 @@ <body> Device: $device.name<br> Maintenance is required<br> -Time: $event.serverTime<br> +Time: $dateTool.format("YYYY-MM-dd HH:mm:ss", $event.serverTime, $locale, $timezone)<br> Point: <a href="$webUrl?eventId=$event.id">#{if}($position.address)$position.address#{else}$position.latitude°, $position.longitude°#{end}</a><br> </body> </html> diff --git a/templates/sms/alarm.vm b/templates/sms/alarm.vm index e35a573b3..389341cf1 100644 --- a/templates/sms/alarm.vm +++ b/templates/sms/alarm.vm @@ -1 +1 @@ -$device.name alarm: $position.getString("alarm") at $event.serverTime +$device.name alarm: $position.getString("alarm") at $dateTool.format("YYYY-MM-dd HH:mm:ss", $event.serverTime, $locale, $timezone) diff --git a/templates/sms/commandResult.vm b/templates/sms/commandResult.vm index 38fddcf25..4a327d850 100644 --- a/templates/sms/commandResult.vm +++ b/templates/sms/commandResult.vm @@ -1 +1 @@ -$device.name command result received: $position.getString("result") at $event.serverTime +$device.name command result received: $position.getString("result") at $dateTool.format("YYYY-MM-dd HH:mm:ss", $event.serverTime, $locale, $timezone) diff --git a/templates/sms/deviceMoving.vm b/templates/sms/deviceMoving.vm index febae6331..aaa72ab0d 100644 --- a/templates/sms/deviceMoving.vm +++ b/templates/sms/deviceMoving.vm @@ -1 +1 @@ -$device.name moving at $event.serverTime +$device.name moving at $dateTool.format("YYYY-MM-dd HH:mm:ss", $event.serverTime, $locale, $timezone) diff --git a/templates/sms/deviceOffline.vm b/templates/sms/deviceOffline.vm index 490502946..8c4a02335 100644 --- a/templates/sms/deviceOffline.vm +++ b/templates/sms/deviceOffline.vm @@ -1 +1 @@ -$device.name offline at $event.serverTime +$device.name offline at $dateTool.format("YYYY-MM-dd HH:mm:ss", $event.serverTime, $locale, $timezone) diff --git a/templates/sms/deviceOnline.vm b/templates/sms/deviceOnline.vm index e8bc0a5bf..62854feb0 100644 --- a/templates/sms/deviceOnline.vm +++ b/templates/sms/deviceOnline.vm @@ -1 +1 @@ -$device.name online at $event.serverTime +$device.name online at $dateTool.format("YYYY-MM-dd HH:mm:ss", $event.serverTime, $locale, $timezone) diff --git a/templates/sms/deviceOverspeed.vm b/templates/sms/deviceOverspeed.vm index 63f967bc3..3c9eae628 100644 --- a/templates/sms/deviceOverspeed.vm +++ b/templates/sms/deviceOverspeed.vm @@ -5,4 +5,4 @@ #else #set($speedString = "$position.speed kn") #end -$device.name exceeds the speed $speedString at $event.serverTime +$device.name exceeds the speed $speedString at $dateTool.format("YYYY-MM-dd HH:mm:ss", $event.serverTime, $locale, $timezone) diff --git a/templates/sms/deviceStopped.vm b/templates/sms/deviceStopped.vm index cf9118cac..c2437b5f6 100644 --- a/templates/sms/deviceStopped.vm +++ b/templates/sms/deviceStopped.vm @@ -1 +1 @@ -$device.name stopped at $event.serverTime +$device.name stopped at $dateTool.format("YYYY-MM-dd HH:mm:ss", $event.serverTime, $locale, $timezone) diff --git a/templates/sms/deviceUnknown.vm b/templates/sms/deviceUnknown.vm index 533219799..07f39f3a9 100644 --- a/templates/sms/deviceUnknown.vm +++ b/templates/sms/deviceUnknown.vm @@ -1 +1 @@ -$device.name status is unknown at $event.serverTime +$device.name status is unknown at $dateTool.format("YYYY-MM-dd HH:mm:ss", $event.serverTime, $locale, $timezone) diff --git a/templates/sms/geofenceEnter.vm b/templates/sms/geofenceEnter.vm index 5a216f361..9dd861931 100644 --- a/templates/sms/geofenceEnter.vm +++ b/templates/sms/geofenceEnter.vm @@ -1 +1 @@ -$device.name has entered geofence $geofence.name at $event.serverTime +$device.name has entered geofence $dateTool.format("YYYY-MM-dd HH:mm:ss", $event.serverTime, $locale, $timezone) diff --git a/templates/sms/geofenceExit.vm b/templates/sms/geofenceExit.vm index fd8a8409d..8553458f7 100644 --- a/templates/sms/geofenceExit.vm +++ b/templates/sms/geofenceExit.vm @@ -1 +1 @@ -$device.name has exited geofence $geofence.name at $event.serverTime +$device.name has exited geofence $geofence.name at $dateTool.format("YYYY-MM-dd HH:mm:ss", $event.serverTime, $locale, $timezone) diff --git a/templates/sms/ignitionOff.vm b/templates/sms/ignitionOff.vm index 6f90eb552..ddf860413 100644 --- a/templates/sms/ignitionOff.vm +++ b/templates/sms/ignitionOff.vm @@ -1 +1 @@ -$device.name ignition OFF at $event.serverTime +$device.name ignition OFF at $dateTool.format("YYYY-MM-dd HH:mm:ss", $event.serverTime, $locale, $timezone) diff --git a/templates/sms/ignitionOn.vm b/templates/sms/ignitionOn.vm index 9c0d79888..00b365be9 100644 --- a/templates/sms/ignitionOn.vm +++ b/templates/sms/ignitionOn.vm @@ -1 +1 @@ -$device.name ignition ON at $event.serverTime +$device.name ignition ON at $dateTool.format("YYYY-MM-dd HH:mm:ss", $event.serverTime, $locale, $timezone) diff --git a/templates/sms/maintenance.vm b/templates/sms/maintenance.vm index f2e6940f6..554b74400 100644 --- a/templates/sms/maintenance.vm +++ b/templates/sms/maintenance.vm @@ -1 +1 @@ -$device.name maintenance is required at $event.serverTime +$device.name maintenance is required at $dateTool.format("YYYY-MM-dd HH:mm:ss", $event.serverTime, $locale, $timezone) |