Coverage Report - org.kuali.rice.kew.engine.transition.DynamicTransitionEngine
 
Classes in this File Line Coverage Branch Coverage Complexity
DynamicTransitionEngine
0%
0/55
0%
0/32
4.8
 
 1  
 /**
 2  
  * Copyright 2005-2011 The Kuali Foundation
 3  
  *
 4  
  * Licensed under the Educational Community 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.opensource.org/licenses/ecl2.php
 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.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  
  * The DynamicTransitionEngine operates on a {@link DynamicNode} and takes the next node instances returned 
 30  
  * by the node and runs returns them in a TransitionResult after doing some processing and validation on them.
 31  
  * 
 32  
  * @see DynamicNode
 33  
  * 
 34  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 35  
  */
 36  0
 public class DynamicTransitionEngine extends TransitionEngine {
 37  
 
 38  
     // TODO interate the all the nodes and attach the dynamic node as the 'process'
 39  
     // don't include the dynamic node instance in the routing structure - require a correctly built graph
 40  
     // change dynamic node signiture to next node because of above
 41  
     // reconcile branching if necessary
 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  
      * This method checks the next node returned by the user and walks the resulting node graph, filling in required data where possible.
 95  
      * Will throw errors if there is a problem with what the implementor has returned to us. This allows them to do things like return next
 96  
      * nodes with no attached branches, and we will go ahead and generate the branches for them, etc.
 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  
             // TODO implement this feature
 108  
 //            throw new UnsupportedOperationException("Need to implement support for branch generation!");
 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  
 }