use of org.activiti.engine.HistoryService in project Activiti by Activiti.
the class ReplayEventLogTest method testProcessInstanceStartEvents.
@Test
public void testProcessInstanceStartEvents() throws Exception {
ProcessEngineImpl processEngine = initProcessEngine();
TaskService taskService = processEngine.getTaskService();
RuntimeService runtimeService = processEngine.getRuntimeService();
ManagementService managementService = processEngine.getManagementService();
HistoryService historyService = processEngine.getHistoryService();
// record events
Map<String, Object> variables = new HashMap<String, Object>();
variables.put(TEST_VARIABLE, TEST_VALUE);
ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(USERTASK_PROCESS, BUSINESS_KEY, variables);
Task task = taskService.createTaskQuery().taskDefinitionKey("userTask").singleResult();
TimeUnit.MILLISECONDS.sleep(50);
variables = new HashMap<String, Object>();
variables.put(TASK_TEST_VARIABLE, TASK_TEST_VALUE);
taskService.complete(task.getId(), variables);
// transform log events
List<EventLogEntry> eventLogEntries = managementService.getEventLogEntries(null, null);
EventLogTransformer transformer = new EventLogTransformer(getTransformers());
List<SimulationEvent> simulationEvents = transformer.transform(eventLogEntries);
SimpleEventCalendar eventCalendar = new SimpleEventCalendar(processEngine.getProcessEngineConfiguration().getClock(), new SimulationEventComparator());
eventCalendar.addEvents(simulationEvents);
// replay process instance run
final SimulationDebugger simRun = new ReplaySimulationRun(processEngine, eventCalendar, getReplayHandlers(processInstance.getId()));
simRun.init(new NoExecutionVariableScope());
// original process is finished - there should not be any running process instance/task
assertEquals(0, runtimeService.createProcessInstanceQuery().processDefinitionKey(USERTASK_PROCESS).count());
assertEquals(0, taskService.createTaskQuery().taskDefinitionKey("userTask").count());
simRun.step();
// replay process was started
ProcessInstance replayProcessInstance = runtimeService.createProcessInstanceQuery().processDefinitionKey(USERTASK_PROCESS).singleResult();
assertNotNull(replayProcessInstance);
assertTrue(replayProcessInstance.getId().equals(processInstance.getId()) == false);
assertEquals(TEST_VALUE, runtimeService.getVariable(replayProcessInstance.getId(), TEST_VARIABLE));
// there should be one task
assertEquals(1, taskService.createTaskQuery().taskDefinitionKey("userTask").count());
simRun.step();
// userTask was completed - replay process was finished
assertEquals(0, runtimeService.createProcessInstanceQuery().processDefinitionKey(USERTASK_PROCESS).count());
assertEquals(0, taskService.createTaskQuery().taskDefinitionKey("userTask").count());
HistoricVariableInstance variableInstance = historyService.createHistoricVariableInstanceQuery().processInstanceId(replayProcessInstance.getId()).variableName(TASK_TEST_VARIABLE).singleResult();
assertNotNull(variableInstance);
assertEquals(TASK_TEST_VALUE, variableInstance.getValue());
// close simulation
simRun.close();
processEngine.close();
ProcessEngines.destroy();
}
use of org.activiti.engine.HistoryService in project midpoint by Evolveum.
the class ActivitiInterface method queryActivitiProcessInstance.
public void queryActivitiProcessInstance(QueryProcessCommand qpc, Task task, OperationResult result) throws ObjectAlreadyExistsException, ObjectNotFoundException, SchemaException {
String pid = qpc.getPid();
LOGGER.trace("Querying process instance id {}", pid);
HistoryService hs = activitiEngine.getHistoryService();
HistoricDetailQuery hdq = hs.createHistoricDetailQuery().variableUpdates().processInstanceId(pid).orderByTime().desc();
Map<String, Object> variables = new HashMap<>();
for (HistoricDetail hd : hdq.list()) {
HistoricVariableUpdate hvu = (HistoricVariableUpdate) hd;
String varname = hvu.getVariableName();
Object value = hvu.getValue();
LOGGER.trace(" - found historic variable update: {} <- {}", varname, value);
if (!variables.containsKey(varname)) {
variables.put(varname, value);
}
}
HistoricDetailQuery hdq2 = hs.createHistoricDetailQuery().formProperties().processInstanceId(pid).orderByVariableRevision().desc();
for (HistoricDetail hd : hdq2.list()) {
HistoricFormProperty hfp = (HistoricFormProperty) hd;
String varname = hfp.getPropertyId();
Object value = hfp.getPropertyValue();
LOGGER.trace(" - found historic form property: {} <- {}", varname, value);
variables.put(varname, value);
}
QueryProcessResponse qpr = new QueryProcessResponse(pid, variables, processInterfaceFinder);
ProcessInstance pi = activitiEngine.getProcessEngine().getRuntimeService().createProcessInstanceQuery().processInstanceId(pid).singleResult();
qpr.setRunning(pi != null && !pi.isEnded());
LOGGER.trace("Running process instance = {}, isRunning: {}", pi, qpr.isRunning());
LOGGER.trace("Response to be sent to midPoint: {}", qpr);
wfTaskController.onProcessEvent(qpr, task, result);
}
use of org.activiti.engine.HistoryService in project carbon-business-process by wso2.
the class HistoricProcessInstanceQueryService method queryProcessInstances.
@POST
@Path("/")
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
public Response queryProcessInstances(HistoricProcessInstanceQueryRequest queryRequest) {
Map<String, String> allRequestParams = new HashMap<>();
for (String property : allPropertiesList) {
String value = uriInfo.getQueryParameters().getFirst(property);
if (value != null) {
allRequestParams.put(property, value);
}
}
HistoryService historyService = BPMNOSGIService.getHistoryService();
org.activiti.engine.history.HistoricProcessInstanceQuery query = historyService.createHistoricProcessInstanceQuery();
// Populate query based on request
if (queryRequest.getProcessInstanceId() != null) {
query.processInstanceId(queryRequest.getProcessInstanceId());
}
if (queryRequest.getProcessInstanceIds() != null && !queryRequest.getProcessInstanceIds().isEmpty()) {
query.processInstanceIds(new HashSet<>(queryRequest.getProcessInstanceIds()));
}
if (queryRequest.getProcessDefinitionKey() != null) {
query.processDefinitionKey(queryRequest.getProcessDefinitionKey());
}
if (queryRequest.getProcessDefinitionId() != null) {
query.processDefinitionId(queryRequest.getProcessDefinitionId());
}
if (queryRequest.getProcessBusinessKey() != null) {
query.processInstanceBusinessKey(queryRequest.getProcessBusinessKey());
}
if (queryRequest.getInvolvedUser() != null) {
query.involvedUser(queryRequest.getInvolvedUser());
}
if (queryRequest.getSuperProcessInstanceId() != null) {
query.superProcessInstanceId(queryRequest.getSuperProcessInstanceId());
}
if (queryRequest.getExcludeSubprocesses() != null) {
query.excludeSubprocesses(queryRequest.getExcludeSubprocesses());
}
if (queryRequest.getFinishedAfter() != null) {
query.finishedAfter(queryRequest.getFinishedAfter());
}
if (queryRequest.getFinishedBefore() != null) {
query.finishedBefore(queryRequest.getFinishedBefore());
}
if (queryRequest.getStartedAfter() != null) {
query.startedAfter(queryRequest.getStartedAfter());
}
if (queryRequest.getStartedBefore() != null) {
query.startedBefore(queryRequest.getStartedBefore());
}
if (queryRequest.getStartedBy() != null) {
query.startedBy(queryRequest.getStartedBy());
}
if (queryRequest.getFinished() != null) {
if (queryRequest.getFinished()) {
query.finished();
} else {
query.unfinished();
}
}
if (queryRequest.getIncludeProcessVariables() != null) {
if (queryRequest.getIncludeProcessVariables()) {
query.includeProcessVariables();
}
}
if (queryRequest.getVariables() != null) {
addVariables(query, queryRequest.getVariables());
}
if (queryRequest.getTenantId() != null) {
query.processInstanceTenantId(queryRequest.getTenantId());
}
if (queryRequest.getTenantIdLike() != null) {
query.processInstanceTenantIdLike(queryRequest.getTenantIdLike());
}
if (Boolean.TRUE.equals(queryRequest.getWithoutTenantId())) {
query.processInstanceWithoutTenantId();
}
DataResponse dataResponse = new HistoricProcessInstancePaginateList(new RestResponseFactory(), uriInfo).paginateList(allRequestParams, queryRequest, query, "processInstanceId", allowedSortProperties);
return Response.ok().entity(dataResponse).build();
}
use of org.activiti.engine.HistoryService in project carbon-business-process by wso2.
the class ProcessStatisticsService method getAvgTimeDurationForCompletedProcesses.
/**
* Get the average time duration of completed processes
*
* @return list with the completed processes and the average time duration taken for each process
*/
@GET
@Path("/process-instances/duration/average")
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
public ResponseHolder getAvgTimeDurationForCompletedProcesses() {
ResponseHolder response = new ResponseHolder();
List<Object> list = new ArrayList<>();
HistoryService historyService = BPMNOSGIService.getHistoryService();
RepositoryService repositoryService = BPMNOSGIService.getRepositoryService();
List<ProcessDefinition> deployedProcessList = repositoryService.createProcessDefinitionQuery().processDefinitionTenantId(getTenantIdStr()).list();
for (ProcessDefinition instance : deployedProcessList) {
ProcessInstanceAverageInfo bpmnProcessInstance = new ProcessInstanceAverageInfo();
bpmnProcessInstance.setProcessDefinitionId(instance.getId());
double totalTime = 0;
double averageTime;
String processDefinitionID = instance.getId();
HistoricProcessInstanceQuery historicProcessInstanceQuery = historyService.createHistoricProcessInstanceQuery().processInstanceTenantId(getTenantIdStr()).processDefinitionId(processDefinitionID).finished();
long instanceCount = historicProcessInstanceQuery.count();
if (instanceCount != 0) {
List<HistoricProcessInstance> instanceList = historicProcessInstanceQuery.list();
for (HistoricProcessInstance completedProcess : instanceList) {
double timeDurationOfTask = completedProcess.getDurationInMillis();
double timeInMins = timeDurationOfTask / (1000 * 60);
totalTime += timeInMins;
}
averageTime = totalTime / instanceCount;
bpmnProcessInstance.setAverageTimeForCompletion(averageTime);
list.add(bpmnProcessInstance);
}
}
response.setData(list);
return response;
}
use of org.activiti.engine.HistoryService in project carbon-business-process by wso2.
the class BPMNDeploymentService method deleteInstances.
private void deleteInstances(BPMNDeletableInstances bpmnDeletableInstances, ProcessEngine processEngine) {
List<HistoricProcessInstance> activeHistoricProcessInstance = bpmnDeletableInstances.getActiveHistoricProcessInstance();
for (HistoricProcessInstance instance : activeHistoricProcessInstance) {
String instanceId = instance.getId();
RuntimeService runtimeService = processEngine.getRuntimeService();
List<ProcessInstance> processInstances = runtimeService.createProcessInstanceQuery().processInstanceTenantId(bpmnDeletableInstances.getTenantId().toString()).processInstanceId(instanceId).list();
if (!processInstances.isEmpty()) {
runtimeService.deleteProcessInstance(instance.getId(), "Deleted by user: " + bpmnDeletableInstances.getTenantId());
}
}
List<String> completedProcessDefinitionIds = bpmnDeletableInstances.getCompletedProcessDefinitionIds();
for (String processId : completedProcessDefinitionIds) {
HistoricProcessInstanceQuery runtimeQuery = processEngine.getHistoryService().createHistoricProcessInstanceQuery().processInstanceTenantId(bpmnDeletableInstances.getTenantId().toString()).includeProcessVariables().finished().processDefinitionId(processId);
int completedProcessInstanceCount = (int) runtimeQuery.count();
if (completedProcessInstanceCount > 0) {
List<HistoricProcessInstance> instances = runtimeQuery.listPage(0, completedProcessInstanceCount + 1);
HistoryService historyService = processEngine.getHistoryService();
for (HistoricProcessInstance instance : instances) {
String instanceId = instance.getId();
historyService.deleteHistoricProcessInstance(instanceId);
}
}
}
}
Aggregations