Search in sources :

Example 1 with HistoryService

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();
}
Also used : ReplaySimulationRun(org.activiti.crystalball.simulator.ReplaySimulationRun) Task(org.activiti.engine.task.Task) RuntimeService(org.activiti.engine.RuntimeService) HashMap(java.util.HashMap) TaskService(org.activiti.engine.TaskService) EventLogEntry(org.activiti.engine.event.EventLogEntry) SimpleEventCalendar(org.activiti.crystalball.simulator.SimpleEventCalendar) NoExecutionVariableScope(org.activiti.engine.impl.el.NoExecutionVariableScope) HistoryService(org.activiti.engine.HistoryService) SimulationEventComparator(org.activiti.crystalball.simulator.SimulationEventComparator) EventLogTransformer(org.activiti.crystalball.simulator.delegate.event.impl.EventLogTransformer) ManagementService(org.activiti.engine.ManagementService) SimulationDebugger(org.activiti.crystalball.simulator.SimulationDebugger) ProcessInstance(org.activiti.engine.runtime.ProcessInstance) HistoricVariableInstance(org.activiti.engine.history.HistoricVariableInstance) SimulationEvent(org.activiti.crystalball.simulator.SimulationEvent) ProcessEngineImpl(org.activiti.engine.impl.ProcessEngineImpl) Test(org.junit.Test)

Example 2 with HistoryService

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);
}
Also used : HistoricVariableUpdate(org.activiti.engine.history.HistoricVariableUpdate) HistoricDetail(org.activiti.engine.history.HistoricDetail) HistoricFormProperty(org.activiti.engine.history.HistoricFormProperty) HashMap(java.util.HashMap) HistoryService(org.activiti.engine.HistoryService) ProcessInstance(org.activiti.engine.runtime.ProcessInstance) HistoricDetailQuery(org.activiti.engine.history.HistoricDetailQuery)

Example 3 with HistoryService

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();
}
Also used : RestResponseFactory(org.wso2.carbon.bpmn.rest.common.RestResponseFactory) DataResponse(org.wso2.carbon.bpmn.rest.model.common.DataResponse) HistoryService(org.activiti.engine.HistoryService) HistoricProcessInstancePaginateList(org.wso2.carbon.bpmn.rest.model.history.HistoricProcessInstancePaginateList) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Produces(javax.ws.rs.Produces) Consumes(javax.ws.rs.Consumes)

Example 4 with HistoryService

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;
}
Also used : HistoricProcessInstanceQuery(org.activiti.engine.history.HistoricProcessInstanceQuery) HistoricProcessInstance(org.activiti.engine.history.HistoricProcessInstance) ArrayList(java.util.ArrayList) HistoryService(org.activiti.engine.HistoryService) ProcessDefinition(org.activiti.engine.repository.ProcessDefinition) ResponseHolder(org.wso2.carbon.bpmn.rest.model.stats.ResponseHolder) ProcessInstanceAverageInfo(org.wso2.carbon.bpmn.rest.model.stats.ProcessInstanceAverageInfo) RepositoryService(org.activiti.engine.RepositoryService) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 5 with HistoryService

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);
            }
        }
    }
}
Also used : HistoricProcessInstanceQuery(org.activiti.engine.history.HistoricProcessInstanceQuery) RuntimeService(org.activiti.engine.RuntimeService) HistoricProcessInstance(org.activiti.engine.history.HistoricProcessInstance) HistoryService(org.activiti.engine.HistoryService) HistoricProcessInstance(org.activiti.engine.history.HistoricProcessInstance) ProcessInstance(org.activiti.engine.runtime.ProcessInstance)

Aggregations

HistoryService (org.activiti.engine.HistoryService)31 RestResponseFactory (org.wso2.carbon.bpmn.rest.common.RestResponseFactory)11 HistoricProcessInstance (org.activiti.engine.history.HistoricProcessInstance)10 ActivitiObjectNotFoundException (org.activiti.engine.ActivitiObjectNotFoundException)6 HistoricProcessInstanceQuery (org.activiti.engine.history.HistoricProcessInstanceQuery)6 HistoricTaskInstance (org.activiti.engine.history.HistoricTaskInstance)5 ProcessInstance (org.activiti.engine.runtime.ProcessInstance)5 ArrayList (java.util.ArrayList)4 Path (javax.ws.rs.Path)4 Produces (javax.ws.rs.Produces)4 RuntimeService (org.activiti.engine.RuntimeService)4 HashMap (java.util.HashMap)3 HistoricVariableInstance (org.activiti.engine.history.HistoricVariableInstance)3 Consumes (javax.ws.rs.Consumes)2 GET (javax.ws.rs.GET)2 POST (javax.ws.rs.POST)2 RepositoryService (org.activiti.engine.RepositoryService)2 HistoricActivityInstanceQuery (org.activiti.engine.history.HistoricActivityInstanceQuery)2 HistoricDetail (org.activiti.engine.history.HistoricDetail)2 HistoricDetailQuery (org.activiti.engine.history.HistoricDetailQuery)2