Search in sources :

Example 11 with HumanTaskNode

use of org.jbpm.workflow.core.node.HumanTaskNode in project jbpm by kiegroup.

the class DynamicProcessTest method testDynamicProcess.

@Test
public void testDynamicProcess() throws Exception {
    RuleFlowProcessFactory factory = RuleFlowProcessFactory.createProcess("org.jbpm.HelloWorld");
    factory.name("HelloWorldProcess").version("1.0").packageName("org.jbpm").startNode(1).name("Start").done().humanTaskNode(2).name("Task1").actorId("krisv").taskName("MyTask").done().endNode(3).name("End").done().connection(1, 2).connection(2, 3);
    final RuleFlowProcess process = factory.validate().getProcess();
    Resource resource = ResourceFactory.newByteArrayResource(XmlRuleFlowProcessDumper.INSTANCE.dump(process).getBytes());
    // source path or target path must be set to be added into kbase
    resource.setSourcePath("/tmp/dynamicProcess.bpmn2");
    KieBase kbase = createKnowledgeBaseFromResources(resource);
    StatefulKnowledgeSession ksession = createKnowledgeSession(kbase);
    TestWorkItemHandler testHandler = new TestWorkItemHandler();
    ksession.getWorkItemManager().registerWorkItemHandler("Human Task", testHandler);
    ksession.addEventListener(new ProcessEventListener() {

        public void beforeVariableChanged(ProcessVariableChangedEvent arg0) {
        }

        public void beforeProcessStarted(ProcessStartedEvent arg0) {
            logger.info("{}", arg0);
        }

        public void beforeProcessCompleted(ProcessCompletedEvent arg0) {
            logger.info("{}", arg0);
        }

        public void beforeNodeTriggered(ProcessNodeTriggeredEvent arg0) {
            logger.info("{}", arg0);
        }

        public void beforeNodeLeft(ProcessNodeLeftEvent arg0) {
            logger.info("{}", arg0);
        }

        public void afterVariableChanged(ProcessVariableChangedEvent arg0) {
        }

        public void afterProcessStarted(ProcessStartedEvent arg0) {
        }

        public void afterProcessCompleted(ProcessCompletedEvent arg0) {
        }

        public void afterNodeTriggered(ProcessNodeTriggeredEvent arg0) {
        }

        public void afterNodeLeft(ProcessNodeLeftEvent arg0) {
        }
    });
    final ProcessInstanceImpl processInstance = (ProcessInstanceImpl) ksession.startProcess("org.jbpm.HelloWorld");
    HumanTaskNode node = new HumanTaskNode();
    node.setName("Task2");
    node.setId(4);
    insertNodeInBetween(process, 2, 3, node);
    ((CommandBasedStatefulKnowledgeSession) ksession).getRunner().execute(new ExecutableCommand<Void>() {

        public Void execute(Context context) {
            StatefulKnowledgeSession ksession = (StatefulKnowledgeSession) ((RegistryContext) context).lookup(KieSession.class);
            ((ProcessInstanceImpl) ksession.getProcessInstance(processInstance.getId())).updateProcess(process);
            return null;
        }
    });
    assertProcessInstanceActive(processInstance);
    ksession.getWorkItemManager().completeWorkItem(testHandler.getWorkItem().getId(), null);
    assertProcessInstanceActive(processInstance);
    ksession.getWorkItemManager().completeWorkItem(testHandler.getWorkItem().getId(), null);
    assertProcessInstanceFinished(processInstance, ksession);
    ksession.dispose();
}
Also used : TestWorkItemHandler(org.jbpm.persistence.session.objects.TestWorkItemHandler) ProcessVariableChangedEvent(org.kie.api.event.process.ProcessVariableChangedEvent) RegistryContext(org.drools.core.command.impl.RegistryContext) Context(org.kie.api.runtime.Context) RuleFlowProcess(org.jbpm.ruleflow.core.RuleFlowProcess) ProcessInstanceImpl(org.jbpm.process.instance.impl.ProcessInstanceImpl) CommandBasedStatefulKnowledgeSession(org.drools.core.command.impl.CommandBasedStatefulKnowledgeSession) StatefulKnowledgeSession(org.kie.internal.runtime.StatefulKnowledgeSession) ProcessEventListener(org.kie.api.event.process.ProcessEventListener) Resource(org.kie.api.io.Resource) ProcessStartedEvent(org.kie.api.event.process.ProcessStartedEvent) RegistryContext(org.drools.core.command.impl.RegistryContext) ProcessCompletedEvent(org.kie.api.event.process.ProcessCompletedEvent) RuleFlowProcessFactory(org.jbpm.ruleflow.core.RuleFlowProcessFactory) KieBase(org.kie.api.KieBase) ProcessNodeTriggeredEvent(org.kie.api.event.process.ProcessNodeTriggeredEvent) ProcessNodeLeftEvent(org.kie.api.event.process.ProcessNodeLeftEvent) HumanTaskNode(org.jbpm.workflow.core.node.HumanTaskNode) Test(org.junit.Test)

Example 12 with HumanTaskNode

use of org.jbpm.workflow.core.node.HumanTaskNode in project jbpm by kiegroup.

the class ServicesProcessDataEventListener method onNodeAdded.

@SuppressWarnings("unchecked")
@Override
public void onNodeAdded(Node node) {
    logger.debug("Added node " + node);
    if (node instanceof HumanTaskNode) {
        HumanTaskNode humanTaskNode = (HumanTaskNode) node;
        String name = humanTaskNode.getName();
        UserTaskDefinitionImpl task = (UserTaskDefinitionImpl) processDescriptor.getTasks().get(name);
        if (task == null) {
            task = new UserTaskDefinitionImpl();
            task.setId(humanTaskNode.getUniqueId());
            task.setName(name);
            processDescriptor.getTasks().put(task.getName(), task);
        }
        Map<String, Object> parameters = humanTaskNode.getWork().getParameters();
        Collection<String> currentAssignment = processDescriptor.getTaskAssignments().get(humanTaskNode.getName());
        for (String parameter : parameters.keySet()) {
            if (parameter.equals("GroupId") || parameter.equals("ActorId")) {
                if (currentAssignment == null) {
                    currentAssignment = new ArrayList<String>();
                    processDescriptor.getTaskAssignments().put(humanTaskNode.getName(), currentAssignment);
                }
                currentAssignment.add(humanTaskNode.getWork().getParameter(parameter).toString());
            }
        }
        ((UserTaskDefinitionImpl) processDescriptor.getTasks().get(humanTaskNode.getName())).setAssociatedEntities(currentAssignment);
        Map<String, String> inputParams = new HashMap<String, String>();
        for (Map.Entry<String, String> in : ((Map<String, String>) humanTaskNode.getMetaData("DataInputs")).entrySet()) {
            inputParams.put(in.getKey(), in.getValue());
        }
        Map<String, String> outputParams = new HashMap<String, String>();
        for (Map.Entry<String, String> out : ((Map<String, String>) humanTaskNode.getMetaData("DataOutputs")).entrySet()) {
            outputParams.put(out.getKey(), out.getValue());
        }
        task.setTaskInputMappings(inputParams);
        task.setTaskOutputMappings(outputParams);
        task.setComment(asString(humanTaskNode.getWork().getParameter("Comment")));
        task.setCreatedBy(asString(humanTaskNode.getWork().getParameter("CreatedBy")));
        task.setPriority(asInt(humanTaskNode.getWork().getParameter("Priority")));
        task.setSkippable(asBoolean(humanTaskNode.getWork().getParameter("Skippable")));
        task.setFormName(asString(humanTaskNode.getWork().getParameter("TaskName")));
        processDescriptor.getTaskInputMappings().put(task.getName(), inputParams);
        processDescriptor.getTaskOutputMappings().put(task.getName(), outputParams);
    } else if (node instanceof RuleSetNode) {
        RuleSetNode ruleSetNode = (RuleSetNode) node;
        String ruleFlowGroup = ruleSetNode.getRuleFlowGroup();
        if (ruleFlowGroup != null) {
            processDescriptor.getReferencedRules().add(ruleFlowGroup);
        }
    } else if (node instanceof WorkItemNode) {
        processDescriptor.getServiceTasks().put(node.getName(), ((WorkItemNode) node).getWork().getName());
    } else if (node instanceof SubProcessNode) {
        SubProcessNode subProcess = (SubProcessNode) node;
        String processId = subProcess.getProcessId();
        if (subProcess.getProcessName() != null) {
            processDescriptor.addReusableSubProcessName(subProcess.getProcessName());
        } else {
            processDescriptor.getReusableSubProcesses().add(processId);
        }
    }
}
Also used : HashMap(java.util.HashMap) RuleSetNode(org.jbpm.workflow.core.node.RuleSetNode) UserTaskDefinitionImpl(org.jbpm.kie.services.impl.bpmn2.UserTaskDefinitionImpl) SubProcessNode(org.jbpm.workflow.core.node.SubProcessNode) WorkItemNode(org.jbpm.workflow.core.node.WorkItemNode) HashMap(java.util.HashMap) Map(java.util.Map) HumanTaskNode(org.jbpm.workflow.core.node.HumanTaskNode)

Example 13 with HumanTaskNode

use of org.jbpm.workflow.core.node.HumanTaskNode in project jbpm by kiegroup.

the class MigrationManager method updateNodeInstances.

@SuppressWarnings("unchecked")
private void updateNodeInstances(NodeInstanceContainer nodeInstanceContainer, Map<String, String> nodeMapping, NodeContainer nodeContainer, EntityManager em) {
    for (NodeInstance nodeInstance : nodeInstanceContainer.getNodeInstances()) {
        Long upgradedNodeId = null;
        String oldNodeId = (String) ((NodeImpl) ((org.jbpm.workflow.instance.NodeInstance) nodeInstance).getNode()).getMetaData().get("UniqueId");
        String newNodeId = nodeMapping.get(oldNodeId);
        if (newNodeId == null) {
            newNodeId = oldNodeId;
        }
        Node upgradedNode = findNodeByUniqueId(newNodeId, nodeContainer);
        if (upgradedNode == null) {
            try {
                upgradedNodeId = Long.parseLong(newNodeId);
            } catch (NumberFormatException e) {
                continue;
            }
        } else {
            upgradedNodeId = upgradedNode.getId();
        }
        ((NodeInstanceImpl) nodeInstance).setNodeId(upgradedNodeId);
        if (upgradedNode != null) {
            // update log information for new node information
            Query nodeInstanceIdQuery = em.createQuery("select nodeInstanceId from NodeInstanceLog nil" + " where nil.nodeId = :oldNodeId and processInstanceId = :processInstanceId " + " GROUP BY nil.nodeInstanceId" + " HAVING sum(nil.type) = 0");
            nodeInstanceIdQuery.setParameter("oldNodeId", oldNodeId).setParameter("processInstanceId", nodeInstance.getProcessInstance().getId());
            List<Long> nodeInstanceIds = nodeInstanceIdQuery.getResultList();
            report.addEntry(Type.INFO, "Mapping: Node instance logs to be updated  = " + nodeInstanceIds);
            Query nodeLogQuery = em.createQuery("update NodeInstanceLog set nodeId = :nodeId, nodeName = :nodeName, nodeType = :nodeType " + "where nodeInstanceId in (:ids) and processInstanceId = :processInstanceId");
            nodeLogQuery.setParameter("nodeId", (String) upgradedNode.getMetaData().get("UniqueId")).setParameter("nodeName", upgradedNode.getName()).setParameter("nodeType", upgradedNode.getClass().getSimpleName()).setParameter("ids", nodeInstanceIds).setParameter("processInstanceId", nodeInstance.getProcessInstance().getId());
            int nodesUpdated = nodeLogQuery.executeUpdate();
            report.addEntry(Type.INFO, "Mapping: Node instance logs updated = " + nodesUpdated + " for node instance id " + nodeInstance.getId());
            if (upgradedNode instanceof HumanTaskNode && nodeInstance instanceof HumanTaskNodeInstance) {
                Long taskId = (Long) em.createQuery("select id from TaskImpl where workItemId = :workItemId").setParameter("workItemId", ((HumanTaskNodeInstance) nodeInstance).getWorkItemId()).getSingleResult();
                String name = ((HumanTaskNode) upgradedNode).getName();
                String description = (String) ((HumanTaskNode) upgradedNode).getWork().getParameter("Description");
                // update task audit instance log with new deployment and process id
                Query auditTaskLogQuery = em.createQuery("update AuditTaskImpl set name = :name, description = :description where taskId = :taskId");
                auditTaskLogQuery.setParameter("name", name).setParameter("description", description).setParameter("taskId", taskId);
                int auditTaskUpdated = auditTaskLogQuery.executeUpdate();
                report.addEntry(Type.INFO, "Mapping: Task audit updated = " + auditTaskUpdated + " for task id " + taskId);
                // update task  instance log with new deployment and process id
                Query taskLogQuery = em.createQuery("update TaskImpl set name = :name, description = :description where id = :taskId");
                taskLogQuery.setParameter("name", name).setParameter("description", description).setParameter("taskId", taskId);
                int taskUpdated = taskLogQuery.executeUpdate();
                report.addEntry(Type.INFO, "Mapping: Task updated = " + taskUpdated + " for task id " + taskId);
            }
        }
        if (nodeInstance instanceof NodeInstanceContainer) {
            updateNodeInstances((NodeInstanceContainer) nodeInstance, nodeMapping, nodeContainer, em);
        }
    }
}
Also used : NodeInstanceImpl(org.jbpm.workflow.instance.impl.NodeInstanceImpl) NodeInstanceContainer(org.jbpm.workflow.instance.NodeInstanceContainer) NodeImpl(org.jbpm.workflow.core.impl.NodeImpl) Query(javax.persistence.Query) HumanTaskNode(org.jbpm.workflow.core.node.HumanTaskNode) Node(org.kie.api.definition.process.Node) HumanTaskNodeInstance(org.jbpm.workflow.instance.node.HumanTaskNodeInstance) TimerNodeInstance(org.jbpm.workflow.instance.node.TimerNodeInstance) StateBasedNodeInstance(org.jbpm.workflow.instance.node.StateBasedNodeInstance) NodeInstance(org.kie.api.runtime.process.NodeInstance) HumanTaskNodeInstance(org.jbpm.workflow.instance.node.HumanTaskNodeInstance) HumanTaskNode(org.jbpm.workflow.core.node.HumanTaskNode)

Aggregations

HumanTaskNode (org.jbpm.workflow.core.node.HumanTaskNode)13 StartNode (org.jbpm.workflow.core.node.StartNode)6 EndNode (org.jbpm.workflow.core.node.EndNode)5 RuleSetNode (org.jbpm.workflow.core.node.RuleSetNode)5 SubProcessNode (org.jbpm.workflow.core.node.SubProcessNode)5 WorkItemNode (org.jbpm.workflow.core.node.WorkItemNode)5 ArrayList (java.util.ArrayList)4 Work (org.jbpm.process.core.Work)4 RuleFlowProcess (org.jbpm.ruleflow.core.RuleFlowProcess)4 ActionNode (org.jbpm.workflow.core.node.ActionNode)4 CompositeContextNode (org.jbpm.workflow.core.node.CompositeContextNode)4 EventNode (org.jbpm.workflow.core.node.EventNode)4 FaultNode (org.jbpm.workflow.core.node.FaultNode)4 StateNode (org.jbpm.workflow.core.node.StateNode)4 HashMap (java.util.HashMap)3 NodeImpl (org.jbpm.workflow.core.impl.NodeImpl)3 Node (org.kie.api.definition.process.Node)3 StringReader (java.io.StringReader)2 HashSet (java.util.HashSet)2 SemanticModules (org.drools.core.xml.SemanticModules)2