From 24140619789c88d96364673240b172bbd2ae2c82 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Sat, 18 Jun 2022 12:22:21 -0700 Subject: Refactor reports dependencies --- src/main/java/org/traccar/Context.java | 1 - .../org/traccar/database/PermissionsManager.java | 4 -- .../org/traccar/reports/EventsReportProvider.java | 11 ++--- .../org/traccar/reports/RouteReportProvider.java | 11 ++--- .../org/traccar/reports/StopsReportProvider.java | 11 ++--- .../org/traccar/reports/SummaryReportProvider.java | 7 ++-- .../org/traccar/reports/TripsReportProvider.java | 11 ++--- .../org/traccar/reports/common/ReportUtils.java | 49 +++++++++++++++------- 8 files changed, 54 insertions(+), 51 deletions(-) (limited to 'src/main/java/org') diff --git a/src/main/java/org/traccar/Context.java b/src/main/java/org/traccar/Context.java index dba0357c9..5f555858d 100644 --- a/src/main/java/org/traccar/Context.java +++ b/src/main/java/org/traccar/Context.java @@ -23,7 +23,6 @@ import org.traccar.database.PermissionsManager; import org.traccar.helper.Log; import org.traccar.model.BaseModel; import org.traccar.model.Device; -import org.traccar.storage.Storage; public final class Context { diff --git a/src/main/java/org/traccar/database/PermissionsManager.java b/src/main/java/org/traccar/database/PermissionsManager.java index 0ff20775a..c52c72c4d 100644 --- a/src/main/java/org/traccar/database/PermissionsManager.java +++ b/src/main/java/org/traccar/database/PermissionsManager.java @@ -22,11 +22,7 @@ import org.traccar.model.Device; import org.traccar.model.Group; import org.traccar.model.Permission; import org.traccar.model.User; -import org.traccar.storage.Storage; import org.traccar.storage.StorageException; -import org.traccar.storage.query.Columns; -import org.traccar.storage.query.Condition; -import org.traccar.storage.query.Request; import java.util.HashMap; import java.util.HashSet; diff --git a/src/main/java/org/traccar/reports/EventsReportProvider.java b/src/main/java/org/traccar/reports/EventsReportProvider.java index 69d95d1be..878c0265d 100644 --- a/src/main/java/org/traccar/reports/EventsReportProvider.java +++ b/src/main/java/org/traccar/reports/EventsReportProvider.java @@ -73,11 +73,10 @@ public class EventsReportProvider { long userId, Collection deviceIds, Collection groupIds, Collection types, Date from, Date to) throws StorageException { reportUtils.checkPeriodLimit(from, to); - reportUtils.checkPermissions(userId, deviceIds, groupIds); ArrayList result = new ArrayList<>(); - for (long deviceId: reportUtils.getDeviceList(deviceIds, groupIds)) { - Collection events = getEvents(deviceId, from, to); + for (Device device: reportUtils.getAccessibleDevices(userId, deviceIds, groupIds)) { + Collection events = getEvents(device.getId(), from, to); boolean all = types.isEmpty() || types.contains(Event.ALL_EVENTS); for (Event event : events) { if (all || types.contains(event.getType())) { @@ -98,14 +97,13 @@ public class EventsReportProvider { OutputStream outputStream, long userId, Collection deviceIds, Collection groupIds, Collection types, Date from, Date to) throws StorageException, IOException { reportUtils.checkPeriodLimit(from, to); - reportUtils.checkPermissions(userId, deviceIds, groupIds); ArrayList devicesEvents = new ArrayList<>(); ArrayList sheetNames = new ArrayList<>(); HashMap geofenceNames = new HashMap<>(); HashMap maintenanceNames = new HashMap<>(); - for (long deviceId: reportUtils.getDeviceList(deviceIds, groupIds)) { - Collection events = getEvents(deviceId, from, to); + for (Device device: reportUtils.getAccessibleDevices(userId, deviceIds, groupIds)) { + Collection events = getEvents(device.getId(), from, to); boolean all = types.isEmpty() || types.contains(Event.ALL_EVENTS); for (Iterator iterator = events.iterator(); iterator.hasNext();) { Event event = iterator.next(); @@ -132,7 +130,6 @@ public class EventsReportProvider { } } DeviceReportSection deviceEvents = new DeviceReportSection(); - Device device = reportUtils.getDevice(deviceId); deviceEvents.setDeviceName(device.getName()); sheetNames.add(WorkbookUtil.createSafeSheetName(deviceEvents.getDeviceName())); if (device.getGroupId() > 0) { diff --git a/src/main/java/org/traccar/reports/RouteReportProvider.java b/src/main/java/org/traccar/reports/RouteReportProvider.java index 2364cc0f3..0f618822e 100644 --- a/src/main/java/org/traccar/reports/RouteReportProvider.java +++ b/src/main/java/org/traccar/reports/RouteReportProvider.java @@ -58,11 +58,10 @@ public class RouteReportProvider { public Collection getObjects(long userId, Collection deviceIds, Collection groupIds, Date from, Date to) throws StorageException { reportUtils.checkPeriodLimit(from, to); - reportUtils.checkPermissions(userId, deviceIds, groupIds); ArrayList result = new ArrayList<>(); - for (long deviceId: reportUtils.getDeviceList(deviceIds, groupIds)) { - result.addAll(PositionUtil.getPositions(storage, deviceId, from, to)); + for (Device device: reportUtils.getAccessibleDevices(userId, deviceIds, groupIds)) { + result.addAll(PositionUtil.getPositions(storage, device.getId(), from, to)); } return result; } @@ -71,14 +70,12 @@ public class RouteReportProvider { long userId, Collection deviceIds, Collection groupIds, Date from, Date to) throws StorageException, IOException { reportUtils.checkPeriodLimit(from, to); - reportUtils.checkPermissions(userId, deviceIds, groupIds); ArrayList devicesRoutes = new ArrayList<>(); ArrayList sheetNames = new ArrayList<>(); - for (long deviceId: reportUtils.getDeviceList(deviceIds, groupIds)) { - var positions = PositionUtil.getPositions(storage, deviceId, from, to); + for (Device device: reportUtils.getAccessibleDevices(userId, deviceIds, groupIds)) { + var positions = PositionUtil.getPositions(storage, device.getId(), from, to); DeviceReportSection deviceRoutes = new DeviceReportSection(); - Device device = reportUtils.getDevice(deviceId); deviceRoutes.setDeviceName(device.getName()); sheetNames.add(WorkbookUtil.createSafeSheetName(deviceRoutes.getDeviceName())); if (device.getGroupId() > 0) { diff --git a/src/main/java/org/traccar/reports/StopsReportProvider.java b/src/main/java/org/traccar/reports/StopsReportProvider.java index 3b1f3c1fe..6e4cd74d6 100644 --- a/src/main/java/org/traccar/reports/StopsReportProvider.java +++ b/src/main/java/org/traccar/reports/StopsReportProvider.java @@ -65,11 +65,10 @@ public class StopsReportProvider { long userId, Collection deviceIds, Collection groupIds, Date from, Date to) throws StorageException { reportUtils.checkPeriodLimit(from, to); - reportUtils.checkPermissions(userId, deviceIds, groupIds); ArrayList result = new ArrayList<>(); - for (long deviceId: reportUtils.getDeviceList(deviceIds, groupIds)) { - result.addAll(detectStops(deviceId, from, to)); + for (Device device: reportUtils.getAccessibleDevices(userId, deviceIds, groupIds)) { + result.addAll(detectStops(device.getId(), from, to)); } return result; } @@ -78,14 +77,12 @@ public class StopsReportProvider { OutputStream outputStream, long userId, Collection deviceIds, Collection groupIds, Date from, Date to) throws StorageException, IOException { reportUtils.checkPeriodLimit(from, to); - reportUtils.checkPermissions(userId, deviceIds, groupIds); ArrayList devicesStops = new ArrayList<>(); ArrayList sheetNames = new ArrayList<>(); - for (long deviceId: reportUtils.getDeviceList(deviceIds, groupIds)) { - Collection stops = detectStops(deviceId, from, to); + for (Device device: reportUtils.getAccessibleDevices(userId, deviceIds, groupIds)) { + Collection stops = detectStops(device.getId(), from, to); DeviceReportSection deviceStops = new DeviceReportSection(); - Device device = reportUtils.getDevice(deviceId); deviceStops.setDeviceName(device.getName()); sheetNames.add(WorkbookUtil.createSafeSheetName(deviceStops.getDeviceName())); if (device.getGroupId() > 0) { diff --git a/src/main/java/org/traccar/reports/SummaryReportProvider.java b/src/main/java/org/traccar/reports/SummaryReportProvider.java index 1f136adeb..26d79c899 100644 --- a/src/main/java/org/traccar/reports/SummaryReportProvider.java +++ b/src/main/java/org/traccar/reports/SummaryReportProvider.java @@ -23,6 +23,7 @@ import org.traccar.config.Keys; import org.traccar.helper.UnitsConverter; import org.traccar.helper.model.PositionUtil; import org.traccar.helper.model.UserUtil; +import org.traccar.model.Device; import org.traccar.model.Position; import org.traccar.reports.common.ReportUtils; import org.traccar.reports.model.SummaryReportItem; @@ -145,11 +146,11 @@ public class SummaryReportProvider { long userId, Collection deviceIds, Collection groupIds, Date from, Date to, boolean daily) throws StorageException { reportUtils.checkPeriodLimit(from, to); - reportUtils.checkPermissions(userId, deviceIds, groupIds); ArrayList result = new ArrayList<>(); - for (long deviceId: reportUtils.getDeviceList(deviceIds, groupIds)) { - Collection deviceResults = calculateSummaryResults(userId, deviceId, from, to, daily); + for (Device device: reportUtils.getAccessibleDevices(userId, deviceIds, groupIds)) { + Collection deviceResults = + calculateSummaryResults(userId, device.getId(), from, to, daily); for (SummaryReportItem summaryReport : deviceResults) { if (summaryReport.getStartTime() != null && summaryReport.getEndTime() != null) { result.add(summaryReport); diff --git a/src/main/java/org/traccar/reports/TripsReportProvider.java b/src/main/java/org/traccar/reports/TripsReportProvider.java index 95612ab30..7d10879b7 100644 --- a/src/main/java/org/traccar/reports/TripsReportProvider.java +++ b/src/main/java/org/traccar/reports/TripsReportProvider.java @@ -64,11 +64,10 @@ public class TripsReportProvider { public Collection getObjects(long userId, Collection deviceIds, Collection groupIds, Date from, Date to) throws StorageException { reportUtils.checkPeriodLimit(from, to); - reportUtils.checkPermissions(userId, deviceIds, groupIds); ArrayList result = new ArrayList<>(); - for (long deviceId: reportUtils.getDeviceList(deviceIds, groupIds)) { - result.addAll(detectTrips(deviceId, from, to)); + for (Device device: reportUtils.getAccessibleDevices(userId, deviceIds, groupIds)) { + result.addAll(detectTrips(device.getId(), from, to)); } return result; } @@ -77,14 +76,12 @@ public class TripsReportProvider { long userId, Collection deviceIds, Collection groupIds, Date from, Date to) throws StorageException, IOException { reportUtils.checkPeriodLimit(from, to); - reportUtils.checkPermissions(userId, deviceIds, groupIds); ArrayList devicesTrips = new ArrayList<>(); ArrayList sheetNames = new ArrayList<>(); - for (long deviceId: reportUtils.getDeviceList(deviceIds, groupIds)) { - Collection trips = detectTrips(deviceId, from, to); + for (Device device: reportUtils.getAccessibleDevices(userId, deviceIds, groupIds)) { + Collection trips = detectTrips(device.getId(), from, to); DeviceReportSection deviceTrips = new DeviceReportSection(); - Device device = reportUtils.getDevice(deviceId); deviceTrips.setDeviceName(device.getName()); sheetNames.add(WorkbookUtil.createSafeSheetName(deviceTrips.getDeviceName())); if (device.getGroupId() > 0) { diff --git a/src/main/java/org/traccar/reports/common/ReportUtils.java b/src/main/java/org/traccar/reports/common/ReportUtils.java index ab009161f..3960546e9 100644 --- a/src/main/java/org/traccar/reports/common/ReportUtils.java +++ b/src/main/java/org/traccar/reports/common/ReportUtils.java @@ -26,7 +26,6 @@ import org.jxls.formula.StandardFormulaProcessor; import org.jxls.transform.Transformer; import org.jxls.transform.poi.PoiTransformer; import org.jxls.util.TransformerFactory; -import org.traccar.Context; import org.traccar.api.security.PermissionsService; import org.traccar.config.Config; import org.traccar.config.Keys; @@ -62,11 +61,14 @@ import java.math.BigDecimal; import java.math.RoundingMode; import java.util.ArrayList; import java.util.Collection; +import java.util.Collections; import java.util.Date; -import java.util.LinkedHashSet; +import java.util.LinkedList; import java.util.List; import java.util.Locale; import java.util.Map; +import java.util.Objects; +import java.util.stream.Collectors; public class ReportUtils { @@ -114,22 +116,39 @@ public class ReportUtils { } } - public void checkPermissions( + public Collection getAccessibleDevices( long userId, Collection deviceIds, Collection groupIds) throws StorageException { - for (long deviceId : deviceIds) { - permissionsService.checkPermission(Device.class, userId, deviceId); - } - for (long groupId : groupIds) { - permissionsService.checkPermission(Group.class, userId, groupId); - } - } - public Collection getDeviceList(Collection deviceIds, Collection groupIds) { - Collection result = new LinkedHashSet<>(deviceIds); - for (long groupId : groupIds) { - result.addAll(Context.getPermissionsManager().getGroupDevices(groupId)); + var devices = storage.getObjects(Device.class, new Request( + new Columns.All(), + new Condition.Permission(User.class, userId, Device.class))); + var deviceById = devices.stream() + .collect(Collectors.toUnmodifiableMap(Device::getId, x -> x)); + var devicesByGroup = devices.stream() + .filter(x -> x.getGroupId() > 0) + .collect(Collectors.groupingBy(Device::getGroupId)); + + var groups = storage.getObjects(Group.class, new Request( + new Columns.All(), + new Condition.Permission(User.class, userId, Group.class))); + var groupsByGroup = groups.stream() + .filter(x -> x.getGroupId() > 0) + .collect(Collectors.groupingBy(Group::getGroupId)); + + var results = deviceIds.stream() + .map(deviceById::get) + .filter(Objects::nonNull) + .collect(Collectors.toSet()); + + var groupQueue = new LinkedList<>(groupIds); + while (!groupQueue.isEmpty()) { + long groupId = groupQueue.pop(); + results.addAll(devicesByGroup.getOrDefault(groupId, Collections.emptyList())); + groupQueue.addAll(groupsByGroup.getOrDefault(groupId, Collections.emptyList()) + .stream().map(Group::getId).collect(Collectors.toUnmodifiableList())); } - return result; + + return results; } public double calculateFuel(Position firstPosition, Position lastPosition) { -- cgit v1.2.3