Search in sources :

Example 86 with RuntimeService

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

the class BaseProcessInstanceService method getProcessInstanceFromRequest.

protected ProcessInstance getProcessInstanceFromRequest(String processInstanceId) {
    RuntimeService runtimeService = BPMNOSGIService.getRuntimeService();
    ProcessInstance processInstance = runtimeService.createProcessInstanceQuery().processInstanceId(processInstanceId).includeProcessVariables().singleResult();
    if (processInstance == null) {
        throw new ActivitiObjectNotFoundException(" Could not find a process instance with id " + processInstanceId + "'.", ProcessInstance.class);
    }
    return processInstance;
}
Also used : RuntimeService(org.activiti.engine.RuntimeService) ProcessInstance(org.activiti.engine.runtime.ProcessInstance) ActivitiObjectNotFoundException(org.activiti.engine.ActivitiObjectNotFoundException)

Example 87 with RuntimeService

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

the class BaseProcessInstanceService method suspendProcessInstance.

protected ProcessInstanceResponse suspendProcessInstance(ProcessInstance processInstance, RestResponseFactory restResponseFactory, UriInfo uriInfo) {
    if (processInstance.isSuspended()) {
        throw new BPMNConflictException("Process instance with id '" + processInstance.getId() + "' is already suspended.");
    }
    RuntimeService runtimeService = BPMNOSGIService.getRuntimeService();
    runtimeService.suspendProcessInstanceById(processInstance.getId());
    ProcessInstanceResponse response = restResponseFactory.createProcessInstanceResponse(processInstance, uriInfo.getBaseUri().toString());
    // No need to re-fetch the instance, just alter the suspended state of the result-object
    response.setSuspended(true);
    return response;
}
Also used : BPMNConflictException(org.wso2.carbon.bpmn.rest.common.exception.BPMNConflictException) RuntimeService(org.activiti.engine.RuntimeService) ProcessInstanceResponse(org.wso2.carbon.bpmn.rest.model.runtime.ProcessInstanceResponse)

Example 88 with RuntimeService

use of org.activiti.engine.RuntimeService 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 89 with RuntimeService

use of org.activiti.engine.RuntimeService 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 90 with RuntimeService

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

the class BPMNInstanceService method activateProcessInstance.

/**
 * Activate a process instance by passing the instance id
 *
 * @param instanceId
 * @throws BPSFault
 */
public void activateProcessInstance(String instanceId) throws BPSFault {
    Integer tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
    RuntimeService runtimeService = BPMNServerHolder.getInstance().getEngine().getRuntimeService();
    List<ProcessInstance> processInstances = runtimeService.createProcessInstanceQuery().processInstanceTenantId(tenantId.toString()).processInstanceId(instanceId).list();
    if (processInstances.isEmpty()) {
        String msg = "No process instances with the ID: " + instanceId;
        log.error(msg);
        throw new BPSFault(msg);
    }
    runtimeService.activateProcessInstanceById(instanceId);
}
Also used : RuntimeService(org.activiti.engine.RuntimeService) BPSFault(org.wso2.carbon.bpmn.core.BPSFault) HistoricProcessInstance(org.activiti.engine.history.HistoricProcessInstance) ProcessInstance(org.activiti.engine.runtime.ProcessInstance)

Aggregations

RuntimeService (org.activiti.engine.RuntimeService)95 ProcessInstance (org.activiti.engine.runtime.ProcessInstance)35 Test (org.junit.Test)25 ProcessEngine (org.activiti.engine.ProcessEngine)17 ActivitiObjectNotFoundException (org.activiti.engine.ActivitiObjectNotFoundException)15 TaskService (org.activiti.engine.TaskService)15 RestResponseFactory (org.wso2.carbon.bpmn.rest.common.RestResponseFactory)14 HistoricProcessInstance (org.activiti.engine.history.HistoricProcessInstance)13 Deployment (org.activiti.engine.test.Deployment)13 RepositoryService (org.activiti.engine.RepositoryService)12 RestVariable (org.wso2.carbon.bpmn.rest.engine.variable.RestVariable)12 ArrayList (java.util.ArrayList)8 HashMap (java.util.HashMap)8 ActivitiIllegalArgumentException (org.activiti.engine.ActivitiIllegalArgumentException)8 Task (org.activiti.engine.task.Task)8 Path (javax.ws.rs.Path)7 HistoricProcessInstanceQuery (org.activiti.engine.history.HistoricProcessInstanceQuery)7 Execution (org.activiti.engine.runtime.Execution)7 Produces (javax.ws.rs.Produces)6 ProcessDefinition (org.activiti.engine.repository.ProcessDefinition)6