1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.kuali.rice.kew.engine.transition; |
17 | |
|
18 | |
import org.kuali.rice.kew.engine.RouteContext; |
19 | |
import org.kuali.rice.kew.engine.node.*; |
20 | |
import org.kuali.rice.kew.exception.RouteManagerException; |
21 | |
|
22 | |
import java.util.HashSet; |
23 | |
import java.util.Iterator; |
24 | |
import java.util.List; |
25 | |
import java.util.Set; |
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | 0 | public class DynamicTransitionEngine extends TransitionEngine { |
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
public RouteNodeInstance transitionTo(RouteNodeInstance dynamicNodeInstance, RouteContext context) throws Exception { |
43 | 0 | dynamicNodeInstance.setInitial(false); |
44 | 0 | dynamicNodeInstance.setActive(false); |
45 | 0 | DynamicNode dynamicNode = (DynamicNode) getNode(dynamicNodeInstance.getRouteNode(), DynamicNode.class); |
46 | 0 | DynamicResult result = dynamicNode.transitioningInto(context, dynamicNodeInstance, getRouteHelper()); |
47 | 0 | RouteNodeInstance nextNodeInstance = result.getNextNodeInstance(); |
48 | 0 | RouteNodeInstance finalNodeInstance = null; |
49 | 0 | if (result.isComplete()) { |
50 | 0 | dynamicNodeInstance.setComplete(true); |
51 | 0 | finalNodeInstance = getFinalNodeInstance(dynamicNodeInstance, context); |
52 | 0 | if (nextNodeInstance == null) { |
53 | 0 | nextNodeInstance = finalNodeInstance; |
54 | |
} |
55 | |
} |
56 | |
|
57 | 0 | if (nextNodeInstance !=null) { |
58 | 0 | initializeNodeGraph(context, dynamicNodeInstance, nextNodeInstance, new HashSet<RouteNodeInstance>(), finalNodeInstance); |
59 | |
} |
60 | 0 | return nextNodeInstance; |
61 | |
} |
62 | |
|
63 | |
public ProcessResult isComplete(RouteContext context) throws Exception { |
64 | 0 | throw new UnsupportedOperationException("isComplete() should not be invoked on a Dynamic node!"); |
65 | |
} |
66 | |
|
67 | |
public Transition transitionFrom(RouteContext context, ProcessResult processResult) throws Exception { |
68 | |
|
69 | 0 | Transition transition = new Transition(); |
70 | 0 | RouteNodeInstance dynamicNodeInstance = context.getNodeInstance().getProcess(); |
71 | 0 | DynamicNode dynamicNode = (DynamicNode) getNode(dynamicNodeInstance.getRouteNode(), DynamicNode.class); |
72 | 0 | DynamicResult result = dynamicNode.transitioningOutOf(context, getRouteHelper()); |
73 | 0 | if (result.getNextNodeInstance() == null && result.getNextNodeInstances().isEmpty() && result.isComplete()) { |
74 | 0 | dynamicNodeInstance.setComplete(true); |
75 | 0 | RouteNodeInstance finalNodeInstance = getFinalNodeInstance(dynamicNodeInstance, context); |
76 | 0 | if (finalNodeInstance != null) { |
77 | 0 | transition.getNextNodeInstances().add(finalNodeInstance); |
78 | |
} |
79 | 0 | } else { |
80 | 0 | if (result.getNextNodeInstance() != null) { |
81 | 0 | result.getNextNodeInstance().setProcess(dynamicNodeInstance); |
82 | 0 | transition.getNextNodeInstances().add(result.getNextNodeInstance()); |
83 | |
} |
84 | 0 | for (Iterator iter = result.getNextNodeInstances().iterator(); iter.hasNext();) { |
85 | 0 | RouteNodeInstance nextNodeInstance = (RouteNodeInstance) iter.next(); |
86 | 0 | nextNodeInstance.setProcess(dynamicNodeInstance); |
87 | 0 | } |
88 | 0 | transition.getNextNodeInstances().addAll(result.getNextNodeInstances()); |
89 | |
} |
90 | 0 | return transition; |
91 | |
} |
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | |
private void initializeNodeGraph(RouteContext context, RouteNodeInstance dynamicNodeInstance, RouteNodeInstance nodeInstance, Set<RouteNodeInstance> nodeInstances, RouteNodeInstance finalNodeInstance) throws Exception { |
99 | 0 | if (nodeInstances.contains(nodeInstance)) { |
100 | 0 | throw new RouteManagerException("A cycle was detected in the node graph returned from the dynamic node.", context); |
101 | |
} |
102 | 0 | nodeInstances.add(nodeInstance); |
103 | 0 | nodeInstance.setProcess(dynamicNodeInstance); |
104 | 0 | List<RouteNodeInstance> nextNodeInstances = nodeInstance.getNextNodeInstances(); |
105 | |
|
106 | 0 | if (nextNodeInstances.size() > 1) { |
107 | |
|
108 | |
|
109 | |
} |
110 | 0 | for (RouteNodeInstance nextNodeInstance : nextNodeInstances) |
111 | |
{ |
112 | 0 | initializeNodeGraph(context, dynamicNodeInstance, nextNodeInstance, nodeInstances, finalNodeInstance); |
113 | |
} |
114 | 0 | if (nextNodeInstances.isEmpty() && finalNodeInstance != null) { |
115 | 0 | nodeInstance.addNextNodeInstance(finalNodeInstance); |
116 | |
} |
117 | 0 | } |
118 | |
|
119 | |
private RouteNodeInstance getFinalNodeInstance(RouteNodeInstance dynamicNodeInstance, RouteContext context) throws Exception { |
120 | 0 | List<RouteNode> nextNodes = dynamicNodeInstance.getRouteNode().getNextNodes(); |
121 | 0 | if (nextNodes.size() > 1) { |
122 | 0 | throw new RouteManagerException("There should only be 1 next node following a dynamic node, there were " + nextNodes.size(), context); |
123 | |
} |
124 | 0 | RouteNodeInstance finalNodeInstance = null; |
125 | 0 | if (!nextNodes.isEmpty()) { |
126 | 0 | finalNodeInstance = getRouteHelper().getNodeFactory().createRouteNodeInstance(context.getDocument().getDocumentId(), (RouteNode) nextNodes.get(0)); |
127 | 0 | finalNodeInstance.setBranch(dynamicNodeInstance.getBranch()); |
128 | 0 | finalNodeInstance.setProcess(dynamicNodeInstance.getProcess()); |
129 | |
} |
130 | 0 | return finalNodeInstance; |
131 | |
} |
132 | |
} |