Search in sources :

Example 1 with RestVariableScope

use of org.activiti.rest.service.api.engine.variable.RestVariable.RestVariableScope in project Activiti by Activiti.

the class BaseExecutionVariableResource method getVariableFromRequest.

public RestVariable getVariableFromRequest(Execution execution, String variableName, String scope, boolean includeBinary) {
    boolean variableFound = false;
    Object value = null;
    if (execution == null) {
        throw new ActivitiObjectNotFoundException("Could not find an execution", Execution.class);
    }
    RestVariableScope variableScope = RestVariable.getScopeFromString(scope);
    if (variableScope == null) {
        // First, check local variables (which have precedence when no scope is supplied)
        if (runtimeService.hasVariableLocal(execution.getId(), variableName)) {
            value = runtimeService.getVariableLocal(execution.getId(), variableName);
            variableScope = RestVariableScope.LOCAL;
            variableFound = true;
        } else {
            if (execution.getParentId() != null) {
                value = runtimeService.getVariable(execution.getParentId(), variableName);
                variableScope = RestVariableScope.GLOBAL;
                variableFound = true;
            }
        }
    } else if (variableScope == RestVariableScope.GLOBAL) {
        // Use parent to get variables
        if (execution.getParentId() != null) {
            value = runtimeService.getVariable(execution.getParentId(), variableName);
            variableScope = RestVariableScope.GLOBAL;
            variableFound = true;
        }
    } else if (variableScope == RestVariableScope.LOCAL) {
        value = runtimeService.getVariableLocal(execution.getId(), variableName);
        variableScope = RestVariableScope.LOCAL;
        variableFound = true;
    }
    if (!variableFound) {
        throw new ActivitiObjectNotFoundException("Execution '" + execution.getId() + "' doesn't have a variable with name: '" + variableName + "'.", VariableInstanceEntity.class);
    } else {
        return constructRestVariable(variableName, value, variableScope, execution.getId(), includeBinary);
    }
}
Also used : RestVariableScope(org.activiti.rest.service.api.engine.variable.RestVariable.RestVariableScope) ActivitiObjectNotFoundException(org.activiti.engine.ActivitiObjectNotFoundException)

Example 2 with RestVariableScope

use of org.activiti.rest.service.api.engine.variable.RestVariable.RestVariableScope in project Activiti by Activiti.

the class TaskVariableBaseResource method setSimpleVariable.

protected RestVariable setSimpleVariable(RestVariable restVariable, Task task, boolean isNew) {
    if (restVariable.getName() == null) {
        throw new ActivitiIllegalArgumentException("Variable name is required");
    }
    // Figure out scope, revert to local is omitted
    RestVariableScope scope = restVariable.getVariableScope();
    if (scope == null) {
        scope = RestVariableScope.LOCAL;
    }
    Object actualVariableValue = restResponseFactory.getVariableValue(restVariable);
    setVariable(task, restVariable.getName(), actualVariableValue, scope, isNew);
    return restResponseFactory.createRestVariable(restVariable.getName(), actualVariableValue, scope, task.getId(), RestResponseFactory.VARIABLE_TASK, false);
}
Also used : ActivitiIllegalArgumentException(org.activiti.engine.ActivitiIllegalArgumentException) RestVariableScope(org.activiti.rest.service.api.engine.variable.RestVariable.RestVariableScope)

Example 3 with RestVariableScope

use of org.activiti.rest.service.api.engine.variable.RestVariable.RestVariableScope in project Activiti by Activiti.

the class TaskVariableBaseResource method setBinaryVariable.

protected RestVariable setBinaryVariable(MultipartHttpServletRequest request, Task task, boolean isNew) {
    // Validate input and set defaults
    if (request.getFileMap().size() == 0) {
        throw new ActivitiIllegalArgumentException("No file content was found in request body.");
    }
    // Get first file in the map, ignore possible other files
    MultipartFile file = request.getFile(request.getFileMap().keySet().iterator().next());
    if (file == null) {
        throw new ActivitiIllegalArgumentException("No file content was found in request body.");
    }
    String variableScope = null;
    String variableName = null;
    String variableType = null;
    Map<String, String[]> paramMap = request.getParameterMap();
    for (String parameterName : paramMap.keySet()) {
        if (paramMap.get(parameterName).length > 0) {
            if (parameterName.equalsIgnoreCase("scope")) {
                variableScope = paramMap.get(parameterName)[0];
            } else if (parameterName.equalsIgnoreCase("name")) {
                variableName = paramMap.get(parameterName)[0];
            } else if (parameterName.equalsIgnoreCase("type")) {
                variableType = paramMap.get(parameterName)[0];
            }
        }
    }
    try {
        if (variableName == null) {
            throw new ActivitiIllegalArgumentException("No variable name was found in request body.");
        }
        if (variableType != null) {
            if (!RestResponseFactory.BYTE_ARRAY_VARIABLE_TYPE.equals(variableType) && !RestResponseFactory.SERIALIZABLE_VARIABLE_TYPE.equals(variableType)) {
                throw new ActivitiIllegalArgumentException("Only 'binary' and 'serializable' are supported as variable type.");
            }
        } else {
            variableType = RestResponseFactory.BYTE_ARRAY_VARIABLE_TYPE;
        }
        RestVariableScope scope = RestVariableScope.LOCAL;
        if (variableScope != null) {
            scope = RestVariable.getScopeFromString(variableScope);
        }
        if (variableType.equals(RestResponseFactory.BYTE_ARRAY_VARIABLE_TYPE)) {
            // Use raw bytes as variable value
            byte[] variableBytes = IOUtils.toByteArray(file.getInputStream());
            setVariable(task, variableName, variableBytes, scope, isNew);
        } else if (isSerializableVariableAllowed) {
            // Try deserializing the object
            ObjectInputStream stream = new ObjectInputStream(file.getInputStream());
            Object value = stream.readObject();
            setVariable(task, variableName, value, scope, isNew);
            stream.close();
        } else {
            throw new ActivitiContentNotSupportedException("Serialized objects are not allowed");
        }
        return restResponseFactory.createBinaryRestVariable(variableName, scope, variableType, task.getId(), null, null);
    } catch (IOException ioe) {
        throw new ActivitiIllegalArgumentException("Error getting binary variable", ioe);
    } catch (ClassNotFoundException ioe) {
        throw new ActivitiContentNotSupportedException("The provided body contains a serialized object for which the class is nog found: " + ioe.getMessage());
    }
}
Also used : MultipartFile(org.springframework.web.multipart.MultipartFile) ActivitiContentNotSupportedException(org.activiti.rest.exception.ActivitiContentNotSupportedException) ActivitiIllegalArgumentException(org.activiti.engine.ActivitiIllegalArgumentException) RestVariableScope(org.activiti.rest.service.api.engine.variable.RestVariable.RestVariableScope) IOException(java.io.IOException) ObjectInputStream(java.io.ObjectInputStream)

Example 4 with RestVariableScope

use of org.activiti.rest.service.api.engine.variable.RestVariable.RestVariableScope in project Activiti by Activiti.

the class TaskVariableCollectionResource method createOrUpdateTaskVariables.

private Object createOrUpdateTaskVariables(HttpServletRequest request, HttpServletResponse response, Task task, boolean override) {
    Object result = null;
    if (request instanceof MultipartHttpServletRequest) {
        result = setBinaryVariable((MultipartHttpServletRequest) request, task, 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(task, var.getName(), varScope)) {
                throw new ActivitiConflictException("Variable '" + var.getName() + "' is already present on task '" + task.getId() + "'.");
            }
            Object actualVariableValue = restResponseFactory.getVariableValue(var);
            variablesToSet.put(var.getName(), actualVariableValue);
            resultVariables.add(restResponseFactory.createRestVariable(var.getName(), actualVariableValue, varScope, task.getId(), RestResponseFactory.VARIABLE_TASK, false));
        }
        if (!variablesToSet.isEmpty()) {
            if (sharedScope == RestVariableScope.LOCAL) {
                taskService.setVariablesLocal(task.getId(), variablesToSet);
            } else {
                if (task.getExecutionId() != null) {
                    // Explicitly set on execution, setting non-local variables on task will override local-variables if exists
                    runtimeService.setVariables(task.getExecutionId(), variablesToSet);
                } else {
                    // Standalone task, no global variables possible
                    throw new ActivitiIllegalArgumentException("Cannot set global variables on task '" + task.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)

Example 5 with RestVariableScope

use of org.activiti.rest.service.api.engine.variable.RestVariable.RestVariableScope in project Activiti by Activiti.

the class TaskVariableCollectionResource method getVariables.

@RequestMapping(value = "/runtime/tasks/{taskId}/variables", method = RequestMethod.GET, produces = "application/json")
public List<RestVariable> getVariables(@PathVariable String taskId, @RequestParam(value = "scope", required = false) String scope, HttpServletRequest request) {
    List<RestVariable> result = new ArrayList<RestVariable>();
    Map<String, RestVariable> variableMap = new HashMap<String, RestVariable>();
    // Check if it's a valid task to get the variables for
    Task task = getTaskFromRequest(taskId);
    RestVariableScope variableScope = RestVariable.getScopeFromString(scope);
    if (variableScope == null) {
        // Use both local and global variables
        addLocalVariables(task, variableMap);
        addGlobalVariables(task, variableMap);
    } else if (variableScope == RestVariableScope.GLOBAL) {
        addGlobalVariables(task, variableMap);
    } else if (variableScope == RestVariableScope.LOCAL) {
        addLocalVariables(task, variableMap);
    }
    // Get unique variables from map
    result.addAll(variableMap.values());
    return result;
}
Also used : RestVariable(org.activiti.rest.service.api.engine.variable.RestVariable) Task(org.activiti.engine.task.Task) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) RestVariableScope(org.activiti.rest.service.api.engine.variable.RestVariable.RestVariableScope) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

RestVariableScope (org.activiti.rest.service.api.engine.variable.RestVariable.RestVariableScope)14 ActivitiIllegalArgumentException (org.activiti.engine.ActivitiIllegalArgumentException)6 ActivitiObjectNotFoundException (org.activiti.engine.ActivitiObjectNotFoundException)6 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)4 RestVariable (org.activiti.rest.service.api.engine.variable.RestVariable)4 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)4 Task (org.activiti.engine.task.Task)3 IOException (java.io.IOException)2 ObjectInputStream (java.io.ObjectInputStream)2 List (java.util.List)2 Execution (org.activiti.engine.runtime.Execution)2 ActivitiConflictException (org.activiti.rest.exception.ActivitiConflictException)2 ActivitiContentNotSupportedException (org.activiti.rest.exception.ActivitiContentNotSupportedException)2 MultipartFile (org.springframework.web.multipart.MultipartFile)2 MultipartHttpServletRequest (org.springframework.web.multipart.MultipartHttpServletRequest)2 HistoricTaskInstance (org.activiti.engine.history.HistoricTaskInstance)1 HistoricTaskInstanceQuery (org.activiti.engine.history.HistoricTaskInstanceQuery)1