Search in sources :

Example 81 with RuntimeManager

use of org.kie.api.runtime.manager.RuntimeManager in project jbpm by kiegroup.

the class StartCaseWorkItemHandler method executeWorkItem.

@Override
public void executeWorkItem(WorkItem workItem, WorkItemManager manager) {
    String deploymentId = (String) workItem.getParameter(DEPLOYMENT_ID);
    if (deploymentId == null) {
        deploymentId = ((org.drools.core.process.instance.WorkItem) workItem).getDeploymentId();
    }
    RuntimeManager targetRuntimeManager = RuntimeManagerRegistry.get().getManager(deploymentId);
    if (targetRuntimeManager == null || !(targetRuntimeManager instanceof PerCaseRuntimeManager)) {
        throw new IllegalArgumentException("Requested target deployment does not exist or is not per case strategy");
    }
    String caseDefinitionId = (String) workItem.getParameter(CASE_DEFINITION_ID);
    if (caseDefinitionId == null || caseDefinitionId.trim().isEmpty()) {
        throw new IllegalArgumentException(CASE_DEFINITION_ID + " is a required parameter for StartCaseWorkItemHandler");
    }
    Map<String, Object> caseFileData = new HashMap<>();
    Map<String, List<String>> accessRestrictions = new HashMap<>();
    Map<String, OrganizationalEntity> roleAssignments = new HashMap<>();
    parseParameters(workItem, caseFileData, roleAssignments, accessRestrictions);
    long processInstanceId = ((WorkItemImpl) workItem).getProcessInstanceId();
    long workItemId = workItem.getId();
    logger.debug("Parent process instance id {} and work item instance id {} for new case instance", processInstanceId, workItemId);
    CaseService caseService = (CaseService) ServiceRegistry.get().service(ServiceRegistry.CASE_SERVICE);
    CaseFileInstance subCaseFile = caseService.newCaseFileInstanceWithRestrictions(deploymentId, caseDefinitionId, caseFileData, roleAssignments, accessRestrictions);
    ((CaseFileInstanceImpl) subCaseFile).setParentInstanceId(processInstanceId);
    ((CaseFileInstanceImpl) subCaseFile).setParentWorkItemId(workItemId);
    String caseId = caseService.startCase(deploymentId, caseDefinitionId, subCaseFile);
    logger.debug("Case with id {} has been successfully started");
    boolean independent = Boolean.parseBoolean((String) workItem.getParameter(INDEPENDENT));
    if (independent) {
        Map<String, Object> results = new HashMap<>();
        results.put(CASE_ID, caseId);
        try {
            CaseFileInstance snapshot = caseService.getCaseFileInstance(caseId);
            results.putAll(snapshot.getData());
        } catch (CaseNotFoundException e) {
            // case is already completed
            logger.debug("Case is already completed, not possible to fetch case file data any more");
        }
        logger.debug("Completing directly (without waiting for case instance {} completion) work item with id {}", caseId, workItem.getId());
        ((CaseFileInstanceImpl) subCaseFile).setParentInstanceId(null);
        ((CaseFileInstanceImpl) subCaseFile).setParentWorkItemId(null);
        manager.completeWorkItem(workItem.getId(), results);
    } else {
        // save case id so the abort work item can abort/destroy the case instance
        ((WorkItemImpl) workItem).setParameter(CASE_ID, caseId);
        logger.debug("Waiting for case instance {} completion before completing work item with id {}", caseId, workItem.getId());
    }
}
Also used : PerCaseRuntimeManager(org.jbpm.runtime.manager.impl.PerCaseRuntimeManager) OrganizationalEntity(org.kie.api.task.model.OrganizationalEntity) HashMap(java.util.HashMap) PerCaseRuntimeManager(org.jbpm.runtime.manager.impl.PerCaseRuntimeManager) RuntimeManager(org.kie.api.runtime.manager.RuntimeManager) CaseFileInstanceImpl(org.jbpm.casemgmt.impl.model.instance.CaseFileInstanceImpl) CaseNotFoundException(org.jbpm.casemgmt.api.CaseNotFoundException) CaseFileInstance(org.jbpm.casemgmt.api.model.instance.CaseFileInstance) WorkItemImpl(org.drools.core.process.instance.impl.WorkItemImpl) ArrayList(java.util.ArrayList) List(java.util.List) CaseService(org.jbpm.casemgmt.api.CaseService)

Example 82 with RuntimeManager

use of org.kie.api.runtime.manager.RuntimeManager in project jbpm by kiegroup.

the class WorkflowProcessInstanceImpl method setState.

public void setState(final int state, String outcome) {
    super.setState(state, outcome);
    // TODO move most of this to ProcessInstanceImpl
    if (state == ProcessInstance.STATE_COMPLETED || state == ProcessInstance.STATE_ABORTED) {
        if (this.slaCompliance == SLA_PENDING) {
            if (System.currentTimeMillis() > slaDueDate.getTime()) {
                // completion of the process instance is after expected SLA due date, mark it accordingly
                this.slaCompliance = SLA_VIOLATED;
            } else {
                this.slaCompliance = state == ProcessInstance.STATE_COMPLETED ? SLA_MET : SLA_ABORTED;
            }
        }
        InternalKnowledgeRuntime kruntime = getKnowledgeRuntime();
        InternalProcessRuntime processRuntime = (InternalProcessRuntime) kruntime.getProcessRuntime();
        processRuntime.getProcessEventSupport().fireBeforeProcessCompleted(this, kruntime);
        // deactivate all node instances of this process instance
        while (!nodeInstances.isEmpty()) {
            NodeInstance nodeInstance = nodeInstances.get(0);
            ((org.jbpm.workflow.instance.NodeInstance) nodeInstance).cancel();
        }
        if (this.slaTimerId > -1) {
            processRuntime.getTimerManager().cancelTimer(this.slaTimerId);
            logger.debug("SLA Timer {} has been canceled", this.slaTimerId);
        }
        removeEventListeners();
        processRuntime.getProcessInstanceManager().removeProcessInstance(this);
        processRuntime.getProcessEventSupport().fireAfterProcessCompleted(this, kruntime);
        if (isSignalCompletion()) {
            RuntimeManager manager = (RuntimeManager) kruntime.getEnvironment().get(EnvironmentName.RUNTIME_MANAGER);
            if (getParentProcessInstanceId() > 0 && manager != null) {
                try {
                    org.kie.api.runtime.manager.Context<?> context = ProcessInstanceIdContext.get(getParentProcessInstanceId());
                    String caseId = (String) kruntime.getEnvironment().get(EnvironmentName.CASE_ID);
                    if (caseId != null) {
                        context = CaseContext.get(caseId);
                    }
                    RuntimeEngine runtime = manager.getRuntimeEngine(context);
                    KieRuntime managedkruntime = (KieRuntime) runtime.getKieSession();
                    managedkruntime.signalEvent("processInstanceCompleted:" + getId(), this);
                } catch (SessionNotFoundException e) {
                // in case no session is found for parent process let's skip signal for process instance completion
                }
            } else {
                processRuntime.getSignalManager().signalEvent("processInstanceCompleted:" + getId(), this);
            }
        }
    }
}
Also used : RuntimeEngine(org.kie.api.runtime.manager.RuntimeEngine) KieRuntime(org.kie.api.runtime.KieRuntime) RuntimeManager(org.kie.api.runtime.manager.RuntimeManager) InternalRuntimeManager(org.kie.internal.runtime.manager.InternalRuntimeManager) InternalKnowledgeRuntime(org.drools.core.common.InternalKnowledgeRuntime) InternalProcessRuntime(org.jbpm.process.instance.InternalProcessRuntime) EventSubProcessNodeInstance(org.jbpm.workflow.instance.node.EventSubProcessNodeInstance) DynamicNodeInstance(org.jbpm.workflow.instance.node.DynamicNodeInstance) EndNodeInstance(org.jbpm.workflow.instance.node.EndNodeInstance) NodeInstance(org.jbpm.workflow.instance.NodeInstance) EventNodeInstance(org.jbpm.workflow.instance.node.EventNodeInstance) CompositeNodeInstance(org.jbpm.workflow.instance.node.CompositeNodeInstance) SessionNotFoundException(org.kie.internal.runtime.manager.SessionNotFoundException)

Example 83 with RuntimeManager

use of org.kie.api.runtime.manager.RuntimeManager in project jbpm by kiegroup.

the class EvaluationExample method main.

public static final void main(String[] args) {
    try {
        RuntimeManager manager = getRuntimeManager("evaluation/Evaluation.bpmn");
        RuntimeEngine runtime = manager.getRuntimeEngine(null);
        KieSession ksession = runtime.getKieSession();
        // start a new process instance
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("employee", "krisv");
        params.put("reason", "Yearly performance evaluation");
        ksession.startProcess("com.sample.evaluation", params);
        // complete Self Evaluation
        TaskService taskService = runtime.getTaskService();
        List<TaskSummary> tasks = taskService.getTasksAssignedAsPotentialOwner("krisv", "en-UK");
        TaskSummary task = tasks.get(0);
        System.out.println("'krisv' completing task " + task.getName() + ": " + task.getDescription());
        taskService.start(task.getId(), "krisv");
        Map<String, Object> results = new HashMap<String, Object>();
        results.put("performance", "exceeding");
        taskService.complete(task.getId(), "krisv", results);
        // john from HR
        tasks = taskService.getTasksAssignedAsPotentialOwner("john", "en-UK");
        task = tasks.get(0);
        System.out.println("'john' completing task " + task.getName() + ": " + task.getDescription());
        taskService.start(task.getId(), "john");
        results = new HashMap<String, Object>();
        results.put("performance", "acceptable");
        taskService.complete(task.getId(), "john", results);
        // mary from PM
        tasks = taskService.getTasksAssignedAsPotentialOwner("mary", "en-UK");
        task = tasks.get(0);
        System.out.println("'mary' completing task " + task.getName() + ": " + task.getDescription());
        taskService.start(task.getId(), "mary");
        results = new HashMap<String, Object>();
        results.put("performance", "outstanding");
        taskService.complete(task.getId(), "mary", results);
        System.out.println("Process instance completed");
        manager.disposeRuntimeEngine(runtime);
    } catch (Throwable t) {
        t.printStackTrace();
    }
    System.exit(0);
}
Also used : RuntimeEngine(org.kie.api.runtime.manager.RuntimeEngine) HashMap(java.util.HashMap) TaskService(org.kie.api.task.TaskService) RuntimeManager(org.kie.api.runtime.manager.RuntimeManager) TaskSummary(org.kie.api.task.model.TaskSummary) KieSession(org.kie.api.runtime.KieSession)

Example 84 with RuntimeManager

use of org.kie.api.runtime.manager.RuntimeManager in project jbpm by kiegroup.

the class MultipleInstanceExample method main.

public static final void main(String[] args) {
    try {
        RuntimeManager manager = getRuntimeManager("multipleinstance/multipleinstance.bpmn");
        RuntimeEngine runtime = manager.getRuntimeEngine(null);
        KieSession ksession = runtime.getKieSession();
        // start a new process instance
        Map<String, Object> params = new HashMap<String, Object>();
        List<String> list = new ArrayList<String>();
        list.add("krisv");
        list.add("john doe");
        list.add("superman");
        params.put("list", list);
        ksession.startProcess("com.sample.multipleinstance", params);
        TaskService taskService = runtime.getTaskService();
        List<TaskSummary> tasks = taskService.getTasksAssignedAsPotentialOwner("sales-rep", "en-UK");
        for (TaskSummary task : tasks) {
            System.out.println("Sales-rep executing task " + task.getName() + "(" + task.getId() + ": " + task.getDescription() + ")");
            taskService.start(task.getId(), "sales-rep");
            taskService.complete(task.getId(), "sales-rep", null);
        }
        manager.disposeRuntimeEngine(runtime);
    } catch (Throwable t) {
        t.printStackTrace();
    }
    System.exit(0);
}
Also used : RuntimeEngine(org.kie.api.runtime.manager.RuntimeEngine) HashMap(java.util.HashMap) TaskService(org.kie.api.task.TaskService) RuntimeManager(org.kie.api.runtime.manager.RuntimeManager) ArrayList(java.util.ArrayList) TaskSummary(org.kie.api.task.model.TaskSummary) KieSession(org.kie.api.runtime.KieSession)

Example 85 with RuntimeManager

use of org.kie.api.runtime.manager.RuntimeManager in project jbpm by kiegroup.

the class AbstractRuntimeEJBServicesTest method cleanup.

@After
@Override
public void cleanup() {
    List<Long> pids = archive.getPids();
    List<Long> all = (List<Long>) ((ArrayList<Long>) pids).clone();
    for (Long pid : all) {
        ProcessInstanceDesc pi = runtimeDataService.getProcessInstanceById(pid);
        if (pi == null || pi.getState() != ProcessInstance.STATE_ACTIVE) {
            pids.remove(pid);
        }
    }
    if (!pids.isEmpty()) {
        processService.abortProcessInstances(pids);
    }
    pids.clear();
    cleanupSingletonSessionId();
    List<DeploymentUnit> units = archive.getUnits();
    if (units != null && !units.isEmpty()) {
        for (DeploymentUnit unit : units) {
            // clear audit logs
            RuntimeManager manager = deploymentService.getRuntimeManager(unit.getIdentifier());
            RuntimeEngine engine = manager.getRuntimeEngine(ProcessInstanceIdContext.get());
            engine.getAuditService().clear();
            deploymentService.undeploy(unit);
        }
        units.clear();
    }
    kieJar = null;
}
Also used : RuntimeEngine(org.kie.api.runtime.manager.RuntimeEngine) RuntimeManager(org.kie.api.runtime.manager.RuntimeManager) ArrayList(java.util.ArrayList) List(java.util.List) ProcessInstanceDesc(org.jbpm.services.api.model.ProcessInstanceDesc) DeploymentUnit(org.jbpm.services.api.model.DeploymentUnit) After(org.junit.After)

Aggregations

RuntimeManager (org.kie.api.runtime.manager.RuntimeManager)150 RuntimeEngine (org.kie.api.runtime.manager.RuntimeEngine)116 InternalRuntimeManager (org.kie.internal.runtime.manager.InternalRuntimeManager)79 KieSession (org.kie.api.runtime.KieSession)55 TaskService (org.kie.api.task.TaskService)53 Test (org.junit.Test)51 HashMap (java.util.HashMap)49 ProcessInstance (org.kie.api.runtime.process.ProcessInstance)46 InternalTaskService (org.kie.internal.task.api.InternalTaskService)44 DeployedUnit (org.jbpm.services.api.model.DeployedUnit)38 UserTaskService (org.jbpm.services.api.UserTaskService)36 UserTaskInstanceDesc (org.jbpm.services.api.model.UserTaskInstanceDesc)35 AbstractKieServicesBaseTest (org.jbpm.kie.test.util.AbstractKieServicesBaseTest)23 TaskSummary (org.kie.api.task.model.TaskSummary)21 ArrayList (java.util.ArrayList)19 KModuleDeploymentUnit (org.jbpm.kie.services.impl.KModuleDeploymentUnit)19 DeploymentNotFoundException (org.jbpm.services.api.DeploymentNotFoundException)18 TaskNotFoundException (org.jbpm.services.api.TaskNotFoundException)15 DeploymentUnit (org.jbpm.services.api.model.DeploymentUnit)15 PermissionDeniedException (org.jbpm.services.task.exception.PermissionDeniedException)15