Search in sources :

Example 1 with BPMNVariable

use of org.wso2.carbon.bpmn.core.mgt.model.BPMNVariable in project carbon-business-process by wso2.

the class BPMNInstanceService method formatVariables.

/**
 * Internally used method to format variables
 *
 * @param processVariables
 * @return list of BPMNInstances
 */
private BPMNVariable[] formatVariables(Map<String, Object> processVariables) {
    BPMNVariable[] vars = new BPMNVariable[processVariables.size()];
    int currentVar = 0;
    for (Map.Entry entry : processVariables.entrySet()) {
        Object value = processVariables.get(entry.getKey().toString());
        if (value == null) {
            value = "null";
        } else {
            value = String.valueOf(value);
        }
        vars[currentVar] = new BPMNVariable(entry.getKey().toString(), value.toString());
        currentVar++;
    }
    return vars;
}
Also used : Map(java.util.Map) BPMNVariable(org.wso2.carbon.bpmn.core.mgt.model.BPMNVariable)

Example 2 with BPMNVariable

use of org.wso2.carbon.bpmn.core.mgt.model.BPMNVariable 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)

Aggregations

BPMNVariable (org.wso2.carbon.bpmn.core.mgt.model.BPMNVariable)2 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 ProcessEngine (org.activiti.engine.ProcessEngine)1 RuntimeService (org.activiti.engine.RuntimeService)1 HistoricProcessInstance (org.activiti.engine.history.HistoricProcessInstance)1 HistoricProcessInstanceQuery (org.activiti.engine.history.HistoricProcessInstanceQuery)1 ProcessDefinition (org.activiti.engine.repository.ProcessDefinition)1 ProcessInstanceQuery (org.activiti.engine.runtime.ProcessInstanceQuery)1 BPMNInstance (org.wso2.carbon.bpmn.core.mgt.model.BPMNInstance)1