Search in sources :

Example 6 with ActivitiConflictException

use of org.activiti.rest.exception.ActivitiConflictException in project Activiti by Activiti.

the class BaseVariableCollectionResource method createExecutionVariable.

protected Object createExecutionVariable(Execution execution, boolean override, int variableType, HttpServletRequest request, HttpServletResponse response) {
    Object result = null;
    if (request instanceof MultipartHttpServletRequest) {
        result = setBinaryVariable((MultipartHttpServletRequest) request, execution, variableType, true);
    } else {
        List<RestVariable> inputVariables = new ArrayList<RestVariable>();
        List<RestVariable> resultVariables = new ArrayList<RestVariable>();
        result = resultVariables;
        try {
            @SuppressWarnings("unchecked") List<Object> variableObjects = (List<Object>) objectMapper.readValue(request.getInputStream(), List.class);
            for (Object restObject : variableObjects) {
                RestVariable restVariable = objectMapper.convertValue(restObject, RestVariable.class);
                inputVariables.add(restVariable);
            }
        } catch (Exception e) {
            throw new ActivitiIllegalArgumentException("Failed to serialize to a RestVariable instance", e);
        }
        if (inputVariables == null || inputVariables.size() == 0) {
            throw new ActivitiIllegalArgumentException("Request didn't contain a list of variables to create.");
        }
        RestVariableScope sharedScope = null;
        RestVariableScope varScope = null;
        Map<String, Object> variablesToSet = new HashMap<String, Object>();
        for (RestVariable var : inputVariables) {
            // Validate if scopes match
            varScope = var.getVariableScope();
            if (var.getName() == null) {
                throw new ActivitiIllegalArgumentException("Variable name is required");
            }
            if (varScope == null) {
                varScope = RestVariableScope.LOCAL;
            }
            if (sharedScope == null) {
                sharedScope = varScope;
            }
            if (varScope != sharedScope) {
                throw new ActivitiIllegalArgumentException("Only allowed to update multiple variables in the same scope.");
            }
            if (!override && hasVariableOnScope(execution, var.getName(), varScope)) {
                throw new ActivitiConflictException("Variable '" + var.getName() + "' is already present on execution '" + execution.getId() + "'.");
            }
            Object actualVariableValue = restResponseFactory.getVariableValue(var);
            variablesToSet.put(var.getName(), actualVariableValue);
            resultVariables.add(restResponseFactory.createRestVariable(var.getName(), actualVariableValue, varScope, execution.getId(), variableType, false));
        }
        if (!variablesToSet.isEmpty()) {
            if (sharedScope == RestVariableScope.LOCAL) {
                runtimeService.setVariablesLocal(execution.getId(), variablesToSet);
            } else {
                if (execution.getParentId() != null) {
                    // Explicitly set on parent, setting non-local variables on execution itself will override local-variables if exists
                    runtimeService.setVariables(execution.getParentId(), variablesToSet);
                } else {
                    // Standalone task, no global variables possible
                    throw new ActivitiIllegalArgumentException("Cannot set global variables on execution '" + execution.getId() + "', task is not part of process.");
                }
            }
        }
    }
    response.setStatus(HttpStatus.CREATED.value());
    return result;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) RestVariableScope(org.activiti.rest.service.api.engine.variable.RestVariable.RestVariableScope) ActivitiConflictException(org.activiti.rest.exception.ActivitiConflictException) ActivitiConflictException(org.activiti.rest.exception.ActivitiConflictException) ActivitiIllegalArgumentException(org.activiti.engine.ActivitiIllegalArgumentException) RestVariable(org.activiti.rest.service.api.engine.variable.RestVariable) ActivitiIllegalArgumentException(org.activiti.engine.ActivitiIllegalArgumentException) ArrayList(java.util.ArrayList) List(java.util.List) MultipartHttpServletRequest(org.springframework.web.multipart.MultipartHttpServletRequest)

Aggregations

ActivitiIllegalArgumentException (org.activiti.engine.ActivitiIllegalArgumentException)6 ActivitiConflictException (org.activiti.rest.exception.ActivitiConflictException)6 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)4 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Group (org.activiti.engine.identity.Group)2 User (org.activiti.engine.identity.User)2 RestVariable (org.activiti.rest.service.api.engine.variable.RestVariable)2 RestVariableScope (org.activiti.rest.service.api.engine.variable.RestVariable.RestVariableScope)2 MultipartHttpServletRequest (org.springframework.web.multipart.MultipartHttpServletRequest)2