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-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.node;
 17  
 
 18  
 import org.kuali.rice.core.framework.persistence.jpa.OrmUtils;
 19  
 import org.kuali.rice.kew.api.WorkflowRuntimeException;
 20  
 import org.kuali.rice.kew.engine.RouteContext;
 21  
 import org.kuali.rice.kew.service.KEWServiceLocator;
 22  
 
 23  
 import java.util.HashSet;
 24  
 import java.util.Iterator;
 25  
 import java.util.Set;
 26  
 import java.util.StringTokenizer;
 27  
 
 28  
 
 29  
 /**
 30  
  * A basic implementation of the JoinEngine which handles join setup and makes determinations
 31  
  * as to when a join condition has been satisfied.
 32  
  *
 33  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 34  
  */
 35  0
 public class BasicJoinEngine implements JoinEngine {
 36  
 
 37  
     public static final String EXPECTED_JOINERS = "ExpectedJoiners";
 38  
     public static final String ACTUAL_JOINERS = "ActualJoiners";
 39  
     
 40  
     public void createExpectedJoinState(RouteContext context, RouteNodeInstance joinInstance, RouteNodeInstance previousNodeInstance) {
 41  0
         RouteNodeInstance splitNode = previousNodeInstance.getBranch().getSplitNode();
 42  0
         if (splitNode == null) {
 43  0
             throw new WorkflowRuntimeException("The split node retrieved from node with name '" + previousNodeInstance.getName() + "' and branch with name '" + previousNodeInstance.getBranch().getName() + "' was null");
 44  
         }
 45  0
         for (Iterator iter = splitNode.getNextNodeInstances().iterator(); iter.hasNext();) {
 46  0
             RouteNodeInstance splitNodeNextNode = (RouteNodeInstance) iter.next();
 47  0
             splitNodeNextNode.getBranch().setJoinNode(joinInstance);
 48  
             // The saveBranch() call below is necessary for parallel routing to work properly with OJB, but it breaks parallel routing with JPA,
 49  
             // so only perform it if KEW is not JPA-enabled.
 50  0
             if (!OrmUtils.isJpaEnabled("rice.kew")) {
 51  0
                     saveBranch(context, splitNodeNextNode.getBranch());
 52  
             }
 53  0
             addExpectedJoiner(joinInstance, splitNodeNextNode.getBranch());
 54  0
         }
 55  0
         joinInstance.setBranch(splitNode.getBranch());
 56  0
         joinInstance.setProcess(splitNode.getProcess());
 57  0
     }
 58  
     
 59  
     public void addExpectedJoiner(RouteNodeInstance nodeInstance, Branch branch) {
 60  0
         addJoinState(nodeInstance, branch, EXPECTED_JOINERS);
 61  0
     }
 62  
 
 63  
     public void addActualJoiner(RouteNodeInstance nodeInstance, Branch branch) {
 64  0
         addJoinState(nodeInstance, branch, ACTUAL_JOINERS);
 65  0
     }
 66  
     
 67  
     private void addJoinState(RouteNodeInstance nodeInstance, Branch branch, String key) {
 68  0
         NodeState state = nodeInstance.getNodeState(key);
 69  0
         if (state == null) {
 70  0
             state = new NodeState();
 71  0
             state.setKey(key);
 72  0
             state.setValue("");
 73  0
             state.setNodeInstance(nodeInstance);
 74  0
             nodeInstance.addNodeState(state);
 75  
         }
 76  0
         state.setValue(state.getValue()+branch.getBranchId()+",");
 77  0
     }
 78  
 
 79  
     public boolean isJoined(RouteNodeInstance nodeInstance) {
 80  0
         NodeState expectedState = nodeInstance.getNodeState(EXPECTED_JOINERS);
 81  0
         if (expectedState == null || org.apache.commons.lang.StringUtils.isEmpty(expectedState.getValue())) {
 82  0
             return true;
 83  
         }
 84  0
         NodeState actualState = nodeInstance.getNodeState(ACTUAL_JOINERS);
 85  0
         Set expectedSet = loadIntoSet(expectedState);
 86  0
         Set actualSet = loadIntoSet(actualState);
 87  0
         for (Iterator iterator = expectedSet.iterator(); iterator.hasNext();) {
 88  0
             String value = (String) iterator.next();
 89  0
             if (actualSet.contains(value)) {
 90  0
                 iterator.remove();
 91  
             }            
 92  0
         }
 93  0
         return expectedSet.size() == 0;
 94  
     }
 95  
     
 96  
     private Set loadIntoSet(NodeState state) {
 97  0
         Set set = new HashSet();
 98  0
         StringTokenizer tokenizer = new StringTokenizer(state.getValue(), ",");
 99  0
         while (tokenizer.hasMoreTokens()) {
 100  0
             set.add(tokenizer.nextToken());
 101  
         }
 102  0
         return set;
 103  
     }
 104  
     
 105  
     private void saveBranch(RouteContext context, Branch branch) {
 106  0
         if (!context.isSimulation()) {
 107  0
             KEWServiceLocator.getRouteNodeService().save(branch);
 108  
         }
 109  0
     }
 110  
 
 111  
 }