net.fortuna.ical4j.model.property.DtEnd Java Examples

The following examples show how to use net.fortuna.ical4j.model.property.DtEnd. 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: ICalendarUtils.java    From cosmo with Apache License 2.0 6 votes vote down vote up
/**
 * Get the duration for an event.  If the DURATION property
 * exist, use that.  Else, calculate duration from DTSTART and
 * DTEND.
 * @param event The event.
 * @return duration for event
 */
public static Dur getDuration(VEvent event) {
    Duration duration = (Duration)
        event.getProperties().getProperty(Property.DURATION);
    if (duration != null) {
        return duration.getDuration();
    }
    DtStart dtstart = event.getStartDate();
    if (dtstart == null) {
        return null;
    }
    DtEnd dtend = (DtEnd) event.getProperties().getProperty(Property.DTEND);
    if (dtend == null) {
        return null;
    }
    return new Duration(dtstart.getDate(), dtend.getDate()).getDuration();
}
 
Example #2
Source File: EventValidator.java    From cosmo with Apache License 2.0 6 votes vote down vote up
private static boolean isEventValid(VEvent event, ValidationConfig config) {
    if (config == null) {
        LOG.error("ValidationConfig cannot be null");
        return false;
    }
    DtStart startDate = event.getStartDate();
    DtEnd endDate = event.getEndDate(true);
    if (startDate == null || startDate.getDate() == null
            || endDate != null && startDate.getDate().after(endDate.getDate())) {

        return false;
    }

    for (PropertyValidator validator : values()) {
        if (!validator.isValid(event, config)) {
            return false;
        }
    }

    return areTimeZoneIdsValid(event);
}
 
Example #3
Source File: StandardContentServiceTest.java    From cosmo with Apache License 2.0 6 votes vote down vote up
@Test(expected=IllegalArgumentException.class)
public void testCreateContentThrowsExceptionForInvalidDates() throws Exception {
    User user = testHelper.makeDummyUser();
    CollectionItem rootCollection = contentDao.createRootItem(user);

    NoteItem noteItem = new MockNoteItem();
    noteItem.getAttributeValue("");
    noteItem.setName("foo");
    noteItem.setOwner(user);
    
    Calendar c = new Calendar();
    VEvent e = new VEvent();
    e.getProperties().add(new DtStart("20131010T101010Z"));
    e.getProperties().add(new DtEnd("20131010T091010Z"));
    
    c.getComponents().add(e);
    MockEventStamp mockEventStamp = new MockEventStamp();
    mockEventStamp.setEventCalendar(c);
    noteItem.addStamp(mockEventStamp);
    
    service.createContent(rootCollection, noteItem);
}
 
Example #4
Source File: StandardContentServiceTest.java    From cosmo with Apache License 2.0 6 votes vote down vote up
@Test(expected=IllegalArgumentException.class)
public void testUpdateCollectionFailsForEventsWithInvalidDates() throws Exception {
    User user = testHelper.makeDummyUser();
    CollectionItem rootCollection = contentDao.createRootItem(user);

    NoteItem noteItem = new MockNoteItem();
    noteItem.getAttributeValue("");
    noteItem.setName("foo");
    noteItem.setOwner(user);
    
    Calendar c = new Calendar();
    VEvent e = new VEvent();
    e.getProperties().add(new DtStart("20131010T101010Z"));
    e.getProperties().add(new DtEnd("20131010T091010Z"));
    
    c.getComponents().add(e);
    MockEventStamp mockEventStamp = new MockEventStamp();
    mockEventStamp.setEventCalendar(c);
    noteItem.addStamp(mockEventStamp);
    
    service.updateCollection(rootCollection, Collections.singleton((Item)noteItem));
}
 
Example #5
Source File: Util.java    From openhab1-addons with Eclipse Public License 2.0 6 votes vote down vote up
public static Calendar createCalendar(CalDavEvent calDavEvent, DateTimeZone timeZone) {
    TimeZoneRegistry registry = TimeZoneRegistryFactory.getInstance().createRegistry();
    TimeZone timezone = registry.getTimeZone(timeZone.getID());

    Calendar calendar = new Calendar();
    calendar.getProperties().add(Version.VERSION_2_0);
    calendar.getProperties().add(new ProdId("openHAB"));
    VEvent vEvent = new VEvent();
    vEvent.getProperties().add(new Summary(calDavEvent.getName()));
    vEvent.getProperties().add(new Description(calDavEvent.getContent()));
    final DtStart dtStart = new DtStart(new net.fortuna.ical4j.model.DateTime(calDavEvent.getStart().toDate()));
    dtStart.setTimeZone(timezone);
    vEvent.getProperties().add(dtStart);
    final DtEnd dtEnd = new DtEnd(new net.fortuna.ical4j.model.DateTime(calDavEvent.getEnd().toDate()));
    dtEnd.setTimeZone(timezone);
    vEvent.getProperties().add(dtEnd);
    vEvent.getProperties().add(new Uid(calDavEvent.getId()));
    vEvent.getProperties().add(Clazz.PUBLIC);
    vEvent.getProperties()
            .add(new LastModified(new net.fortuna.ical4j.model.DateTime(calDavEvent.getLastChanged().toDate())));
    calendar.getComponents().add(vEvent);

    return calendar;
}
 
Example #6
Source File: ICalUtils.java    From camel-quarkus with Apache License 2.0 5 votes vote down vote up
protected static Calendar createTestCalendar(ZonedDateTime start, ZonedDateTime end, String summary, String attendee) {
    // Create a TimeZone
    TimeZoneRegistry registry = TimeZoneRegistryFactory.getInstance().createRegistry();
    String tzId = start.getZone().getId();
    TimeZone timezone = registry.getTimeZone(tzId.equals("Z") ? "UTC" : tzId);
    VTimeZone tz = timezone.getVTimeZone();

    // Create the event
    PropertyList propertyList = new PropertyList();
    DateTime ts = new DateTime(true);
    ts.setTime(0);
    propertyList.add(new DtStamp(ts));
    propertyList.add(new DtStart(toDateTime(start, registry)));
    propertyList.add(new DtEnd(toDateTime(end, registry)));
    propertyList.add(new Summary(summary));
    VEvent meeting = new VEvent(propertyList);

    // add timezone info..
    meeting.getProperties().add(tz.getTimeZoneId());

    // generate unique identifier..
    meeting.getProperties().add(new Uid("00000000"));

    // add attendees..
    Attendee dev1 = new Attendee(URI.create("mailto:" + attendee));
    dev1.getParameters().add(Role.REQ_PARTICIPANT);
    dev1.getParameters().add(new Cn(attendee));
    meeting.getProperties().add(dev1);

    // Create a calendar
    net.fortuna.ical4j.model.Calendar icsCalendar = new net.fortuna.ical4j.model.Calendar();
    icsCalendar.getProperties().add(Version.VERSION_2_0);
    icsCalendar.getProperties().add(new ProdId("-//Events Calendar//iCal4j 1.0//EN"));
    icsCalendar.getProperties().add(CalScale.GREGORIAN);

    // Add the event and print
    icsCalendar.getComponents().add(meeting);
    return icsCalendar;
}
 
Example #7
Source File: ICalConverter.java    From scipio-erp with Apache License 2.0 5 votes vote down vote up
protected static Timestamp fromDtEnd(PropertyList<Property> propertyList) {
    DtEnd iCalObj = (DtEnd) propertyList.getProperty(DtEnd.DTEND);
    if (iCalObj == null) {
        return null;
    }
    Date date = iCalObj.getDate();
    return new Timestamp(date.getTime());
}
 
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: StandardContentService.java    From cosmo with Apache License 2.0 5 votes vote down vote up
private void checkDatesForComponent(Component component){
    if(component == null){
        return;
    }
    
    Property dtStart = component.getProperty(Property.DTSTART);
    Property dtEnd = component.getProperty(Property.DTEND);
    
    if( dtStart instanceof DtStart && dtStart.getValue()!= null 
        && dtEnd instanceof DtEnd && dtEnd.getValue() != null 
       && ((DtStart)dtStart).getDate().compareTo(((DtEnd)dtEnd).getDate()) > 0 ){
        throw new IllegalArgumentException("End date [" + dtEnd + " is lower than start date [" + dtStart + "]");
    }
    
}
 
Example #10
Source File: StandardContentServiceTest.java    From cosmo with Apache License 2.0 5 votes vote down vote up
private Calendar createEventCalendarWithDates(String start, String end) throws ParseException{
    Calendar c = new Calendar();
    VEvent e = new VEvent();
    e.getProperties().add(new DtStart(start));
    e.getProperties().add(new DtEnd(end));
    
    c.getComponents().add(e);
    
    return c;
}
 
Example #11
Source File: ICalFormatTest.java    From wildfly-camel with Apache License 2.0 5 votes vote down vote up
private Calendar createTestCalendar() throws ParseException {
    // Create a TimeZone
    TimeZoneRegistry registry = TimeZoneRegistryFactory.getInstance().createRegistry();
    TimeZone timezone = registry.getTimeZone("America/New_York");
    VTimeZone tz = timezone.getVTimeZone();

    // Start Date is on: April 1, 2013, 9:00 am
    java.util.Calendar startDate = new GregorianCalendar();
    startDate.setTimeZone(timezone);
    startDate.set(java.util.Calendar.MONTH, java.util.Calendar.APRIL);
    startDate.set(java.util.Calendar.DAY_OF_MONTH, 1);
    startDate.set(java.util.Calendar.YEAR, 2013);
    startDate.set(java.util.Calendar.HOUR_OF_DAY, 17);
    startDate.set(java.util.Calendar.MINUTE, 0);
    startDate.set(java.util.Calendar.SECOND, 0);

    // End Date is on: April 1, 2013, 13:00
    java.util.Calendar endDate = new GregorianCalendar();
    endDate.setTimeZone(timezone);
    endDate.set(java.util.Calendar.MONTH, java.util.Calendar.APRIL);
    endDate.set(java.util.Calendar.DAY_OF_MONTH, 1);
    endDate.set(java.util.Calendar.YEAR, 2013);
    endDate.set(java.util.Calendar.HOUR_OF_DAY, 21);
    endDate.set(java.util.Calendar.MINUTE, 0);
    endDate.set(java.util.Calendar.SECOND, 0);

    // Create the event
    PropertyList propertyList = new PropertyList();
    propertyList.add(new DtStamp("20130324T180000Z"));
    propertyList.add(new DtStart(new DateTime(startDate.getTime())));
    propertyList.add(new DtEnd(new DateTime(endDate.getTime())));
    propertyList.add(new Summary("Progress Meeting"));
    VEvent meeting = new VEvent(propertyList);

    // add timezone info..
    meeting.getProperties().add(tz.getTimeZoneId());

    // generate unique identifier..
    meeting.getProperties().add(new Uid("00000000"));

    // add attendees..
    Attendee dev1 = new Attendee(URI.create("mailto:[email protected]"));
    dev1.getParameters().add(Role.REQ_PARTICIPANT);
    dev1.getParameters().add(new Cn("Developer 1"));
    meeting.getProperties().add(dev1);

    Attendee dev2 = new Attendee(URI.create("mailto:[email protected]"));
    dev2.getParameters().add(Role.OPT_PARTICIPANT);
    dev2.getParameters().add(new Cn("Developer 2"));
    meeting.getProperties().add(dev2);

    // Create a calendar
    net.fortuna.ical4j.model.Calendar icsCalendar = new net.fortuna.ical4j.model.Calendar();
    icsCalendar.getProperties().add(Version.VERSION_2_0);
    icsCalendar.getProperties().add(new ProdId("-//Events Calendar//iCal4j 1.0//EN"));
    icsCalendar.getProperties().add(CalScale.GREGORIAN);

    // Add the event and print
    icsCalendar.getComponents().add(meeting);
    return icsCalendar;
}
 
Example #12
Source File: ICalConverter.java    From scipio-erp with Apache License 2.0 4 votes vote down vote up
protected static DtEnd toDtEnd(Timestamp javaObj) {
    if (javaObj == null) {
        return null;
    }
    return new DtEnd(new DateTime(javaObj));
}
 
Example #13
Source File: ICalendarUtils.java    From cosmo with Apache License 2.0 4 votes vote down vote up
/**
 * Return the date that a trigger refers to, which can be an absolute
 * date or a date relative to the start or end time of a parent 
 * component (VEVENT/VTODO).
 * @param trigger The trigger.
 * @param parent The component.
 * @return date of trigger.
 */
public static Date getTriggerDate(Trigger trigger, Component parent) {
    
    if(trigger==null) {
        return null;
    }
    
    // if its absolute then we are done
    if(trigger.getDateTime()!=null) {
        return trigger.getDateTime();
    }
    
    // otherwise we need a start date if VEVENT
    DtStart start = (DtStart) parent.getProperty(Property.DTSTART);
    if(start==null && parent instanceof VEvent) {
        return null;
    }
    
    // is trigger relative to start or end
    Related related = (Related) trigger.getParameter(Parameter.RELATED);
    if(related==null || related.equals(Related.START)) {    
        // must have start date
        if(start==null) {
            return null;
        }
        
        // relative to start
        return Dates.getInstance(trigger.getDuration().getTime(start.getDate()), start.getDate());
    } else {
        // relative to end
        Date endDate = null;
        
        // need an end date or duration or due 
        DtEnd end = (DtEnd) parent.getProperty(Property.DTEND);
        if(end!=null) {
            endDate = end.getDate();
        }
       
        if(endDate==null) {
            Duration dur = (Duration) parent.getProperty(Property.DURATION);
            if(dur!=null && start!=null) {
                endDate= Dates.getInstance(dur.getDuration().getTime(start.getDate()), start.getDate());
            }
        }
        
        if(endDate==null) {
            Due due = (Due) parent.getProperty(Property.DUE);
            if(due!=null) {
                endDate = due.getDate();
            }
        }
        
        // require end date
        if(endDate==null) {
            return null;
        }
        
        return Dates.getInstance(trigger.getDuration().getTime(endDate), endDate);
    }
}
 
Example #14
Source File: ICalendarService.java    From axelor-open-suite with GNU Affero General Public License v3.0 4 votes vote down vote up
@Transactional
protected ICalendarEvent findOrCreateEvent(VEvent vEvent, ICalendar calendar) {

  String uid = vEvent.getUid().getValue();
  DtStart dtStart = vEvent.getStartDate();
  DtEnd dtEnd = vEvent.getEndDate();

  ICalendarEvent event = iEventRepo.findByUid(uid);
  if (event == null) {
    event = ICalendarEventFactory.getNewIcalEvent(calendar);
    event.setUid(uid);
    event.setCalendar(calendar);
  }

  ZoneId zoneId = OffsetDateTime.now().getOffset();
  if (dtStart.getDate() != null) {
    if (dtStart.getTimeZone() != null) {
      zoneId = dtStart.getTimeZone().toZoneId();
    }
    event.setStartDateTime(LocalDateTime.ofInstant(dtStart.getDate().toInstant(), zoneId));
  }

  if (dtEnd.getDate() != null) {
    if (dtEnd.getTimeZone() != null) {
      zoneId = dtEnd.getTimeZone().toZoneId();
    }
    event.setEndDateTime(LocalDateTime.ofInstant(dtEnd.getDate().toInstant(), zoneId));
  }

  event.setAllDay(!(dtStart.getDate() instanceof DateTime));

  event.setSubject(getValue(vEvent, Property.SUMMARY));
  event.setDescription(getValue(vEvent, Property.DESCRIPTION));
  event.setLocation(getValue(vEvent, Property.LOCATION));
  event.setGeo(getValue(vEvent, Property.GEO));
  event.setUrl(getValue(vEvent, Property.URL));
  event.setSubjectTeam(event.getSubject());
  if (Clazz.PRIVATE.getValue().equals(getValue(vEvent, Property.CLASS))) {
    event.setVisibilitySelect(ICalendarEventRepository.VISIBILITY_PRIVATE);
  } else {
    event.setVisibilitySelect(ICalendarEventRepository.VISIBILITY_PUBLIC);
  }
  if (Transp.TRANSPARENT.getValue().equals(getValue(vEvent, Property.TRANSP))) {
    event.setDisponibilitySelect(ICalendarEventRepository.DISPONIBILITY_AVAILABLE);
  } else {
    event.setDisponibilitySelect(ICalendarEventRepository.DISPONIBILITY_BUSY);
  }
  if (event.getVisibilitySelect() == ICalendarEventRepository.VISIBILITY_PRIVATE) {
    event.setSubjectTeam(I18n.get("Available"));
    if (event.getDisponibilitySelect() == ICalendarEventRepository.DISPONIBILITY_BUSY) {
      event.setSubjectTeam(I18n.get("Busy"));
    }
  }
  ICalendarUser organizer = findOrCreateUser(vEvent.getOrganizer(), event);
  if (organizer != null) {
    event.setOrganizer(organizer);
    iCalendarUserRepository.save(organizer);
  }

  for (Object item : vEvent.getProperties(Property.ATTENDEE)) {
    ICalendarUser attendee = findOrCreateUser((Property) item, event);
    if (attendee != null) {
      event.addAttendee(attendee);
      iCalendarUserRepository.save(attendee);
    }
  }
  iEventRepo.save(event);
  return event;
}
 
Example #15
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;
}