/**
   * uses getCalendar(fixture.getHttpClient(), query)
   *
   * @throws Exception
   */
  @Test
  public void testGetEventResources() throws Exception {
    Date beginDate = ICalendarUtils.createDateTime(2006, 0, 1, null, true);
    Date endDate = ICalendarUtils.createDateTime(2006, 11, 9, null, true);
    List<Calendar> l = collection.getEventResources(fixture.getHttpClient(), beginDate, endDate);

    for (Calendar calendar : l) {
      ComponentList vevents = calendar.getComponents().getComponents(Component.VEVENT);
      VEvent ve = (VEvent) vevents.get(0);
      String uid = ICalendarUtils.getUIDValue(ve);
      int correctNumberOfEvents = -1;
      if (ICS_DAILY_NY_5PM_UID.equals(uid)) {
        // one for each day
        correctNumberOfEvents = 1;
      } else if (ICS_ALL_DAY_JAN1_UID.equals(uid)) {
        correctNumberOfEvents = 1;
      } else if (ICS_NORMAL_PACIFIC_1PM_UID.equals(uid)) {
        correctNumberOfEvents = 1;
      } else if (ICS_FLOATING_JAN2_7PM_UID.equals(uid)) {
        correctNumberOfEvents = 0;
      } else {
        fail(uid + " is not the uid of any event that should have been returned");
      }

      assertEquals(correctNumberOfEvents, vevents.size());
    }

    // 3 calendars - one for each resource (not including expanded
    // recurrences)
    assertEquals(3, l.size());
  }
  public boolean isValid(Object value) {

    Calendar calendar = null;
    try {
      calendar = (Calendar) value;

      // validate entire icalendar object
      calendar.validate(true);

      // additional check to prevent bad .ics
      CalendarUtils.parseCalendar(calendar.toString());

      // make sure we have a VEVENT with a recurrenceid
      ComponentList comps = calendar.getComponents();
      if (comps == null) {
        log.warn("error validating event exception: " + calendar.toString());
        return false;
      }

      comps = comps.getComponents(Component.VEVENT);
      if (comps == null || comps.size() == 0) {
        log.warn("error validating event exception: " + calendar.toString());
        return false;
      }

      VEvent event = (VEvent) comps.get(0);
      if (event == null) {
        log.warn("error validating event exception: " + calendar.toString());
        return false;
      }

      RecurrenceId recurrenceId = event.getRecurrenceId();

      if (recurrenceId == null
          || recurrenceId.getValue() == null
          || "".equals(recurrenceId.getValue())) {
        log.warn("error validating event exception: " + calendar.toString());
        return false;
      }

      return true;
    } catch (ValidationException ve) {
      log.warn("event validation error", ve);
      if (calendar != null) {
        log.warn("error validating event: " + calendar.toString());
      }
      return false;
    } catch (RuntimeException e) {
      return false;
    } catch (IOException e) {
      return false;
    } catch (ParserException e) {
      log.warn("parse error", e);
      if (calendar != null) {
        log.warn("error parsing event: " + calendar.toString());
      }
      return false;
    }
  }
  //
  // private
  //
  private boolean hasEventWithUID(List<Calendar> cals, String uid) {
    for (Calendar cal : cals) {
      ComponentList vEvents = cal.getComponents().getComponents(Component.VEVENT);
      if (vEvents.size() == 0) {
        return false;
      }
      VEvent ve = (VEvent) vEvents.get(0);
      String curUid = ICalendarUtils.getUIDValue(ve);
      if (curUid != null && uid.equals(curUid)) {
        return true;
      }
    }

    return false;
  }
Beispiel #4
0
  @Override
  @SuppressWarnings("unchecked")
  public void parseEntity(@NonNull InputStream entity)
      throws IOException, InvalidResourceException {
    net.fortuna.ical4j.model.Calendar ical;
    try {
      CalendarBuilder builder = new CalendarBuilder();
      ical = builder.build(entity);

      if (ical == null) throw new InvalidResourceException("No iCalendar found");
    } catch (ParserException e) {
      throw new InvalidResourceException(e);
    }

    // event
    ComponentList events = ical.getComponents(Component.VEVENT);
    if (events == null || events.isEmpty()) throw new InvalidResourceException("No VEVENT found");
    VEvent event = (VEvent) events.get(0);

    if (event.getUid() != null) uid = event.getUid().getValue();
    else {
      Log.w(TAG, "Received VEVENT without UID, generating new one");
      generateUID();
    }

    if ((dtStart = event.getStartDate()) == null || (dtEnd = event.getEndDate()) == null)
      throw new InvalidResourceException("Invalid start time/end time/duration");

    if (hasTime(dtStart)) {
      validateTimeZone(dtStart);
      validateTimeZone(dtEnd);
    }

    // all-day events and "events on that day":
    // * related UNIX times must be in UTC
    // * must have a duration (set to one day if missing)
    if (!hasTime(dtStart) && !dtEnd.getDate().after(dtStart.getDate())) {
      Log.i(TAG, "Repairing iCal: DTEND := DTSTART+1");
      Calendar c = Calendar.getInstance(TimeZone.getTimeZone(Time.TIMEZONE_UTC));
      c.setTime(dtStart.getDate());
      c.add(Calendar.DATE, 1);
      dtEnd.setDate(new Date(c.getTimeInMillis()));
    }

    rrule = (RRule) event.getProperty(Property.RRULE);
    rdate = (RDate) event.getProperty(Property.RDATE);
    exrule = (ExRule) event.getProperty(Property.EXRULE);
    exdate = (ExDate) event.getProperty(Property.EXDATE);

    if (event.getSummary() != null) summary = event.getSummary().getValue();
    if (event.getLocation() != null) location = event.getLocation().getValue();
    if (event.getDescription() != null) description = event.getDescription().getValue();

    status = event.getStatus();

    opaque = true;
    if (event.getTransparency() == Transp.TRANSPARENT) opaque = false;

    organizer = event.getOrganizer();
    for (Object o : event.getProperties(Property.ATTENDEE)) attendees.add((Attendee) o);

    Clazz classification = event.getClassification();
    if (classification != null) {
      if (classification == Clazz.PUBLIC) forPublic = true;
      else if (classification == Clazz.CONFIDENTIAL || classification == Clazz.PRIVATE)
        forPublic = false;
    }

    this.alarms = event.getAlarms();
  }