aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/traccar/api/resource
diff options
context:
space:
mode:
authorAnton Tananaev <anton@traccar.org>2023-01-25 14:25:40 -0800
committerAnton Tananaev <anton@traccar.org>2023-01-25 14:25:40 -0800
commitc828e6af3ca96e51c20f5e189ab1293c4fe29c8a (patch)
treec0faf8ce03cc2fee252f8f07fea7bddaebd0a2a4 /src/main/java/org/traccar/api/resource
parente5a52e47b5fcae268ee89748ffe9bc05ab726c31 (diff)
downloadtrackermap-server-c828e6af3ca96e51c20f5e189ab1293c4fe29c8a.tar.gz
trackermap-server-c828e6af3ca96e51c20f5e189ab1293c4fe29c8a.tar.bz2
trackermap-server-c828e6af3ca96e51c20f5e189ab1293c4fe29c8a.zip
Add scheduled reports task
Diffstat (limited to 'src/main/java/org/traccar/api/resource')
-rw-r--r--src/main/java/org/traccar/api/resource/ReportResource.java39
1 files changed, 6 insertions, 33 deletions
diff --git a/src/main/java/org/traccar/api/resource/ReportResource.java b/src/main/java/org/traccar/api/resource/ReportResource.java
index 70177dd4d..6944de9cb 100644
--- a/src/main/java/org/traccar/api/resource/ReportResource.java
+++ b/src/main/java/org/traccar/api/resource/ReportResource.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2016 - 2022 Anton Tananaev (anton@traccar.org)
+ * Copyright 2016 - 2023 Anton Tananaev (anton@traccar.org)
* Copyright 2016 - 2018 Andrey Kunitsyn (andrey@traccar.org)
*
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -19,27 +19,23 @@ package org.traccar.api.resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.traccar.api.BaseResource;
-import org.traccar.mail.MailManager;
import org.traccar.helper.LogAction;
import org.traccar.model.Event;
import org.traccar.model.Position;
-import org.traccar.model.User;
import org.traccar.model.UserRestrictions;
import org.traccar.reports.EventsReportProvider;
import org.traccar.reports.RouteReportProvider;
import org.traccar.reports.StopsReportProvider;
import org.traccar.reports.SummaryReportProvider;
import org.traccar.reports.TripsReportProvider;
+import org.traccar.reports.common.ReportExecutor;
+import org.traccar.reports.common.ReportMailer;
import org.traccar.reports.model.StopReportItem;
import org.traccar.reports.model.SummaryReportItem;
import org.traccar.reports.model.TripReportItem;
import org.traccar.storage.StorageException;
-import javax.activation.DataHandler;
import javax.inject.Inject;
-import javax.mail.MessagingException;
-import javax.mail.internet.MimeBodyPart;
-import javax.mail.util.ByteArrayDataSource;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
@@ -51,9 +47,6 @@ import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.StreamingOutput;
-import java.io.ByteArrayOutputStream;
-import java.io.IOException;
-import java.io.OutputStream;
import java.util.Collection;
import java.util.Date;
import java.util.List;
@@ -83,31 +76,11 @@ public class ReportResource extends BaseResource {
private TripsReportProvider tripsReportProvider;
@Inject
- private MailManager mailManager;
+ private ReportMailer reportMailer;
- private interface ReportExecutor {
- void execute(OutputStream stream) throws StorageException, IOException;
- }
-
- private Response executeReport(
- long userId, boolean mail, ReportExecutor executor) {
+ private Response executeReport(long userId, boolean mail, ReportExecutor executor) {
if (mail) {
- new Thread(() -> {
- try {
- var stream = new ByteArrayOutputStream();
- executor.execute(stream);
-
- MimeBodyPart attachment = new MimeBodyPart();
- attachment.setFileName("report.xlsx");
- attachment.setDataHandler(new DataHandler(new ByteArrayDataSource(
- stream.toByteArray(), "application/octet-stream")));
-
- User user = permissionsService.getUser(userId);
- mailManager.sendMessage(user, "Report", "The report is in the attachment.", attachment);
- } catch (StorageException | IOException | MessagingException e) {
- LOGGER.warn("Report failed", e);
- }
- }).start();
+ reportMailer.sendAsync(userId, executor);
return Response.noContent().build();
} else {
StreamingOutput stream = output -> {