From 0e1217d3b58528e87ffba2b7d81c3ce616afe128 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Sun, 28 Jul 2024 09:00:14 -0700 Subject: Update calendar library --- src/main/java/org/traccar/model/Calendar.java | 24 ++++++++-------------- .../java/org/traccar/schedule/TaskReports.java | 8 +++++--- 2 files changed, 13 insertions(+), 19 deletions(-) (limited to 'src') diff --git a/src/main/java/org/traccar/model/Calendar.java b/src/main/java/org/traccar/model/Calendar.java index 76c9a2cc1..c1a570037 100644 --- a/src/main/java/org/traccar/model/Calendar.java +++ b/src/main/java/org/traccar/model/Calendar.java @@ -19,9 +19,6 @@ package org.traccar.model; import com.fasterxml.jackson.annotation.JsonIgnore; import net.fortuna.ical4j.data.CalendarBuilder; import net.fortuna.ical4j.data.ParserException; -import net.fortuna.ical4j.filter.Filter; -import net.fortuna.ical4j.filter.predicate.PeriodRule; -import net.fortuna.ical4j.model.DateTime; import net.fortuna.ical4j.model.Period; import net.fortuna.ical4j.model.component.CalendarComponent; import net.fortuna.ical4j.model.component.VEvent; @@ -31,9 +28,8 @@ import org.traccar.storage.StorageName; import java.io.ByteArrayInputStream; import java.io.IOException; import java.time.Duration; -import java.util.Collection; +import java.time.Instant; import java.util.Date; -import java.util.List; import java.util.Set; import java.util.stream.Collectors; @@ -70,23 +66,19 @@ public class Calendar extends ExtendedModel { return calendar; } - private Collection findEvents(Date date) { + public Set> findPeriods(Date date) { if (calendar != null) { - var filter = new Filter(new PeriodRule<>(new Period(new DateTime(date), Duration.ZERO))); - return filter.filter(calendar.getComponents(CalendarComponent.VEVENT)); + var period = new Period<>(date.toInstant(), Duration.ZERO); + return calendar.getComponents(CalendarComponent.VEVENT).stream() + .flatMap(c -> c.calculateRecurrenceSet(period).stream()) + .collect(Collectors.toUnmodifiableSet()); } else { - return List.of(); + return Set.of(); } } - public Set findPeriods(Date date) { - return findEvents(date).stream() - .flatMap((e) -> e.calculateRecurrenceSet(new Period(new DateTime(date), Duration.ZERO)).stream()) - .collect(Collectors.toSet()); - } - public boolean checkMoment(Date date) { - return !findEvents(date).isEmpty(); + return !findPeriods(date).isEmpty(); } } diff --git a/src/main/java/org/traccar/schedule/TaskReports.java b/src/main/java/org/traccar/schedule/TaskReports.java index 1b9b15749..32f6fea87 100644 --- a/src/main/java/org/traccar/schedule/TaskReports.java +++ b/src/main/java/org/traccar/schedule/TaskReports.java @@ -41,6 +41,8 @@ import org.traccar.storage.query.Condition; import org.traccar.storage.query.Request; import jakarta.inject.Inject; + +import java.time.Instant; import java.util.Collections; import java.util.Date; import java.util.HashSet; @@ -83,12 +85,12 @@ public class TaskReports extends SingleScheduleTask { var lastEvents = calendar.findPeriods(lastCheck); var currentEvents = calendar.findPeriods(currentCheck); - Set finishedEvents = new HashSet<>(lastEvents); + Set> finishedEvents = new HashSet<>(lastEvents); finishedEvents.removeAll(currentEvents); - for (Period period : finishedEvents) { + for (Period period : finishedEvents) { RequestScoper scope = ServletScopes.scopeRequest(Collections.emptyMap()); try (RequestScoper.CloseableScope ignored = scope.open()) { - executeReport(report, period.getStart(), period.getEnd()); + executeReport(report, Date.from(period.getStart()), Date.from(period.getEnd())); } } } -- cgit v1.2.3