Coverage Report - org.kuali.rice.kew.engine.node.BasicJoinEngine
 
Classes in this File Line Coverage Branch Coverage Complexity
BasicJoinEngine
0%
0/47
0%
0/22
2.714
 
 1  
 /*
 2  
  * Copyright 2005-2007 The Kuali Foundation
 3  
  * 
 4  
  * 
 5  
  * Licensed under the Educational Community License, Version 2.0 (the "License");
 6  
  * you may not use this file except in compliance with the License.
 7  
  * You may obtain a copy of the License at
 8  
  * 
 9  
  * http://www.opensource.org/licenses/ecl2.php
 10  
  * 
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 16  
  */
 17  
 package org.kuali.rice.kew.engine.node;
 18  
 
 19  
 import org.kuali.rice.core.framework.persistence.jpa.OrmUtils;
 20  
 import org.kuali.rice.kew.api.WorkflowRuntimeException;
 21  
 import org.kuali.rice.kew.engine.RouteContext;
 22  
 import org.kuali.rice.kew.service.KEWServiceLocator;
 23  
 
 24  
 import java.util.HashSet;
 25  
 import java.util.Iterator;
 26  
 import java.util.Set;
 27  
 import java.util.StringTokenizer;
 28  
 
 29  
 
 30  
 /**
 31  
  * A basic implementation of the JoinEngine which handles join setup and makes determinations
 32  
  * as to when a join condition has been satisfied.
 33  
  *
 34  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 35  
  */
 36  0
 public class BasicJoinEngine implements JoinEngine {
 37  
 
 38  
     public static final String EXPECTED_JOINERS = "ExpectedJoiners";
 39  
     public static final String ACTUAL_JOINERS = "ActualJoiners";
 40  
     
 41  
     public void createExpectedJoinState(RouteContext context, RouteNodeInstance joinInstance, RouteNodeInstance previousNodeInstance) {
 42  0
         RouteNodeInstance splitNode = previousNodeInstance.getBranch().getSplitNode();
 43  0
         if (splitNode == null) {
 44  0
             throw new WorkflowRuntimeException("The split node retrieved from node with name '" + previousNodeInstance.getName() + "' and branch with name '" + previousNodeInstance.getBranch().getName() + "' was null");
 45  
         }
 46  0
         for (Iterator iter = splitNode.getNextNodeInstances().iterator(); iter.hasNext();) {
 47  0
             RouteNodeInstance splitNodeNextNode = (RouteNodeInstance) iter.next();
 48  0
             splitNodeNextNode.getBranch().setJoinNode(joinInstance);
 49  
             // The saveBranch() call below is necessary for parallel routing to work properly with OJB, but it breaks parallel routing with JPA,
 50  
             // so only perform it if KEW is not JPA-enabled.
 51  0
             if (!OrmUtils.isJpaEnabled("rice.kew")) {
 52  0
                     saveBranch(context, splitNodeNextNode.getBranch());
 53  
             }
 54  0
             addExpectedJoiner(joinInstance, splitNodeNextNode.getBranch());
 55  0
         }
 56  0
         joinInstance.setBranch(splitNode.getBranch());
 57  0
         joinInstance.setProcess(splitNode.getProcess());
 58  0
     }
 59  
     
 60  
     public void addExpectedJoiner(RouteNodeInstance nodeInstance, Branch branch) {
 61  0
         addJoinState(nodeInstance, branch, EXPECTED_JOINERS);
 62  0
     }
 63  
 
 64  
     public void addActualJoiner(RouteNodeInstance nodeInstance, Branch branch) {
 65  0
         addJoinState(nodeInstance, branch, ACTUAL_JOINERS);
 66  0
     }
 67  
     
 68  
     private void addJoinState(RouteNodeInstance nodeInstance, Branch branch, String key) {
 69  0
         NodeState state = nodeInstance.getNodeState(key);
 70  0
         if (state == null) {
 71  0
             state = new NodeState();
 72  0
             state.setKey(key);
 73  0
             state.setValue("");
 74  0
             state.setNodeInstance(nodeInstance);
 75  0
             nodeInstance.addNodeState(state);
 76  
         }
 77  0
         state.setValue(state.getValue()+branch.getBranchId()+",");
 78  0
     }
 79  
 
 80  
     public boolean isJoined(RouteNodeInstance nodeInstance) {
 81  0
         NodeState expectedState = nodeInstance.getNodeState(EXPECTED_JOINERS);
 82  0
         if (expectedState == null || org.apache.commons.lang.StringUtils.isEmpty(expectedState.getValue())) {
 83  0
             return true;
 84  
         }
 85  0
         NodeState actualState = nodeInstance.getNodeState(ACTUAL_JOINERS);
 86  0
         Set expectedSet = loadIntoSet(expectedState);
 87  0
         Set actualSet = loadIntoSet(actualState);
 88  0
         for (Iterator iterator = expectedSet.iterator(); iterator.hasNext();) {
 89  0
             String value = (String) iterator.next();
 90  0
             if (actualSet.contains(value)) {
 91  0
                 iterator.remove();
 92  
             }            
 93  0
         }
 94  0
         return expectedSet.size() == 0;
 95  
     }
 96  
     
 97  
     private Set loadIntoSet(NodeState state) {
 98  0
         Set set = new HashSet();
 99  0
         StringTokenizer tokenizer = new StringTokenizer(state.getValue(), ",");
 100  0
         while (tokenizer.hasMoreTokens()) {
 101  0
             set.add(tokenizer.nextToken());
 102  
         }
 103  0
         return set;
 104  
     }
 105  
     
 106  
     private void saveBranch(RouteContext context, Branch branch) {
 107  0
         if (!context.isSimulation()) {
 108  0
             KEWServiceLocator.getRouteNodeService().save(branch);
 109  
         }
 110  0
     }
 111  
 
 112  
 }