Search in sources :

Example 1 with WorkItemNodeInstance

use of org.jbpm.workflow.instance.node.WorkItemNodeInstance in project jbpm-work-items by kiegroup.

the class JavaHandlerWorkItemHandler method executeWorkItem.

@SuppressWarnings("unchecked")
public void executeWorkItem(WorkItem workItem, WorkItemManager manager) {
    String className = (String) workItem.getParameter("Class");
    try {
        Class<JavaHandler> c = (Class<JavaHandler>) Class.forName(className);
        JavaHandler handler = c.newInstance();
        ProcessContext kcontext = new ProcessContext(ksession);
        WorkflowProcessInstance processInstance = (WorkflowProcessInstance) ksession.getProcessInstance(workItem.getProcessInstanceId());
        kcontext.setProcessInstance(processInstance);
        WorkItemNodeInstance nodeInstance = findNodeInstance(workItem.getId(), processInstance);
        kcontext.setNodeInstance(nodeInstance);
        Map<String, Object> results = handler.execute(kcontext);
        manager.completeWorkItem(workItem.getId(), results);
        return;
    } catch (ClassNotFoundException cnfe) {
        handleException(cnfe);
    } catch (InstantiationException ie) {
        handleException(ie);
    } catch (IllegalAccessException iae) {
        handleException(iae);
    }
}
Also used : WorkItemNodeInstance(org.jbpm.workflow.instance.node.WorkItemNodeInstance) ProcessContext(org.drools.core.spi.ProcessContext) WorkflowProcessInstance(org.kie.api.runtime.process.WorkflowProcessInstance)

Example 2 with WorkItemNodeInstance

use of org.jbpm.workflow.instance.node.WorkItemNodeInstance in project jbpm by kiegroup.

the class AbstractWorkItemHandler method findWorkItemNodeInstance.

private WorkItemNodeInstance findWorkItemNodeInstance(long workItemId, Collection<NodeInstance> nodeInstances) {
    for (NodeInstance nodeInstance : nodeInstances) {
        if (nodeInstance instanceof WorkItemNodeInstance) {
            WorkItemNodeInstance workItemNodeInstance = (WorkItemNodeInstance) nodeInstance;
            WorkItem workItem = workItemNodeInstance.getWorkItem();
            if (workItem != null && workItemId == workItem.getId()) {
                return workItemNodeInstance;
            }
        }
        if (nodeInstance instanceof NodeInstanceContainer) {
            WorkItemNodeInstance result = findWorkItemNodeInstance(workItemId, ((NodeInstanceContainer) nodeInstance).getNodeInstances());
            if (result != null) {
                return result;
            }
        }
    }
    return null;
}
Also used : NodeInstanceContainer(org.kie.api.runtime.process.NodeInstanceContainer) WorkItemNodeInstance(org.jbpm.workflow.instance.node.WorkItemNodeInstance) NodeInstance(org.kie.api.runtime.process.NodeInstance) WorkItemNodeInstance(org.jbpm.workflow.instance.node.WorkItemNodeInstance) WorkItem(org.kie.api.runtime.process.WorkItem)

Example 3 with WorkItemNodeInstance

use of org.jbpm.workflow.instance.node.WorkItemNodeInstance in project jbpm by kiegroup.

the class ActivityTest method testUserTaskVerifyParameters.

@Test
public void testUserTaskVerifyParameters() throws Exception {
    KieBase kbase = createKnowledgeBase("BPMN2-UserTask.bpmn2");
    KieSession ksession = createKnowledgeSession(kbase);
    ksession.getEnvironment().set("deploymentId", "test-deployment-id");
    TestWorkItemHandler workItemHandler = new TestWorkItemHandler();
    ksession.getWorkItemManager().registerWorkItemHandler("Human Task", workItemHandler);
    ProcessInstance processInstance = ksession.startProcess("UserTask");
    assertTrue(processInstance.getState() == ProcessInstance.STATE_ACTIVE);
    ksession = restoreSession(ksession, true);
    final WorkItem workItem = workItemHandler.getWorkItem();
    assertNotNull(workItem);
    assertEquals("john", workItem.getParameter("ActorId"));
    final long pId = processInstance.getId();
    ksession.execute(new ExecutableCommand<Void>() {

        @Override
        public Void execute(Context context) {
            KieSession ksession = ((RegistryContext) context).lookup(KieSession.class);
            ProcessInstance processInstance = ksession.getProcessInstance(pId);
            assertNotNull(processInstance);
            NodeInstance nodeInstance = ((WorkflowProcessInstance) processInstance).getNodeInstance(((org.drools.core.process.instance.WorkItem) workItem).getNodeInstanceId());
            assertNotNull(nodeInstance);
            assertTrue(nodeInstance instanceof WorkItemNodeInstance);
            String deploymentId = ((WorkItemNodeInstance) nodeInstance).getWorkItem().getDeploymentId();
            long nodeInstanceId = ((WorkItemNodeInstance) nodeInstance).getWorkItem().getNodeInstanceId();
            long nodeId = ((WorkItemNodeInstance) nodeInstance).getWorkItem().getNodeId();
            assertEquals(((org.drools.core.process.instance.WorkItem) workItem).getDeploymentId(), deploymentId);
            assertEquals(((org.drools.core.process.instance.WorkItem) workItem).getNodeId(), nodeId);
            assertEquals(((org.drools.core.process.instance.WorkItem) workItem).getNodeInstanceId(), nodeInstanceId);
            return null;
        }
    });
    ksession.getWorkItemManager().completeWorkItem(workItem.getId(), null);
    assertProcessInstanceFinished(processInstance, ksession);
    ksession.dispose();
}
Also used : TestWorkItemHandler(org.jbpm.bpmn2.objects.TestWorkItemHandler) InitialContext(javax.naming.InitialContext) RegistryContext(org.drools.core.command.impl.RegistryContext) Context(org.kie.api.runtime.Context) WorkItemNodeInstance(org.jbpm.workflow.instance.node.WorkItemNodeInstance) WorkItem(org.kie.api.runtime.process.WorkItem) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) ProcessInstance(org.kie.api.runtime.process.ProcessInstance) WorkflowProcessInstance(org.kie.api.runtime.process.WorkflowProcessInstance) DynamicNodeInstance(org.jbpm.workflow.instance.node.DynamicNodeInstance) NodeInstance(org.kie.api.runtime.process.NodeInstance) WorkItemNodeInstance(org.jbpm.workflow.instance.node.WorkItemNodeInstance) Test(org.junit.Test)

Example 4 with WorkItemNodeInstance

use of org.jbpm.workflow.instance.node.WorkItemNodeInstance in project jbpm by kiegroup.

the class ProcessInstanceMigrationTest method testProcessInstanceMigrationWithGatewaysAndSameUniqueId.

@Test
public void testProcessInstanceMigrationWithGatewaysAndSameUniqueId() throws Exception {
    PersistenceWorkItemHandler handler = new PersistenceWorkItemHandler();
    ksession.getWorkItemManager().registerWorkItemHandler("Human Task", handler);
    Map<String, Object> params = new HashMap<String, Object>();
    params.put("x", 5);
    WorkflowProcessInstance processInstance = (WorkflowProcessInstance) ksession.startProcess("com.sample.BPMN2ProcessVersion3", params);
    long pid = processInstance.getId();
    assertEquals(1, processInstance.getNodeInstances().size());
    Map<String, String> mapping = new HashMap<String, String>();
    mapping.put("UserTask", "StartTask");
    // upgrade to version to of the process
    ExplicitUpgradeCommand c = new ExplicitUpgradeCommand(pid, mapping, "com.sample.BPMN2ProcessVersion4");
    ksession.execute(c);
    assertEquals("com.sample.BPMN2ProcessVersion4", ksession.getProcessInstance(pid).getProcessId());
    assertEquals(1, processInstance.getNodeInstances().size());
    NodeInstance current = processInstance.getNodeInstances().iterator().next();
    handler.completeWorkItem(((WorkItemNodeInstance) current).getWorkItem(), ksession.getWorkItemManager());
    handler.completeWorkItem(handler.getWorkItem("FirstPath"), ksession.getWorkItemManager());
    handler.completeWorkItem(handler.getWorkItem("SecondPath"), ksession.getWorkItemManager());
    List<? extends VariableInstanceLog> vars = engine.getAuditService().findVariableInstances(pid, "x");
    assertNotNull(vars);
    assertEquals(2, vars.size());
    assertEquals("10", vars.get(1).getValue());
    assertDefinitionChanged(pid, "com.sample.BPMN2ProcessVersion4", true);
}
Also used : HashMap(java.util.HashMap) NodeInstance(org.kie.api.runtime.process.NodeInstance) WorkItemNodeInstance(org.jbpm.workflow.instance.node.WorkItemNodeInstance) WorkflowProcessInstance(org.jbpm.workflow.instance.WorkflowProcessInstance) Test(org.junit.Test)

Example 5 with WorkItemNodeInstance

use of org.jbpm.workflow.instance.node.WorkItemNodeInstance in project jbpm by kiegroup.

the class RuntimeDataServiceImplTest method testGetAuditTaskByStatus.

@Test
public void testGetAuditTaskByStatus() throws Exception {
    processInstanceId = processService.startProcess(deploymentUnit.getIdentifier(), "org.jbpm.writedocument");
    assertNotNull(processInstanceId);
    ProcessInstance instance = processService.getProcessInstance(processInstanceId);
    assertNotNull(instance);
    Collection<NodeInstance> activeNodes = ((WorkflowProcessInstanceImpl) instance).getNodeInstances();
    assertNotNull(activeNodes);
    assertEquals(1, activeNodes.size());
    NodeInstance node = activeNodes.iterator().next();
    assertNotNull(node);
    assertTrue(node instanceof WorkItemNodeInstance);
    Long workItemId = ((WorkItemNodeInstance) node).getWorkItemId();
    assertNotNull(workItemId);
    List<String> statuses = new ArrayList();
    statuses.add(Status.Reserved.toString());
    Map<String, Object> params = new HashMap<String, Object>();
    params.put("statuses", statuses);
    QueryFilter queryFilter = new QueryFilter();
    queryFilter.setParams(params);
    List<AuditTask> auditTasks = runtimeDataService.getAllAuditTaskByStatus("salaboy", queryFilter);
    assertNotNull(auditTasks);
    assertEquals(1, auditTasks.size());
    assertEquals("Write a Document", auditTasks.get(0).getName());
    processService.abortProcessInstance(processInstanceId);
    processInstanceId = null;
}
Also used : HashMap(java.util.HashMap) WorkflowProcessInstanceImpl(org.jbpm.workflow.instance.impl.WorkflowProcessInstanceImpl) ArrayList(java.util.ArrayList) WorkItemNodeInstance(org.jbpm.workflow.instance.node.WorkItemNodeInstance) AuditTask(org.kie.internal.task.api.AuditTask) QueryFilter(org.kie.internal.query.QueryFilter) ProcessInstance(org.kie.api.runtime.process.ProcessInstance) NodeInstance(org.kie.api.runtime.process.NodeInstance) WorkItemNodeInstance(org.jbpm.workflow.instance.node.WorkItemNodeInstance) AbstractKieServicesBaseTest(org.jbpm.kie.test.util.AbstractKieServicesBaseTest) Test(org.junit.Test)

Aggregations

WorkItemNodeInstance (org.jbpm.workflow.instance.node.WorkItemNodeInstance)18 NodeInstance (org.kie.api.runtime.process.NodeInstance)13 ProcessInstance (org.kie.api.runtime.process.ProcessInstance)12 Test (org.junit.Test)11 WorkflowProcessInstanceImpl (org.jbpm.workflow.instance.impl.WorkflowProcessInstanceImpl)9 AbstractKieServicesBaseTest (org.jbpm.kie.test.util.AbstractKieServicesBaseTest)5 SubProcessNodeInstance (org.jbpm.workflow.instance.node.SubProcessNodeInstance)5 QueryFilter (org.kie.internal.query.QueryFilter)5 HashMap (java.util.HashMap)4 UserTaskInstanceDesc (org.jbpm.services.api.model.UserTaskInstanceDesc)4 NodeInstanceImpl (org.jbpm.workflow.instance.impl.NodeInstanceImpl)4 WorkflowProcessInstance (org.kie.api.runtime.process.WorkflowProcessInstance)4 ArrayList (java.util.ArrayList)3 NodeInstanceLog (org.jbpm.process.audit.NodeInstanceLog)3 DynamicNodeInstance (org.jbpm.workflow.instance.node.DynamicNodeInstance)3 KieSession (org.kie.api.runtime.KieSession)3 DefaultFactHandle (org.drools.core.common.DefaultFactHandle)2 NodeInstanceContent (org.jbpm.marshalling.impl.JBPMMessages.ProcessInstance.NodeInstanceContent)2 TextMapEntry (org.jbpm.marshalling.impl.JBPMMessages.ProcessInstance.NodeInstanceContent.RuleSetNode.TextMapEntry)2 ProcessInstanceImpl (org.jbpm.process.instance.impl.ProcessInstanceImpl)2