Search in sources :

Example 11 with NodeInstanceContainer

use of org.jbpm.workflow.instance.NodeInstanceContainer in project jbpm by kiegroup.

the class WorkflowReuseContextInstanceFactory method getContextInstance.

public ContextInstance getContextInstance(Context context, ContextInstanceContainer contextInstanceContainer, ProcessInstance processInstance) {
    ContextInstance result = contextInstanceContainer.getContextInstance(context.getType(), context.getId());
    if (result != null) {
        return result;
    }
    try {
        AbstractContextInstance contextInstance = (AbstractContextInstance) cls.newInstance();
        contextInstance.setContextId(context.getId());
        contextInstance.setContextInstanceContainer(contextInstanceContainer);
        contextInstance.setProcessInstance(processInstance);
        contextInstanceContainer.addContextInstance(context.getType(), contextInstance);
        NodeInstanceContainer nodeInstanceContainer = null;
        if (contextInstanceContainer instanceof NodeInstanceContainer) {
            nodeInstanceContainer = (NodeInstanceContainer) contextInstanceContainer;
        } else if (contextInstanceContainer instanceof ContextInstance) {
            ContextInstanceContainer parent = ((ContextInstance) contextInstanceContainer).getContextInstanceContainer();
            while (parent != null) {
                if (parent instanceof NodeInstanceContainer) {
                    nodeInstanceContainer = (NodeInstanceContainer) parent;
                } else if (contextInstanceContainer instanceof ContextInstance) {
                    parent = ((ContextInstance) contextInstanceContainer).getContextInstanceContainer();
                } else {
                    parent = null;
                }
            }
        }
        ((WorkflowContextInstance) contextInstance).setNodeInstanceContainer(nodeInstanceContainer);
        return contextInstance;
    } catch (Exception e) {
        throw new RuntimeException("Unable to instantiate context '" + this.cls.getName() + "': " + e.getMessage());
    }
}
Also used : AbstractContextInstance(org.jbpm.process.instance.context.AbstractContextInstance) NodeInstanceContainer(org.jbpm.workflow.instance.NodeInstanceContainer) AbstractContextInstance(org.jbpm.process.instance.context.AbstractContextInstance) ContextInstance(org.jbpm.process.instance.ContextInstance) ContextInstanceContainer(org.jbpm.process.instance.ContextInstanceContainer)

Example 12 with NodeInstanceContainer

use of org.jbpm.workflow.instance.NodeInstanceContainer in project jbpm by kiegroup.

the class CompensationEventListener method createNodeInstanceContainers.

private Stack<NodeInstance> createNodeInstanceContainers(Node toCompensateNode, boolean generalCompensation) {
    Stack<NodeContainer> nestedNodes = new Stack<NodeContainer>();
    Stack<NodeInstance> generatedInstances = new Stack<NodeInstance>();
    NodeContainer parentContainer = toCompensateNode.getNodeContainer();
    while (!(parentContainer instanceof RuleFlowProcess)) {
        nestedNodes.add(parentContainer);
        parentContainer = ((Node) parentContainer).getNodeContainer();
    }
    NodeInstanceContainer parentInstance;
    if (nestedNodes.isEmpty()) {
        // nestedNodes is empty
        parentInstance = (NodeInstanceContainer) getProcessInstance();
    } else {
        parentInstance = (NodeInstanceContainer) ((WorkflowProcessInstanceImpl) getProcessInstance()).getNodeInstance((Node) nestedNodes.pop());
        generatedInstances.add((NodeInstance) parentInstance);
    }
    NodeInstanceContainer childInstance = parentInstance;
    while (!nestedNodes.isEmpty()) {
        // generate
        childInstance = (NodeInstanceContainer) parentInstance.getNodeInstance((Node) nestedNodes.pop());
        assert childInstance instanceof CompositeNodeInstance : "A node with child nodes should end up creating a CompositeNodeInstance type.";
        // track and modify
        generatedInstances.add((NodeInstance) childInstance);
        // loop
        parentInstance = (CompositeContextNodeInstance) childInstance;
    }
    if (generalCompensation) {
        childInstance = (NodeInstanceContainer) parentInstance.getNodeInstance(toCompensateNode);
        generatedInstances.add((NodeInstance) childInstance);
    }
    return generatedInstances;
}
Also used : RuleFlowProcess(org.jbpm.ruleflow.core.RuleFlowProcess) NodeInstanceContainer(org.jbpm.workflow.instance.NodeInstanceContainer) CompositeNodeInstance(org.jbpm.workflow.instance.node.CompositeNodeInstance) NodeContainer(org.kie.api.definition.process.NodeContainer) NodeInstance(org.jbpm.workflow.instance.NodeInstance) CompositeNodeInstance(org.jbpm.workflow.instance.node.CompositeNodeInstance) CompositeContextNodeInstance(org.jbpm.workflow.instance.node.CompositeContextNodeInstance) Stack(java.util.Stack)

Example 13 with NodeInstanceContainer

use of org.jbpm.workflow.instance.NodeInstanceContainer in project jbpm by kiegroup.

the class ListTimersCommand method processNodeInstance.

protected void processNodeInstance(TimerManager tm, NodeInstanceContainer container, List<TimerInstance> timers) {
    for (NodeInstance nodeInstance : container.getNodeInstances()) {
        if (nodeInstance instanceof TimerNodeInstance) {
            TimerNodeInstance tni = (TimerNodeInstance) nodeInstance;
            org.jbpm.process.instance.timer.TimerInstance timer = tm.getTimerMap().get(tni.getTimerId());
            TimerInstanceImpl details = buildTimer(timer);
            details.setTimerName(resolveVariable(tni.getNodeName(), tni));
            timers.add(details);
        } else if (nodeInstance instanceof StateBasedNodeInstance) {
            StateBasedNodeInstance sbni = (StateBasedNodeInstance) nodeInstance;
            List<Long> timerList = sbni.getTimerInstances();
            if (timerList != null) {
                for (Long timerId : timerList) {
                    org.jbpm.process.instance.timer.TimerInstance timer = tm.getTimerMap().get(timerId);
                    TimerInstanceImpl details = buildTimer(timer);
                    details.setTimerName(resolveVariable(sbni.getNodeName(), sbni));
                    timers.add(details);
                }
            }
        }
        if (nodeInstance instanceof NodeInstanceContainer) {
            processNodeInstance(tm, (NodeInstanceContainer) nodeInstance, timers);
        }
    }
}
Also used : StateBasedNodeInstance(org.jbpm.workflow.instance.node.StateBasedNodeInstance) NodeInstanceContainer(org.jbpm.workflow.instance.NodeInstanceContainer) TimerInstance(org.jbpm.services.api.admin.TimerInstance) ArrayList(java.util.ArrayList) List(java.util.List) StateBasedNodeInstance(org.jbpm.workflow.instance.node.StateBasedNodeInstance) NodeInstance(org.kie.api.runtime.process.NodeInstance) TimerNodeInstance(org.jbpm.workflow.instance.node.TimerNodeInstance) TimerNodeInstance(org.jbpm.workflow.instance.node.TimerNodeInstance) TimerInstanceImpl(org.jbpm.kie.services.impl.admin.TimerInstanceImpl)

Example 14 with NodeInstanceContainer

use of org.jbpm.workflow.instance.NodeInstanceContainer 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

NodeInstanceContainer (org.jbpm.workflow.instance.NodeInstanceContainer)14 NodeInstance (org.kie.api.runtime.process.NodeInstance)7 NodeImpl (org.jbpm.workflow.core.impl.NodeImpl)3 NodeInstance (org.jbpm.workflow.instance.NodeInstance)3 Connection (org.kie.api.definition.process.Connection)3 Node (org.kie.api.definition.process.Node)3 ArrayList (java.util.ArrayList)2 List (java.util.List)2 InternalKnowledgeRuntime (org.drools.core.common.InternalKnowledgeRuntime)2 ContextInstanceContainer (org.jbpm.process.instance.ContextInstanceContainer)2 ProcessInstance (org.jbpm.process.instance.ProcessInstance)2 Constraint (org.jbpm.workflow.core.Constraint)2 BoundaryEventNode (org.jbpm.workflow.core.node.BoundaryEventNode)2 WorkflowRuntimeException (org.jbpm.workflow.instance.WorkflowRuntimeException)2 NodeInstanceImpl (org.jbpm.workflow.instance.impl.NodeInstanceImpl)2 WorkflowProcessInstanceImpl (org.jbpm.workflow.instance.impl.WorkflowProcessInstanceImpl)2 EventNodeInstance (org.jbpm.workflow.instance.node.EventNodeInstance)2 EventSubProcessNodeInstance (org.jbpm.workflow.instance.node.EventSubProcessNodeInstance)2 StateBasedNodeInstance (org.jbpm.workflow.instance.node.StateBasedNodeInstance)2 TimerNodeInstance (org.jbpm.workflow.instance.node.TimerNodeInstance)2