aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/traccar/reports
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/reports
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/reports')
-rw-r--r--src/main/java/org/traccar/reports/common/ReportExecutor.java25
-rw-r--r--src/main/java/org/traccar/reports/common/ReportMailer.java70
2 files changed, 95 insertions, 0 deletions
diff --git a/src/main/java/org/traccar/reports/common/ReportExecutor.java b/src/main/java/org/traccar/reports/common/ReportExecutor.java
new file mode 100644
index 000000000..aed4b8c23
--- /dev/null
+++ b/src/main/java/org/traccar/reports/common/ReportExecutor.java
@@ -0,0 +1,25 @@
+/*
+ * Copyright 2023 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.common;
+
+import org.traccar.storage.StorageException;
+
+import java.io.IOException;
+import java.io.OutputStream;
+
+public interface ReportExecutor {
+ void execute(OutputStream stream) throws StorageException, IOException;
+}
diff --git a/src/main/java/org/traccar/reports/common/ReportMailer.java b/src/main/java/org/traccar/reports/common/ReportMailer.java
new file mode 100644
index 000000000..32bbd4b17
--- /dev/null
+++ b/src/main/java/org/traccar/reports/common/ReportMailer.java
@@ -0,0 +1,70 @@
+/*
+ * Copyright 2023 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.common;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.traccar.api.security.PermissionsService;
+import org.traccar.mail.MailManager;
+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 javax.activation.DataHandler;
+import javax.inject.Inject;
+import javax.mail.MessagingException;
+import javax.mail.internet.MimeBodyPart;
+import javax.mail.util.ByteArrayDataSource;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+
+public class ReportMailer {
+
+ private static final Logger LOGGER = LoggerFactory.getLogger(ReportMailer.class);
+
+ private final Storage storage;
+ private final MailManager mailManager;
+
+ @Inject
+ public ReportMailer(Storage storage, MailManager mailManager) {
+ this.storage = storage;
+ this.mailManager = mailManager;
+ }
+
+ public void sendAsync(long userId, ReportExecutor executor) {
+ 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 = storage.getObject(
+ User.class, new Request(new Columns.All(), new Condition.Equals("id", userId)));
+ mailManager.sendMessage(user, "Report", "The report is in the attachment.", attachment);
+ } catch (StorageException | IOException | MessagingException e) {
+ LOGGER.warn("Email report failed", e);
+ }
+ }).start();
+ }
+
+}