001    /*
002     * Copyright 2011 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     */
016    package org.kuali.kfs.sys.document.workflow;
017    
018    import java.util.ArrayList;
019    import java.util.List;
020    
021    import org.kuali.kfs.sys.context.SpringContext;
022    import org.kuali.kfs.sys.document.FinancialSystemMaintenanceDocument;
023    import org.kuali.kfs.sys.document.FinancialSystemTransactionalDocument;
024    import org.kuali.kfs.sys.document.FinancialSystemTransactionalDocumentBase;
025    import org.kuali.rice.kew.engine.RouteContext;
026    import org.kuali.rice.kew.engine.RouteHelper;
027    import org.kuali.rice.kew.engine.node.SimpleResult;
028    import org.kuali.rice.kew.engine.node.SplitNode;
029    import org.kuali.rice.kew.engine.node.SplitResult;
030    import org.kuali.rice.kns.document.Document;
031    import org.kuali.rice.kns.service.DocumentService;
032    
033    public class SimpleBooleanSplitNode implements SplitNode {
034    
035        /**
036         * @see org.kuali.rice.kew.engine.node.SimpleNode#process(org.kuali.rice.kew.engine.RouteContext, org.kuali.rice.kew.engine.RouteHelper)
037         */
038        public SplitResult process(RouteContext context, RouteHelper helper) throws Exception {
039            SplitResult result = null;
040            
041            String documentID = context.getDocument().getRouteHeaderId().toString();
042            Document document = SpringContext.getBean(DocumentService.class).getByDocumentHeaderId(documentID);
043            String nodeName = context.getNodeInstance().getRouteNode().getRouteNodeName();
044            if (document instanceof FinancialSystemTransactionalDocument) {
045               boolean ret = ((FinancialSystemTransactionalDocument)document).answerSplitNodeQuestion(nodeName);
046               result = booleanToSplitResult(ret);
047            } else if (document instanceof FinancialSystemMaintenanceDocument) {
048                boolean ret = ((FinancialSystemMaintenanceDocument) document).answerSplitNodeQuestion(nodeName);
049                result = booleanToSplitResult(ret);
050            } else {
051                throw new IllegalArgumentException("Document "+document.getDocumentTitle() +" with id " + documentID + " is not an instance of " + FinancialSystemMaintenanceDocument.class + " or " + FinancialSystemTransactionalDocument.class);
052            }
053            
054            return result;
055        }
056        
057        /**
058         * Converts a boolean value to SplitResult where the branch name is "True" or "False" based on the value of the given boolean
059         * @param b a boolean to convert to a SplitResult
060         * @return the converted SplitResult
061         */
062        protected SplitResult booleanToSplitResult(boolean b) {
063            List<String> branches = new ArrayList<String>();
064            final String branchName = b ? "True" : "False";
065            branches.add(branchName);
066            return new SplitResult(branches);
067        }
068    
069    }