001/**
002 * Copyright 2005-2016 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.rice.kew.engine.transition;
017
018import org.kuali.rice.kew.engine.RouteContext;
019import org.kuali.rice.kew.engine.RouteHelper;
020import org.kuali.rice.kew.engine.node.*;
021
022import java.util.ArrayList;
023import java.util.List;
024
025
026/**
027 * Handles transitions into and out of {@link SplitNode} nodes.
028 * 
029 * @see SplitNode
030 *
031 * @author Kuali Rice Team (rice.collab@kuali.org)
032 */
033public class SplitTransitionEngine extends TransitionEngine {
034        
035    public ProcessResult isComplete(RouteContext context) throws Exception {
036        SplitNode node = (SplitNode)getNode(context.getNodeInstance().getRouteNode(), SplitNode.class);
037        return node.process(context, getRouteHelper());
038    }
039    
040        public Transition transitionFrom(RouteContext context, ProcessResult processResult)
041                        throws Exception {
042                RouteNodeInstance splitInstance = context.getNodeInstance();
043                List<RouteNodeInstance> nextNodeInstances = new ArrayList<RouteNodeInstance>();
044        SplitResult result = (SplitResult)processResult;
045        for (String branchName : result.getBranchNames())
046        {
047            for (RouteNode routeNode : splitInstance.getRouteNode().getNextNodes())
048            {
049                if (routeNode.getBranch() != null && routeNode.getBranch().getName().equals(branchName))
050                {
051                    nextNodeInstances.add(createSplitChild(branchName, routeNode, splitInstance));
052                }
053            }
054        }
055                return new Transition(nextNodeInstances);
056        }
057        
058        public static RouteNodeInstance createSplitChild(String branchName, RouteNode routeNode, RouteNodeInstance splitInstance) {
059            RouteHelper routeHelper = new RouteHelper();
060            RouteNodeInstance nextNodeInstance = routeHelper.getNodeFactory().createRouteNodeInstance(splitInstance.getDocumentId(), routeNode);
061                Branch branch = routeHelper.getNodeFactory().createBranch(branchName, splitInstance.getBranch(), nextNodeInstance);
062                branch.setSplitNode(splitInstance);
063                nextNodeInstance.setBranch(branch);
064                nextNodeInstance.setProcess(splitInstance.getProcess());
065                return nextNodeInstance;
066        }
067        
068}