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);
}
}
}
}
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;
}
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;
}
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);
}
}
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);
}
}
Aggregations