Search in sources :

Example 16 with HistoricProcessInstanceQuery

use of org.activiti.engine.history.HistoricProcessInstanceQuery in project alfresco-repository by Alfresco.

the class ActivitiWorkflowEngine method createQuery.

@SuppressWarnings("unchecked")
private HistoricProcessInstanceQuery createQuery(WorkflowInstanceQuery workflowInstanceQuery, boolean isActive) {
    // MNT-9074 My Tasks fails to render if tasks quantity is excessive
    String processDefId = workflowInstanceQuery.getWorkflowDefinitionId() == null ? null : createLocalId(workflowInstanceQuery.getWorkflowDefinitionId());
    HistoricProcessInstanceQuery query;
    if (isActive) {
        // Don't use ProcessInstanceQuery here because in any case they will be converted to WorkflowInstance thro HistoricProcessInstance.
        query = historyService.createHistoricProcessInstanceQuery().unfinished();
    } else {
        query = historyService.createHistoricProcessInstanceQuery().finished();
    }
    if (activitiUtil.isMultiTenantWorkflowDeploymentEnabled() && tenantService.isEnabled() && !TenantUtil.isCurrentDomainDefault() && !StringUtils.isEmpty(TenantUtil.getCurrentDomain())) {
        // Process definition keys are prefixed with the tenant ID, in case MT is enabled and
        // deployments are done in tenant-context
        query.processDefinitionKey("@" + TenantUtil.getCurrentDomain() + "@%");
    }
    if (processDefId != null) {
        query = query.processDefinitionId(processDefId);
    }
    if (workflowInstanceQuery.getExcludedDefinitions() != null) {
        List<String> exDefIds = new ArrayList<String>();
        for (String excludedDef : workflowInstanceQuery.getExcludedDefinitions()) {
            String exDef = createLocalId(excludedDef);
            exDef = exDef.replaceAll("\\*", "%");
            exDefIds.add(exDef);
        }
        if (exDefIds.size() > 0) {
            query.processDefinitionKeyNotIn(exDefIds);
        }
    }
    // Check start range
    if (workflowInstanceQuery.getStartBefore() != null) {
        query.startedBefore(workflowInstanceQuery.getStartBefore());
    }
    if (workflowInstanceQuery.getStartAfter() != null) {
        query.startedAfter(workflowInstanceQuery.getStartAfter());
    }
    // check end range
    if (workflowInstanceQuery.getEndBefore() != null) {
        query.finishedBefore(workflowInstanceQuery.getEndBefore());
    }
    if (workflowInstanceQuery.getEndAfter() != null) {
        query.finishedAfter(workflowInstanceQuery.getEndAfter());
    }
    if (workflowInstanceQuery.getCustomProps() != null) {
        Map<QName, Object> customProps = workflowInstanceQuery.getCustomProps();
        // CLOUD-667: Extract initiator-property and use 'startedBy' instead
        Object initiatorObject = customProps.get(QNAME_INITIATOR);
        if (initiatorObject != null && initiatorObject instanceof NodeRef) {
            // Extract username from person-node
            NodeRef initiator = (NodeRef) initiatorObject;
            if (this.nodeService.exists(initiator)) {
                String initiatorUserName = (String) nodeService.getProperty(initiator, ContentModel.PROP_USERNAME);
                query.startedBy(initiatorUserName);
                // Clone properties map and remove initiator
                customProps = new HashMap<QName, Object>();
                customProps.putAll(workflowInstanceQuery.getCustomProps());
                customProps.remove(QNAME_INITIATOR);
            }
        }
        for (Map.Entry<QName, Object> prop : customProps.entrySet()) {
            String propertyName = factory.mapQNameToName(prop.getKey());
            if (prop.getValue() == null) {
                query.variableValueEquals(propertyName, null);
            } else {
                PropertyDefinition propertyDefinition = dictionaryService.getProperty(prop.getKey());
                if (propertyDefinition == null) {
                    Object converted = propertyConverter.convertPropertyToValue(prop.getValue());
                    query.variableValueEquals(propertyName, converted);
                } else {
                    String propertyType = propertyDefinition.getDataType().getJavaClassName();
                    if (propertyType.equals("java.util.Date")) {
                        Map<DatePosition, Date> dateProps = (Map<DatePosition, Date>) prop.getValue();
                        for (Map.Entry<DatePosition, Date> dateProp : dateProps.entrySet()) {
                            if (dateProp.getValue() != null) {
                                if (dateProp.getKey() == DatePosition.BEFORE) {
                                    query.variableValueLessThanOrEqual(propertyName, dateProp.getValue());
                                }
                                if (dateProp.getKey() == DatePosition.AFTER) {
                                    query.variableValueGreaterThanOrEqual(propertyName, dateProp.getValue());
                                }
                            }
                        }
                    } else {
                        Object convertedValue = DefaultTypeConverter.INSTANCE.convert(propertyDefinition.getDataType(), prop.getValue());
                        query.variableValueEquals(propertyName, convertedValue);
                    }
                }
            }
        }
    }
    return query;
}
Also used : HistoricProcessInstanceQuery(org.activiti.engine.history.HistoricProcessInstanceQuery) QName(org.alfresco.service.namespace.QName) ArrayList(java.util.ArrayList) PropertyDefinition(org.alfresco.service.cmr.dictionary.PropertyDefinition) Date(java.util.Date) NodeRef(org.alfresco.service.cmr.repository.NodeRef) DatePosition(org.alfresco.service.cmr.workflow.WorkflowInstanceQuery.DatePosition) Map(java.util.Map) HashMap(java.util.HashMap) NamedNodeMap(org.w3c.dom.NamedNodeMap)

Example 17 with HistoricProcessInstanceQuery

use of org.activiti.engine.history.HistoricProcessInstanceQuery in project midpoint by Evolveum.

the class ProcessInstanceManager method getActivitiToMidpoint.

private Map<String, String> getActivitiToMidpoint(Set<String> activeProcessInstances, OperationResult result) {
    Map<String, String> rv = new HashMap<>();
    int processWithoutTaskOidCount = 0;
    HistoricProcessInstanceQuery query = activitiEngine.getHistoryService().createHistoricProcessInstanceQuery().includeProcessVariables().excludeSubprocesses(true);
    List<HistoricProcessInstance> processes = query.list();
    for (HistoricProcessInstance process : processes) {
        String taskOid = (String) process.getProcessVariables().get(CommonProcessVariableNames.VARIABLE_MIDPOINT_TASK_OID);
        rv.put(process.getId(), taskOid);
        if (taskOid == null) {
            processWithoutTaskOidCount++;
        }
        if (process.getEndTime() == null) {
            activeProcessInstances.add(process.getId());
        }
    }
    LOGGER.info("Found {} processes; among these, {} have no task OID. Active processes: {}", rv.size(), processWithoutTaskOidCount, activeProcessInstances.size());
    return rv;
}
Also used : HistoricProcessInstanceQuery(org.activiti.engine.history.HistoricProcessInstanceQuery) HistoricProcessInstance(org.activiti.engine.history.HistoricProcessInstance)

Example 18 with HistoricProcessInstanceQuery

use of org.activiti.engine.history.HistoricProcessInstanceQuery in project herd by FINRAOS.

the class ActivitiServiceTest method testGetHistoricProcessInstancesByStatusAndProcessDefinitionKeysWhenStatusCompleted.

@Test
public void testGetHistoricProcessInstancesByStatusAndProcessDefinitionKeysWhenStatusCompleted() {
    JobStatusEnum jobStatus = JobStatusEnum.COMPLETED;
    Collection<String> processDefinitionKeys = new ArrayList<>();
    DateTime startTime = new DateTime();
    DateTime endTime = new DateTime();
    HistoricProcessInstanceQuery historicProcessInstanceQuery = mock(HistoricProcessInstanceQuery.class);
    when(activitiHistoryService.createHistoricProcessInstanceQuery()).thenReturn(historicProcessInstanceQuery);
    when(historicProcessInstanceQuery.processDefinitionKeyIn(new ArrayList<>(processDefinitionKeys))).thenReturn(historicProcessInstanceQuery);
    when(historicProcessInstanceQuery.unfinished()).thenReturn(historicProcessInstanceQuery);
    when(historicProcessInstanceQuery.startedAfter(startTime.toDate())).thenReturn(historicProcessInstanceQuery);
    when(historicProcessInstanceQuery.finishedBefore(endTime.toDate())).thenReturn(historicProcessInstanceQuery);
    List<HistoricProcessInstance> expectedHistoricProcessInstances = new ArrayList<>();
    when(historicProcessInstanceQuery.list()).thenReturn(expectedHistoricProcessInstances);
    List<HistoricProcessInstance> actualHistoricProcessInstance = activitiService.getHistoricProcessInstancesByStatusAndProcessDefinitionKeys(jobStatus, processDefinitionKeys, startTime, endTime);
    assertSame(expectedHistoricProcessInstances, actualHistoricProcessInstance);
    InOrder inOrder = inOrder(historicProcessInstanceQuery);
    inOrder.verify(historicProcessInstanceQuery).processDefinitionKeyIn(new ArrayList<>(processDefinitionKeys));
    inOrder.verify(historicProcessInstanceQuery).finished();
    inOrder.verify(historicProcessInstanceQuery).startedAfter(startTime.toDate());
    inOrder.verify(historicProcessInstanceQuery).finishedBefore(endTime.toDate());
    inOrder.verify(historicProcessInstanceQuery).list();
    inOrder.verifyNoMoreInteractions();
}
Also used : InOrder(org.mockito.InOrder) HistoricProcessInstanceQuery(org.activiti.engine.history.HistoricProcessInstanceQuery) JobStatusEnum(org.finra.herd.model.api.xml.JobStatusEnum) HistoricProcessInstance(org.activiti.engine.history.HistoricProcessInstance) ArrayList(java.util.ArrayList) DateTime(org.joda.time.DateTime) Test(org.junit.Test)

Example 19 with HistoricProcessInstanceQuery

use of org.activiti.engine.history.HistoricProcessInstanceQuery in project herd by FINRAOS.

the class ActivitiServiceTest method testGetHistoricProcessInstanceByProcessInstanceId.

@Test
public void testGetHistoricProcessInstanceByProcessInstanceId() {
    String processInstanceId = "processInstanceId";
    HistoricProcessInstanceQuery historicProcessInstanceQuery = mock(HistoricProcessInstanceQuery.class);
    when(activitiHistoryService.createHistoricProcessInstanceQuery()).thenReturn(historicProcessInstanceQuery);
    when(historicProcessInstanceQuery.processInstanceId(processInstanceId)).thenReturn(historicProcessInstanceQuery);
    when(historicProcessInstanceQuery.includeProcessVariables()).thenReturn(historicProcessInstanceQuery);
    HistoricProcessInstance expectedHistoricProcessInstance = mock(HistoricProcessInstance.class);
    when(historicProcessInstanceQuery.singleResult()).thenReturn(expectedHistoricProcessInstance);
    HistoricProcessInstance actualHistoricProcessInstance = activitiService.getHistoricProcessInstanceByProcessInstanceId(processInstanceId);
    assertSame(expectedHistoricProcessInstance, actualHistoricProcessInstance);
    InOrder inOrder = inOrder(historicProcessInstanceQuery);
    inOrder.verify(historicProcessInstanceQuery).processInstanceId(processInstanceId);
    inOrder.verify(historicProcessInstanceQuery).includeProcessVariables();
    inOrder.verify(historicProcessInstanceQuery).singleResult();
    inOrder.verifyNoMoreInteractions();
}
Also used : InOrder(org.mockito.InOrder) HistoricProcessInstanceQuery(org.activiti.engine.history.HistoricProcessInstanceQuery) HistoricProcessInstance(org.activiti.engine.history.HistoricProcessInstance) Test(org.junit.Test)

Example 20 with HistoricProcessInstanceQuery

use of org.activiti.engine.history.HistoricProcessInstanceQuery in project herd by FINRAOS.

the class ActivitiServiceTest method testGetHistoricProcessInstancesByStatusAndProcessDefinitionKeysWhenEndTimeNotSpecified.

@Test
public void testGetHistoricProcessInstancesByStatusAndProcessDefinitionKeysWhenEndTimeNotSpecified() {
    JobStatusEnum jobStatus = JobStatusEnum.RUNNING;
    Collection<String> processDefinitionKeys = new ArrayList<>();
    DateTime startTime = new DateTime();
    DateTime endTime = null;
    HistoricProcessInstanceQuery historicProcessInstanceQuery = mock(HistoricProcessInstanceQuery.class);
    when(activitiHistoryService.createHistoricProcessInstanceQuery()).thenReturn(historicProcessInstanceQuery);
    when(historicProcessInstanceQuery.processDefinitionKeyIn(new ArrayList<>(processDefinitionKeys))).thenReturn(historicProcessInstanceQuery);
    when(historicProcessInstanceQuery.unfinished()).thenReturn(historicProcessInstanceQuery);
    when(historicProcessInstanceQuery.startedAfter(startTime.toDate())).thenReturn(historicProcessInstanceQuery);
    List<HistoricProcessInstance> expectedHistoricProcessInstances = new ArrayList<>();
    when(historicProcessInstanceQuery.list()).thenReturn(expectedHistoricProcessInstances);
    List<HistoricProcessInstance> actualHistoricProcessInstance = activitiService.getHistoricProcessInstancesByStatusAndProcessDefinitionKeys(jobStatus, processDefinitionKeys, startTime, endTime);
    assertSame(expectedHistoricProcessInstances, actualHistoricProcessInstance);
    InOrder inOrder = inOrder(historicProcessInstanceQuery);
    inOrder.verify(historicProcessInstanceQuery).processDefinitionKeyIn(new ArrayList<>(processDefinitionKeys));
    inOrder.verify(historicProcessInstanceQuery).unfinished();
    inOrder.verify(historicProcessInstanceQuery).startedAfter(startTime.toDate());
    inOrder.verify(historicProcessInstanceQuery).list();
    inOrder.verifyNoMoreInteractions();
}
Also used : InOrder(org.mockito.InOrder) HistoricProcessInstanceQuery(org.activiti.engine.history.HistoricProcessInstanceQuery) JobStatusEnum(org.finra.herd.model.api.xml.JobStatusEnum) HistoricProcessInstance(org.activiti.engine.history.HistoricProcessInstance) ArrayList(java.util.ArrayList) DateTime(org.joda.time.DateTime) Test(org.junit.Test)

Aggregations

HistoricProcessInstanceQuery (org.activiti.engine.history.HistoricProcessInstanceQuery)38 HistoricProcessInstance (org.activiti.engine.history.HistoricProcessInstance)30 ArrayList (java.util.ArrayList)21 Test (org.junit.Test)8 ProcessInstance (org.activiti.engine.runtime.ProcessInstance)7 InOrder (org.mockito.InOrder)7 HistoryService (org.activiti.engine.HistoryService)6 RuntimeService (org.activiti.engine.RuntimeService)6 Deployment (org.activiti.engine.test.Deployment)6 JobStatusEnum (org.finra.herd.model.api.xml.JobStatusEnum)6 DateTime (org.joda.time.DateTime)6 BPMNInstance (org.wso2.carbon.bpmn.core.mgt.model.BPMNInstance)6 ProcessDefinition (org.activiti.engine.repository.ProcessDefinition)4 Date (java.util.Date)3 HashMap (java.util.HashMap)3 HashSet (java.util.HashSet)3 ProcessEngine (org.activiti.engine.ProcessEngine)3 ProcessInstanceQuery (org.activiti.engine.runtime.ProcessInstanceQuery)3 GET (javax.ws.rs.GET)2 Path (javax.ws.rs.Path)2