Search in sources :

Example 26 with HistoricProcessInstanceQuery

use of org.activiti.engine.history.HistoricProcessInstanceQuery in project carbon-business-process by wso2.

the class BPMNInstanceService method getTenantBPMNInstances.

/**
 * Internally used method to get all tenant BPMN instances from a passed instance list
 *
 * @param instances
 * @return list of BPMNInstances
 */
private BPMNInstance[] getTenantBPMNInstances(List<ProcessInstance> instances) {
    BPMNInstance bpmnInstance;
    Integer tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
    List<BPMNInstance> bpmnInstances = new ArrayList<BPMNInstance>();
    RuntimeService runtimeService = BPMNServerHolder.getInstance().getEngine().getRuntimeService();
    HistoricProcessInstanceQuery query = BPMNServerHolder.getInstance().getEngine().getHistoryService().createHistoricProcessInstanceQuery().processInstanceTenantId(tenantId.toString());
    for (ProcessInstance instance : instances) {
        bpmnInstance = new BPMNInstance();
        bpmnInstance.setInstanceId(instance.getId());
        bpmnInstance.setProcessId(instance.getProcessDefinitionId());
        bpmnInstance.setSuspended(instance.isSuspended());
        bpmnInstance.setStartTime(query.processInstanceId(instance.getId()).singleResult().getStartTime());
        bpmnInstance.setVariables(formatVariables(runtimeService.getVariables(instance.getId())));
        bpmnInstances.add(bpmnInstance);
    }
    return bpmnInstances.toArray(new BPMNInstance[bpmnInstances.size()]);
}
Also used : BPMNInstance(org.wso2.carbon.bpmn.core.mgt.model.BPMNInstance) HistoricProcessInstanceQuery(org.activiti.engine.history.HistoricProcessInstanceQuery) RuntimeService(org.activiti.engine.RuntimeService) ArrayList(java.util.ArrayList) HistoricProcessInstance(org.activiti.engine.history.HistoricProcessInstance) ProcessInstance(org.activiti.engine.runtime.ProcessInstance)

Example 27 with HistoricProcessInstanceQuery

use of org.activiti.engine.history.HistoricProcessInstanceQuery in project carbon-business-process by wso2.

the class BPMNInstanceService method getPaginatedInstanceByFilter.

/**
 * Returns Paginated Instances by passing filter parameters
 *
 * @param finished
 * @param instanceId
 * @param startAfter
 * @param startBefore
 * @param processId
 * @param isActive
 * @param variables
 * @param start
 * @param size
 * @return list of BPMNInstances for given filter parameters
 */
public BPMNInstance[] getPaginatedInstanceByFilter(boolean finished, String instanceId, Date startAfter, Date startBefore, String processId, boolean isActive, String variables, int start, int size) {
    List<BPMNInstance> bpmnInstanceList = new ArrayList<>();
    Integer tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
    ProcessEngine engine = BPMNServerHolder.getInstance().getEngine();
    RuntimeService runtimeService = engine.getRuntimeService();
    ProcessInstanceQuery query = runtimeService.createProcessInstanceQuery().processInstanceTenantId(tenantId.toString());
    HistoricProcessInstanceQuery historicQuery = BPMNServerHolder.getInstance().getEngine().getHistoryService().createHistoricProcessInstanceQuery().processInstanceTenantId(tenantId.toString()).includeProcessVariables();
    query = query.includeProcessVariables();
    if (finished) {
        historicQuery = historicQuery.finished();
        if (instanceId != null && !instanceId.equals("")) {
            return getInstanceById(instanceId, finished);
        }
        if (processId != null && !processId.equals("")) {
            historicQuery = historicQuery.processDefinitionId(processId);
        }
        if (variables != null && !variables.trim().equals("")) {
            String[] variablePairs = variables.split(",");
            BPMNVariable[] bpmnVariables = new BPMNVariable[variablePairs.length];
            for (int i = 0; i < variablePairs.length; i++) {
                String[] pair = variablePairs[i].split(":");
                if (pair.length == 1) {
                    bpmnVariables[i] = new BPMNVariable(pair[0], "");
                } else {
                    bpmnVariables[i] = new BPMNVariable(pair[0], pair[1]);
                }
            }
            if (variablePairs != null && variablePairs.length > 0) {
                for (BPMNVariable variable : bpmnVariables) {
                    if (variable.getName() != null && !variable.getName().equals("")) {
                        historicQuery = historicQuery.variableValueLike(variable.getName(), "%" + variable.getValue().toString() + "%");
                    }
                }
            }
        }
        if (startAfter != null) {
            historicQuery = historicQuery.startedAfter(startAfter);
        }
        if (startBefore != null) {
            historicQuery = historicQuery.startedBefore(startBefore);
        }
        processInstanceCount = (int) historicQuery.count();
        List<HistoricProcessInstance> instances = historicQuery.listPage(start, size);
        for (HistoricProcessInstance instance : instances) {
            BPMNInstance bpmnInstance = new BPMNInstance();
            bpmnInstance.setInstanceId(instance.getId());
            bpmnInstance.setProcessId(instance.getProcessDefinitionId());
            List<ProcessDefinition> processes = BPMNServerHolder.getInstance().getEngine().getRepositoryService().createProcessDefinitionQuery().processDefinitionTenantId(tenantId.toString()).processDefinitionId(instance.getProcessDefinitionId()).list();
            String processName = instance.getProcessDefinitionId();
            if (!processes.isEmpty()) {
                processName = processes.get(0).getName();
            }
            bpmnInstance.setProcessName(processName);
            bpmnInstance.setStartTime(instance.getStartTime());
            bpmnInstance.setEndTime(instance.getEndTime());
            bpmnInstance.setVariables(formatVariables(instance.getProcessVariables()));
            bpmnInstanceList.add(bpmnInstance);
        }
    } else {
        historicQuery = historicQuery.unfinished();
        if (instanceId != null && !instanceId.equals("")) {
            return getInstanceById(instanceId, finished);
        }
        if (processId != null && !processId.equals("")) {
            historicQuery = historicQuery.processDefinitionId(processId);
        }
        if (variables != null && !variables.trim().equals("")) {
            String[] variablePairs = variables.split(",");
            BPMNVariable[] bpmnVariables = new BPMNVariable[variablePairs.length];
            for (int i = 0; i < variablePairs.length; i++) {
                String[] pair = variablePairs[i].split(":");
                if (pair.length == 1) {
                    bpmnVariables[i] = new BPMNVariable(pair[0], "");
                } else {
                    bpmnVariables[i] = new BPMNVariable(pair[0], pair[1]);
                }
            }
            if (variablePairs != null && variablePairs.length > 0) {
                for (BPMNVariable variable : bpmnVariables) {
                    if (variable.getName() != null && !variable.getName().equals("")) {
                        historicQuery = historicQuery.variableValueLike(variable.getName(), "%" + variable.getValue().toString() + "%");
                    }
                }
            }
        }
        if (startAfter != null) {
            historicQuery = historicQuery.startedAfter(startAfter);
        }
        if (startBefore != null) {
            historicQuery = historicQuery.startedBefore(startBefore);
        }
        processInstanceCount = (int) historicQuery.count();
        List<HistoricProcessInstance> instances = historicQuery.listPage(start, size);
        for (HistoricProcessInstance instance : instances) {
            boolean isSuspended = query.processInstanceId(instance.getId()).list().get(0).isSuspended();
            if (isSuspended == !isActive) {
                BPMNInstance bpmnInstance = new BPMNInstance();
                bpmnInstance.setInstanceId(instance.getId());
                bpmnInstance.setProcessId(instance.getProcessDefinitionId());
                List<ProcessDefinition> processes = BPMNServerHolder.getInstance().getEngine().getRepositoryService().createProcessDefinitionQuery().processDefinitionTenantId(tenantId.toString()).processDefinitionId(instance.getProcessDefinitionId()).list();
                String processName = instance.getProcessDefinitionId();
                if (!processes.isEmpty()) {
                    processName = processes.get(0).getName();
                }
                bpmnInstance.setProcessName(processName);
                if (!query.processInstanceId(instance.getId()).list().isEmpty()) {
                    bpmnInstance.setSuspended(isSuspended);
                }
                bpmnInstance.setStartTime(instance.getStartTime());
                bpmnInstance.setEndTime(instance.getEndTime());
                bpmnInstance.setVariables(formatVariables(instance.getProcessVariables()));
                bpmnInstanceList.add(bpmnInstance);
            }
        }
    }
    return bpmnInstanceList.toArray(new BPMNInstance[bpmnInstanceList.size()]);
}
Also used : BPMNInstance(org.wso2.carbon.bpmn.core.mgt.model.BPMNInstance) HistoricProcessInstanceQuery(org.activiti.engine.history.HistoricProcessInstanceQuery) RuntimeService(org.activiti.engine.RuntimeService) HistoricProcessInstance(org.activiti.engine.history.HistoricProcessInstance) ArrayList(java.util.ArrayList) ProcessDefinition(org.activiti.engine.repository.ProcessDefinition) ProcessInstanceQuery(org.activiti.engine.runtime.ProcessInstanceQuery) HistoricProcessInstanceQuery(org.activiti.engine.history.HistoricProcessInstanceQuery) ProcessEngine(org.activiti.engine.ProcessEngine) BPMNVariable(org.wso2.carbon.bpmn.core.mgt.model.BPMNVariable)

Example 28 with HistoricProcessInstanceQuery

use of org.activiti.engine.history.HistoricProcessInstanceQuery in project carbon-business-process by wso2.

the class BPMNInstanceService method getInstanceById.

/**
 * Get instances by instance Id and state
 *
 * @param instanceId
 * @param finished
 * @return list of BPMNInstances
 */
private BPMNInstance[] getInstanceById(String instanceId, boolean finished) {
    List<BPMNInstance> bpmnInstanceList = new ArrayList<>();
    Integer tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
    ProcessEngine engine = BPMNServerHolder.getInstance().getEngine();
    RuntimeService runtimeService = engine.getRuntimeService();
    ProcessInstanceQuery query = runtimeService.createProcessInstanceQuery().processInstanceTenantId(tenantId.toString());
    HistoricProcessInstanceQuery historicQuery = BPMNServerHolder.getInstance().getEngine().getHistoryService().createHistoricProcessInstanceQuery().processInstanceTenantId(tenantId.toString()).includeProcessVariables();
    query = query.includeProcessVariables();
    if (finished) {
        historicQuery.finished();
    } else {
        historicQuery.unfinished();
    }
    historicQuery = historicQuery.processInstanceId(instanceId);
    HistoricProcessInstance instance = historicQuery.singleResult();
    if (instance != null) {
        processInstanceCount = 1;
        BPMNInstance bpmnInstance = new BPMNInstance();
        bpmnInstance.setInstanceId(instance.getId());
        bpmnInstance.setProcessId(instance.getProcessDefinitionId());
        List<ProcessDefinition> processes = BPMNServerHolder.getInstance().getEngine().getRepositoryService().createProcessDefinitionQuery().processDefinitionTenantId(tenantId.toString()).processDefinitionId(instance.getProcessDefinitionId()).list();
        String processName = instance.getProcessDefinitionId();
        if (!processes.isEmpty()) {
            processName = processes.get(0).getName();
        }
        bpmnInstance.setProcessName(processName);
        if (!query.processInstanceId(instance.getId()).list().isEmpty()) {
            bpmnInstance.setSuspended(query.processInstanceId(instance.getId()).list().get(0).isSuspended());
        }
        bpmnInstance.setStartTime(instance.getStartTime());
        bpmnInstance.setEndTime(instance.getEndTime());
        bpmnInstance.setVariables(formatVariables(instance.getProcessVariables()));
        bpmnInstanceList.add(bpmnInstance);
    } else {
        processInstanceCount = 0;
    }
    return bpmnInstanceList.toArray(new BPMNInstance[bpmnInstanceList.size()]);
}
Also used : BPMNInstance(org.wso2.carbon.bpmn.core.mgt.model.BPMNInstance) ProcessInstanceQuery(org.activiti.engine.runtime.ProcessInstanceQuery) HistoricProcessInstanceQuery(org.activiti.engine.history.HistoricProcessInstanceQuery) HistoricProcessInstanceQuery(org.activiti.engine.history.HistoricProcessInstanceQuery) RuntimeService(org.activiti.engine.RuntimeService) HistoricProcessInstance(org.activiti.engine.history.HistoricProcessInstance) ArrayList(java.util.ArrayList) ProcessDefinition(org.activiti.engine.repository.ProcessDefinition) ProcessEngine(org.activiti.engine.ProcessEngine)

Example 29 with HistoricProcessInstanceQuery

use of org.activiti.engine.history.HistoricProcessInstanceQuery in project carbon-business-process by wso2.

the class BPMNInstanceService method getHistoryInstanceCount.

/**
 * Get total history instance count
 *
 * @return count int
 * @throws BPSFault
 */
public int getHistoryInstanceCount() throws BPSFault {
    if (historyInstanceCount == -1) {
        Integer tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
        HistoryService historyService = BPMNServerHolder.getInstance().getEngine().getHistoryService();
        HistoricProcessInstanceQuery query = historyService.createHistoricProcessInstanceQuery().processInstanceTenantId(tenantId.toString()).finished();
        historyInstanceCount = (int) query.count();
    }
    return historyInstanceCount;
}
Also used : HistoricProcessInstanceQuery(org.activiti.engine.history.HistoricProcessInstanceQuery) HistoryService(org.activiti.engine.HistoryService)

Example 30 with HistoricProcessInstanceQuery

use of org.activiti.engine.history.HistoricProcessInstanceQuery in project CzechIdMng by bcvsolutions.

the class DefaultWorkflowHistoricProcessInstanceService method getQuery.

/**
 * Get activiti query for historic processes.
 *
 * @param filter
 * @param pageable
 * @param permission
 * @return
 */
protected HistoricProcessInstanceQuery getQuery(WorkflowFilterDto filter, Pageable pageable, BasePermission... permission) {
    HistoricProcessInstanceQuery query = historyService.createHistoricProcessInstanceQuery();
    if (filter != null) {
        String processDefinitionId = filter.getProcessDefinitionId();
        String processInstanceId = filter.getProcessInstanceId();
        Map<String, Object> equalsVariables = filter.getEqualsVariables();
        if (processInstanceId != null) {
            // Process variables will be included only for get by instance ID
            query.includeProcessVariables();
            query.processInstanceId(processInstanceId);
        }
        if (processDefinitionId != null) {
            query.processDefinitionId(processDefinitionId);
        }
        if (filter.getSuperProcessInstanceId() != null) {
            query.superProcessInstanceId(filter.getSuperProcessInstanceId());
        }
        if (filter.getProcessDefinitionKey() != null) {
            // For case when we have only process id, we will convert him to key
            query.processDefinitionKey(convertProcessIdToKey(filter.getProcessDefinitionKey()));
        }
        if (filter.getName() != null) {
            // with case sensitive
            query.variableValueLike(WorkflowHistoricProcessInstanceService.PROCESS_INSTANCE_NAME, "%" + filter.getName() + "%");
        }
        if (equalsVariables != null) {
            equalsVariables.entrySet().forEach((entry) -> {
                query.variableValueEquals(entry.getKey(), entry.getValue());
            });
        }
    }
    // TODO: refactor and use username/id from filter
    if (!securityService.isAdmin() && Boolean.TRUE == filter.getOnlyInvolved()) {
        // Applicant and Implementer is added to involved user after process
        // (subprocess) started. This modification allow not use OR clause.
        query.involvedUser(securityService.getCurrentId() == null ? UUID.randomUUID().toString() : securityService.getCurrentId().toString());
    }
    String fieldForSort = null;
    boolean ascSort = false;
    boolean descSort = false;
    // 
    if (pageable == null) {
        pageable = PageRequest.of(0, Integer.MAX_VALUE);
    }
    Sort sort = pageable.getSort();
    if (sort != null) {
        for (Order order : sort) {
            if (!StringUtils.isEmpty(order.getProperty())) {
                // TODO: now is implemented only one property sort
                fieldForSort = order.getProperty();
                if (order.getDirection() == Direction.ASC) {
                    ascSort = true;
                } else if (order.getDirection() == Direction.DESC) {
                    descSort = true;
                }
                break;
            }
        }
    }
    if (WorkflowHistoricProcessInstanceService.SORT_BY_START_TIME.equals(fieldForSort)) {
        query.orderByProcessInstanceStartTime();
    } else if (WorkflowHistoricProcessInstanceService.SORT_BY_END_TIME.equals(fieldForSort)) {
        query.orderByProcessInstanceEndTime();
    } else {
        query.orderByProcessDefinitionId();
        // there must be default order
        query.asc();
    }
    if (ascSort) {
        query.asc();
    }
    if (descSort) {
        query.desc();
    }
    return query;
}
Also used : Order(org.springframework.data.domain.Sort.Order) HistoricProcessInstanceQuery(org.activiti.engine.history.HistoricProcessInstanceQuery) Sort(org.springframework.data.domain.Sort)

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