Search in sources :

Example 6 with Component

use of net.fortuna.ical4j.model.Component in project opencast by opencast.

the class SchedulerServiceImplTest method checkIcalFeed.

private void checkIcalFeed(Map<String, String> caProps, String title) throws Exception {
    final String cs = schedSvc.getCalendar(Opt.<String>none(), Opt.<String>none(), Opt.<Date>none());
    final Calendar cal = new CalendarBuilder().build(new StringReader(cs));
    assertEquals("number of entries", 1, cal.getComponents().size());
    for (Object co : cal.getComponents()) {
        final Component c = (Component) co;
        assertEquals("SUMMARY property should contain the DC title", title, c.getProperty(Property.SUMMARY).getValue());
        final Monadics.ListMonadic<Property> attachments = mlist(c.getProperties(Property.ATTACH)).map(Misc.<Object, Property>cast());
        // episode dublin core
        final List<DublinCoreCatalog> dcsIcal = attachments.filter(byParamNameAndValue("X-APPLE-FILENAME", "episode.xml")).map(parseDc.o(decodeBase64).o(getValue)).value();
        assertEquals("number of episode DCs", 1, dcsIcal.size());
        assertEquals("dcterms:title", title, dcsIcal.get(0).getFirst(PROPERTY_TITLE));
        // capture agent properties
        final List<Properties> caPropsIcal = attachments.filter(byParamNameAndValue("X-APPLE-FILENAME", "org.opencastproject.capture.agent.properties")).map(parseProperties.o(decodeBase64).o(getValue)).value();
        assertEquals("number of CA property sets", 1, caPropsIcal.size());
        assertTrue("CA properties", eqObj(caProps, caPropsIcal.get(0)));
    }
}
Also used : Monadics(org.opencastproject.util.data.Monadics) CalendarBuilder(net.fortuna.ical4j.data.CalendarBuilder) Calendar(net.fortuna.ical4j.model.Calendar) Properties(java.util.Properties) StringReader(java.io.StringReader) Component(net.fortuna.ical4j.model.Component) DublinCoreCatalog(org.opencastproject.metadata.dublincore.DublinCoreCatalog) Property(net.fortuna.ical4j.model.Property)

Example 7 with Component

use of net.fortuna.ical4j.model.Component in project zm-mailbox by Zimbra.

the class ZoneInfo2iCalendar method makeOldTimeZonesMap.

private static Map<String, VTimeZone> makeOldTimeZonesMap(Params params) {
    Map<String, VTimeZone> oldTimeZones = Maps.newHashMap();
    if (null != params.oldTimezonesFileName) {
        try (FileInputStream fin = new FileInputStream(params.oldTimezonesFileName)) {
            CalendarBuilder builder = new CalendarBuilder();
            net.fortuna.ical4j.model.Calendar calendar = builder.build(fin, "UTF-8");
            for (Iterator i = calendar.getComponents().iterator(); i.hasNext(); ) {
                Component component = (Component) i.next();
                if (Component.VTIMEZONE.equals(component.getName())) {
                    VTimeZone vtz = (VTimeZone) component;
                    Property tzprop = vtz.getProperties().getProperty(Property.TZID);
                    if (null != tzprop) {
                        oldTimeZones.put(tzprop.getValue(), vtz);
                    }
                }
            }
        } catch (IOException | ParserException e) {
            System.err.println("Problem loading old timezones.ics - ignoring it.  " + e.getMessage());
        }
    }
    return oldTimeZones;
}
Also used : ParserException(net.fortuna.ical4j.data.ParserException) CalendarBuilder(net.fortuna.ical4j.data.CalendarBuilder) VTimeZone(net.fortuna.ical4j.model.component.VTimeZone) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) Iterator(java.util.Iterator) Component(net.fortuna.ical4j.model.Component) XProperty(net.fortuna.ical4j.model.property.XProperty) Property(net.fortuna.ical4j.model.Property)

Example 8 with Component

use of net.fortuna.ical4j.model.Component in project zm-mailbox by Zimbra.

the class TimeZoneDefinition method addVtimezone.

public void addVtimezone(ContentHandler icalOutput) throws ParserException, URISyntaxException, IOException, ParseException {
    if (getTimezoneName() == null) {
        return;
    }
    if (effectiveRule == null) {
        return;
    }
    getTimeZone();
    if (theZone == null) {
        return;
    }
    VTimeZone vtz = theZone.getVTimeZone();
    icalOutput.startComponent(Component.VTIMEZONE);
    for (Object obj : vtz.getProperties()) {
        if (obj instanceof Property) {
            Property currProp = (Property) obj;
            IcalUtil.addProperty(icalOutput, currProp);
        }
    }
    for (Object obj : vtz.getObservances()) {
        if (obj instanceof Component) {
            Component currComp = (Component) obj;
            icalOutput.startComponent(currComp.getName());
            for (Object propObj : currComp.getProperties()) {
                if (propObj instanceof Property) {
                    Property obsProp = (Property) propObj;
                    IcalUtil.addProperty(icalOutput, obsProp);
                }
            }
            icalOutput.endComponent(currComp.getName());
        }
    }
    icalOutput.endComponent(Component.VTIMEZONE);
    if (true) {
        return;
    }
}
Also used : VTimeZone(net.fortuna.ical4j.model.component.VTimeZone) Component(net.fortuna.ical4j.model.Component) Property(net.fortuna.ical4j.model.Property)

Example 9 with Component

use of net.fortuna.ical4j.model.Component in project ofbiz-framework by apache.

the class ICalConverter method toCalendarComponent.

protected static ResponseProperties toCalendarComponent(ComponentList components, GenericValue workEffort, Map<String, Object> context) throws GenericEntityException {
    Delegator delegator = workEffort.getDelegator();
    String workEffortId = workEffort.getString("workEffortId");
    String workEffortUid = workEffort.getString("universalId");
    String workEffortTypeId = workEffort.getString("workEffortTypeId");
    GenericValue typeValue = EntityQuery.use(delegator).from("WorkEffortType").where("workEffortTypeId", workEffortTypeId).cache().queryOne();
    boolean isTask = false;
    boolean newComponent = true;
    ComponentList resultList = null;
    ComponentList alarms = null;
    Component result = null;
    if ("TASK".equals(workEffortTypeId) || (typeValue != null && "TASK".equals(typeValue.get("parentTypeId")))) {
        isTask = true;
        resultList = components.getComponents("VTODO");
    } else if ("EVENT".equals(workEffortTypeId) || (typeValue != null && "EVENT".equals(typeValue.get("parentTypeId")))) {
        resultList = components.getComponents("VEVENT");
    } else {
        return null;
    }
    Iterator<Component> i = UtilGenerics.cast(resultList.iterator());
    while (i.hasNext()) {
        result = i.next();
        Property xProperty = result.getProperty(workEffortIdXPropName);
        if (xProperty != null && workEffortId.equals(xProperty.getValue())) {
            newComponent = false;
            break;
        }
        Property uid = result.getProperty(Uid.UID);
        if (uid != null && uid.getValue().equals(workEffortUid)) {
            newComponent = false;
            break;
        }
    }
    if (isTask) {
        VToDo toDo = null;
        if (newComponent) {
            toDo = new VToDo();
            result = toDo;
        } else {
            toDo = (VToDo) result;
        }
        alarms = toDo.getAlarms();
    } else {
        VEvent event = null;
        if (newComponent) {
            event = new VEvent();
            result = event;
        } else {
            event = (VEvent) result;
        }
        alarms = event.getAlarms();
    }
    if (newComponent) {
        components.add(result);
    }
    PropertyList componentProps = result.getProperties();
    loadWorkEffort(componentProps, workEffort);
    if (isTask) {
        replaceProperty(componentProps, toCompleted(workEffort.getTimestamp("actualCompletionDate")));
        replaceProperty(componentProps, toPercentComplete(workEffort.getLong("percentComplete")));
    } else {
        replaceProperty(componentProps, toDtEnd(workEffort.getTimestamp("estimatedCompletionDate")));
    }
    if (workEffort.get("estimatedCompletionDate") == null) {
        replaceProperty(componentProps, toDuration(workEffort.getDouble("estimatedMilliSeconds")));
    }
    List<GenericValue> relatedParties = EntityQuery.use(delegator).from("WorkEffortPartyAssignView").where("workEffortId", workEffortId).cache(true).filterByDate().queryList();
    if (relatedParties.size() > 0) {
        loadRelatedParties(relatedParties, componentProps, context);
    }
    if (newComponent) {
        if (UtilValidate.isNotEmpty(workEffort.getString("tempExprId"))) {
            TemporalExpression tempExpr = TemporalExpressionWorker.getTemporalExpression(delegator, workEffort.getString("tempExprId"));
            if (tempExpr != null) {
                try {
                    ICalRecurConverter.convert(tempExpr, componentProps);
                } catch (Exception e) {
                    replaceProperty(componentProps, new Description("Error while converting recurrence: " + e));
                }
            }
        }
        getAlarms(workEffort, alarms);
    }
    if (Debug.verboseOn()) {
        try {
            result.validate(true);
            Debug.logVerbose("iCalendar component passes validation", module);
        } catch (ValidationException e) {
            Debug.logVerbose(e, "iCalendar component fails validation: ", module);
        }
    }
    return null;
}
Also used : VEvent(net.fortuna.ical4j.model.component.VEvent) GenericValue(org.apache.ofbiz.entity.GenericValue) TemporalExpression(org.apache.ofbiz.service.calendar.TemporalExpression) Description(net.fortuna.ical4j.model.property.Description) ValidationException(net.fortuna.ical4j.model.ValidationException) ComponentList(net.fortuna.ical4j.model.ComponentList) URISyntaxException(java.net.URISyntaxException) GenericServiceException(org.apache.ofbiz.service.GenericServiceException) ParserException(net.fortuna.ical4j.data.ParserException) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) GeneralException(org.apache.ofbiz.base.util.GeneralException) ValidationException(net.fortuna.ical4j.model.ValidationException) IOException(java.io.IOException) PropertyList(net.fortuna.ical4j.model.PropertyList) Delegator(org.apache.ofbiz.entity.Delegator) Component(net.fortuna.ical4j.model.Component) XProperty(net.fortuna.ical4j.model.property.XProperty) Property(net.fortuna.ical4j.model.Property) VToDo(net.fortuna.ical4j.model.component.VToDo)

Example 10 with Component

use of net.fortuna.ical4j.model.Component in project ofbiz-framework by apache.

the class ICalConverter method storeCalendar.

/**
 * Update work efforts from an incoming iCalendar request.
 * @param is
 * @param context
 * @throws IOException
 * @throws ParserException
 * @throws GenericEntityException
 * @throws GenericServiceException
 */
public static ResponseProperties storeCalendar(InputStream is, Map<String, Object> context) throws IOException, ParserException, GenericEntityException, GenericServiceException {
    CalendarBuilder builder = new CalendarBuilder();
    Calendar calendar = null;
    try {
        calendar = builder.build(is);
    } finally {
        if (is != null) {
            is.close();
        }
    }
    if (Debug.verboseOn()) {
        Debug.logVerbose("Processing calendar:\r\n" + calendar, module);
    }
    String workEffortId = fromXProperty(calendar.getProperties(), workEffortIdXPropName);
    if (workEffortId == null) {
        workEffortId = (String) context.get("workEffortId");
    }
    if (!workEffortId.equals(context.get("workEffortId"))) {
        Debug.logWarning("Spoof attempt: received calendar workEffortId " + workEffortId + " on URL workEffortId " + context.get("workEffortId"), module);
        return ICalWorker.createForbiddenResponse(null);
    }
    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 (context.get("userLogin") == null) {
        return ICalWorker.createNotAuthorizedResponse(null);
    }
    if (!hasPermission(workEffortId, "UPDATE", context)) {
        return ICalWorker.createForbiddenResponse(null);
    }
    boolean hasCreatePermission = hasPermission(workEffortId, "CREATE", context);
    List<GenericValue> workEfforts = getRelatedWorkEfforts(publishProperties, context);
    Set<String> validWorkEfforts = new HashSet<>();
    if (UtilValidate.isNotEmpty(workEfforts)) {
        // Security issue: make sure only related work efforts get updated
        for (GenericValue workEffort : workEfforts) {
            validWorkEfforts.add(workEffort.getString("workEffortId"));
        }
    }
    List<Component> components = UtilGenerics.checkList(calendar.getComponents(), Component.class);
    ResponseProperties responseProps = null;
    for (Component component : components) {
        if (Component.VEVENT.equals(component.getName()) || Component.VTODO.equals(component.getName())) {
            workEffortId = fromXProperty(component.getProperties(), workEffortIdXPropName);
            if (workEffortId == null) {
                Property uid = component.getProperty(Uid.UID);
                if (uid != null) {
                    GenericValue workEffort = EntityQuery.use(delegator).from("WorkEffort").where("universalId", uid.getValue()).queryFirst();
                    if (workEffort != null) {
                        workEffortId = workEffort.getString("workEffortId");
                    }
                }
            }
            if (workEffortId != null) {
                if (validWorkEfforts.contains(workEffortId)) {
                    replaceProperty(component.getProperties(), toXProperty(workEffortIdXPropName, workEffortId));
                    responseProps = storeWorkEffort(component, context);
                } else {
                    Debug.logWarning("Spoof attempt: unrelated workEffortId " + workEffortId + " on URL workEffortId " + context.get("workEffortId"), module);
                    responseProps = ICalWorker.createForbiddenResponse(null);
                }
            } else if (hasCreatePermission) {
                responseProps = createWorkEffort(component, context);
            }
            if (responseProps != null) {
                return responseProps;
            }
        }
    }
    Map<String, ? extends Object> serviceMap = UtilMisc.toMap("workEffortId", context.get("workEffortId"), "icalData", calendar.toString());
    GenericValue iCalData = publishProperties.getRelatedOne("WorkEffortIcalData", false);
    Map<String, Object> serviceResult = null;
    if (iCalData == null) {
        serviceResult = invokeService("createWorkEffortICalData", serviceMap, context);
    } else {
        serviceResult = invokeService("updateWorkEffortICalData", serviceMap, context);
    }
    if (ServiceUtil.isError(serviceResult)) {
        return ICalWorker.createPartialContentResponse(ServiceUtil.getErrorMessage(serviceResult));
    }
    return ICalWorker.createOkResponse(null);
}
Also used : GenericValue(org.apache.ofbiz.entity.GenericValue) CalendarBuilder(net.fortuna.ical4j.data.CalendarBuilder) Calendar(net.fortuna.ical4j.model.Calendar) Delegator(org.apache.ofbiz.entity.Delegator) ResponseProperties(org.apache.ofbiz.workeffort.workeffort.ICalWorker.ResponseProperties) Component(net.fortuna.ical4j.model.Component) XProperty(net.fortuna.ical4j.model.property.XProperty) Property(net.fortuna.ical4j.model.Property) HashSet(java.util.HashSet)

Aggregations

Component (net.fortuna.ical4j.model.Component)14 Property (net.fortuna.ical4j.model.Property)9 CalFacadeException (org.bedework.calfacade.exc.CalFacadeException)8 BwEvent (org.bedework.calfacade.BwEvent)7 XProperty (net.fortuna.ical4j.model.property.XProperty)6 Calendar (net.fortuna.ical4j.model.Calendar)5 EventInfo (org.bedework.calfacade.svc.EventInfo)5 Iterator (java.util.Iterator)4 DateListProperty (net.fortuna.ical4j.model.property.DateListProperty)4 BwFreeBusyComponent (org.bedework.calfacade.BwFreeBusyComponent)4 BwString (org.bedework.calfacade.BwString)4 StringReader (java.io.StringReader)3 CalendarBuilder (net.fortuna.ical4j.data.CalendarBuilder)3 ParserException (net.fortuna.ical4j.data.ParserException)3 ComponentList (net.fortuna.ical4j.model.ComponentList)3 PropertyList (net.fortuna.ical4j.model.PropertyList)3 CalendarComponent (net.fortuna.ical4j.model.component.CalendarComponent)3 VEvent (net.fortuna.ical4j.model.component.VEvent)3 VTimeZone (net.fortuna.ical4j.model.component.VTimeZone)3 StartEndComponent (org.bedework.calfacade.base.StartEndComponent)3