/jbpm-flow/src/main/java/org/jbpm/workflow/core/impl/NodeImpl.java

https://github.com/michelpohle/jbpm · Java · 267 lines · 201 code · 40 blank · 26 comment · 39 complexity · e64ede24e06cf869f5de2a9875522d39 MD5 · raw file

  1. /**
  2. * Copyright 2010 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.impl;
  17. import java.io.Serializable;
  18. import java.util.ArrayList;
  19. import java.util.Collections;
  20. import java.util.HashMap;
  21. import java.util.List;
  22. import java.util.Map;
  23. import org.drools.definition.process.Connection;
  24. import org.drools.definition.process.NodeContainer;
  25. import org.jbpm.process.core.Context;
  26. import org.jbpm.process.core.ContextResolver;
  27. import org.jbpm.workflow.core.Node;
  28. import org.jbpm.workflow.core.node.CompositeNode;
  29. /**
  30. * Default implementation of a node.
  31. *
  32. * @author <a href="mailto:kris_verlaenen@hotmail.com">Kris Verlaenen</a>
  33. */
  34. public abstract class NodeImpl implements Node, Serializable, ContextResolver {
  35. private static final long serialVersionUID = 510l;
  36. protected static final NodeImpl[] EMPTY_NODE_ARRAY = new NodeImpl[0];
  37. private long id;
  38. private String name;
  39. private Map<String, List<Connection>> incomingConnections;
  40. private Map<String, List<Connection>> outgoingConnections;
  41. private NodeContainer nodeContainer;
  42. private Map<String, Context> contexts = new HashMap<String, Context>();
  43. private Map<String, Object> metaData = new HashMap<String, Object>();
  44. public NodeImpl() {
  45. this.id = -1;
  46. this.incomingConnections = new HashMap<String, List<Connection>>();
  47. this.outgoingConnections = new HashMap<String, List<Connection>>();
  48. }
  49. public long getId() {
  50. return this.id;
  51. }
  52. public String getUniqueId() {
  53. String result = id + "";
  54. NodeContainer nodeContainer = getNodeContainer();
  55. while (nodeContainer instanceof CompositeNode) {
  56. CompositeNode composite = (CompositeNode) nodeContainer;
  57. result = composite.getId() + ":" + result;
  58. nodeContainer = composite.getNodeContainer();
  59. }
  60. return result;
  61. }
  62. public void setId(final long id) {
  63. this.id = id;
  64. }
  65. public String getName() {
  66. return this.name;
  67. }
  68. public void setName(final String name) {
  69. this.name = name;
  70. }
  71. public Map<String, List<Connection>> getIncomingConnections() {
  72. // TODO: users can still modify the lists inside this Map
  73. return Collections.unmodifiableMap(this.incomingConnections);
  74. }
  75. public Map<String, List<Connection>> getOutgoingConnections() {
  76. // TODO: users can still modify the lists inside this Map
  77. return Collections.unmodifiableMap(this.outgoingConnections);
  78. }
  79. public void addIncomingConnection(final String type, final Connection connection) {
  80. validateAddIncomingConnection(type, connection);
  81. List<Connection> connections = this.incomingConnections.get(type);
  82. if (connections == null) {
  83. connections = new ArrayList<Connection>();
  84. this.incomingConnections.put(type, connections);
  85. }
  86. connections.add(connection);
  87. }
  88. public void validateAddIncomingConnection(final String type, final Connection connection) {
  89. if (type == null) {
  90. throw new IllegalArgumentException("Connection type cannot be null");
  91. }
  92. if (connection == null) {
  93. throw new IllegalArgumentException("Connection cannot be null");
  94. }
  95. }
  96. public List<Connection> getIncomingConnections(String type) {
  97. List<Connection> result = incomingConnections.get(type);
  98. if (result == null) {
  99. return new ArrayList<Connection>();
  100. }
  101. return result;
  102. }
  103. public void addOutgoingConnection(final String type, final Connection connection) {
  104. validateAddOutgoingConnection(type, connection);
  105. List<Connection> connections = this.outgoingConnections.get(type);
  106. if (connections == null) {
  107. connections = new ArrayList<Connection>();
  108. this.outgoingConnections.put(type, connections);
  109. }
  110. connections.add(connection);
  111. }
  112. public void validateAddOutgoingConnection(final String type, final Connection connection) {
  113. if (type == null) {
  114. throw new IllegalArgumentException("Connection type cannot be null");
  115. }
  116. if (connection == null) {
  117. throw new IllegalArgumentException("Connection cannot be null");
  118. }
  119. }
  120. public List<Connection> getOutgoingConnections(String type) {
  121. List<Connection> result = outgoingConnections.get(type);
  122. if (result == null) {
  123. return new ArrayList<Connection>();
  124. }
  125. return result;
  126. }
  127. public void removeIncomingConnection(final String type, final Connection connection) {
  128. validateRemoveIncomingConnection(type, connection);
  129. this.incomingConnections.get(type).remove(connection);
  130. }
  131. public void validateRemoveIncomingConnection(final String type, final Connection connection) {
  132. if (type == null) {
  133. throw new IllegalArgumentException("Connection type cannot be null");
  134. }
  135. if (connection == null) {
  136. throw new IllegalArgumentException("Connection is null");
  137. }
  138. if (!incomingConnections.get(type).contains(connection)) {
  139. throw new IllegalArgumentException("Given connection <"
  140. + connection + "> is not part of the incoming connections");
  141. }
  142. }
  143. public void removeOutgoingConnection(final String type, final Connection connection) {
  144. validateRemoveOutgoingConnection(type, connection);
  145. this.outgoingConnections.get(type).remove(connection);
  146. }
  147. public void validateRemoveOutgoingConnection(final String type, final Connection connection) {
  148. if (type == null) {
  149. throw new IllegalArgumentException("Connection type cannot be null");
  150. }
  151. if (connection == null) {
  152. throw new IllegalArgumentException("Connection is null");
  153. }
  154. if (!this.outgoingConnections.get(type).contains(connection)) {
  155. throw new IllegalArgumentException("Given connection <"
  156. + connection + "> is not part of the outgoing connections");
  157. }
  158. }
  159. /** Helper method for nodes that have at most one default incoming connection */
  160. public Connection getFrom() {
  161. final List<Connection> list =
  162. getIncomingConnections(org.jbpm.workflow.core.Node.CONNECTION_DEFAULT_TYPE);
  163. if (list.size() == 0) {
  164. return null;
  165. }
  166. if (list.size() == 1) {
  167. return list.get(0);
  168. }
  169. throw new IllegalArgumentException(
  170. "Trying to retrieve the from connection but multiple connections are present");
  171. }
  172. /** Helper method for nodes that have at most one default outgoing connection */
  173. public Connection getTo() {
  174. final List<Connection> list =
  175. getOutgoingConnections(org.jbpm.workflow.core.Node.CONNECTION_DEFAULT_TYPE);
  176. if (list.size() == 0) {
  177. return null;
  178. }
  179. if (list.size() == 1) {
  180. return list.get(0);
  181. }
  182. throw new IllegalArgumentException(
  183. "Trying to retrieve the to connection but multiple connections are present");
  184. }
  185. /** Helper method for nodes that have multiple default incoming connections */
  186. public List<Connection> getDefaultIncomingConnections() {
  187. return getIncomingConnections(org.jbpm.workflow.core.Node.CONNECTION_DEFAULT_TYPE);
  188. }
  189. /** Helper method for nodes that have multiple default outgoing connections */
  190. public List<Connection> getDefaultOutgoingConnections() {
  191. return getOutgoingConnections(org.jbpm.workflow.core.Node.CONNECTION_DEFAULT_TYPE);
  192. }
  193. public NodeContainer getNodeContainer() {
  194. return nodeContainer;
  195. }
  196. public void setNodeContainer(NodeContainer nodeContainer) {
  197. this.nodeContainer = nodeContainer;
  198. }
  199. public void setContext(String contextId, Context context) {
  200. this.contexts.put(contextId, context);
  201. }
  202. public Context getContext(String contextId) {
  203. return this.contexts.get(contextId);
  204. }
  205. public Context resolveContext(String contextId, Object param) {
  206. Context context = getContext(contextId);
  207. if (context != null) {
  208. context = context.resolveContext(param);
  209. if (context != null) {
  210. return context;
  211. }
  212. }
  213. return ((org.jbpm.workflow.core.NodeContainer) nodeContainer).resolveContext(contextId, param);
  214. }
  215. public void setMetaData(String name, Object value) {
  216. this.metaData.put(name, value);
  217. }
  218. public Object getMetaData(String name) {
  219. return this.metaData.get(name);
  220. }
  221. public Map<String, Object> getMetaData() {
  222. return this.metaData;
  223. }
  224. public void setMetaData(Map<String, Object> metaData) {
  225. this.metaData = metaData;
  226. }
  227. }