From f1680218e85ff3310eb5898d8ec28feaac5797c6 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Sat, 2 Mar 2024 21:31:37 -0800 Subject: Add device export report --- .../org/traccar/api/resource/ReportResource.java | 15 ++++ .../org/traccar/reports/DevicesReportProvider.java | 78 +++++++++++++++++++++ .../traccar/reports/model/DeviceReportItem.java | 48 +++++++++++++ templates/export/devices.xlsx | Bin 0 -> 9282 bytes 4 files changed, 141 insertions(+) create mode 100644 src/main/java/org/traccar/reports/DevicesReportProvider.java create mode 100644 src/main/java/org/traccar/reports/model/DeviceReportItem.java create mode 100644 templates/export/devices.xlsx diff --git a/src/main/java/org/traccar/api/resource/ReportResource.java b/src/main/java/org/traccar/api/resource/ReportResource.java index b4882f219..55a96fa90 100644 --- a/src/main/java/org/traccar/api/resource/ReportResource.java +++ b/src/main/java/org/traccar/api/resource/ReportResource.java @@ -23,6 +23,7 @@ import org.traccar.model.Position; import org.traccar.model.Report; import org.traccar.model.UserRestrictions; import org.traccar.reports.CombinedReportProvider; +import org.traccar.reports.DevicesReportProvider; import org.traccar.reports.EventsReportProvider; import org.traccar.reports.RouteReportProvider; import org.traccar.reports.StopsReportProvider; @@ -77,6 +78,9 @@ public class ReportResource extends SimpleObjectResource { @Inject private TripsReportProvider tripsReportProvider; + @Inject + private DevicesReportProvider devicesReportProvider; + @Inject private ReportMailer reportMailer; @@ -319,4 +323,15 @@ public class ReportResource extends SimpleObjectResource { return getStopsExcel(deviceIds, groupIds, from, to, type.equals("mail")); } + @Path("devices/{type:xlsx|mail}") + @GET + @Produces(EXCEL) + public Response geDevicesExcel( + @PathParam("type") String type) throws StorageException { + permissionsService.checkRestriction(getUserId(), UserRestrictions::getDisableReports); + return executeReport(getUserId(), type.equals("mail"), stream -> { + devicesReportProvider.getExcel(stream, getUserId()); + }); + } + } diff --git a/src/main/java/org/traccar/reports/DevicesReportProvider.java b/src/main/java/org/traccar/reports/DevicesReportProvider.java new file mode 100644 index 000000000..7b4294d53 --- /dev/null +++ b/src/main/java/org/traccar/reports/DevicesReportProvider.java @@ -0,0 +1,78 @@ +/* + * Copyright 2024 Anton Tananaev (anton@traccar.org) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.traccar.reports; + +import jakarta.inject.Inject; +import org.jxls.util.JxlsHelper; +import org.traccar.config.Config; +import org.traccar.config.Keys; +import org.traccar.helper.model.PositionUtil; +import org.traccar.model.Device; +import org.traccar.model.Message; +import org.traccar.model.User; +import org.traccar.reports.common.ReportUtils; +import org.traccar.reports.model.DeviceReportItem; +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.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.nio.file.Paths; +import java.util.Collection; +import java.util.stream.Collectors; + +public class DevicesReportProvider { + + private final Config config; + private final ReportUtils reportUtils; + private final Storage storage; + + @Inject + public DevicesReportProvider(Config config, ReportUtils reportUtils, Storage storage) { + this.config = config; + this.reportUtils = reportUtils; + this.storage = storage; + } + + public Collection getObjects(long userId) throws StorageException { + + var positions = PositionUtil.getLatestPositions(storage, userId).stream() + .collect(Collectors.toMap(Message::getDeviceId, p -> p)); + + return storage.getObjects(Device.class, new Request( + new Columns.All(), + new Condition.Permission(User.class, userId, Device.class))).stream() + .map(device -> new DeviceReportItem(device, positions.get(device.getId()))) + .collect(Collectors.toUnmodifiableList()); + } + + public void getExcel(OutputStream outputStream, long userId) throws StorageException, IOException { + + File file = Paths.get(config.getString(Keys.TEMPLATES_ROOT), "export", "devices.xlsx").toFile(); + try (InputStream inputStream = new FileInputStream(file)) { + var context = reportUtils.initializeContext(userId); + context.putVar("items", getObjects(userId)); + JxlsHelper.getInstance().setUseFastFormulaProcessor(false) + .processTemplate(inputStream, outputStream, context); + } + } +} diff --git a/src/main/java/org/traccar/reports/model/DeviceReportItem.java b/src/main/java/org/traccar/reports/model/DeviceReportItem.java new file mode 100644 index 000000000..74cd5f32c --- /dev/null +++ b/src/main/java/org/traccar/reports/model/DeviceReportItem.java @@ -0,0 +1,48 @@ +/* + * Copyright 2024 Anton Tananaev (anton@traccar.org) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.traccar.reports.model; + +import org.traccar.model.Device; +import org.traccar.model.Position; + +public class DeviceReportItem { + + public DeviceReportItem(Device device, Position position) { + this.device = device; + this.position = position; + } + + private Device device; + + public Device getDevice() { + return device; + } + + public void setDevice(Device device) { + this.device = device; + } + + private Position position; + + public Position getPosition() { + return position; + } + + public void setPosition(Position position) { + this.position = position; + } + +} diff --git a/templates/export/devices.xlsx b/templates/export/devices.xlsx new file mode 100644 index 000000000..ab6d9d5be Binary files /dev/null and b/templates/export/devices.xlsx differ -- cgit v1.2.3