use of org.wso2.siddhi.query.api.expression.Variable in project carbon-business-process by wso2.
the class ProcessInstanceService 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);
}
RuntimeService runtimeService = BPMNOSGIService.getRuntimeService();
RestVariable.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 = RestVariable.RestVariableScope.LOCAL;
variableFound = true;
} else {
if (execution.getParentId() != null) {
value = runtimeService.getVariable(execution.getParentId(), variableName);
variableScope = RestVariable.RestVariableScope.GLOBAL;
variableFound = true;
}
}
} else if (variableScope == RestVariable.RestVariableScope.GLOBAL) {
// Use parent to get variables
if (execution.getParentId() != null) {
value = runtimeService.getVariable(execution.getParentId(), variableName);
variableScope = RestVariable.RestVariableScope.GLOBAL;
variableFound = true;
}
} else if (variableScope == RestVariable.RestVariableScope.LOCAL) {
value = runtimeService.getVariableLocal(execution.getId(), variableName);
variableScope = RestVariable.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.wso2.siddhi.query.api.expression.Variable in project carbon-business-process by wso2.
the class ProcessInstanceService method getVariableDataByteArray.
protected byte[] getVariableDataByteArray(Execution execution, String variableName, String scope, Response.ResponseBuilder responseBuilder) {
try {
byte[] result;
RestVariable variable = getVariableFromRequest(execution, variableName, scope, true);
if (RestResponseFactory.BYTE_ARRAY_VARIABLE_TYPE.equals(variable.getType())) {
result = (byte[]) variable.getValue();
responseBuilder.type(MediaType.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();
responseBuilder.type("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.wso2.siddhi.query.api.expression.Variable in project carbon-business-process by wso2.
the class ExecutionService method deleteVariable.
@DELETE
@Path("/{executionId}/variables/{variableName}")
@Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
public Response deleteVariable(@PathParam("executionId") String executionId, @PathParam("variableName") String variableName) {
String scope = uriInfo.getQueryParameters().getFirst("scope");
Execution execution = getExecutionFromRequest(executionId);
// Determine scope
RestVariable.RestVariableScope variableScope = RestVariable.RestVariableScope.LOCAL;
if (scope != null) {
variableScope = RestVariable.getScopeFromString(scope);
}
if (!hasVariableOnScope(execution, variableName, variableScope)) {
throw new ActivitiObjectNotFoundException("Execution '" + execution.getId() + "' doesn't have a variable '" + variableName + "' in scope " + variableScope.name().toLowerCase(), VariableInstanceEntity.class);
}
RuntimeService runtimeService = BPMNOSGIService.getRuntimeService();
if (variableScope == RestVariable.RestVariableScope.LOCAL) {
runtimeService.removeVariableLocal(execution.getId(), variableName);
} else {
// Safe to use parentId, as the hasVariableOnScope would have stopped a global-var update on a root-execution
runtimeService.removeVariable(execution.getParentId(), variableName);
}
return Response.ok().status(Response.Status.NO_CONTENT).build();
}
use of org.wso2.siddhi.query.api.expression.Variable in project carbon-business-process by wso2.
the class WorkflowTaskService method deleteVariable.
@DELETE
@Path("/{taskId}/variables/{variableName}")
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
public Response deleteVariable(@PathParam("taskId") String taskId, @PathParam("variableName") String variableName) {
String scopeString = uriInfo.getQueryParameters().getFirst("scope");
Task task = getTaskFromRequest(taskId);
// Determine scope
RestVariable.RestVariableScope scope = RestVariable.RestVariableScope.LOCAL;
if (scopeString != null) {
scope = RestVariable.getScopeFromString(scopeString);
}
if (!hasVariableOnScope(task, variableName, scope)) {
throw new ActivitiObjectNotFoundException("Task '" + task.getId() + "' doesn't have a variable '" + variableName + "' in scope " + scope.name().toLowerCase(), VariableInstanceEntity.class);
}
RuntimeService runtimeService = BPMNOSGIService.getRuntimeService();
TaskService taskService = BPMNOSGIService.getTaskService();
if (scope == RestVariable.RestVariableScope.LOCAL) {
taskService.removeVariableLocal(task.getId(), variableName);
} else {
// Safe to use executionId, as the hasVariableOnScope whould have stopped a global-var update on standalone task
runtimeService.removeVariable(task.getExecutionId(), variableName);
}
return Response.ok().status(Response.Status.NO_CONTENT).build();
}
use of org.wso2.siddhi.query.api.expression.Variable in project carbon-business-process by wso2.
the class WorkflowTaskService method createTaskVariable.
@POST
@Path("/{taskId}/variables")
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
public Response createTaskVariable(@PathParam("taskId") String taskId, @Context HttpServletRequest httpServletRequest) {
Task task = getTaskFromRequest(taskId);
List<RestVariable> inputVariables = new ArrayList<>();
List<RestVariable> resultVariables = new ArrayList<>();
Response.ResponseBuilder responseBuilder = Response.ok();
try {
if (Utils.isApplicationJsonRequest(httpServletRequest)) {
try {
@SuppressWarnings("unchecked") List<Object> variableObjects = (List<Object>) new ObjectMapper().readValue(httpServletRequest.getInputStream(), List.class);
for (Object restObject : variableObjects) {
RestVariable restVariable = new ObjectMapper().convertValue(restObject, RestVariable.class);
inputVariables.add(restVariable);
}
} catch (IOException e) {
throw new ActivitiIllegalArgumentException("request body could not be transformed to a RestVariable " + "instance.", e);
}
} else if (Utils.isApplicationXmlRequest(httpServletRequest)) {
JAXBContext jaxbContext = null;
try {
jaxbContext = JAXBContext.newInstance(RestVariableCollection.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
RestVariableCollection restVariableCollection = (RestVariableCollection) jaxbUnmarshaller.unmarshal(getXMLReader(httpServletRequest.getInputStream()));
if (restVariableCollection == null) {
throw new ActivitiIllegalArgumentException("xml request body could not be transformed to a " + "RestVariable Collection instance.");
}
List<RestVariable> restVariableList = restVariableCollection.getRestVariables();
if (restVariableList.size() == 0) {
throw new ActivitiIllegalArgumentException("xml request body could not identify any rest " + "variables to be updated");
}
for (RestVariable restVariable : restVariableList) {
inputVariables.add(restVariable);
}
} catch (JAXBException | IOException e) {
throw new ActivitiIllegalArgumentException("xml request body could not be transformed to a " + "RestVariable instance.", e);
}
}
} catch (Exception e) {
throw new ActivitiIllegalArgumentException("Failed to serialize to a RestVariable instance", e);
}
if (inputVariables.size() == 0) {
throw new ActivitiIllegalArgumentException("Request didn't contain a list of variables to create.");
}
RestVariable.RestVariableScope sharedScope = null;
RestVariable.RestVariableScope varScope = null;
Map<String, Object> variablesToSet = new HashMap<>();
RestResponseFactory restResponseFactory = new RestResponseFactory();
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 = RestVariable.RestVariableScope.LOCAL;
}
if (sharedScope == null) {
sharedScope = varScope;
}
if (varScope != sharedScope) {
throw new ActivitiIllegalArgumentException("Only allowed to update multiple variables in the same scope.");
}
if (hasVariableOnScope(task, var.getName(), varScope)) {
throw new BPMNConflictException("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, uriInfo.getBaseUri().toString()));
}
RuntimeService runtimeService = BPMNOSGIService.getRuntimeService();
TaskService taskService = BPMNOSGIService.getTaskService();
if (!variablesToSet.isEmpty()) {
if (sharedScope == RestVariable.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.");
}
}
}
RestVariableCollection restVariableCollection = new RestVariableCollection();
restVariableCollection.setRestVariables(resultVariables);
responseBuilder.entity(restVariableCollection);
return responseBuilder.status(Response.Status.CREATED).build();
}
Aggregations