Search in sources :

Example 1 with ResponseProperties

use of org.apache.ofbiz.workeffort.workeffort.ICalWorker.ResponseProperties in project ofbiz-framework by apache.

the class ICalConverter method storePartyAssignments.

protected static ResponseProperties storePartyAssignments(String workEffortId, Component component, Map<String, Object> context) {
    ResponseProperties responseProps = null;
    Map<String, Object> serviceMap = new HashMap<>();
    List<Property> partyList = new LinkedList<>();
    partyList.addAll(UtilGenerics.checkList(component.getProperties("ATTENDEE"), Property.class));
    partyList.addAll(UtilGenerics.checkList(component.getProperties("CONTACT"), Property.class));
    partyList.addAll(UtilGenerics.checkList(component.getProperties("ORGANIZER"), Property.class));
    for (Property property : partyList) {
        String partyId = fromXParameter(property.getParameters(), partyIdXParamName);
        if (partyId == null) {
            serviceMap.clear();
            String address = property.getValue();
            if (address.toUpperCase(Locale.getDefault()).startsWith("MAILTO:")) {
                address = address.substring(7);
            }
            serviceMap.put("address", address);
            Map<String, Object> result = invokeService("findPartyFromEmailAddress", serviceMap, context);
            partyId = (String) result.get("partyId");
            if (partyId == null) {
                continue;
            }
            replaceParameter(property.getParameters(), toXParameter(partyIdXParamName, partyId));
        }
        serviceMap.clear();
        serviceMap.put("workEffortId", workEffortId);
        serviceMap.put("partyId", partyId);
        serviceMap.put("roleTypeId", fromRoleMap.get(property.getName()));
        Delegator delegator = (Delegator) context.get("delegator");
        List<GenericValue> assignments = null;
        try {
            assignments = EntityQuery.use(delegator).from("WorkEffortPartyAssignment").where(serviceMap).filterByDate().queryList();
            if (assignments.size() == 0) {
                serviceMap.put("statusId", "PRTYASGN_OFFERED");
                serviceMap.put("fromDate", new Timestamp(System.currentTimeMillis()));
                invokeService("assignPartyToWorkEffort", serviceMap, context);
            }
        } catch (GenericEntityException e) {
            responseProps = ICalWorker.createPartialContentResponse(e.getMessage());
            break;
        }
    }
    return responseProps;
}
Also used : GenericValue(org.apache.ofbiz.entity.GenericValue) HashMap(java.util.HashMap) Timestamp(java.sql.Timestamp) LinkedList(java.util.LinkedList) Delegator(org.apache.ofbiz.entity.Delegator) ResponseProperties(org.apache.ofbiz.workeffort.workeffort.ICalWorker.ResponseProperties) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) XProperty(net.fortuna.ical4j.model.property.XProperty) Property(net.fortuna.ical4j.model.Property)

Example 2 with ResponseProperties

use of org.apache.ofbiz.workeffort.workeffort.ICalWorker.ResponseProperties in project ofbiz-framework by apache.

the class ICalConverter method getICalendar.

/**
 * 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 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());
}
Also used : GenericValue(org.apache.ofbiz.entity.GenericValue) ValidationException(net.fortuna.ical4j.model.ValidationException) Delegator(org.apache.ofbiz.entity.Delegator) ResponseProperties(org.apache.ofbiz.workeffort.workeffort.ICalWorker.ResponseProperties) Calendar(net.fortuna.ical4j.model.Calendar) ComponentList(net.fortuna.ical4j.model.ComponentList)

Example 3 with ResponseProperties

use of org.apache.ofbiz.workeffort.workeffort.ICalWorker.ResponseProperties 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

Delegator (org.apache.ofbiz.entity.Delegator)3 GenericValue (org.apache.ofbiz.entity.GenericValue)3 ResponseProperties (org.apache.ofbiz.workeffort.workeffort.ICalWorker.ResponseProperties)3 Calendar (net.fortuna.ical4j.model.Calendar)2 Property (net.fortuna.ical4j.model.Property)2 XProperty (net.fortuna.ical4j.model.property.XProperty)2 Timestamp (java.sql.Timestamp)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 LinkedList (java.util.LinkedList)1 CalendarBuilder (net.fortuna.ical4j.data.CalendarBuilder)1 Component (net.fortuna.ical4j.model.Component)1 ComponentList (net.fortuna.ical4j.model.ComponentList)1 ValidationException (net.fortuna.ical4j.model.ValidationException)1 GenericEntityException (org.apache.ofbiz.entity.GenericEntityException)1