Search in sources :

Example 26 with ProcessInstanceQuery

use of org.activiti.engine.runtime.ProcessInstanceQuery in project Activiti by Activiti.

the class ProcessInstanceQueryTest method testQueryBySuperProcessInstanceId.

@Deployment(resources = { "org/activiti/engine/test/api/runtime/superProcess.bpmn20.xml", "org/activiti/engine/test/api/runtime/subProcess.bpmn20.xml" })
public void testQueryBySuperProcessInstanceId() {
    ProcessInstance superProcessInstance = runtimeService.startProcessInstanceByKey("subProcessQueryTest");
    ProcessInstanceQuery query = runtimeService.createProcessInstanceQuery().superProcessInstanceId(superProcessInstance.getId());
    ProcessInstance subProcessInstance = query.singleResult();
    assertNotNull(subProcessInstance);
    assertEquals(1, query.list().size());
    assertEquals(1, query.count());
}
Also used : ProcessInstanceQuery(org.activiti.engine.runtime.ProcessInstanceQuery) ProcessInstance(org.activiti.engine.runtime.ProcessInstance) Deployment(org.activiti.engine.test.Deployment)

Example 27 with ProcessInstanceQuery

use of org.activiti.engine.runtime.ProcessInstanceQuery in project Activiti by Activiti.

the class ProcessInstanceQueryTest method testQueryLongVariable.

@Deployment(resources = { "org/activiti/engine/test/api/oneTaskProcess.bpmn20.xml" })
public void testQueryLongVariable() {
    Map<String, Object> vars = new HashMap<String, Object>();
    vars.put("longVar", 12345L);
    ProcessInstance processInstance1 = runtimeService.startProcessInstanceByKey("oneTaskProcess", vars);
    vars = new HashMap<String, Object>();
    vars.put("longVar", 12345L);
    vars.put("longVar2", 67890L);
    ProcessInstance processInstance2 = runtimeService.startProcessInstanceByKey("oneTaskProcess", vars);
    vars = new HashMap<String, Object>();
    vars.put("longVar", 55555L);
    ProcessInstance processInstance3 = runtimeService.startProcessInstanceByKey("oneTaskProcess", vars);
    // Query on single long variable, should result in 2 matches
    ProcessInstanceQuery query = runtimeService.createProcessInstanceQuery().variableValueEquals("longVar", 12345L);
    List<ProcessInstance> processInstances = query.list();
    assertNotNull(processInstances);
    assertEquals(2, processInstances.size());
    // Query on two long variables, should result in single match
    query = runtimeService.createProcessInstanceQuery().variableValueEquals("longVar", 12345L).variableValueEquals("longVar2", 67890L);
    ProcessInstance resultInstance = query.singleResult();
    assertNotNull(resultInstance);
    assertEquals(processInstance2.getId(), resultInstance.getId());
    // Query with unexisting variable value
    resultInstance = runtimeService.createProcessInstanceQuery().variableValueEquals("longVar", 999L).singleResult();
    assertNull(resultInstance);
    // Test NOT_EQUALS
    resultInstance = runtimeService.createProcessInstanceQuery().variableValueNotEquals("longVar", 12345L).singleResult();
    assertNotNull(resultInstance);
    assertEquals(processInstance3.getId(), resultInstance.getId());
    // Test GREATER_THAN
    resultInstance = runtimeService.createProcessInstanceQuery().variableValueGreaterThan("longVar", 44444L).singleResult();
    assertNotNull(resultInstance);
    assertEquals(processInstance3.getId(), resultInstance.getId());
    assertEquals(0, runtimeService.createProcessInstanceQuery().variableValueGreaterThan("longVar", 55555L).count());
    assertEquals(3, runtimeService.createProcessInstanceQuery().variableValueGreaterThan("longVar", 1L).count());
    // Test GREATER_THAN_OR_EQUAL
    resultInstance = runtimeService.createProcessInstanceQuery().variableValueGreaterThanOrEqual("longVar", 44444L).singleResult();
    assertNotNull(resultInstance);
    assertEquals(processInstance3.getId(), resultInstance.getId());
    resultInstance = runtimeService.createProcessInstanceQuery().variableValueGreaterThanOrEqual("longVar", 55555L).singleResult();
    assertNotNull(resultInstance);
    assertEquals(processInstance3.getId(), resultInstance.getId());
    assertEquals(3, runtimeService.createProcessInstanceQuery().variableValueGreaterThanOrEqual("longVar", 1L).count());
    // Test LESS_THAN
    processInstances = runtimeService.createProcessInstanceQuery().variableValueLessThan("longVar", 55555L).list();
    assertEquals(2, processInstances.size());
    List<String> expecedIds = Arrays.asList(processInstance1.getId(), processInstance2.getId());
    List<String> ids = new ArrayList<String>(Arrays.asList(processInstances.get(0).getId(), processInstances.get(1).getId()));
    ids.removeAll(expecedIds);
    assertTrue(ids.isEmpty());
    assertEquals(0, runtimeService.createProcessInstanceQuery().variableValueLessThan("longVar", 12345L).count());
    assertEquals(3, runtimeService.createProcessInstanceQuery().variableValueLessThan("longVar", 66666L).count());
    // Test LESS_THAN_OR_EQUAL
    processInstances = runtimeService.createProcessInstanceQuery().variableValueLessThanOrEqual("longVar", 55555L).list();
    assertEquals(3, processInstances.size());
    assertEquals(0, runtimeService.createProcessInstanceQuery().variableValueLessThanOrEqual("longVar", 12344L).count());
    // Test value-only matching
    resultInstance = runtimeService.createProcessInstanceQuery().variableValueEquals(55555L).singleResult();
    assertNotNull(resultInstance);
    assertEquals(processInstance3.getId(), resultInstance.getId());
    processInstances = runtimeService.createProcessInstanceQuery().variableValueEquals(12345L).list();
    assertEquals(2, processInstances.size());
    expecedIds = Arrays.asList(processInstance1.getId(), processInstance2.getId());
    ids = new ArrayList<String>(Arrays.asList(processInstances.get(0).getId(), processInstances.get(1).getId()));
    ids.removeAll(expecedIds);
    assertTrue(ids.isEmpty());
    resultInstance = runtimeService.createProcessInstanceQuery().variableValueEquals(999L).singleResult();
    assertNull(resultInstance);
    runtimeService.deleteProcessInstance(processInstance1.getId(), "test");
    runtimeService.deleteProcessInstance(processInstance2.getId(), "test");
    runtimeService.deleteProcessInstance(processInstance3.getId(), "test");
}
Also used : ProcessInstanceQuery(org.activiti.engine.runtime.ProcessInstanceQuery) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ProcessInstance(org.activiti.engine.runtime.ProcessInstance) Deployment(org.activiti.engine.test.Deployment)

Example 28 with ProcessInstanceQuery

use of org.activiti.engine.runtime.ProcessInstanceQuery in project daijie-example by daijiejay.

the class BaseService method getProcesses.

@Override
public ModelResult<PageResult<Map<String, Object>>> getProcesses() {
    List<Map<String, Object>> rows = new ArrayList<>();
    PageResult<Map<String, Object>> datas = new PageResult<>();
    ProcessInstanceQuery query = runtimeService.createProcessInstanceQuery();
    List<ProcessInstance> processInstances = query.list();
    processInstances.forEach(processInstance -> {
        List<HistoricActivityInstance> historicActivityInstances = historyService.createHistoricActivityInstanceQuery().processInstanceId(processInstance.getId()).unfinished().list();
        if (!historicActivityInstances.isEmpty()) {
            historicActivityInstances.forEach(historicActivityInstance -> {
                Map<String, Object> row = new HashMap<>();
                row.put("processInstanceId", processInstance.getId());
                Task task = taskService.createTaskQuery().taskId(historicActivityInstance.getTaskId()).singleResult();
                row.put("activityName", historicActivityInstance.getActivityName());
                row.put("taskId", historicActivityInstance.getTaskId());
                row.put("createTime", task.getCreateTime());
                row.put("assignee", task.getAssignee());
                rows.add(row);
            });
        } else {
            Map<String, Object> row = new HashMap<>();
            row.put("processInstanceId", processInstance.getId());
            row.put("activityName", "流程结束");
            rows.add(row);
        }
    });
    datas.setTotal(query.count());
    datas.setRows(rows);
    return Result.build(datas);
}
Also used : Task(org.activiti.engine.task.Task) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) PageResult(org.daijie.core.result.PageResult) ProcessInstanceQuery(org.activiti.engine.runtime.ProcessInstanceQuery) ProcessInstance(org.activiti.engine.runtime.ProcessInstance) HashMap(java.util.HashMap) Map(java.util.Map) HistoricActivityInstance(org.activiti.engine.history.HistoricActivityInstance)

Example 29 with ProcessInstanceQuery

use of org.activiti.engine.runtime.ProcessInstanceQuery in project herd by FINRAOS.

the class JobServiceTest method waitUntilActiveProcessesThreshold.

/**
 * Blocks the current calling thread until number of active processes for the process definition id reaches the specified threshold. This method will
 * timeout with an assertion error if the waiting takes longer than 15,000 ms. This is a reasonable amount of time for the JUnits that use this method.
 *
 * @param processDefinitionId the process definition id
 * @param activeProcessesThreshold the threshold for the number of active processes
 */
private void waitUntilActiveProcessesThreshold(String processDefinitionId, int activeProcessesThreshold) throws Exception {
    // Set the start time.
    long startTime = System.currentTimeMillis();
    // Create a process instance query for the active sub-processes.
    ProcessInstanceQuery processInstanceQuery = activitiRuntimeService.createProcessInstanceQuery().processDefinitionId(processDefinitionId).active();
    // Get the current count of the active processes.
    long activeProcessesCount = processInstanceQuery.count();
    // Run while there are less active processes than the specified threshold.
    while (activeProcessesCount < activeProcessesThreshold) {
        // Get the elapsed time.
        long currentTime = System.currentTimeMillis();
        long elapsedTime = currentTime - startTime;
        // If time spent waiting is longer than 15,000 ms
        if (elapsedTime > 15000) {
            // Dump the current runtime variables into the error log to make it easier to debug
            StringBuilder builder = new StringBuilder("Dumping workflow variables due to error:\n");
            builder.append("Process definition id: ").append(processDefinitionId).append('\n');
            builder.append("Active processes threshold: ").append(activeProcessesThreshold).append('\n');
            builder.append("Number of active processes: ").append(activeProcessesCount).append('\n');
            List<Execution> executions = activitiRuntimeService.createExecutionQuery().list();
            builder.append("Total number of executions: ").append(executions.size()).append('\n');
            for (Execution execution : executions) {
                builder.append("Execution - ").append(execution).append(":\n");
                builder.append("    execution.getId():").append(execution.getId()).append('\n');
                builder.append("    execution.getActivityId():").append(execution.getActivityId()).append('\n');
                builder.append("    execution.getParentId():").append(execution.getParentId()).append('\n');
                builder.append("    execution.getProcessInstanceId():").append(execution.getProcessInstanceId()).append('\n');
                builder.append("    execution.isEnded():").append(execution.isEnded()).append('\n');
                builder.append("    execution.isSuspended():").append(execution.isSuspended()).append('\n');
                Map<String, Object> executionVariables = activitiRuntimeService.getVariables(execution.getId());
                for (Map.Entry<String, Object> variable : executionVariables.entrySet()) {
                    builder.append("    ").append(variable).append('\n');
                }
            }
            LOGGER.error(builder.toString());
            // Fail assertion
            fail("The test did not finished in the specified timeout (15s). See error logs for variable dump.");
        }
        // Sleep for 100 ms.
        Thread.sleep(100);
        // Update the current count of the active processes.
        activeProcessesCount = processInstanceQuery.count();
    }
}
Also used : ProcessInstanceQuery(org.activiti.engine.runtime.ProcessInstanceQuery) Execution(org.activiti.engine.runtime.Execution) Map(java.util.Map) HashMap(java.util.HashMap)

Example 30 with ProcessInstanceQuery

use of org.activiti.engine.runtime.ProcessInstanceQuery in project herd by FINRAOS.

the class ActivitiServiceTest method testGetProcessInstanceById.

@Test
public void testGetProcessInstanceById() {
    String processInstanceId = "processInstanceId";
    ProcessInstanceQuery processInstanceQuery = mock(ProcessInstanceQuery.class);
    when(activitiRuntimeService.createProcessInstanceQuery()).thenReturn(processInstanceQuery);
    when(processInstanceQuery.processInstanceId(processInstanceId)).thenReturn(processInstanceQuery);
    when(processInstanceQuery.includeProcessVariables()).thenReturn(processInstanceQuery);
    ProcessInstance expectedProcessInstance = mock(ProcessInstance.class);
    when(processInstanceQuery.singleResult()).thenReturn(expectedProcessInstance);
    ProcessInstance actualProcessInstance = activitiService.getProcessInstanceById(processInstanceId);
    assertSame(expectedProcessInstance, actualProcessInstance);
    InOrder inOrder = inOrder(processInstanceQuery);
    inOrder.verify(processInstanceQuery).processInstanceId(processInstanceId);
    inOrder.verify(processInstanceQuery).includeProcessVariables();
    inOrder.verify(processInstanceQuery).singleResult();
    inOrder.verifyNoMoreInteractions();
}
Also used : ProcessInstanceQuery(org.activiti.engine.runtime.ProcessInstanceQuery) HistoricProcessInstanceQuery(org.activiti.engine.history.HistoricProcessInstanceQuery) InOrder(org.mockito.InOrder) HistoricProcessInstance(org.activiti.engine.history.HistoricProcessInstance) ProcessInstance(org.activiti.engine.runtime.ProcessInstance) Test(org.junit.Test)

Aggregations

ProcessInstanceQuery (org.activiti.engine.runtime.ProcessInstanceQuery)32 ProcessInstance (org.activiti.engine.runtime.ProcessInstance)19 Deployment (org.activiti.engine.test.Deployment)13 ArrayList (java.util.ArrayList)12 HashMap (java.util.HashMap)12 ActivitiException (org.activiti.engine.ActivitiException)5 RuntimeService (org.activiti.engine.RuntimeService)5 HistoricProcessInstanceQuery (org.activiti.engine.history.HistoricProcessInstanceQuery)5 ProcessDefinition (org.activiti.engine.repository.ProcessDefinition)5 HashSet (java.util.HashSet)4 ProcessEngine (org.activiti.engine.ProcessEngine)4 HistoricProcessInstance (org.activiti.engine.history.HistoricProcessInstance)4 Date (java.util.Date)3 BPMNInstance (org.wso2.carbon.bpmn.core.mgt.model.BPMNInstance)3 Map (java.util.Map)2 Execution (org.activiti.engine.runtime.Execution)2 JobQuery (org.activiti.engine.runtime.JobQuery)2 ResourcesWrapper (eu.bcvsolutions.idm.core.api.rest.domain.ResourcesWrapper)1 WorkflowProcessInstanceDto (eu.bcvsolutions.idm.core.workflow.model.dto.WorkflowProcessInstanceDto)1 SimpleDateFormat (java.text.SimpleDateFormat)1