/**
   * 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 Calendar createiCalender(Collection<Appointment> appointments, Preferences preferences) {

    boolean doExportAsMeeting =
        preferences == null
            ? global_export_attendees
            : preferences.getEntryAsBoolean(
                Export2iCalPlugin.EXPORT_ATTENDEES_PREFERENCE, global_export_attendees);

    String exportAttendeesParticipationStatus =
        preferences == null
            ? global_export_attendees_participation_status
            : preferences.getEntryAsString(
                Export2iCalPlugin.EXPORT_ATTENDEES_PARTICIPATION_STATUS_PREFERENCE,
                global_export_attendees_participation_status);

    // ensure the stored value is not empty string, if so, do not export attendees
    doExportAsMeeting =
        doExportAsMeeting
            && (exportAttendeesAttribute != null && exportAttendeesAttribute.trim().length() > 0);

    Calendar calendar = initiCalendar();
    addICalMethod(calendar, Method.PUBLISH);
    addVTimeZone(calendar);
    ComponentList components = calendar.getComponents();
    for (Appointment app : appointments) {
      VEvent event = createVEvent(app, doExportAsMeeting, exportAttendeesParticipationStatus);
      components.add(event);
    }
    return calendar;
  }
  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;
    }
  }
Beispiel #4
0
  /**
   * Imports a calendar object containing a VJOURNAL. Sets the following properties:
   *
   * <ul>
   *   <li>display name: the VJOURNAL's SUMMARY (or the item's name, if the SUMMARY is blank)
   *   <li>icalUid: the VJOURNAL's UID
   *   <li>body: the VJOURNAL's DESCRIPTION
   * </ul>
   */
  public void setCalendar(Calendar cal) throws DavException {
    NoteItem note = (NoteItem) getItem();

    ComponentList vjournals = cal.getComponents(Component.VJOURNAL);
    if (vjournals.isEmpty())
      throw new UnprocessableEntityException("VCALENDAR does not contain any VJOURNALS");

    EntityConverter converter = new EntityConverter(getEntityFactory());
    converter.convertJournalCalendar(note, cal);
  }
Beispiel #5
0
  /**
   * Imports a calendar object containing a VTODO. Sets the following properties:
   *
   * <ul>
   *   <li>display name: the VTODO's SUMMARY (or the item's name, if the SUMMARY is blank)
   *   <li>icalUid: the VTODO's UID
   *   <li>body: the VTODO's DESCRIPTION
   *   <li>reminderTime: if the VTODO has a DISPLAY VALARM the reminderTime will be set to the
   *       trigger time
   * </ul>
   *
   * @param cal The calendar imported.
   * @throws CosmoDavException - if something is wrong this exception is thrown.
   */
  public void setCalendar(Calendar cal) throws CosmoDavException {
    NoteItem note = (NoteItem) getItem();

    ComponentList vtodos = cal.getComponents(Component.VTODO);
    if (vtodos.isEmpty()) {
      throw new UnprocessableEntityException("VCALENDAR does not contain any VTODOS");
    }

    EntityConverter converter = new EntityConverter(getEntityFactory());
    converter.convertTaskCalendar(note, cal);
  }
  //
  // 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;
  }
  /**
   * Tests limit recurrence set.
   *
   * @throws Exception - if something is wrong this exception is thrown.
   */
  @Test
  public void testLimitRecurrenceSet() throws Exception {
    CalendarBuilder cb = new CalendarBuilder();
    FileInputStream fis = new FileInputStream(baseDir + "limit_recurr_test.ics");
    Calendar calendar = cb.build(fis);

    Assert.assertEquals(5, calendar.getComponents().getComponents("VEVENT").size());

    VTimeZone vtz = (VTimeZone) calendar.getComponents().getComponent("VTIMEZONE");
    TimeZone tz = new TimeZone(vtz);
    OutputFilter filter = new OutputFilter("test");
    DateTime start = new DateTime("20060104T010000", tz);
    DateTime end = new DateTime("20060106T010000", tz);
    start.setUtc(true);
    end.setUtc(true);

    Period period = new Period(start, end);
    filter.setLimit(period);
    filter.setAllSubComponents();
    filter.setAllProperties();

    StringBuffer buffer = new StringBuffer();
    filter.filter(calendar, buffer);
    StringReader sr = new StringReader(buffer.toString());

    Calendar filterCal = cb.build(sr);

    ComponentList comps = filterCal.getComponents();
    Assert.assertEquals(3, comps.getComponents("VEVENT").size());
    Assert.assertEquals(1, comps.getComponents("VTIMEZONE").size());

    // Make sure 3rd and 4th override are dropped
    @SuppressWarnings("unchecked")
    Iterator<Component> it = comps.getComponents("VEVENT").iterator();
    while (it.hasNext()) {
      Component c = it.next();
      Assert.assertNotSame(
          "event 6 changed 3", c.getProperties().getProperty("SUMMARY").getValue());
      Assert.assertNotSame(
          "event 6 changed 4", c.getProperties().getProperty("SUMMARY").getValue());
    }
  }
  /**
   * Tests the set of limit recurrence.
   *
   * @throws Exception - if something is wrong this exception is thrown.
   */
  @Test
  public void testLimitRecurrenceSetThisAndFuture() throws Exception {
    CalendarBuilder cb = new CalendarBuilder();
    FileInputStream fis = new FileInputStream(baseDir + "limit_recurr_taf_test.ics");
    Calendar calendar = cb.build(fis);

    Assert.assertEquals(4, calendar.getComponents().getComponents("VEVENT").size());

    VTimeZone vtz = (VTimeZone) calendar.getComponents().getComponent("VTIMEZONE");
    TimeZone tz = new TimeZone(vtz);
    OutputFilter filter = new OutputFilter("test");
    DateTime start = new DateTime("20060108T170000", tz);
    DateTime end = new DateTime("20060109T170000", tz);
    start.setUtc(true);
    end.setUtc(true);

    Period period = new Period(start, end);
    filter.setLimit(period);
    filter.setAllSubComponents();
    filter.setAllProperties();

    StringBuffer buffer = new StringBuffer();
    filter.filter(calendar, buffer);
    StringReader sr = new StringReader(buffer.toString());

    Calendar filterCal = cb.build(sr);

    Assert.assertEquals(2, filterCal.getComponents().getComponents("VEVENT").size());
    // Make sure 2nd and 3rd override are dropped
    ComponentList vevents = filterCal.getComponents().getComponents(VEvent.VEVENT);
    Iterator<VEvent> it = vevents.iterator();
    while (it.hasNext()) {
      Component c = it.next();
      Assert.assertNotSame("event 6 changed", c.getProperties().getProperty("SUMMARY").getValue());
      Assert.assertNotSame(
          "event 6 changed 2", c.getProperties().getProperty("SUMMARY").getValue());
    }
  }
Beispiel #9
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();
  }