net.fortuna.ical4j.model.Period Java Examples

The following examples show how to use net.fortuna.ical4j.model.Period. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: StandardItemFilterProcessorTest.java    From cosmo with Apache License 2.0 6 votes vote down vote up
/**
 * Tests event stamp time range query.
 * 
 * @throws Exception
 *             - if something is wrong this exception is thrown.
 */
@Test
public void testEventStampTimeRangeQuery() throws Exception {
    NoteItemFilter filter = new NoteItemFilter();
    EventStampFilter eventFilter = new EventStampFilter();
    Period period = new Period(new DateTime("20070101T100000Z"), new DateTime("20070201T100000Z"));
    eventFilter.setPeriod(period);
    eventFilter.setTimezone(registry.getTimeZone("America/Chicago"));

    CollectionItem parent = new HibCollectionItem();
    filter.setParent(parent);
    filter.getStampFilters().add(eventFilter);
    QueryImpl<Item> query = queryBuilder.buildQuery(filter);
    Assert.assertEquals("select i from HibNoteItem i join i.parentDetails pd, "
            + "HibBaseEventStamp es where pd.primaryKey.collection=:parent and es.item=i "
            + "and ( (es.timeRangeIndex.isFloating=true and "
            + "es.timeRangeIndex.startDate < '20070201T040000' and "
            + "es.timeRangeIndex.endDate > '20070101T040000') or " + "(es.timeRangeIndex.isFloating=false and "
            + "es.timeRangeIndex.startDate < '20070201T100000Z' and "
            + "es.timeRangeIndex.endDate > '20070101T100000Z') or "
            + "(es.timeRangeIndex.startDate=es.timeRangeIndex.endDate and "
            + "(es.timeRangeIndex.startDate='20070101T040000' or "
            + "es.timeRangeIndex.startDate='20070101T100000Z')))", query.getQueryString());
}
 
Example #2
Source File: HibernateCalendarDaoTest.java    From cosmo with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldMatchEventOneWithTimeRangeFilter() throws ParseException {
    // Time range test
    eventFilter.getPropFilters().clear();
    DateTime start = new DateTime("20050817T115000Z");
    DateTime end = new DateTime("20050818T115000Z");

    Period period = new Period(start, end);
    TimeRangeFilter timeRangeFilter = new TimeRangeFilter(period);
    eventFilter.setTimeRangeFilter(timeRangeFilter);

    // should match ics.1
    Set<ICalendarItem> queryEvents = calendarDao.findCalendarItems(calendar, filter);
    assertEquals(1, queryEvents.size());
    ContentItem nextItem = (ContentItem) queryEvents.iterator().next();
    assertEquals("test1.ics", nextItem.getName());
}
 
Example #3
Source File: TimeRangeFilter.java    From cosmo with Apache License 2.0 6 votes vote down vote up
/**
 * Construct a TimeRangeFilter object from a DOM Element
 * @param element The DOM Element.
 * @throws ParseException - if something is wrong this exception is thrown.
 */
public TimeRangeFilter(Element element, VTimeZone timezone) throws ParseException {        
    // Get start (must be present)
    String start =
        DomUtil.getAttribute(element, ATTR_CALDAV_START, null);
    if (start == null) {
        throw new ParseException("CALDAV:comp-filter time-range requires a start time", -1);
    }
    
    DateTime trstart = new DateTime(start);
    if (! trstart.isUtc()) {
        throw new ParseException("CALDAV:param-filter timerange start must be UTC", -1);
    }

    // Get end (must be present)
    String end =
        DomUtil.getAttribute(element, ATTR_CALDAV_END, null);        
    DateTime trend = end != null ? new DateTime(end) : getDefaultEndDate(trstart);
    
    if (! trend.isUtc()) {
        throw new ParseException("CALDAV:param-filter timerange end must be UTC", -1);
    }

    setPeriod(new Period(trstart, trend));
    setTimezone(timezone);
}
 
Example #4
Source File: StandardCalendarQueryProcessor.java    From cosmo with Apache License 2.0 6 votes vote down vote up
/**
 * VFreeBusy query.
 * @param user The user.
 * @param period The period.
 * @return VFreeBusy. 
 */
public VFreeBusy freeBusyQuery(User user, Period period) {
    PeriodList busyPeriods = new PeriodList();
    PeriodList busyTentativePeriods = new PeriodList();
    PeriodList busyUnavailablePeriods = new PeriodList();
    
    HomeCollectionItem home = contentDao.getRootItem(user);
    for(Item item: home.getChildren()) {
        if(! (item instanceof CollectionItem)) {
            continue;
        }
        
        CollectionItem collection = (CollectionItem) item;
        if(StampUtils.getCalendarCollectionStamp(collection)==null ||
                collection.isExcludeFreeBusyRollup()) {
            continue;
        }
        
        doFreeBusyQuery(busyPeriods, busyTentativePeriods, busyUnavailablePeriods,
                collection, period);  
    }

    return createVFreeBusy(busyPeriods, busyTentativePeriods,
            busyUnavailablePeriods, period);
}
 
Example #5
Source File: LimitRecurrenceSetTest.java    From cosmo with Apache License 2.0 5 votes vote down vote up
/**
 * 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();
    
    StringBuilder buffer = new StringBuilder();
    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<VEvent> vevents = filterCal.getComponents().getComponents(VEvent.VEVENT);
    
    for(VEvent c : vevents) {            
        Assert.assertNotSame("event 6 changed",c.getProperties().getProperty("SUMMARY").getValue());
        Assert.assertNotSame("event 6 changed 2",c.getProperties().getProperty("SUMMARY").getValue());
    }   
}
 
Example #6
Source File: HibernateCalendarDaoTest.java    From cosmo with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldMatchAllEventsInTenYearsPeriod() {
    eventFilter.getPropFilters().clear();
    // 10 year period
    DateTime start = new DateTime();
    DateTime end = new DateTime();
    start.setTime(new GregorianCalendar(1996, 1, 22).getTimeInMillis());
    end.setTime(System.currentTimeMillis());
    TimeRangeFilter timeRangeFilter = new TimeRangeFilter(new Period(start, end));
    eventFilter.setTimeRangeFilter(timeRangeFilter);

    // should match all now
    Set<ICalendarItem> queryEvents = calendarDao.findCalendarItems(calendar, filter);
    assertEquals(5, queryEvents.size());
}
 
Example #7
Source File: HibernateCalendarDaoTest.java    From cosmo with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldMatchNoneInTimeRange() {
    eventFilter.getPropFilters().clear();
    // 10 year period
    DateTime start = new DateTime();
    DateTime end = new DateTime();
    start.setTime(new GregorianCalendar(2006, 8, 6).getTimeInMillis());
    end.setTime(System.currentTimeMillis());
    TimeRangeFilter timeRangeFilter = new TimeRangeFilter(new Period(start, end));
    eventFilter.setTimeRangeFilter(timeRangeFilter);
    // should match none
    Set<ICalendarItem> queryEvents = calendarDao.findCalendarItems(calendar, filter);
    assertEquals(0, queryEvents.size());
}
 
Example #8
Source File: CalendarFilterEvaluater.java    From cosmo with Apache License 2.0 5 votes vote down vote up
/**
 * Evaluates VFreeBusy time range.
 * @param freeBusy freebusy.
 * @param filter The filter.
 * @return The result.
 */
private boolean evaulateVFreeBusyTimeRange(VFreeBusy freeBusy, TimeRangeFilter filter) {
    DtStart start = freeBusy.getStartDate();
    DtEnd end = freeBusy.getEndDate();
     
    if (start != null && end != null) {
        InstanceList instances = new InstanceList();
        if (filter.getTimezone() != null) {
            instances.setTimezone(new TimeZone(filter.getTimezone()));
        }
        instances.addComponent(freeBusy, filter.getPeriod().getStart(),
                filter.getPeriod().getEnd());
        return instances.size() > 0;
    }
    
    PropertyList<FreeBusy> props = freeBusy.getProperties(Property.FREEBUSY);
    if(props.size()==0) {
        return false;
    }
    
    for (FreeBusy fb : props) {            
        PeriodList periods = fb.getPeriods();
        Iterator<Period> periodIt = periods.iterator();
        while(periodIt.hasNext()) {
            Period period = periodIt.next();
            if(filter.getPeriod().getStart().before(period.getEnd()) &&
               filter.getPeriod().getEnd().after(period.getStart())) {
                return true;
            }
        }
    }
    
    return false;
}
 
Example #9
Source File: StandardCalendarQueryProcessor.java    From cosmo with Apache License 2.0 5 votes vote down vote up
/**
 * Add all periods that intersect a given period to the result PeriodList.
 */
private void addRelevantPeriods(PeriodList results, PeriodList periods,
        Period range) {

    for (Iterator<Period> it = periods.iterator(); it.hasNext();) {
        Period p = it.next();
        if (p.intersects(range))
            results.add(p);
    }
}
 
Example #10
Source File: StandardCalendarQueryProcessor.java    From cosmo with Apache License 2.0 5 votes vote down vote up
/**
 * Adds The FreeBusy query.
 * @param busyPeriods The period list.
 * @param busyTentativePeriods The period list.
 * @param busyUnavailablePeriods The busy unavailable periods periods.
 * @param collection The collecton item.
 * @param period The period.
 */
protected void doFreeBusyQuery(PeriodList busyPeriods,
        PeriodList busyTentativePeriods, PeriodList busyUnavailablePeriods,
        CollectionItem collection, Period period) {

    CalendarCollectionStamp ccs = StampUtils.getCalendarCollectionStamp(collection);
    if(ccs==null) {
        return;
    }
    
    HashSet<ContentItem> results = new HashSet<ContentItem>();
    TimeZone tz = ccs.getTimezone();
    
    // For the time being, use CalendarFilters to get relevant
    // items.
    CalendarFilter[] filters = createQueryFilters(collection, period);
    for(CalendarFilter filter: filters) {
        results.addAll(calendarDao.findCalendarItems(collection, filter));
    }
    
    for(ContentItem content: results) {
        Calendar calendar = entityConverter.convertContent(content);
        if(calendar==null) {
            continue;
        }
        // Add busy details from the calendar data
        addBusyPeriods(calendar, tz, period, busyPeriods,
                busyTentativePeriods, busyUnavailablePeriods);
    }
}
 
Example #11
Source File: StandardCalendarQueryProcessor.java    From cosmo with Apache License 2.0 5 votes vote down vote up
/**
 * Creates VFreeBusy query.
 * @param item The ICalendar item.
 * @param period The period.
 * @return The created VFreeBusy query.
 */
public VFreeBusy freeBusyQuery(ICalendarItem item, Period period) {
    PeriodList busyPeriods = new PeriodList();
    PeriodList busyTentativePeriods = new PeriodList();
    PeriodList busyUnavailablePeriods = new PeriodList();
    
    Calendar calendar = entityConverter.convertContent(item);
    
    // Add busy details from the calendar data
    addBusyPeriods(calendar, null, period, busyPeriods,
            busyTentativePeriods, busyUnavailablePeriods);
    
    return createVFreeBusy(busyPeriods, busyTentativePeriods,
            busyUnavailablePeriods, period);
}
 
Example #12
Source File: StandardCalendarQueryProcessor.java    From cosmo with Apache License 2.0 5 votes vote down vote up
/**
 * Creates FreeBusyQuery.
 * @param collection The collection item.
 * @param period The period.
 * @return Created VFreeBusy.
 */
public VFreeBusy freeBusyQuery(CollectionItem collection, Period period) {
    PeriodList busyPeriods = new PeriodList();
    PeriodList busyTentativePeriods = new PeriodList();
    PeriodList busyUnavailablePeriods = new PeriodList();
    
    doFreeBusyQuery(busyPeriods, busyTentativePeriods, busyUnavailablePeriods,
            collection, period);

    return createVFreeBusy(busyPeriods, busyTentativePeriods,
            busyUnavailablePeriods, period);
}
 
Example #13
Source File: LimitRecurrenceSetTest.java    From cosmo with Apache License 2.0 5 votes vote down vote up
/**
 * 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();
    
    StringBuilder buffer = new StringBuilder();
    filter.filter(calendar, buffer);
    StringReader sr = new StringReader(buffer.toString());
    
    Calendar filterCal = cb.build(sr);
    
    ComponentList<CalendarComponent> 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

    ComponentList<CalendarComponent> events = comps.getComponents("VEVENT");
    for(CalendarComponent c : events) {            
        Assert.assertNotSame("event 6 changed 3",c.getProperties().getProperty("SUMMARY").getValue());
        Assert.assertNotSame("event 6 changed 4",c.getProperties().getProperty("SUMMARY").getValue());
    }
}
 
Example #14
Source File: TimeRangeFilter.java    From cosmo with Apache License 2.0 5 votes vote down vote up
/**
 * 
 * @param dtStart The timerange start.
 * @param dtEnd The timerange end.
 */
public TimeRangeFilter(DateTime dtStart, DateTime dtEnd) {
    if (!dtStart.isUtc()) {
        throw new IllegalArgumentException("timerange start must be UTC");
    }

    if (!dtEnd.isUtc()) {
        throw new IllegalArgumentException("timerange start must be UTC");
    }

    Period period = new Period(dtStart, dtEnd);
    setPeriod(period);
}
 
Example #15
Source File: LimitRecurrenceSetTest.java    From cosmo with Apache License 2.0 5 votes vote down vote up
/**
 * Tests limit floating recurrence set.
 * @throws Exception - if something is wrong this exception is thrown.
 */
@Test
public void testLimitFloatingRecurrenceSet() throws Exception {
    CalendarBuilder cb = new CalendarBuilder();
    FileInputStream fis = new FileInputStream(baseDir + "limit_recurr_float_test.ics");
    Calendar calendar = cb.build(fis);
    
    Assert.assertEquals(3, calendar.getComponents().getComponents("VEVENT").size());
    
    OutputFilter filter = new OutputFilter("test");
    DateTime start = new DateTime("20060102T170000");
    DateTime end = new DateTime("20060104T170000");
    
    start.setUtc(true);
    end.setUtc(true);
    
    Period period = new Period(start, end);
    filter.setLimit(period);
    filter.setAllSubComponents();
    filter.setAllProperties();
    
    StringBuilder buffer = new StringBuilder();
    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 override is dropped
    ComponentList<VEvent> vevents = filterCal.getComponents().getComponents(VEvent.VEVENT);        
    for(VEvent c : vevents) {            
        Assert.assertNotSame("event 6 changed 2",c.getProperties().getProperty("SUMMARY").getValue());
    }   
}
 
Example #16
Source File: EventStampFilter.java    From cosmo with Apache License 2.0 5 votes vote down vote up
/**
 * Matches events that occur in a given time-range.
 * @param period time-range
 */
public void setPeriod(Period period) {
    this.period = period;
    // Get fixed start/end time
    dstart = period.getStart();
    dend = period.getEnd();
    
    // set timezone on floating times
    updateFloatingTimes();
}
 
Example #17
Source File: EventStampFilter.java    From cosmo with Apache License 2.0 5 votes vote down vote up
/**
 * Matches events that occur in a given time-range.
 * @param start range start
 * @param end range end
 */
public void setTimeRange(Date start, Date end) {
    dstart = utc(start);
    dend = utc(end);
    
    fstart = start;
    fend = end;
    
    updateFloatingTimes();
    period = new Period(dstart, dend);
}
 
Example #18
Source File: ICalRecurConverter.java    From scipio-erp with Apache License 2.0 5 votes vote down vote up
@Override
public void visit(TemporalExpressions.DateRange expr) {
    if (this.state.isExcluded) {
        throw new IllegalStateException("iCalendar does not support excluded date ranges");
    }
    org.ofbiz.base.util.DateRange range = expr.getDateRange();
    PeriodList periodList = new PeriodList();
    periodList.add(new Period(new DateTime(range.start()), new DateTime(range.end())));
    this.incDateList.add(new RDate(periodList));
}
 
Example #19
Source File: CalendarFilterEvaluaterTest.java    From cosmo with Apache License 2.0 4 votes vote down vote up
/**
 * Tests evaluate complicated. 
 * @throws Exception - if something is wrong this exception is thrown.
 */
@Test
public void testEvaluateComplicated() throws Exception {
    
    CalendarFilterEvaluater evaluater = new CalendarFilterEvaluater();
    Calendar calendar = getCalendar("cal1.ics");
    
    CalendarFilter filter = new CalendarFilter();
    ComponentFilter compFilter = new ComponentFilter("VCALENDAR");
    ComponentFilter eventFilter = new ComponentFilter("VEVENT");
    filter.setFilter(compFilter);
    compFilter.getComponentFilters().add(eventFilter);
    
    DateTime start = new DateTime("20050816T115000Z");
    DateTime end = new DateTime("20050916T115000Z");

    Period period = new Period(start, end);
    TimeRangeFilter timeRangeFilter = new TimeRangeFilter(period);
   
    eventFilter.setTimeRangeFilter(timeRangeFilter);
    
    PropertyFilter propFilter = new PropertyFilter("SUMMARY");
    TextMatchFilter textFilter = new TextMatchFilter("Visible");
    propFilter.setTextMatchFilter(textFilter);
    eventFilter.getPropFilters().add(propFilter);
    
    PropertyFilter propFilter2 = new PropertyFilter("DTSTART");
    ParamFilter paramFilter2 = new ParamFilter("VALUE");
    TextMatchFilter textFilter2 = new TextMatchFilter("DATE-TIME");
    paramFilter2.setTextMatchFilter(textFilter2);
    propFilter2.getParamFilters().add(paramFilter2);
    
    Period period2 = new Period(start, end);
    TimeRangeFilter timeRangeFilter2 = new TimeRangeFilter(period2);
    propFilter2.setTimeRangeFilter(timeRangeFilter2);
    
    eventFilter.getPropFilters().add(propFilter2);
    
    Assert.assertTrue(evaluater.evaluate(calendar, filter));
    
    // change one thing
    paramFilter2.setName("XXX");
    Assert.assertFalse(evaluater.evaluate(calendar, filter));
}
 
Example #20
Source File: ExpandRecurringEventsTest.java    From cosmo with Apache License 2.0 4 votes vote down vote up
/**
 * Tests expand event.
 * @throws Exception - if something is wrong this exception is thrown.
 */
@Test
public void testExpandEvent() throws Exception {
    CalendarBuilder cb = new CalendarBuilder();
    FileInputStream fis = new FileInputStream(baseDir + "expand_recurr_test1.ics");
    Calendar calendar = cb.build(fis);
    
    Assert.assertEquals(1, 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("20060102T140000", tz);
    DateTime end = new DateTime("20060105T140000", tz);
    start.setUtc(true);
    end.setUtc(true);
    
    Period period = new Period(start, end);
    filter.setExpand(period);
    filter.setAllSubComponents();
    filter.setAllProperties();
    
    StringBuilder buffer = new StringBuilder();
    filter.filter(calendar, buffer);
    StringReader sr = new StringReader(buffer.toString());
    
    Calendar filterCal = cb.build(sr);
    
    ComponentList<VEvent> comps = filterCal.getComponents().getComponents("VEVENT");
    
    // Should expand to 3 event components
    Assert.assertEquals(3, comps.size());
            
    Iterator<VEvent> it = comps.iterator();
    VEvent event = it.next();
    
    Assert.assertEquals("event 6", event.getProperties().getProperty(Property.SUMMARY).getValue());
    Assert.assertEquals("20060102T190000Z", event.getStartDate().getDate().toString());
    Assert.assertEquals("20060102T190000Z", event.getRecurrenceId().getDate().toString());
    
    event = it.next();
    Assert.assertEquals("event 6", event.getProperties().getProperty(Property.SUMMARY).getValue());
    Assert.assertEquals("20060103T190000Z", event.getStartDate().getDate().toString());
    Assert.assertEquals("20060103T190000Z", event.getRecurrenceId().getDate().toString());
    
    event = it.next();
    Assert.assertEquals("event 6", event.getProperties().getProperty(Property.SUMMARY).getValue());
    Assert.assertEquals("20060104T190000Z", event.getStartDate().getDate().toString());
    Assert.assertEquals("20060104T190000Z", event.getRecurrenceId().getDate().toString());
    
    verifyExpandedCalendar(filterCal);
}
 
Example #21
Source File: StandardCalendarQueryProcessor.java    From cosmo with Apache License 2.0 4 votes vote down vote up
private CalendarFilter[] createQueryFilters(CollectionItem collection, Period period) {
    DateTime start = period.getStart();
    DateTime end = period.getEnd();
    CalendarFilter[] filters = new CalendarFilter[2];
    TimeZone tz = null;

    // Create calendar-filter elements designed to match
    // VEVENTs/VFREEBUSYs within the specified time range.
    //
    // <C:filter>
    // <C:comp-filter name="VCALENDAR">
    // <C:comp-filter name="VEVENT">
    // <C:time-range start="20051124T000000Z"
    // end="20051125T000000Z"/>
    // </C:comp-filter>
    // <C:comp-filter name="VFREEBUSY">
    // <C:time-range start="20051124T000000Z"
    // end="20051125T000000Z"/>
    // </C:comp-filter>
    // </C:comp-filter>
    // </C:filter>

    // If the calendar collection has a timezone attribute,
    // then use that to convert floating date/times to UTC
    CalendarCollectionStamp ccs = StampUtils.getCalendarCollectionStamp(collection);
    if (ccs!=null) {
        tz = ccs.getTimezone();
    }

    ComponentFilter eventFilter = new ComponentFilter(Component.VEVENT);
    eventFilter.setTimeRangeFilter(new TimeRangeFilter(start, end));
    if(tz!=null) {
        eventFilter.getTimeRangeFilter().setTimezone(tz.getVTimeZone());
    }

    ComponentFilter calFilter = new ComponentFilter(Calendar.VCALENDAR);
    calFilter.getComponentFilters().add(eventFilter);

    CalendarFilter filter = new CalendarFilter();
    filter.setFilter(calFilter);

    filters[0] = filter;

    ComponentFilter freebusyFilter = new ComponentFilter(
            Component.VFREEBUSY);
    freebusyFilter.setTimeRangeFilter(new TimeRangeFilter(start, end));
    if(tz!=null) {
        freebusyFilter.getTimeRangeFilter().setTimezone(tz.getVTimeZone());
    }

    calFilter = new ComponentFilter(Calendar.VCALENDAR);
    calFilter.getComponentFilters().add(freebusyFilter);

    filter = new CalendarFilter();
    filter.setFilter(calFilter);

    filters[1] = filter;

    return filters;
}
 
Example #22
Source File: ExpandRecurringEventsTest.java    From cosmo with Apache License 2.0 4 votes vote down vote up
/**
 * Tests expand event with overrides.
 * @throws Exception - if something is wrong this exception is thrown.
 */
@Test
public void testExpandEventWithOverrides() throws Exception {
    CalendarBuilder cb = new CalendarBuilder();
    FileInputStream fis = new FileInputStream(baseDir + "expand_recurr_test2.ics");
    Calendar calendar = cb.build(fis);
    
    ComponentList<VEvent> comps = calendar.getComponents().getComponents("VEVENT");
    
    Assert.assertEquals(5, comps.size());
    
    VTimeZone vtz = (VTimeZone) calendar.getComponents().getComponent("VTIMEZONE");
    TimeZone tz = new TimeZone(vtz);
    OutputFilter filter = new OutputFilter("test");
    DateTime start = new DateTime("20060102T140000", tz);
    DateTime end = new DateTime("20060105T140000", tz);
    start.setUtc(true);
    end.setUtc(true);
    
    Period period = new Period(start, end);
    filter.setExpand(period);
    filter.setAllSubComponents();
    filter.setAllProperties();
    
    StringBuilder buffer = new StringBuilder();
    filter.filter(calendar, buffer);
    StringReader sr = new StringReader(buffer.toString());
    
    Calendar filterCal = cb.build(sr);
    
    comps = filterCal.getComponents().getComponents("VEVENT");
    
    // Should expand to 3 event components
    Assert.assertEquals(3, comps.size());
            
    Iterator<VEvent> it = comps.iterator();
    VEvent event = it.next();
    
    Assert.assertEquals("event 6", event.getProperties().getProperty(Property.SUMMARY).getValue());
    Assert.assertEquals("20060102T190000Z", event.getStartDate().getDate().toString());
    Assert.assertEquals("20060102T190000Z", event.getRecurrenceId().getDate().toString());
    
    event = it.next();
    Assert.assertEquals("event 6", event.getProperties().getProperty(Property.SUMMARY).getValue());
    Assert.assertEquals("20060103T190000Z", event.getStartDate().getDate().toString());
    Assert.assertEquals("20060103T190000Z", event.getRecurrenceId().getDate().toString());
    
    event = it.next();
    Assert.assertEquals("event 6 changed", event.getProperties().getProperty(Property.SUMMARY).getValue());
    Assert.assertEquals("20060104T210000Z", event.getStartDate().getDate().toString());
    Assert.assertEquals("20060104T190000Z", event.getRecurrenceId().getDate().toString());
    
    verifyExpandedCalendar(filterCal);
}
 
Example #23
Source File: CaldavOutputFilter.java    From cosmo with Apache License 2.0 4 votes vote down vote up
/**
 * Returns an <code>OutputFilter</code> representing the given
 * <code>&lt;C:calendar-data/&gt;> element.
 * @param cdata the given calendar data.
 * @return output filter.
 * @throws CosmoDavException - if something is wrong this exception is thrown.
 */
public static OutputFilter createFromXml(Element cdata)
    throws CosmoDavException {
    OutputFilter result = null;
    Period expand = null;
    Period limit = null;
    Period limitfb = null;

    String contentType =
        DomUtil.getAttribute(cdata, ATTR_CALDAV_CONTENT_TYPE,
                             NAMESPACE_CALDAV);
    if (contentType != null && ! contentType.equals(ICALENDAR_MEDIA_TYPE)) {
        throw new UnsupportedCalendarDataException(contentType);
    }
    String version =
        DomUtil.getAttribute(cdata, ATTR_CALDAV_CONTENT_TYPE,
                             NAMESPACE_CALDAV);
    if (version != null && ! version.equals(ICALENDAR_VERSION)) {
            throw new UnsupportedCalendarDataException();
    }

    // Look at each child element of calendar-data
    for (ElementIterator iter = DomUtil.getChildren(cdata);
         iter.hasNext();) {

        Element child = iter.nextElement();
        if (ELEMENT_CALDAV_COMP.equals(child.getLocalName())) {

            // At the top-level of calendar-data there should only be one
            // <comp> element as VCALENDAR components are the only top-level
            // components allowed in iCalendar data
            if (result != null) {
                return null;
            }

            // Get required name attribute and verify it is VCALENDAR
            String name = 
                DomUtil.getAttribute(child, ATTR_CALDAV_NAME, null);
            if (name == null || !Calendar.VCALENDAR.equals(name)) {
                return null;
            }

            // Now parse filter item
            result = parseCalendarDataComp(child);

        } else if (ELEMENT_CALDAV_EXPAND.equals(child.getLocalName())) {
            expand = parsePeriod(child, true);
        } else if (ELEMENT_CALDAV_LIMIT_RECURRENCE_SET.
                   equals(child.getLocalName())) {
            limit = parsePeriod(child, true);
        } else if (ELEMENT_CALDAV_LIMIT_FREEBUSY_SET.
                   equals(child.getLocalName())) {
            limitfb = parsePeriod(child, true);
        } else {
            LOG.warn("Ignoring child {} of {}", child.getTagName(), cdata.getTagName());
        }
    }

    // Now add any limit/expand options, creating a filter if one is not
    // already present
    if (result == null
            && (expand != null || limit != null || limitfb != null)) {
        result = new OutputFilter("VCALENDAR");
        result.setAllSubComponents();
        result.setAllProperties();
    }
    if (expand != null) {
        result.setExpand(expand);
    }
    if (limit != null) {
        result.setLimit(limit);
    }
    if (limitfb != null) {
        result.setLimitfb(limitfb);
    }

    return result;
}
 
Example #24
Source File: ExpandRecurringEventsTest.java    From cosmo with Apache License 2.0 4 votes vote down vote up
/**
 * Removed test expand with non recurring event.
 * @throws Exception - if something is wrong this exception is thrown.
 */
public void removedTestExpandNonRecurringEvent() throws Exception {
    CalendarBuilder cb = new CalendarBuilder();
    FileInputStream fis = new FileInputStream(baseDir + "expand_nonrecurr_test3.ics");
    Calendar calendar = cb.build(fis);
    
    Assert.assertEquals(1, 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("20060102T140000", tz);
    DateTime end = new DateTime("20060105T140000", tz);
    start.setUtc(true);
    end.setUtc(true);
    
    Period period = new Period(start, end);
    filter.setExpand(period);
    filter.setAllSubComponents();
    filter.setAllProperties();
    
    StringBuilder buffer = new StringBuilder();
    filter.filter(calendar, buffer);
    StringReader sr = new StringReader(buffer.toString());
    
    Calendar filterCal = cb.build(sr);
    
    ComponentList<VEvent> comps = filterCal.getComponents().getComponents("VEVENT");
    
    // Should be the same component
    Assert.assertEquals(1, comps.size());
            
    Iterator<VEvent> it = comps.iterator();
    VEvent event = it.next();
    
    Assert.assertEquals("event 6", event.getProperties().getProperty(Property.SUMMARY).getValue());
    Assert.assertEquals("20060102T190000Z", event.getStartDate().getDate().toString());
    Assert.assertNull(event.getRecurrenceId());
    
    verifyExpandedCalendar(filterCal);
}
 
Example #25
Source File: CalendarFilterConverterTest.java    From cosmo with Apache License 2.0 4 votes vote down vote up
/**
 * Tests translate item to filter.
 * @throws Exception - if something is wrong this exception is thrown.
 */
@Test
public void testTranslateItemToFilter() throws Exception {
    CollectionItem calendar = new HibCollectionItem();
    calendar.setUid("calendar");
    CalendarFilter calFilter = new CalendarFilter();
    ComponentFilter rootComp = new ComponentFilter();
    rootComp.setName("VCALENDAR");
    calFilter.setFilter(rootComp);
    ComponentFilter eventComp = new ComponentFilter();
    eventComp.setName("VEVENT");
    rootComp.getComponentFilters().add(eventComp);
    
    Period period = new Period(new DateTime("20070101T100000Z"), new DateTime("20070201T100000Z"));
    TimeRangeFilter timeRangeFilter = new TimeRangeFilter(period);
    eventComp.setTimeRangeFilter(timeRangeFilter);
    
    PropertyFilter uidFilter = new PropertyFilter();
    uidFilter.setName("UID");
    TextMatchFilter uidMatch = new TextMatchFilter();
    uidMatch.setValue("uid");
    uidMatch.setCaseless(false);
    uidFilter.setTextMatchFilter(uidMatch);
    eventComp.getPropFilters().add(uidFilter);
    
    PropertyFilter summaryFilter = new PropertyFilter();
    summaryFilter.setName("SUMMARY");
    TextMatchFilter summaryMatch = new TextMatchFilter();
    summaryMatch.setValue("summary");
    summaryMatch.setCaseless(false);
    summaryFilter.setTextMatchFilter(summaryMatch);
    eventComp.getPropFilters().add(summaryFilter);
    
    PropertyFilter descFilter = new PropertyFilter();
    descFilter.setName("DESCRIPTION");
    TextMatchFilter descMatch = new TextMatchFilter();
    descMatch.setValue("desc");
    descMatch.setCaseless(true);
    descFilter.setTextMatchFilter(descMatch);
    eventComp.getPropFilters().add(descFilter);
    
    ItemFilter itemFilter = converter.translateToItemFilter(calendar, calFilter);
    
    Assert.assertTrue(itemFilter instanceof NoteItemFilter);
    NoteItemFilter noteFilter = (NoteItemFilter) itemFilter;
    Assert.assertEquals(calendar.getUid(), noteFilter.getParent().getUid());
    Assert.assertTrue(noteFilter.getDisplayName() instanceof LikeExpression);
    verifyFilterExpressionValue(noteFilter.getDisplayName(), "summary");
    Assert.assertTrue(noteFilter.getIcalUid() instanceof LikeExpression);
    verifyFilterExpressionValue(noteFilter.getIcalUid(), "uid");
    Assert.assertTrue(noteFilter.getBody() instanceof ILikeExpression);
    verifyFilterExpressionValue(noteFilter.getBody(), "desc");
   
    EventStampFilter sf = (EventStampFilter) noteFilter.getStampFilter(EventStampFilter.class);
    Assert.assertNotNull(sf);
    Assert.assertNotNull(sf.getPeriod());
    Assert.assertEquals(sf.getPeriod().getStart().toString(), "20070101T100000Z");
    Assert.assertEquals(sf.getPeriod().getEnd().toString(), "20070201T100000Z");
}
 
Example #26
Source File: TimeRangeFilter.java    From cosmo with Apache License 2.0 4 votes vote down vote up
public void setPeriod(Period period) {
    this.period = period;
    // Get fixed start/end time
    dstart = period.getStart();
    dend = period.getEnd();
}
 
Example #27
Source File: TimeRangeFilter.java    From cosmo with Apache License 2.0 4 votes vote down vote up
public Period getPeriod() {
    return period;
}
 
Example #28
Source File: EventStampFilter.java    From cosmo with Apache License 2.0 4 votes vote down vote up
public Period getPeriod() {
    return period;
}
 
Example #29
Source File: StandardCalendarQueryProcessorTest.java    From cosmo with Apache License 2.0 3 votes vote down vote up
/**
 * Test the add of busy periods recurring all day.
 * @throws Exception - if something is wrong this exception is thrown.
 */
@Test
public void testAddBusyPeriodsRecurringAllDay() throws Exception {
    PeriodList busyPeriods = new PeriodList();
    PeriodList busyTentativePeriods = new PeriodList();
    PeriodList busyUnavailablePeriods = new PeriodList();
    
    // the range
    DateTime start = new DateTime("20070103T090000Z");
    DateTime end = new DateTime("20070117T090000Z");
    
    Period fbRange = new Period(start, end);
    
    Calendar calendar = CalendarUtils.parseCalendar(testHelper.getBytes("allday_weekly_recurring.ics"));
    
    // test several timezones
    TimeZone tz = TIMEZONE_REGISTRY.getTimeZone("America/Chicago");
    
    queryProcessor.addBusyPeriods(calendar, tz, fbRange, busyPeriods, busyTentativePeriods, busyUnavailablePeriods);
    
    Assert.assertEquals("20070108T060000Z/20070109T060000Z,20070115T060000Z/20070116T060000Z", busyPeriods.toString());
    
    busyPeriods.clear();
    
    tz = TIMEZONE_REGISTRY.getTimeZone("America/Los_Angeles");
    queryProcessor.addBusyPeriods(calendar, tz, fbRange, busyPeriods, busyTentativePeriods, busyUnavailablePeriods);
    
    Assert.assertEquals("20070108T080000Z/20070109T080000Z,20070115T080000Z/20070116T080000Z", busyPeriods.toString());
    
    busyPeriods.clear();
    
    tz = TIMEZONE_REGISTRY.getTimeZone("Australia/Sydney");
    queryProcessor.addBusyPeriods(calendar, tz, fbRange, busyPeriods, busyTentativePeriods, busyUnavailablePeriods);
    
    Assert.assertEquals("20070107T130000Z/20070108T130000Z,20070114T130000Z/20070115T130000Z", busyPeriods.toString());
}
 
Example #30
Source File: FreeBusyUtils.java    From cosmo with Apache License 2.0 3 votes vote down vote up
/**
    A VFREEBUSY component overlaps a given time range if the condition
    for the corresponding component state specified in the table below
    is satisfied.  The conditions depend on the presence in the
    VFREEBUSY component of the DTSTART and DTEND properties, and any
    FREEBUSY properties in the absence of DTSTART and DTEND.  Any
    DURATION property is ignored, as it has a special meaning when
    used in a VFREEBUSY component.

    When only FREEBUSY properties are used, each period in each
    FREEBUSY property is compared against the time range, irrespective
    of the type of free busy information (free, busy, busy-tentative,
    busy-unavailable) represented by the property.


    +------------------------------------------------------+
    | VFREEBUSY has both the DTSTART and DTEND properties? |
    |   +--------------------------------------------------+
    |   | VFREEBUSY has the FREEBUSY property?             |
    |   |   +----------------------------------------------+
    |   |   | Condition to evaluate                        |
    +---+---+----------------------------------------------+
    | Y | * | (start <= DTEND) AND (end > DTSTART)         |
    +---+---+----------------------------------------------+
    | N | Y | (start <  freebusy-period-end) AND           |
    |   |   | (end   >  freebusy-period-start)             |
    +---+---+----------------------------------------------+
    | N | N | FALSE                                        |
    +---+---+----------------------------------------------+
 *
 * @param freeBusy comoponent to test
 * @param period period to test against
 * @param tz timezone to use for floating times
 * @return true if component overlaps specified range, false otherwise
 */
public static boolean overlapsPeriod(VFreeBusy freeBusy, Period period, TimeZone tz){
    
    DtStart start = freeBusy.getStartDate();
    DtEnd end = freeBusy.getEndDate();
     
    if (start != null && end != null) {
        InstanceList instances = new InstanceList();
        instances.setTimezone(tz);
        instances.addComponent(freeBusy, period.getStart(),period.getEnd());
        return instances.size() > 0;
    }
    
    PropertyList<FreeBusy> props = freeBusy.getProperties(Property.FREEBUSY);
    if (props.size()==0) {
        return false;
    }
    
    for (FreeBusy fb: props) {            
        PeriodList periods = fb.getPeriods();
        Iterator<Period> periodIt = periods.iterator();
        while(periodIt.hasNext()) {
            Period fbPeriod = periodIt.next();
            if(fbPeriod.intersects(period)) {
                return true;
            }
        }
    }
    
    return false;
}