Example #1
0
  /**
   * Build iCalendar-compliant recurrence rule
   *
   * @param recurrence
   * @param recurrenceEnd
   * @return rrule
   */
  public static String getRecurrenceRule(final String recurrence, final Date recurrenceEnd) {
    final TimeZone tz =
        TimeZoneRegistryFactory.getInstance()
            .createRegistry()
            .getTimeZone(java.util.Calendar.getInstance().getTimeZone().getID());

    if (recurrence != null) { // recurrence available
      // create recurrence rule
      final StringBuilder sb = new StringBuilder();
      sb.append("FREQ=");
      if (recurrence.equals(CalendarEntry.WORKDAILY)) {
        // build rule for monday to friday
        sb.append(CalendarEntry.DAILY);
        sb.append(";");
        sb.append("BYDAY=MO,TU,WE,TH,FR");
      } else if (recurrence.equals(CalendarEntry.BIWEEKLY)) {
        // build rule for biweekly
        sb.append(CalendarEntry.WEEKLY);
        sb.append(";");
        sb.append("INTERVAL=2");
      } else {
        // normal supported recurrence
        sb.append(recurrence);
      }
      if (recurrenceEnd != null) {
        final DateTime recurEndDT = new DateTime(recurrenceEnd.getTime());
        recurEndDT.setTimeZone(tz);
        sb.append(";");
        sb.append(CalendarEntry.UNTIL);
        sb.append("=");
        sb.append(recurEndDT.toString());
      }
      try {
        final Recur recur = new Recur(sb.toString());
        final RRule rrule = new RRule(recur);
        return rrule.getValue();
      } catch (final ParseException e) {
        log.error("cannot create recurrence rule: " + recurrence.toString(), e);
      }
    }

    return null;
  }
  /**
   * Returns the latest applicable onset of this observance for the specified date.
   *
   * @param date the latest date that an observance onset may occur
   * @return the latest applicable observance date or null if there is no applicable observance
   *     onset for the specified date
   */
  public final Date getLatestOnset(final Date date) {

    if (initialOnset == null) {
      try {
        initialOnset =
            applyOffsetFrom(calculateOnset(((DtStart) getProperty(Property.DTSTART)).getDate()));
      } catch (ParseException e) {
        Log log = LogFactory.getLog(Observance.class);
        log.error("Unexpected error calculating initial onset", e);
        // XXX: is this correct?
        return null;
      }
    }

    // observance not applicable if date is before the effective date of this observance..
    if (date.before(initialOnset)) {
      return null;
    }

    if ((onsetsMillisec != null) && (onsetLimit == null || date.before(onsetLimit))) {
      return getCachedOnset(date);
    }

    Date onset = initialOnset;
    Date initialOnsetUTC;
    // get first onset without adding TZFROM as this may lead to a day boundary
    // change which would be incompatible with BYDAY RRULES
    // we will have to add the offset to all cacheable onsets
    try {
      initialOnsetUTC = calculateOnset(((DtStart) getProperty(Property.DTSTART)).getDate());
    } catch (ParseException e) {
      Log log = LogFactory.getLog(Observance.class);
      log.error("Unexpected error calculating initial onset", e);
      // XXX: is this correct?
      return null;
    }
    // collect all onsets for the purposes of caching..
    final DateList cacheableOnsets = new DateList();
    cacheableOnsets.setUtc(true);
    cacheableOnsets.add(initialOnset);

    // check rdates for latest applicable onset..
    final PropertyList rdates = getProperties(Property.RDATE);
    for (final Iterator i = rdates.iterator(); i.hasNext(); ) {
      final RDate rdate = (RDate) i.next();
      for (final Iterator j = rdate.getDates().iterator(); j.hasNext(); ) {
        try {
          final DateTime rdateOnset = applyOffsetFrom(calculateOnset((Date) j.next()));
          if (!rdateOnset.after(date) && rdateOnset.after(onset)) {
            onset = rdateOnset;
          }
          /*
           * else if (rdateOnset.after(date) && rdateOnset.after(onset) && (nextOnset == null ||
           * rdateOnset.before(nextOnset))) { nextOnset = rdateOnset; }
           */
          cacheableOnsets.add(rdateOnset);
        } catch (ParseException e) {
          Log log = LogFactory.getLog(Observance.class);
          log.error("Unexpected error calculating onset", e);
        }
      }
    }

    // check recurrence rules for latest applicable onset..
    final PropertyList rrules = getProperties(Property.RRULE);
    for (final Iterator i = rrules.iterator(); i.hasNext(); ) {
      final RRule rrule = (RRule) i.next();
      // include future onsets to determine onset period..
      final Calendar cal = Dates.getCalendarInstance(date);
      cal.setTime(date);
      cal.add(Calendar.YEAR, 10);
      onsetLimit = Dates.getInstance(cal.getTime(), Value.DATE_TIME);
      final DateList recurrenceDates =
          rrule.getRecur().getDates(initialOnsetUTC, onsetLimit, Value.DATE_TIME);
      for (final Iterator j = recurrenceDates.iterator(); j.hasNext(); ) {
        final DateTime rruleOnset = applyOffsetFrom((DateTime) j.next());
        if (!rruleOnset.after(date) && rruleOnset.after(onset)) {
          onset = rruleOnset;
        }
        /*
         * else if (rruleOnset.after(date) && rruleOnset.after(onset) && (nextOnset == null ||
         * rruleOnset.before(nextOnset))) { nextOnset = rruleOnset; }
         */
        cacheableOnsets.add(rruleOnset);
      }
    }

    // cache onsets..
    Collections.sort(cacheableOnsets);
    DateTime cacheableOnset = null;
    this.onsetsMillisec = new long[cacheableOnsets.size()];
    this.onsetsDates = new DateTime[onsetsMillisec.length];

    for (int i = 0; i < onsetsMillisec.length; i++) {
      cacheableOnset = (DateTime) cacheableOnsets.get(i);
      onsetsMillisec[i] = cacheableOnset.getTime();
      onsetsDates[i] = cacheableOnset;
    }

    return onset;
  }