Search in sources :

Example 6 with RestVariable

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

the class TaskVariableCollectionResource method addGlobalVariables.

protected void addGlobalVariables(Task task, Map<String, RestVariable> variableMap) {
    if (task.getExecutionId() != null) {
        Map<String, Object> rawVariables = runtimeService.getVariables(task.getExecutionId());
        List<RestVariable> globalVariables = restResponseFactory.createRestVariables(rawVariables, task.getId(), RestResponseFactory.VARIABLE_TASK, RestVariableScope.GLOBAL);
        // 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 7 with RestVariable

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

Example 8 with RestVariable

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

the class TaskVariableResource method updateVariable.

@RequestMapping(value = "/runtime/tasks/{taskId}/variables/{variableName}", method = RequestMethod.PUT, produces = "application/json")
public RestVariable updateVariable(@PathVariable("taskId") String taskId, @PathVariable("variableName") String variableName, @RequestParam(value = "scope", required = false) String scope, HttpServletRequest request) {
    Task task = getTaskFromRequest(taskId);
    RestVariable result = null;
    if (request instanceof MultipartHttpServletRequest) {
        result = setBinaryVariable((MultipartHttpServletRequest) request, task, 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("Error converting request body to RestVariable instance", e);
        }
        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, task, false);
    }
    return result;
}
Also used : RestVariable(org.activiti.rest.service.api.engine.variable.RestVariable) Task(org.activiti.engine.task.Task) ActivitiException(org.activiti.engine.ActivitiException) 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 9 with RestVariable

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

the class ExecutionVariableDataResource method getVariableData.

@RequestMapping(value = "/runtime/executions/{executionId}/variables/{variableName}/data", method = RequestMethod.GET)
@ResponseBody
public byte[] getVariableData(@PathVariable("executionId") String executionId, @PathVariable("variableName") String variableName, @RequestParam(value = "scope", required = false) String scope, HttpServletRequest request, HttpServletResponse response) {
    try {
        byte[] result = null;
        Execution execution = getExecutionFromRequest(executionId);
        RestVariable variable = getVariableFromRequest(execution, 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) {
        throw new ActivitiException("Error getting variable " + variableName, ioe);
    }
}
Also used : RestVariable(org.activiti.rest.service.api.engine.variable.RestVariable) ActivitiException(org.activiti.engine.ActivitiException) Execution(org.activiti.engine.runtime.Execution) 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)

Example 10 with RestVariable

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

the class ProcessInstanceCollectionResource method createProcessInstance.

@RequestMapping(value = "/runtime/process-instances", method = RequestMethod.POST, produces = "application/json")
public ProcessInstanceResponse createProcessInstance(@RequestBody ProcessInstanceCreateRequest request, HttpServletRequest httpRequest, HttpServletResponse response) {
    if (request.getProcessDefinitionId() == null && request.getProcessDefinitionKey() == null && request.getMessage() == null) {
        throw new ActivitiIllegalArgumentException("Either processDefinitionId, processDefinitionKey or message is required.");
    }
    int paramsSet = ((request.getProcessDefinitionId() != null) ? 1 : 0) + ((request.getProcessDefinitionKey() != null) ? 1 : 0) + ((request.getMessage() != null) ? 1 : 0);
    if (paramsSet > 1) {
        throw new ActivitiIllegalArgumentException("Only one of processDefinitionId, processDefinitionKey or message should be set.");
    }
    if (request.isCustomTenantSet()) {
        // Tenant-id can only be used with either key or message
        if (request.getProcessDefinitionId() != null) {
            throw new ActivitiIllegalArgumentException("TenantId can only be used with either processDefinitionKey or message.");
        }
    }
    Map<String, Object> startVariables = null;
    if (request.getVariables() != null) {
        startVariables = new HashMap<String, Object>();
        for (RestVariable variable : request.getVariables()) {
            if (variable.getName() == null) {
                throw new ActivitiIllegalArgumentException("Variable name is required.");
            }
            startVariables.put(variable.getName(), restResponseFactory.getVariableValue(variable));
        }
    }
    // Actually start the instance based on key or id
    try {
        ProcessInstance instance = null;
        if (request.getProcessDefinitionId() != null) {
            instance = runtimeService.startProcessInstanceById(request.getProcessDefinitionId(), request.getBusinessKey(), startVariables);
        } else if (request.getProcessDefinitionKey() != null) {
            if (request.isCustomTenantSet()) {
                instance = runtimeService.startProcessInstanceByKeyAndTenantId(request.getProcessDefinitionKey(), request.getBusinessKey(), startVariables, request.getTenantId());
            } else {
                instance = runtimeService.startProcessInstanceByKey(request.getProcessDefinitionKey(), request.getBusinessKey(), startVariables);
            }
        } else {
            if (request.isCustomTenantSet()) {
                instance = runtimeService.startProcessInstanceByMessageAndTenantId(request.getMessage(), request.getBusinessKey(), startVariables, request.getTenantId());
            } else {
                instance = runtimeService.startProcessInstanceByMessage(request.getMessage(), request.getBusinessKey(), startVariables);
            }
        }
        response.setStatus(HttpStatus.CREATED.value());
        //Added by Ryan Johnston
        if (request.getReturnVariables()) {
            Map<String, Object> runtimeVariableMap = null;
            List<HistoricVariableInstance> historicVariableList = null;
            if (instance.isEnded()) {
                historicVariableList = historyService.createHistoricVariableInstanceQuery().processInstanceId(instance.getId()).list();
            } else {
                runtimeVariableMap = runtimeService.getVariables(instance.getId());
            }
            return restResponseFactory.createProcessInstanceResponse(instance, true, runtimeVariableMap, historicVariableList);
        } else {
            return restResponseFactory.createProcessInstanceResponse(instance);
        }
    //End Added by Ryan Johnston
    //Removed by Ryan Johnston (obsolete given the above).
    //return factory.createProcessInstanceResponse(this, instance);
    } catch (ActivitiObjectNotFoundException aonfe) {
        throw new ActivitiIllegalArgumentException(aonfe.getMessage(), aonfe);
    }
}
Also used : RestVariable(org.activiti.rest.service.api.engine.variable.RestVariable) ActivitiIllegalArgumentException(org.activiti.engine.ActivitiIllegalArgumentException) ProcessInstance(org.activiti.engine.runtime.ProcessInstance) HistoricVariableInstance(org.activiti.engine.history.HistoricVariableInstance) ActivitiObjectNotFoundException(org.activiti.engine.ActivitiObjectNotFoundException) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

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