/jbpm-flow/src/main/java/org/jbpm/workflow/core/node/ForEachNode.java

https://github.com/jairdomingues/jbpm · Java · 199 lines · 138 code · 34 blank · 27 comment · 4 complexity · 94cf588e058213eced73d01c48b3828e MD5 · raw file

  1. /**
  2. * Copyright 2005 JBoss Inc
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.jbpm.workflow.core.node;
  17. import java.util.ArrayList;
  18. import java.util.List;
  19. import org.drools.definition.process.Node;
  20. import org.drools.process.core.datatype.DataType;
  21. import org.jbpm.process.core.context.variable.Variable;
  22. import org.jbpm.process.core.context.variable.VariableScope;
  23. import org.jbpm.workflow.core.impl.ConnectionImpl;
  24. import org.jbpm.workflow.core.impl.ExtendedNodeImpl;
  25. /**
  26. * A for each node.
  27. *
  28. * This node activates the contained subflow for each element of a collection.
  29. * The node continues if all activated the subflow has been completed for each
  30. * of the elements in the collection.
  31. *
  32. * @author <a href="mailto:kris_verlaenen@hotmail.com">Kris Verlaenen</a>
  33. */
  34. public class ForEachNode extends CompositeNode {
  35. private static final long serialVersionUID = 510l;
  36. private String variableName;
  37. private String collectionExpression;
  38. private boolean waitForCompletion = true;
  39. public ForEachNode() {
  40. // Split
  41. ForEachSplitNode split = new ForEachSplitNode();
  42. split.setName("ForEachSplit");
  43. split.setMetaData("hidden", true);
  44. super.addNode(split);
  45. super.linkIncomingConnections(
  46. org.jbpm.workflow.core.Node.CONNECTION_DEFAULT_TYPE,
  47. new CompositeNode.NodeAndType(split, org.jbpm.workflow.core.Node.CONNECTION_DEFAULT_TYPE));
  48. // Composite node
  49. CompositeContextNode compositeNode = new CompositeContextNode();
  50. compositeNode.setName("ForEachComposite");
  51. compositeNode.setMetaData("hidden", true);
  52. super.addNode(compositeNode);
  53. VariableScope variableScope = new VariableScope();
  54. compositeNode.addContext(variableScope);
  55. compositeNode.setDefaultContext(variableScope);
  56. // Join
  57. ForEachJoinNode join = new ForEachJoinNode();
  58. join.setName("ForEachJoin");
  59. join.setMetaData("hidden", true);
  60. super.addNode(join);
  61. super.linkOutgoingConnections(
  62. new CompositeNode.NodeAndType(join, org.jbpm.workflow.core.Node.CONNECTION_DEFAULT_TYPE),
  63. org.jbpm.workflow.core.Node.CONNECTION_DEFAULT_TYPE);
  64. new ConnectionImpl(
  65. super.getNode(1), org.jbpm.workflow.core.Node.CONNECTION_DEFAULT_TYPE,
  66. getCompositeNode(), org.jbpm.workflow.core.Node.CONNECTION_DEFAULT_TYPE
  67. );
  68. new ConnectionImpl(
  69. getCompositeNode(), org.jbpm.workflow.core.Node.CONNECTION_DEFAULT_TYPE,
  70. super.getNode(3), org.jbpm.workflow.core.Node.CONNECTION_DEFAULT_TYPE
  71. );
  72. }
  73. public String getVariableName() {
  74. return variableName;
  75. }
  76. public DataType getVariableType() {
  77. if (variableName == null) {
  78. return null;
  79. }
  80. for (Variable variable: ((VariableScope) getCompositeNode().getDefaultContext(VariableScope.VARIABLE_SCOPE)).getVariables()) {
  81. if (variableName.equals(variable.getName())) {
  82. return variable.getType();
  83. }
  84. }
  85. return null;
  86. }
  87. public CompositeContextNode getCompositeNode() {
  88. return (CompositeContextNode) super.getNode(2);
  89. }
  90. public ForEachSplitNode getForEachSplitNode() {
  91. return (ForEachSplitNode) super.getNode(1);
  92. }
  93. public ForEachJoinNode getForEachJoinNode() {
  94. return (ForEachJoinNode) super.getNode(3);
  95. }
  96. public void addNode(Node node) {
  97. getCompositeNode().addNode(node);
  98. }
  99. protected void internalAddNode(Node node) {
  100. super.addNode(node);
  101. }
  102. public Node getNode(long id) {
  103. return getCompositeNode().getNode(id);
  104. }
  105. public Node internalGetNode(long id) {
  106. return super.getNode(id);
  107. }
  108. public Node[] getNodes() {
  109. return getCompositeNode().getNodes();
  110. }
  111. public Node[] internalGetNodes() {
  112. return super.getNodes();
  113. }
  114. public void removeNode(Node node) {
  115. getCompositeNode().removeNode(node);
  116. }
  117. protected void internalRemoveNode(Node node) {
  118. super.removeNode(node);
  119. }
  120. public void linkIncomingConnections(String inType, long inNodeId, String inNodeType) {
  121. getCompositeNode().linkIncomingConnections(inType, inNodeId, inNodeType);
  122. }
  123. public void linkOutgoingConnections(long outNodeId, String outNodeType, String outType) {
  124. getCompositeNode().linkOutgoingConnections(outNodeId, outNodeType, outType);
  125. }
  126. public CompositeNode.NodeAndType getLinkedIncomingNode(String inType) {
  127. return getCompositeNode().getLinkedIncomingNode(inType);
  128. }
  129. public CompositeNode.NodeAndType internalGetLinkedIncomingNode(String inType) {
  130. return super.getLinkedIncomingNode(inType);
  131. }
  132. public CompositeNode.NodeAndType getLinkedOutgoingNode(String inType) {
  133. return getCompositeNode().getLinkedOutgoingNode(inType);
  134. }
  135. public CompositeNode.NodeAndType internalGetLinkedOutgoingNode(String inType) {
  136. return super.getLinkedOutgoingNode(inType);
  137. }
  138. public void setVariable(String variableName, DataType type) {
  139. this.variableName = variableName;
  140. List<Variable> variables = new ArrayList<Variable>();
  141. Variable variable = new Variable();
  142. variable.setName(variableName);
  143. variable.setType(type);
  144. variables.add(variable);
  145. ((VariableScope) getCompositeNode().getDefaultContext(VariableScope.VARIABLE_SCOPE)).setVariables(variables);
  146. }
  147. public String getCollectionExpression() {
  148. return collectionExpression;
  149. }
  150. public void setCollectionExpression(String collectionExpression) {
  151. this.collectionExpression = collectionExpression;
  152. }
  153. public boolean isWaitForCompletion() {
  154. return waitForCompletion;
  155. }
  156. public void setWaitForCompletion(boolean waitForCompletion) {
  157. this.waitForCompletion = waitForCompletion;
  158. }
  159. public class ForEachSplitNode extends ExtendedNodeImpl {
  160. private static final long serialVersionUID = 510l;
  161. }
  162. public class ForEachJoinNode extends ExtendedNodeImpl {
  163. private static final long serialVersionUID = 510l;
  164. }
  165. }