From 104281f161f622df8fca0c65a5b9969ceb03c46f Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Tue, 7 Jun 2022 18:04:24 -0700 Subject: Extract user utils from reports --- .../org/traccar/reports/EventsReportProvider.java | 8 ++++-- .../org/traccar/reports/RouteReportProvider.java | 13 +++++++++- .../org/traccar/reports/StopsReportProvider.java | 8 ++++-- .../org/traccar/reports/SummaryReportProvider.java | 19 +++++++++++--- .../org/traccar/reports/TripsReportProvider.java | 8 ++++-- .../org/traccar/reports/common/ReportUtils.java | 30 +++++----------------- 6 files changed, 53 insertions(+), 33 deletions(-) (limited to 'src/main/java/org/traccar/reports') diff --git a/src/main/java/org/traccar/reports/EventsReportProvider.java b/src/main/java/org/traccar/reports/EventsReportProvider.java index 9b4a7df2b..f0c8c31b6 100644 --- a/src/main/java/org/traccar/reports/EventsReportProvider.java +++ b/src/main/java/org/traccar/reports/EventsReportProvider.java @@ -28,6 +28,7 @@ import java.util.Iterator; import org.apache.poi.ss.util.WorkbookUtil; import org.traccar.Context; +import org.traccar.api.security.PermissionsService; import org.traccar.model.Device; import org.traccar.model.Event; import org.traccar.model.Geofence; @@ -42,10 +43,12 @@ import javax.inject.Inject; public class EventsReportProvider { + private final PermissionsService permissionsService; private final Storage storage; @Inject - public EventsReportProvider(Storage storage) { + public EventsReportProvider(PermissionsService permissionsService, Storage storage) { + this.permissionsService = permissionsService; this.storage = storage; } @@ -127,7 +130,8 @@ public class EventsReportProvider { String templatePath = Context.getConfig().getString("report.templatesPath", "templates/export/"); try (InputStream inputStream = new FileInputStream(templatePath + "/events.xlsx")) { - org.jxls.common.Context jxlsContext = ReportUtils.initializeContext(userId); + var jxlsContext = ReportUtils.initializeContext( + permissionsService.getServer(), permissionsService.getUser(userId)); jxlsContext.putVar("devices", devicesEvents); jxlsContext.putVar("sheetNames", sheetNames); jxlsContext.putVar("geofenceNames", geofenceNames); diff --git a/src/main/java/org/traccar/reports/RouteReportProvider.java b/src/main/java/org/traccar/reports/RouteReportProvider.java index 4c4a41405..e20ba6885 100644 --- a/src/main/java/org/traccar/reports/RouteReportProvider.java +++ b/src/main/java/org/traccar/reports/RouteReportProvider.java @@ -26,6 +26,7 @@ import java.util.Date; import org.apache.poi.ss.util.WorkbookUtil; import org.traccar.Context; +import org.traccar.api.security.PermissionsService; import org.traccar.model.Device; import org.traccar.model.Group; import org.traccar.model.Position; @@ -33,8 +34,17 @@ import org.traccar.reports.common.ReportUtils; import org.traccar.reports.model.DeviceReportSection; import org.traccar.storage.StorageException; +import javax.inject.Inject; + public class RouteReportProvider { + private final PermissionsService permissionsService; + + @Inject + public RouteReportProvider(PermissionsService permissionsService) { + this.permissionsService = permissionsService; + } + public Collection getObjects(long userId, Collection deviceIds, Collection groupIds, Date from, Date to) throws StorageException { ReportUtils.checkPeriodLimit(from, to); @@ -72,7 +82,8 @@ public class RouteReportProvider { String templatePath = Context.getConfig().getString("report.templatesPath", "templates/export/"); try (InputStream inputStream = new FileInputStream(templatePath + "/route.xlsx")) { - org.jxls.common.Context jxlsContext = ReportUtils.initializeContext(userId); + var jxlsContext = ReportUtils.initializeContext( + permissionsService.getServer(), permissionsService.getUser(userId)); jxlsContext.putVar("devices", devicesRoutes); jxlsContext.putVar("sheetNames", sheetNames); jxlsContext.putVar("from", from); diff --git a/src/main/java/org/traccar/reports/StopsReportProvider.java b/src/main/java/org/traccar/reports/StopsReportProvider.java index 58dc71d2d..8899dc42f 100644 --- a/src/main/java/org/traccar/reports/StopsReportProvider.java +++ b/src/main/java/org/traccar/reports/StopsReportProvider.java @@ -28,6 +28,7 @@ import java.util.Date; import org.apache.poi.ss.util.WorkbookUtil; import org.traccar.Context; import org.traccar.Main; +import org.traccar.api.security.PermissionsService; import org.traccar.database.DeviceManager; import org.traccar.database.IdentityManager; import org.traccar.model.Device; @@ -42,10 +43,12 @@ import javax.inject.Inject; public class StopsReportProvider { + private final PermissionsService permissionsService; private final Storage storage; @Inject - public StopsReportProvider(Storage storage) { + public StopsReportProvider(PermissionsService permissionsService, Storage storage) { + this.permissionsService = permissionsService; this.storage = storage; } @@ -98,7 +101,8 @@ public class StopsReportProvider { String templatePath = Context.getConfig().getString("report.templatesPath", "templates/export/"); try (InputStream inputStream = new FileInputStream(templatePath + "/stops.xlsx")) { - org.jxls.common.Context jxlsContext = ReportUtils.initializeContext(userId); + var jxlsContext = ReportUtils.initializeContext( + permissionsService.getServer(), permissionsService.getUser(userId)); jxlsContext.putVar("devices", devicesStops); jxlsContext.putVar("sheetNames", sheetNames); jxlsContext.putVar("from", from); diff --git a/src/main/java/org/traccar/reports/SummaryReportProvider.java b/src/main/java/org/traccar/reports/SummaryReportProvider.java index a2306f3d1..c9fac4309 100644 --- a/src/main/java/org/traccar/reports/SummaryReportProvider.java +++ b/src/main/java/org/traccar/reports/SummaryReportProvider.java @@ -27,14 +27,25 @@ import java.util.Date; import org.jxls.util.JxlsHelper; import org.traccar.Context; +import org.traccar.api.security.PermissionsService; import org.traccar.helper.UnitsConverter; +import org.traccar.helper.UserUtil; import org.traccar.model.Position; import org.traccar.reports.common.ReportUtils; import org.traccar.reports.model.SummaryReportItem; import org.traccar.storage.StorageException; +import javax.inject.Inject; + public class SummaryReportProvider { + private final PermissionsService permissionsService; + + @Inject + public SummaryReportProvider(PermissionsService permissionsService) { + this.permissionsService = permissionsService; + } + private SummaryReportItem calculateSummaryResult(long deviceId, Collection positions) { SummaryReportItem result = new SummaryReportItem(); result.setDeviceId(deviceId); @@ -88,8 +99,9 @@ public class SummaryReportProvider { return result; } - private int getDay(long userId, Date date) { - Calendar calendar = Calendar.getInstance(ReportUtils.getTimezone(userId)); + private int getDay(long userId, Date date) throws StorageException { + Calendar calendar = Calendar.getInstance(UserUtil.getTimezone( + permissionsService.getServer(), permissionsService.getUser(userId))); calendar.setTime(date); return calendar.get(Calendar.DAY_OF_MONTH); } @@ -144,7 +156,8 @@ public class SummaryReportProvider { String templatePath = Context.getConfig().getString("report.templatesPath", "templates/export/"); try (InputStream inputStream = new FileInputStream(templatePath + "/summary.xlsx")) { - org.jxls.common.Context jxlsContext = ReportUtils.initializeContext(userId); + var jxlsContext = ReportUtils.initializeContext( + permissionsService.getServer(), permissionsService.getUser(userId)); jxlsContext.putVar("summaries", summaries); jxlsContext.putVar("from", from); jxlsContext.putVar("to", to); diff --git a/src/main/java/org/traccar/reports/TripsReportProvider.java b/src/main/java/org/traccar/reports/TripsReportProvider.java index 5ff31dbe2..bcd79ab25 100644 --- a/src/main/java/org/traccar/reports/TripsReportProvider.java +++ b/src/main/java/org/traccar/reports/TripsReportProvider.java @@ -27,6 +27,7 @@ import java.util.Date; import org.apache.poi.ss.util.WorkbookUtil; import org.traccar.Context; import org.traccar.Main; +import org.traccar.api.security.PermissionsService; import org.traccar.database.DeviceManager; import org.traccar.database.IdentityManager; import org.traccar.model.Device; @@ -41,10 +42,12 @@ import javax.inject.Inject; public class TripsReportProvider { + private final PermissionsService permissionsService; private final Storage storage; @Inject - public TripsReportProvider(Storage storage) { + public TripsReportProvider(PermissionsService permissionsService, Storage storage) { + this.permissionsService = permissionsService; this.storage = storage; } @@ -96,7 +99,8 @@ public class TripsReportProvider { String templatePath = Context.getConfig().getString("report.templatesPath", "templates/export/"); try (InputStream inputStream = new FileInputStream(templatePath + "/trips.xlsx")) { - org.jxls.common.Context jxlsContext = ReportUtils.initializeContext(userId); + var jxlsContext = ReportUtils.initializeContext( + permissionsService.getServer(), permissionsService.getUser(userId)); jxlsContext.putVar("devices", devicesTrips); jxlsContext.putVar("sheetNames", sheetNames); jxlsContext.putVar("from", from); diff --git a/src/main/java/org/traccar/reports/common/ReportUtils.java b/src/main/java/org/traccar/reports/common/ReportUtils.java index 4bcb54899..71c49f65b 100644 --- a/src/main/java/org/traccar/reports/common/ReportUtils.java +++ b/src/main/java/org/traccar/reports/common/ReportUtils.java @@ -31,7 +31,9 @@ import org.traccar.database.DeviceManager; import org.traccar.database.IdentityManager; import org.traccar.handler.events.MotionEventHandler; import org.traccar.helper.UnitsConverter; +import org.traccar.helper.UserUtil; import org.traccar.model.BaseModel; +import org.traccar.model.Server; import org.traccar.model.User; import org.traccar.session.DeviceState; import org.traccar.model.Driver; @@ -58,7 +60,6 @@ import java.util.LinkedHashSet; import java.util.List; import java.util.Locale; import java.util.Map; -import java.util.TimeZone; public final class ReportUtils { @@ -81,23 +82,6 @@ public final class ReportUtils { } } - public static String getDistanceUnit(long userId) { - return (String) Context.getPermissionsManager().lookupAttribute(userId, "distanceUnit", "km"); - } - - public static String getSpeedUnit(long userId) { - return (String) Context.getPermissionsManager().lookupAttribute(userId, "speedUnit", "kn"); - } - - public static String getVolumeUnit(long userId) { - return (String) Context.getPermissionsManager().lookupAttribute(userId, "volumeUnit", "ltr"); - } - - public static TimeZone getTimezone(long userId) { - String timezone = (String) Context.getPermissionsManager().lookupAttribute(userId, "timezone", null); - return timezone != null ? TimeZone.getTimeZone(timezone) : TimeZone.getDefault(); - } - public static Collection getDeviceList(Collection deviceIds, Collection groupIds) { Collection result = new LinkedHashSet<>(deviceIds); for (long groupId : groupIds) { @@ -158,15 +142,15 @@ public final class ReportUtils { return null; } - public static org.jxls.common.Context initializeContext(long userId) { + public static org.jxls.common.Context initializeContext(Server server, User user) { org.jxls.common.Context jxlsContext = PoiTransformer.createInitialContext(); - jxlsContext.putVar("distanceUnit", getDistanceUnit(userId)); - jxlsContext.putVar("speedUnit", getSpeedUnit(userId)); - jxlsContext.putVar("volumeUnit", getVolumeUnit(userId)); + jxlsContext.putVar("distanceUnit", UserUtil.getDistanceUnit(server, user)); + jxlsContext.putVar("speedUnit", UserUtil.getSpeedUnit(server, user)); + jxlsContext.putVar("volumeUnit", UserUtil.getVolumeUnit(server, user)); jxlsContext.putVar("webUrl", Context.getVelocityEngine().getProperty("web.url")); jxlsContext.putVar("dateTool", new DateTool()); jxlsContext.putVar("numberTool", new NumberTool()); - jxlsContext.putVar("timezone", getTimezone(userId)); + jxlsContext.putVar("timezone", UserUtil.getTimezone(server, user)); jxlsContext.putVar("locale", Locale.getDefault()); jxlsContext.putVar("bracketsRegex", "[\\{\\}\"]"); return jxlsContext; -- cgit v1.2.3