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