From 9e07a009a3da41cd3cdd21809e9588e1ed133d6f Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Tue, 28 Jun 2022 07:39:33 -0700 Subject: Fix device name in reports --- .../org/traccar/reports/StopsReportProvider.java | 10 ++++---- .../org/traccar/reports/SummaryReportProvider.java | 20 ++++++++-------- .../org/traccar/reports/TripsReportProvider.java | 15 ++++++------ .../org/traccar/reports/common/ReportUtils.java | 27 ++++++++-------------- 4 files changed, 32 insertions(+), 40 deletions(-) (limited to 'src/main') diff --git a/src/main/java/org/traccar/reports/StopsReportProvider.java b/src/main/java/org/traccar/reports/StopsReportProvider.java index 6e4cd74d6..ba61ef6a1 100644 --- a/src/main/java/org/traccar/reports/StopsReportProvider.java +++ b/src/main/java/org/traccar/reports/StopsReportProvider.java @@ -55,10 +55,10 @@ public class StopsReportProvider { this.storage = storage; } - private Collection detectStops(long deviceId, Date from, Date to) throws StorageException { + private Collection detectStops(Device device, Date from, Date to) throws StorageException { boolean ignoreOdometer = config.getBoolean(Keys.REPORT_IGNORE_ODOMETER); - return reportUtils.detectTripsAndStops( - PositionUtil.getPositions(storage, deviceId, from, to), ignoreOdometer, StopReportItem.class); + var positions = PositionUtil.getPositions(storage, device.getId(), from, to); + return reportUtils.detectTripsAndStops(device, positions, ignoreOdometer, StopReportItem.class); } public Collection getObjects( @@ -68,7 +68,7 @@ public class StopsReportProvider { ArrayList result = new ArrayList<>(); for (Device device: reportUtils.getAccessibleDevices(userId, deviceIds, groupIds)) { - result.addAll(detectStops(device.getId(), from, to)); + result.addAll(detectStops(device, from, to)); } return result; } @@ -81,7 +81,7 @@ public class StopsReportProvider { ArrayList devicesStops = new ArrayList<>(); ArrayList sheetNames = new ArrayList<>(); for (Device device: reportUtils.getAccessibleDevices(userId, deviceIds, groupIds)) { - Collection stops = detectStops(device.getId(), from, to); + Collection stops = detectStops(device, from, to); DeviceReportSection deviceStops = new DeviceReportSection(); deviceStops.setDeviceName(device.getName()); sheetNames.add(WorkbookUtil.createSafeSheetName(deviceStops.getDeviceName())); diff --git a/src/main/java/org/traccar/reports/SummaryReportProvider.java b/src/main/java/org/traccar/reports/SummaryReportProvider.java index 26d79c899..02033b9e5 100644 --- a/src/main/java/org/traccar/reports/SummaryReportProvider.java +++ b/src/main/java/org/traccar/reports/SummaryReportProvider.java @@ -58,11 +58,10 @@ public class SummaryReportProvider { this.storage = storage; } - private SummaryReportItem calculateSummaryResult( - long deviceId, Collection positions) throws StorageException { + private SummaryReportItem calculateSummaryResult(Device device, Collection positions) { SummaryReportItem result = new SummaryReportItem(); - result.setDeviceId(deviceId); - result.setDeviceName(reportUtils.getDevice(deviceId).getName()); + result.setDeviceId(device.getId()); + result.setDeviceName(device.getName()); if (positions != null && !positions.isEmpty()) { Position firstPosition = null; Position previousPosition = null; @@ -119,9 +118,9 @@ public class SummaryReportProvider { } private Collection calculateSummaryResults( - long userId, long deviceId, Date from, Date to, boolean daily) throws StorageException { + long userId, Device device, Date from, Date to, boolean daily) throws StorageException { - var positions = PositionUtil.getPositions(storage, deviceId, from, to); + var positions = PositionUtil.getPositions(storage, device.getId(), from, to); var results = new ArrayList(); if (daily && !positions.isEmpty()) { int startIndex = 0; @@ -129,14 +128,14 @@ public class SummaryReportProvider { for (int i = 0; i < positions.size(); i++) { int currentDay = getDay(userId, positions.get(i).getFixTime()); if (currentDay != startDay) { - results.add(calculateSummaryResult(deviceId, positions.subList(startIndex, i))); + results.add(calculateSummaryResult(device, positions.subList(startIndex, i))); startIndex = i; startDay = currentDay; } } - results.add(calculateSummaryResult(deviceId, positions.subList(startIndex, positions.size()))); + results.add(calculateSummaryResult(device, positions.subList(startIndex, positions.size()))); } else { - results.add(calculateSummaryResult(deviceId, positions)); + results.add(calculateSummaryResult(device, positions)); } return results; @@ -149,8 +148,7 @@ public class SummaryReportProvider { ArrayList result = new ArrayList<>(); for (Device device: reportUtils.getAccessibleDevices(userId, deviceIds, groupIds)) { - Collection deviceResults = - calculateSummaryResults(userId, device.getId(), from, to, daily); + Collection deviceResults = calculateSummaryResults(userId, device, 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 7d10879b7..2d9bcdfbf 100644 --- a/src/main/java/org/traccar/reports/TripsReportProvider.java +++ b/src/main/java/org/traccar/reports/TripsReportProvider.java @@ -55,19 +55,20 @@ public class TripsReportProvider { this.storage = storage; } - private Collection detectTrips(long deviceId, Date from, Date to) throws StorageException { + private Collection detectTrips(Device device, Date from, Date to) throws StorageException { boolean ignoreOdometer = config.getBoolean(Keys.REPORT_IGNORE_ODOMETER); - return reportUtils.detectTripsAndStops( - PositionUtil.getPositions(storage, deviceId, from, to), ignoreOdometer, TripReportItem.class); + var positions = PositionUtil.getPositions(storage, device.getId(), from, to); + return reportUtils.detectTripsAndStops(device, positions, ignoreOdometer, TripReportItem.class); } - public Collection getObjects(long userId, Collection deviceIds, Collection groupIds, - Date from, Date to) throws StorageException { + public Collection getObjects( + long userId, Collection deviceIds, Collection groupIds, + Date from, Date to) throws StorageException { reportUtils.checkPeriodLimit(from, to); ArrayList result = new ArrayList<>(); for (Device device: reportUtils.getAccessibleDevices(userId, deviceIds, groupIds)) { - result.addAll(detectTrips(device.getId(), from, to)); + result.addAll(detectTrips(device, from, to)); } return result; } @@ -80,7 +81,7 @@ public class TripsReportProvider { ArrayList devicesTrips = new ArrayList<>(); ArrayList sheetNames = new ArrayList<>(); for (Device device: reportUtils.getAccessibleDevices(userId, deviceIds, groupIds)) { - Collection trips = detectTrips(device.getId(), from, to); + Collection trips = detectTrips(device, from, to); DeviceReportSection deviceTrips = new DeviceReportSection(); deviceTrips.setDeviceName(device.getName()); sheetNames.add(WorkbookUtil.createSafeSheetName(deviceTrips.getDeviceName())); diff --git a/src/main/java/org/traccar/reports/common/ReportUtils.java b/src/main/java/org/traccar/reports/common/ReportUtils.java index f5f2cd3df..cd6b6ffd5 100644 --- a/src/main/java/org/traccar/reports/common/ReportUtils.java +++ b/src/main/java/org/traccar/reports/common/ReportUtils.java @@ -99,12 +99,6 @@ public class ReportUtils { new Condition.Permission(User.class, userId, clazz)))); } - public Device getDevice(long deviceId) throws StorageException { - return storage.getObject(Device.class, new Request( - new Columns.Include("id"), - new Condition.Equals("id", "id", deviceId))); - } - public void checkPeriodLimit(Date from, Date to) { long limit = config.getLong(Keys.REPORT_PERIOD_LIMIT) * 1000; if (limit > 0 && to.getTime() - from.getTime() > limit) { @@ -211,7 +205,7 @@ public class ReportUtils { } private TripReportItem calculateTrip( - ArrayList positions, int startIndex, int endIndex, + Device device, ArrayList positions, int startIndex, int endIndex, boolean ignoreOdometer) throws StorageException { Position startTrip = positions.get(startIndex); @@ -230,7 +224,7 @@ public class ReportUtils { long tripDuration = endTrip.getFixTime().getTime() - startTrip.getFixTime().getTime(); long deviceId = startTrip.getDeviceId(); trip.setDeviceId(deviceId); - trip.setDeviceName(getDevice(deviceId).getName()); + trip.setDeviceName(device.getName()); trip.setStartPositionId(startTrip.getId()); trip.setStartLat(startTrip.getLatitude()); @@ -277,8 +271,7 @@ public class ReportUtils { } private StopReportItem calculateStop( - ArrayList positions, int startIndex, int endIndex, - boolean ignoreOdometer) throws StorageException { + Device device, ArrayList positions, int startIndex, int endIndex, boolean ignoreOdometer) { Position startStop = positions.get(startIndex); Position endStop = positions.get(endIndex); @@ -287,7 +280,7 @@ public class ReportUtils { long deviceId = startStop.getDeviceId(); stop.setDeviceId(deviceId); - stop.setDeviceName(getDevice(deviceId).getName()); + stop.setDeviceName(device.getName()); stop.setPositionId(startStop.getId()); stop.setLatitude(startStop.getLatitude()); @@ -326,13 +319,13 @@ public class ReportUtils { @SuppressWarnings("unchecked") private T calculateTripOrStop( - ArrayList positions, int startIndex, int endIndex, + Device device, ArrayList positions, int startIndex, int endIndex, boolean ignoreOdometer, Class reportClass) throws StorageException { if (reportClass.equals(TripReportItem.class)) { - return (T) calculateTrip(positions, startIndex, endIndex, ignoreOdometer); + return (T) calculateTrip(device, positions, startIndex, endIndex, ignoreOdometer); } else { - return (T) calculateStop(positions, startIndex, endIndex, ignoreOdometer); + return (T) calculateStop(device, positions, startIndex, endIndex, ignoreOdometer); } } @@ -357,7 +350,7 @@ public class ReportUtils { } public Collection detectTripsAndStops( - Collection positionCollection, boolean ignoreOdometer, + Device device, Collection positionCollection, boolean ignoreOdometer, Class reportClass) throws StorageException { Collection result = new ArrayList<>(); @@ -392,14 +385,14 @@ public class ReportUtils { if (startEventIndex != -1 && startNoEventIndex != -1 && event != null && trips != deviceState.getMotionState()) { result.add(calculateTripOrStop( - positions, startEventIndex, startNoEventIndex, ignoreOdometer, reportClass)); + device, positions, startEventIndex, startNoEventIndex, ignoreOdometer, reportClass)); startEventIndex = -1; } } if (startEventIndex != -1 && (startNoEventIndex != -1 || !trips)) { int endIndex = startNoEventIndex != -1 ? startNoEventIndex : positions.size() - 1; result.add(calculateTripOrStop( - positions, startEventIndex, endIndex, ignoreOdometer, reportClass)); + device, positions, startEventIndex, endIndex, ignoreOdometer, reportClass)); } } -- cgit v1.2.3