Search in sources :

Example 21 with RestVariable

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

the class BaseVariableCollectionResource method addLocalVariables.

protected void addLocalVariables(Execution execution, int variableType, Map<String, RestVariable> variableMap) {
    Map<String, Object> rawLocalvariables = runtimeService.getVariablesLocal(execution.getId());
    List<RestVariable> localVariables = restResponseFactory.createRestVariables(rawLocalvariables, execution.getId(), variableType, RestVariableScope.LOCAL);
    for (RestVariable var : localVariables) {
        variableMap.put(var.getName(), var);
    }
}
Also used : RestVariable(org.activiti.rest.service.api.engine.variable.RestVariable)

Example 22 with RestVariable

use of org.activiti.rest.service.api.engine.variable.RestVariable 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)

Example 23 with RestVariable

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

the class ProcessInstanceVariableCollectionResource method addLocalVariables.

//For process instance there's only one scope. Using the local variables method for that
@Override
protected void addLocalVariables(Execution execution, int variableType, Map<String, RestVariable> variableMap) {
    Map<String, Object> rawVariables = runtimeService.getVariables(execution.getId());
    List<RestVariable> globalVariables = restResponseFactory.createRestVariables(rawVariables, execution.getId(), variableType, RestVariableScope.LOCAL);
    // since local variables get precedence over global ones at all times.
    for (RestVariable var : globalVariables) {
        if (!variableMap.containsKey(var.getName())) {
            variableMap.put(var.getName(), var);
        }
    }
}
Also used : RestVariable(org.activiti.rest.service.api.engine.variable.RestVariable)

Example 24 with RestVariable

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

the class ProcessInstanceVariableResource method updateVariable.

@RequestMapping(value = "/runtime/process-instances/{processInstanceId}/variables/{variableName}", method = RequestMethod.PUT, produces = "application/json")
public RestVariable updateVariable(@PathVariable("processInstanceId") String processInstanceId, @PathVariable("variableName") String variableName, HttpServletRequest request) {
    Execution execution = getProcessInstanceFromRequest(processInstanceId);
    RestVariable result = null;
    if (request instanceof MultipartHttpServletRequest) {
        result = setBinaryVariable((MultipartHttpServletRequest) request, execution, RestResponseFactory.VARIABLE_PROCESS, false);
        if (!result.getName().equals(variableName)) {
            throw new ActivitiIllegalArgumentException("Variable name in the body should be equal to the name used in the requested URL.");
        }
    } else {
        RestVariable restVariable = null;
        try {
            restVariable = objectMapper.readValue(request.getInputStream(), RestVariable.class);
        } catch (Exception e) {
            throw new ActivitiIllegalArgumentException("request body could not be transformed to a RestVariable instance.");
        }
        if (restVariable == null) {
            throw new ActivitiException("Invalid body was supplied");
        }
        if (!restVariable.getName().equals(variableName)) {
            throw new ActivitiIllegalArgumentException("Variable name in the body should be equal to the name used in the requested URL.");
        }
        result = setSimpleVariable(restVariable, execution, false);
    }
    return result;
}
Also used : RestVariable(org.activiti.rest.service.api.engine.variable.RestVariable) ActivitiException(org.activiti.engine.ActivitiException) Execution(org.activiti.engine.runtime.Execution) ActivitiIllegalArgumentException(org.activiti.engine.ActivitiIllegalArgumentException) ActivitiException(org.activiti.engine.ActivitiException) ActivitiObjectNotFoundException(org.activiti.engine.ActivitiObjectNotFoundException) ActivitiIllegalArgumentException(org.activiti.engine.ActivitiIllegalArgumentException) MultipartHttpServletRequest(org.springframework.web.multipart.MultipartHttpServletRequest) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 25 with RestVariable

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

the class TaskVariableDataResource method getVariableData.

@RequestMapping(value = "/runtime/tasks/{taskId}/variables/{variableName}/data", method = RequestMethod.GET, produces = "application/json")
@ResponseBody
public byte[] getVariableData(@PathVariable("taskId") String taskId, @PathVariable("variableName") String variableName, @RequestParam(value = "scope", required = false) String scope, HttpServletRequest request, HttpServletResponse response) {
    try {
        byte[] result = null;
        RestVariable variable = getVariableFromRequest(taskId, variableName, scope, true);
        if (RestResponseFactory.BYTE_ARRAY_VARIABLE_TYPE.equals(variable.getType())) {
            result = (byte[]) variable.getValue();
            response.setContentType("application/octet-stream");
        } else if (RestResponseFactory.SERIALIZABLE_VARIABLE_TYPE.equals(variable.getType())) {
            ByteArrayOutputStream buffer = new ByteArrayOutputStream();
            ObjectOutputStream outputStream = new ObjectOutputStream(buffer);
            outputStream.writeObject(variable.getValue());
            outputStream.close();
            result = buffer.toByteArray();
            response.setContentType("application/x-java-serialized-object");
        } else {
            throw new ActivitiObjectNotFoundException("The variable does not have a binary data stream.", null);
        }
        return result;
    } catch (IOException ioe) {
        // Re-throw IOException
        throw new ActivitiException("Unexpected error getting variable data", ioe);
    }
}
Also used : RestVariable(org.activiti.rest.service.api.engine.variable.RestVariable) ActivitiException(org.activiti.engine.ActivitiException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ObjectOutputStream(java.io.ObjectOutputStream) ActivitiObjectNotFoundException(org.activiti.engine.ActivitiObjectNotFoundException) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Aggregations

RestVariable (org.activiti.rest.service.api.engine.variable.RestVariable)25 ActivitiObjectNotFoundException (org.activiti.engine.ActivitiObjectNotFoundException)11 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)11 ActivitiException (org.activiti.engine.ActivitiException)10 ActivitiIllegalArgumentException (org.activiti.engine.ActivitiIllegalArgumentException)9 ByteArrayOutputStream (java.io.ByteArrayOutputStream)7 IOException (java.io.IOException)7 ObjectOutputStream (java.io.ObjectOutputStream)7 HashMap (java.util.HashMap)6 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)6 MultipartHttpServletRequest (org.springframework.web.multipart.MultipartHttpServletRequest)5 ArrayList (java.util.ArrayList)4 RestVariableScope (org.activiti.rest.service.api.engine.variable.RestVariable.RestVariableScope)4 Execution (org.activiti.engine.runtime.Execution)3 List (java.util.List)2 Task (org.activiti.engine.task.Task)2 ActivitiConflictException (org.activiti.rest.exception.ActivitiConflictException)2 BooleanRestVariableConverter (org.activiti.rest.service.api.engine.variable.BooleanRestVariableConverter)2 DateRestVariableConverter (org.activiti.rest.service.api.engine.variable.DateRestVariableConverter)2 DoubleRestVariableConverter (org.activiti.rest.service.api.engine.variable.DoubleRestVariableConverter)2