net.fortuna.ical4j.model.component.CalendarComponent Java Examples

The following examples show how to use net.fortuna.ical4j.model.component.CalendarComponent. 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: IcalUtils.java    From openmeetings with Apache License 2.0 6 votes vote down vote up
/**
 * Parses a Calendar with multiple VEvents into Appointments
 *
 * @param calendar Calendar to Parse
 * @param ownerId  Owner of the Appointments
 * @return <code>List</code> of Appointments
 */
public List<Appointment> parseCalendartoAppointments(Calendar calendar, Long ownerId) {
	List<Appointment> appointments = new ArrayList<>();
	ComponentList<CalendarComponent> events = calendar.getComponents(Component.VEVENT);
	User owner = userDao.get(ownerId);

	for (CalendarComponent event : events) {
		Appointment a = new Appointment();
		a.setOwner(owner);
		a.setDeleted(false);
		a.setRoom(createDefaultRoom());
		a.setReminder(Appointment.Reminder.NONE);
		a = addVEventPropertiestoAppointment(a, event);
		appointments.add(a);
	}
	return appointments;
}
 
Example #2
Source File: CalendarUtils.java    From cosmo with Apache License 2.0 6 votes vote down vote up
public static boolean hasMultipleComponentTypes(Calendar calendar) {
String found = null;
for (Object component : calendar.getComponents()) {
    if (component instanceof VTimeZone) {
	continue;
    }
    if (found == null) {
	found = ((CalendarComponent) component).getName();
	continue;
    }
    if (!found.equals(((CalendarComponent) component).getName())) {
	return true;
    }
}
return false;
   }
 
Example #3
Source File: FreeBusyUtil.java    From cosmo with Apache License 2.0 6 votes vote down vote up
/**
 * Obfuscates the specified calendar by removing unnecessary properties and replacing text fields with specified
 * text.
 * 
 * @param original
 *            calendar to be obfuscated
 * @param productId
 *            productId to be set for the copy calendar.
 * @param freeBusyText
 * @return obfuscated calendar.
 */
public static Calendar getFreeBusyCalendar(Calendar original, String productId, String freeBusyText) {
    // Make a copy of the original calendar
    Calendar copy = new Calendar();
    copy.getProperties().add(new ProdId(productId));
    copy.getProperties().add(Version.VERSION_2_0);
    copy.getProperties().add(CalScale.GREGORIAN);
    copy.getProperties().add(new XProperty(FREE_BUSY_X_PROPERTY, Boolean.TRUE.toString()));

    ComponentList<CalendarComponent> events = original.getComponents(Component.VEVENT);
    for (Component event : events) {
        copy.getComponents().add(getFreeBusyEvent((VEvent) event, freeBusyText));
    }
    return copy;
}
 
Example #4
Source File: ICalendarStore.java    From axelor-open-suite with GNU Affero General Public License v3.0 6 votes vote down vote up
public static List<VEvent> getModifiedEvents(
    CalDavCalendarCollection calendar, Instant instant, Set<String> remoteUids) {
  final List<VEvent> events = new ArrayList<>();

  for (Calendar cal : calendar.getEvents()) {
    cal.toString();
    for (Object item : ((List<CalendarComponent>) cal.getComponents(Component.VEVENT))) {
      VEvent event = (VEvent) item;
      if (instant == null || event.getLastModified().getDate().toInstant().isAfter(instant)) {
        events.add(event);
      }
      remoteUids.add(event.getUid().getValue());
    }
  }
  return events;
}
 
Example #5
Source File: ICalConverter.java    From scipio-erp with Apache License 2.0 5 votes vote down vote up
/** Returns a calendar derived from a Work Effort calendar publish point.
 * @param workEffortId ID of a work effort with <code>workEffortTypeId</code> equal to
 * <code>PUBLISH_PROPS</code>.
 * @param context The conversion context
 * @return An iCalendar as a <code>String</code>, or <code>null</code>
 * if <code>workEffortId</code> is invalid.
 * @throws GenericEntityException
 */
public static ResponseProperties getICalendar(String workEffortId, Map<String, Object> context) throws GenericEntityException {
    Delegator delegator = (Delegator) context.get("delegator");
    GenericValue publishProperties = EntityQuery.use(delegator).from("WorkEffort").where("workEffortId", workEffortId).queryOne();
    if (!isCalendarPublished(publishProperties)) {
        Debug.logInfo("WorkEffort calendar is not published: " + workEffortId, module);
        return ICalWorker.createNotFoundResponse(null);
    }
    if (!"WES_PUBLIC".equals(publishProperties.get("scopeEnumId"))) {
        if (context.get("userLogin") == null) {
            return ICalWorker.createNotAuthorizedResponse(null);
        }
        if (!hasPermission(workEffortId, "VIEW", context)) {
            return ICalWorker.createForbiddenResponse(null);
        }
    }
    Calendar calendar = makeCalendar(publishProperties, context);
    ComponentList<CalendarComponent> components = calendar.getComponents();
    List<GenericValue> workEfforts = getRelatedWorkEfforts(publishProperties, context);
    if (workEfforts != null) {
        for (GenericValue workEffort : workEfforts) {
            ResponseProperties responseProps = toCalendarComponent(components, workEffort, context);
            if (responseProps != null) {
                return responseProps;
            }
        }
    }
    if (Debug.verboseOn()) {
        try {
            calendar.validate(true);
            Debug.logVerbose("iCalendar passes validation", module);
        } catch (ValidationException e) {
            if (Debug.verboseOn()) Debug.logVerbose("iCalendar fails validation: " + e, module);
        }
    }
    return ICalWorker.createOkResponse(calendar.toString());
}
 
Example #6
Source File: IcalUtils.java    From openmeetings with Apache License 2.0 5 votes vote down vote up
/**
 * Updating Appointments which already exist, by parsing the Calendar. And updating etag.
 * Doesn't work with complex Recurrences.
 * Note: Hasn't been tested to acknowledge DST, timezones should acknowledge this.
 *
 * @param a        Appointment to be updated.
 * @param calendar iCalendar Representation.
 * @param etag     The ETag of the calendar.
 * @return Updated Appointment.
 */
public Appointment parseCalendartoAppointment(Appointment a, Calendar calendar, String etag) {
	if (calendar == null) {
		return a;
	}
	CalendarComponent event = calendar.getComponent(Component.VEVENT);
	if (event != null) {
		a.setEtag(etag);
		a = addVEventPropertiestoAppointment(a, event);
	}
	return a;
}
 
Example #7
Source File: OutputFilter.java    From cosmo with Apache License 2.0 5 votes vote down vote up
/**
 * @param comp The component.
 */
private void componentToUTC(Component comp) {
    // Do to each top-level property
    for (Property prop : (List<Property>) comp.getProperties()) {
        if (prop instanceof DateProperty) {
            DateProperty dprop = (DateProperty) prop;
            Date date = dprop.getDate();
            if (date instanceof DateTime &&
                (((DateTime) date).getTimeZone() != null)) {
                dprop.setUtc(true);
            }
        }
    }

    // Do to each embedded component
    ComponentList<? extends CalendarComponent> subcomps = null;
    if (comp instanceof VEvent) {
        subcomps = ((VEvent) comp).getAlarms() ;
    }
    else if (comp instanceof VToDo) {
        subcomps = ((VToDo)comp).getAlarms();
    }

    if (subcomps != null) {
        for (CalendarComponent subcomp :  subcomps) {
            componentToUTC(subcomp);
        }
    }
}
 
Example #8
Source File: CalendarUtils.java    From cosmo with Apache License 2.0 5 votes vote down vote up
public static boolean hasSupportedComponent(Calendar calendar) {
for (Object component : calendar.getComponents()) {
    if (isSupportedComponent(((CalendarComponent) component).getName())) {
	return true;
    }
}
return false;
   }
 
Example #9
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 #10
Source File: ICalendarUtils.java    From cosmo with Apache License 2.0 4 votes vote down vote up
/**
 * Create a base Calendar containing a single component.
 * @param comp Component to add to the base Calendar
 * @return base Calendar
 */
public static Calendar createBaseCalendar(CalendarComponent comp) {
    Calendar cal = createBaseCalendar(); 
    cal.getComponents().add(comp);
    return cal;
}
 
Example #11
Source File: EntityConverter.java    From cosmo with Apache License 2.0 4 votes vote down vote up
/**
 * Given a calendar with many different components, split into
 * separate calendars that contain only a single component type
 * and a single UID.
 * @param calendar The calendar.
 * @return The split calendar.
 */
private CalendarContext[] splitCalendar(Calendar calendar) {
    Vector<CalendarContext> contexts = new Vector<CalendarContext>();
    Set<String> allComponents = new HashSet<String>();
    Map<String, ComponentList<CalendarComponent>> componentMap = new HashMap<>();
    
    ComponentList<CalendarComponent> comps = calendar.getComponents();
    for(CalendarComponent comp : comps) {            
        // ignore vtimezones for now
        if(comp instanceof VTimeZone) {
            continue;
        }
        
        Uid uid = (Uid) comp.getProperty(Property.UID);
        RecurrenceId rid = (RecurrenceId) comp.getProperty(Property.RECURRENCE_ID);
        
        String key = uid.getValue();
        if(rid!=null) {
            key+=rid.toString();
        }
        
        // ignore duplicates
        if(allComponents.contains(key)) {
            continue;
        }
        
        allComponents.add(key);
        
        ComponentList<CalendarComponent> cl = componentMap.get(uid.getValue());
        
        if(cl==null) {
            cl = new ComponentList<>();
            componentMap.put(uid.getValue(), cl);
        }
        
        cl.add(comp);
    }
    
    for(Entry<String, ComponentList<CalendarComponent>> entry : componentMap.entrySet()) {
       
        Component firstComp = (Component) entry.getValue().get(0);
        
        Calendar cal = ICalendarUtils.createBaseCalendar();
        cal.getComponents().addAll(entry.getValue());
        addTimezones(cal);
        
        CalendarContext cc = new CalendarContext();
        cc.calendar = cal;
        cc.type = firstComp.getName();
        
        contexts.add(cc);
    }
    
    return contexts.toArray(new CalendarContext[contexts.size()]);
}
 
Example #12
Source File: ICalendarUtils.java    From cosmo with Apache License 2.0 2 votes vote down vote up
/**
 * Create a base Calendar containing a single component.
 * @param comp Component to add to the base Calendar
 * @param icalUid uid of component, if null no UID 
 *                property will be added to the component
 * @return base Calendar
 */
public static Calendar createBaseCalendar(CalendarComponent comp, String icalUid) {
    Uid uid = new Uid(icalUid);
    comp.getProperties().add(uid);
    return createBaseCalendar(comp);
}