View Javadoc
1   /*
2    * Licensed to The Apereo Foundation under one or more contributor license
3    * agreements. See the NOTICE file distributed with this work for additional
4    * information regarding copyright ownership.
5    *
6    *
7    * The Apereo Foundation licenses this file to you under the Educational
8    * Community License, Version 2.0 (the "License"); you may not use this file
9    * except in compliance with the License. You may obtain a copy of the License
10   * at:
11   *
12   *   http://opensource.org/licenses/ecl2.txt
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
17   * License for the specific language governing permissions and limitations under
18   * the License.
19   *
20   */
21  
22  package org.opencastproject.scheduler.api;
23  
24  import static net.fortuna.ical4j.model.parameter.Value.DATE_TIME;
25  
26  import net.fortuna.ical4j.model.DateList;
27  import net.fortuna.ical4j.model.DateTime;
28  import net.fortuna.ical4j.model.Period;
29  import net.fortuna.ical4j.model.Recur;
30  import net.fortuna.ical4j.model.TimeZoneRegistry;
31  import net.fortuna.ical4j.model.TimeZoneRegistryFactory;
32  import net.fortuna.ical4j.model.property.RRule;
33  
34  import org.slf4j.Logger;
35  import org.slf4j.LoggerFactory;
36  
37  import java.text.SimpleDateFormat;
38  import java.time.ZoneOffset;
39  import java.time.ZonedDateTime;
40  import java.util.Calendar;
41  import java.util.Date;
42  import java.util.LinkedList;
43  import java.util.List;
44  import java.util.TimeZone;
45  
46  public final class Util {
47    /** The minimum separation between one event ending and the next starting */
48    public static final int EVENT_MINIMUM_SEPARATION_MILLISECONDS = 0;
49  
50    private static final Logger logger = LoggerFactory.getLogger(Util.class);
51    private static final TimeZoneRegistry registry = TimeZoneRegistryFactory.getInstance().createRegistry();
52  
53    private Util() {
54    }
55  
56  
57    /**
58     * Adjust the given UTC rrule to the given timezone.
59     *
60     * @param rRule The rrule to adjust.
61     * @param start The start date in UTC
62     *
63     * @param tz The target timezone.
64     */
65    public static void adjustRrule(final RRule rRule, final Date start, final TimeZone tz) {
66      final Recur recur = rRule.getRecur();
67      if (recur.getHourList().size() != 1 || recur.getMinuteList().size() != 1) {
68        throw new IllegalArgumentException(
69            "RRules with multiple hours/minutes are not supported by Opencast. " + recur.toString());
70      }
71      final ZonedDateTime adjustedDate = ZonedDateTime.ofInstant(start.toInstant(), ZoneOffset.UTC)
72              .withHour(recur.getHourList().get(0))
73              .withMinute(recur.getMinuteList().get(0))
74              .withZoneSameInstant(tz.toZoneId());
75      recur.getHourList().set(0, adjustedDate.getHour());
76      recur.getMinuteList().set(0, adjustedDate.getMinute());
77    }
78  
79    /**
80     * Backward compatibility, converts parameters as required for the updated calculatePeriods
81     *
82     * @param start date in the scheduled time zone
83     * @param end date in the scheduled time zone
84     * @param duration
85     * @param rRule
86     * @param tz, time zone of the scheduled event
87     * @return the calculated periods
88     */
89    public static List<Period> calculatePeriods(Date start, Date end, long duration, RRule rRule, TimeZone tz) {
90      Calendar startCal = Calendar.getInstance(tz);
91      Calendar endCal = Calendar.getInstance(tz);
92      startCal.setTime(start);
93      endCal.setTime(end);
94      return Util.calculatePeriods(startCal, endCal, duration, rRule.getRecur(), tz);
95    }
96  
97    /**
98     * Given a start time and end time with a recurrence rule and a timezone, all periods of the recurrence rule are
99     * calculated taken daylight saving time into account.
100    *
101    * @param startCalTz, Calendar date time of the recurrence in the timezone of the scheduled CA
102    * @param endCalTz, Calendar date time of the recurrence in the timezone of the scheduled CA
103    * @param duration, the length of each event
104    * @param recur, the recurrence rule (based on the Start time zone, including start hour and days of week)
105    * @param tz, the timezone of the scheduled CA
106    * @return a list of event Periods that match the rule and start and end times
107    */
108   public static List<Period> calculatePeriods(
109       Calendar startCalTz, Calendar endCalTz, long duration, Recur recur, TimeZone tz) {
110     SimpleDateFormat simpleDateFormat = new SimpleDateFormat("EE MMM dd HH:mm:ss zzz yyyy");
111     simpleDateFormat.setTimeZone(tz);
112     String tzStr = tz.getID();
113     List<Period> event = new LinkedList<>();
114     logger.debug("Inbound start of recurrence {} to end of recurrence {}, in Tz {}",
115         simpleDateFormat.format(startCalTz.getTime()),
116         simpleDateFormat.format(endCalTz.getTime()), tzStr);
117 
118     DateTime periodStart = new DateTime(startCalTz.getTime());
119     logger.debug("ical4j timeZone for {} is {}", tzStr, registry.getTimeZone(tzStr).toZoneId());
120     periodStart.setTimeZone(registry.getTimeZone(tzStr));
121     DateTime periodEnd = new DateTime(endCalTz.getTime());
122     periodEnd.setTimeZone(registry.getTimeZone(tzStr));
123 
124     logger.trace("is utc {}? Tz is {} ", periodStart.isUtc(), periodStart.getTimeZone().toZoneId());
125     logger.debug("({}) Looking at recurrences for {} to {}, duration {}, {}", periodStart.getTimeZone().toZoneId(),
126         simpleDateFormat.format(new Date(periodStart.getTime())),
127         simpleDateFormat.format(new Date(periodEnd.getTime())), duration, recur.toString());
128 
129     DateList dates = recur.getDates(periodStart, periodEnd, DATE_TIME);
130     logger.trace("Got {} dates: {}, tz '{}'", dates.size(), dates.toString(), dates.getTimeZone().toZoneId());
131 
132     for (Date date : dates) {
133       Date endTZ = new DateTime(date.getTime() + duration);
134       DateTime startDT = new DateTime(date);
135       DateTime endDT = new DateTime(endTZ);
136       Period p = new Period(startDT, endDT);
137       event.add(p);
138     }
139     for (Period e: event) {
140       Calendar cal = Calendar.getInstance(e.getStart().getTimeZone());
141       cal.setTimeInMillis(e.getStart().getTime());
142       logger.debug("EventList start {} Instance {}, calendar hour {}, zone {}",
143           e.getStart().toString(),
144           simpleDateFormat.format(cal.getTime()),
145           cal.get(Calendar.HOUR_OF_DAY),
146           e.getStart().getTimeZone().toZoneId());
147     }
148     return event;
149   }
150 
151   /**
152    * Check if two intervals (for example, for two scheduled events), overlap.
153    * @param start1 start of first interval
154    * @param end1 end of first interval
155    * @param start2 start of second interval
156    * @param end2 end of second interval
157    * @return <code>true</code> if they overlap, <code>false</code> if they don't
158    */
159   public static boolean schedulingIntervalsOverlap(
160       final Date start1, final Date end1, final Date start2, final Date end2) {
161     return end2.after(start1) && end1.after(start2)
162         || eventWithinMinimumSeparation(start1, end1, start2, end2);
163   }
164 
165   /**
166    * Returns true of checkStart is within EVENT_MINIMUM_SEPARATION_SECONDS of either the start or end dates, or checkEnd
167    * is within EVENT_MINIMUM_SEPARATION_SECONDS of either the start or end dates.  False otherwise
168    */
169   private static boolean eventWithinMinimumSeparation(Date checkStart, Date checkEnd, Date start, Date end) {
170     return Math.abs(checkStart.getTime() - start.getTime()) < EVENT_MINIMUM_SEPARATION_MILLISECONDS
171         || Math.abs(checkStart.getTime() - end.getTime()) < EVENT_MINIMUM_SEPARATION_MILLISECONDS
172         || Math.abs(checkEnd.getTime() - start.getTime()) < EVENT_MINIMUM_SEPARATION_MILLISECONDS
173         || Math.abs(checkEnd.getTime() - end.getTime()) < EVENT_MINIMUM_SEPARATION_MILLISECONDS;
174   }
175 }