use of org.activiti.engine.RuntimeService in project carbon-business-process by wso2.
the class ProcessInstanceService method getProcessInstanceDiagram.
@GET
@Path("/{processInstanceId}/diagram")
@Produces(MediaType.APPLICATION_JSON)
public Response getProcessInstanceDiagram(@PathParam("processInstanceId") String processInstanceId) {
ProcessInstance processInstance = getProcessInstanceFromRequest(processInstanceId);
RepositoryService repositoryService = BPMNOSGIService.getRepositoryService();
ProcessDefinition pde = repositoryService.getProcessDefinition(processInstance.getProcessDefinitionId());
if (pde != null && pde.hasGraphicalNotation()) {
RuntimeService runtimeService = BPMNOSGIService.getRuntimeService();
InputStream diagramStream = new DefaultProcessDiagramGenerator().generateDiagram(repositoryService.getBpmnModel(pde.getId()), "png", runtimeService.getActiveActivityIds(processInstanceId));
try {
return Response.ok().type("image/png").entity(IOUtils.toByteArray(diagramStream)).build();
} catch (Exception e) {
throw new ActivitiIllegalArgumentException("Error exporting diagram", e);
}
} else {
throw new ActivitiIllegalArgumentException("Process instance with id '" + processInstance.getId() + "' has no graphical notation defined.");
}
}
use of org.activiti.engine.RuntimeService in project carbon-business-process by wso2.
the class ProcessInstanceService method createExecutionVariable.
protected Response createExecutionVariable(Execution execution, boolean override, int variableType, HttpServletRequest httpServletRequest) throws IOException, ServletException {
boolean debugEnabled = log.isDebugEnabled();
if (debugEnabled) {
log.debug("httpServletRequest.getContentType():" + httpServletRequest.getContentType());
}
Response.ResponseBuilder responseBuilder = Response.ok();
if (debugEnabled) {
log.debug("Processing non binary variable");
}
List<RestVariable> inputVariables = new ArrayList<>();
List<RestVariable> resultVariables = new ArrayList<>();
try {
if (Utils.isApplicationJsonRequest(httpServletRequest)) {
ObjectMapper objectMapper = new ObjectMapper();
@SuppressWarnings("unchecked") List<Object> variableObjects = (List<Object>) objectMapper.readValue(httpServletRequest.getInputStream(), List.class);
for (Object restObject : variableObjects) {
RestVariable restVariable = objectMapper.convertValue(restObject, RestVariable.class);
inputVariables.add(restVariable);
}
} else if (Utils.isApplicationXmlRequest(httpServletRequest)) {
JAXBContext jaxbContext;
try {
XMLInputFactory xif = XMLInputFactory.newInstance();
xif.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false);
xif.setProperty(XMLInputFactory.SUPPORT_DTD, false);
XMLStreamReader xsr = xif.createXMLStreamReader(new StreamSource(httpServletRequest.getInputStream()));
jaxbContext = JAXBContext.newInstance(RestVariableCollection.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
RestVariableCollection restVariableCollection = (RestVariableCollection) jaxbUnmarshaller.unmarshal(xsr);
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;
Map<String, Object> variablesToSet = new HashMap<>();
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 (!override && hasVariableOnScope(execution, var.getName(), varScope)) {
throw new BPMNConflictException("Variable '" + var.getName() + "' is already present on execution '" + execution.getId() + "'.");
}
RestResponseFactory restResponseFactory = new RestResponseFactory();
Object actualVariableValue = restResponseFactory.getVariableValue(var);
variablesToSet.put(var.getName(), actualVariableValue);
resultVariables.add(restResponseFactory.createRestVariable(var.getName(), actualVariableValue, varScope, execution.getId(), variableType, false, uriInfo.getBaseUri().toString()));
}
if (!variablesToSet.isEmpty()) {
RuntimeService runtimeService = BPMNOSGIService.getRuntimeService();
if (sharedScope == RestVariable.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.");
}
}
}
RestVariableCollection restVariableCollection = new RestVariableCollection();
restVariableCollection.setRestVariables(resultVariables);
responseBuilder.entity(restVariableCollection);
// }
return responseBuilder.status(Response.Status.CREATED).build();
}
use of org.activiti.engine.RuntimeService 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.activiti.engine.RuntimeService in project carbon-business-process by wso2.
the class CorrelationProcess method getQueryResponse.
public Response getQueryResponse(CorrelationActionRequest correlationActionRequest, UriInfo uriInfo) {
RuntimeService runtimeService = BPMNOSGIService.getRuntimeService();
ExecutionQuery query = runtimeService.createExecutionQuery();
String value = correlationActionRequest.getProcessDefinitionId();
if (value != null) {
query.processDefinitionId(value);
}
value = correlationActionRequest.getProcessDefinitionKey();
if (value != null) {
query.processDefinitionKey(value);
}
value = correlationActionRequest.getMessageName();
if (value != null) {
query.messageEventSubscriptionName(value);
}
value = correlationActionRequest.getSignalName();
if (value != null) {
query.signalEventSubscriptionName(value);
}
List<QueryVariable> queryVariableList = correlationActionRequest.getCorrelationVariables();
if (queryVariableList != null) {
List<QueryVariable> updatedQueryVariableList = new ArrayList<>();
for (QueryVariable queryVariable : queryVariableList) {
if (queryVariable.getVariableOperation() == null) {
queryVariable.setOperation("equals");
}
updatedQueryVariableList.add(queryVariable);
}
addVariables(query, updatedQueryVariableList, true);
}
value = correlationActionRequest.getTenantId();
if (value != null) {
query.executionTenantId(value);
}
QueryProperty qp = allowedSortProperties.get("processInstanceId");
((AbstractQuery) query).orderBy(qp);
query.asc();
List<Execution> executionList = query.listPage(0, 10);
int size = executionList.size();
if (size == 0) {
throw new ActivitiIllegalArgumentException("No Executions found to correlate with given information");
}
if (size > 1) {
throw new ActivitiIllegalArgumentException("More than one Executions found to correlate with given information");
}
Execution execution = executionList.get(0);
String action = correlationActionRequest.getAction();
if (CorrelationActionRequest.ACTION_SIGNAL.equals(action)) {
if (correlationActionRequest.getVariables() != null) {
runtimeService.signal(execution.getId(), getVariablesToSet(correlationActionRequest));
} else {
runtimeService.signal(execution.getId());
}
} else if (CorrelationActionRequest.ACTION_SIGNAL_EVENT_RECEIVED.equals(action)) {
if (correlationActionRequest.getSignalName() == null) {
throw new ActivitiIllegalArgumentException("Signal name is required");
}
if (correlationActionRequest.getVariables() != null) {
runtimeService.signalEventReceived(correlationActionRequest.getSignalName(), execution.getId(), getVariablesToSet(correlationActionRequest));
} else {
runtimeService.signalEventReceived(correlationActionRequest.getSignalName(), execution.getId());
}
} else if (CorrelationActionRequest.ACTION_MESSAGE_EVENT_RECEIVED.equals(action)) {
if (correlationActionRequest.getMessageName() == null) {
throw new ActivitiIllegalArgumentException("Message name is required");
}
if (correlationActionRequest.getVariables() != null) {
runtimeService.messageEventReceived(correlationActionRequest.getMessageName(), execution.getId(), getVariablesToSet(correlationActionRequest));
} else {
runtimeService.messageEventReceived(correlationActionRequest.getMessageName(), execution.getId());
}
} else {
throw new ActivitiIllegalArgumentException("Invalid action: '" + correlationActionRequest.getAction() + "'.");
}
Response.ResponseBuilder responseBuilder = Response.ok();
// Re-fetch the execution, could have changed due to action or even completed
execution = runtimeService.createExecutionQuery().executionId(execution.getId()).singleResult();
if (execution == null) {
// Execution is finished, return empty body to inform user
responseBuilder.status(Response.Status.NO_CONTENT);
return responseBuilder.build();
} else {
return responseBuilder.entity(new RestResponseFactory().createExecutionResponse(execution, uriInfo.getBaseUri().toString())).build();
}
}
use of org.activiti.engine.RuntimeService in project crnk-framework by crnk-project.
the class ActivitiTestBase method teardown.
@After
public void teardown() {
RuntimeService runtimeService = processEngine.getRuntimeService();
List<ProcessInstance> processInstances = runtimeService.createProcessInstanceQuery().list();
for (ProcessInstance processInstance : processInstances) {
runtimeService.deleteProcessInstance(processInstance.getId(), "");
}
TaskService taskService = processEngine.getTaskService();
List<Task> tasks = taskService.createTaskQuery().list();
for (Task task : tasks) {
taskService.deleteTask(task.getId());
}
}
Aggregations