diff --git a/jbpm-flow/src/main/java/org/jbpm/process/instance/impl/CancelNodeInstanceAction.java b/jbpm-flow/src/main/java/org/jbpm/process/instance/impl/CancelNodeInstanceAction.java index a4e8502832..418f43105b 100644 --- a/jbpm-flow/src/main/java/org/jbpm/process/instance/impl/CancelNodeInstanceAction.java +++ b/jbpm-flow/src/main/java/org/jbpm/process/instance/impl/CancelNodeInstanceAction.java @@ -17,6 +17,7 @@ import java.io.Serializable; import java.util.Collection; +import java.util.HashSet; import org.jbpm.workflow.instance.node.CompositeNodeInstance; import org.kie.api.runtime.process.NodeInstance; @@ -39,36 +40,32 @@ public CancelNodeInstanceAction(String attachedToNodeId) { public void execute(ProcessContext context) throws Exception { - NodeInstanceContainer container = context.getNodeInstance().getNodeInstanceContainer(); - NodeInstance nodeInstance = findNodeByUniqueId(container.getNodeInstances(), attachedToNodeId); + NodeInstanceContainer container = context.getNodeInstance().getNodeInstanceContainer(); + Collection nodeInstances = findNodeByUniqueId(container.getNodeInstances(), attachedToNodeId); - if (nodeInstance == null) { + if (nodeInstances.isEmpty()) { WorkflowProcessInstance pi = context.getNodeInstance().getProcessInstance(); - nodeInstance = findNodeByUniqueId(pi.getNodeInstances(), attachedToNodeId); + nodeInstances = findNodeByUniqueId(pi.getNodeInstances(), attachedToNodeId); } - if (nodeInstance != null) { - ((org.jbpm.workflow.instance.NodeInstance) nodeInstance).cancel(SKIPPED); - } + nodeInstances.forEach(nodeInstance -> ((org.jbpm.workflow.instance.NodeInstance) nodeInstance).cancel(SKIPPED)); } - private NodeInstance findNodeByUniqueId(Collection nodeInstances, String uniqueId) { + private Collection findNodeByUniqueId(Collection nodeInstances, String uniqueId) { + Collection result = new HashSet<>(); if (nodeInstances != null && !nodeInstances.isEmpty()) { for (NodeInstance nInstance : nodeInstances) { String nodeUniqueId = (String) nInstance.getNode().getMetaData().get("UniqueId"); if (uniqueId.equals(nodeUniqueId)) { - return nInstance; + result.add(nInstance); } if (nInstance instanceof CompositeNodeInstance) { - NodeInstance nodeInstance = findNodeByUniqueId(((CompositeNodeInstance) nInstance).getNodeInstances(), uniqueId); - if (nodeInstance != null) { - return nodeInstance; - } + result.addAll(findNodeByUniqueId(((CompositeNodeInstance) nInstance).getNodeInstances(), uniqueId)); } } } - return null; + return result; } } diff --git a/jbpm-flow/src/main/java/org/jbpm/workflow/instance/node/BoundaryEventNodeInstance.java b/jbpm-flow/src/main/java/org/jbpm/workflow/instance/node/BoundaryEventNodeInstance.java index f7c6d10c34..b52235c8df 100644 --- a/jbpm-flow/src/main/java/org/jbpm/workflow/instance/node/BoundaryEventNodeInstance.java +++ b/jbpm-flow/src/main/java/org/jbpm/workflow/instance/node/BoundaryEventNodeInstance.java @@ -17,6 +17,7 @@ import java.util.Collection; import java.util.HashMap; +import java.util.HashSet; import java.util.List; import java.util.Map; @@ -63,35 +64,44 @@ public BoundaryEventNode getEventNode() { public void triggerCompleted(String type, Object event) { Collection nodeInstances = ((NodeInstanceContainer) getNodeInstanceContainer()).getNodeInstances(); String attachedTo = getEventNode().getAttachedToNodeId(); - NodeInstance nodeInstance = getAttachedToNodeActive(nodeInstances, attachedTo, type, event); - + Collection attachedNodeInstances = getAttachedToNodeActive(nodeInstances, attachedTo, type, + event); List dataAssociation = getEventNode().getOutAssociations(); - if(!dataAssociation.isEmpty()) { - - Map outputData = new HashMap<>(); - // this is for backward compatibility - outputData.put(dataAssociation.get(0).getSources().get(0), event); - // added normalized data - outputData.put("nodeInstance", nodeInstance); - outputData.put("signal", type); - outputData.put("event", event); - if(nodeInstance instanceof WorkItemNodeInstance) { - outputData.put("workItem", ((WorkItemNodeInstance) nodeInstance).getWorkItem()); + if (!dataAssociation.isEmpty()) { + if (attachedNodeInstances.isEmpty()) { + mapOutputData(dataAssociation, null, type, event); + } else { + for (NodeInstance nodeInstance : attachedNodeInstances) { + mapOutputData(dataAssociation, nodeInstance, type, event); + } } - - mapOutputSetVariables(this, getEventNode().getOutAssociations(), outputData); } triggerEvent(ExtendedNodeImpl.EVENT_NODE_BOUNDARY); super.triggerCompleted(); } private boolean isAttachedToNodeActive(Collection nodeInstances, String attachedTo, String type, Object event) { - return getAttachedToNodeActive(nodeInstances, attachedTo, type, event) != null; + return !getAttachedToNodeActive(nodeInstances, attachedTo, type, event).isEmpty(); } + private void mapOutputData(List dataAssociation, NodeInstance nodeInstance, String type, + Object event) { + Map outputData = new HashMap<>(); + // this is for backward compatibility + outputData.put(dataAssociation.get(0).getSources().get(0), event); + // added normalized data + outputData.put("nodeInstance", nodeInstance); + outputData.put("signal", type); + outputData.put("event", event); + if (nodeInstance instanceof WorkItemNodeInstance) { + outputData.put("workItem", ((WorkItemNodeInstance) nodeInstance).getWorkItem()); + } + mapOutputSetVariables(this, getEventNode().getOutAssociations(), outputData); + } - - private NodeInstance getAttachedToNodeActive(Collection nodeInstances, String attachedTo, String type, Object event) { + private Collection getAttachedToNodeActive(Collection nodeInstances, String attachedTo, + String type, Object event) { + Collection result = new HashSet<>(); if (nodeInstances != null && !nodeInstances.isEmpty()) { for (NodeInstance nInstance : nodeInstances) { String nodeUniqueId = (String) nInstance.getNode().getMetaData().get("UniqueId"); @@ -100,21 +110,19 @@ private NodeInstance getAttachedToNodeActive(Collection nodeInstan // in case this is timer event make sure it corresponds to the proper node instance if (type.startsWith("Timer-")) { if (Long.valueOf(nInstance.getId()).equals(event)) { - return nInstance; + result.add(nInstance); } } else { - return nInstance; + result.add(nInstance); } } - if (nInstance instanceof CompositeNodeInstance) { - NodeInstance nodeInstance = getAttachedToNodeActive(((CompositeNodeInstance) nInstance).getNodeInstances(), attachedTo, type, event); - if (nodeInstance != null) { - return nodeInstance; - } + else if (nInstance instanceof CompositeNodeInstance) { + result.addAll(getAttachedToNodeActive(((CompositeNodeInstance) nInstance).getNodeInstances(), + attachedTo, type, event)); } } } - return null; + return result; } private boolean isAttachedToNodeCompleted(String attachedTo) {