Coverage Report - org.kuali.rice.kew.rule.xmlrouting.WorkflowFunctionResolver
 
Classes in this File Line Coverage Branch Coverage Complexity
WorkflowFunctionResolver
0%
0/29
0%
0/18
3.083
WorkflowFunctionResolver$1
0%
0/10
0%
0/8
3.083
WorkflowFunctionResolver$2
0%
0/7
0%
0/2
3.083
WorkflowFunctionResolver$3
0%
0/2
N/A
3.083
 
 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.rule.xmlrouting;
 17  
 
 18  
 import java.util.Iterator;
 19  
 import java.util.List;
 20  
 
 21  
 import javax.xml.namespace.QName;
 22  
 import javax.xml.xpath.XPath;
 23  
 import javax.xml.xpath.XPathFunction;
 24  
 import javax.xml.xpath.XPathFunctionResolver;
 25  
 
 26  
 import org.apache.commons.lang.StringUtils;
 27  
 import org.kuali.rice.kew.api.WorkflowRuntimeException;
 28  
 import org.kuali.rice.kew.rule.RuleExtensionBo;
 29  
 import org.kuali.rice.kew.rule.RuleExtensionValue;
 30  
 import org.kuali.rice.kew.xml.xstream.XStreamSafeSearchFunction;
 31  
 import org.w3c.dom.Node;
 32  
 
 33  
 
 34  
 /**
 35  
  * A function resolver for XPath functions provided by KEW.
 36  
  *
 37  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 38  
  */
 39  0
 public class WorkflowFunctionResolver implements XPathFunctionResolver {
 40  
         
 41  
         private List<RuleExtensionBo> ruleExtensions;
 42  
         private Node rootNode;
 43  
         private XPath xpath;
 44  
 
 45  
 
 46  
         public XPathFunction resolveFunction(QName fname, int arity) {
 47  0
                 if (fname == null) {
 48  0
                         throw new NullPointerException("The function name cannot be null.");
 49  
                 }
 50  0
                 if (fname.equals(new QName("http://nothingfornowwf.com", "ruledata", "wf"))) {
 51  0
                         if (ruleExtensions == null) {
 52  0
                                 throw new IllegalArgumentException("There are no rule extensions.");
 53  
                         }
 54  0
                         return new XPathFunction() {
 55  
                                 public Object evaluate(List args) {
 56  0
                                         if (args.size() == 1) {
 57  0
                                                 String name = (String) args.get(0);
 58  0
                                                 for (RuleExtensionBo ruleExtension : ruleExtensions) {
 59  0
                                                     for (Iterator iter = ruleExtension.getExtensionValues().iterator(); iter.hasNext();) {
 60  0
                                                         RuleExtensionValue value = (RuleExtensionValue) iter.next();
 61  0
                                                         if (value.getKey().equals(name)) {
 62  0
                                                             return value.getValue();
 63  
                                                         }
 64  0
                                                     }
 65  
                                                 }
 66  
                                         }
 67  0
                                         return "";
 68  
                                 }
 69  
                         };
 70  0
                 } else if (fname.equals(new QName("http://nothingfornowwf.com", "xstreamsafe", "wf"))) {
 71  0
                         return new XStreamSafeSearchFunction(rootNode, this.getXpath());
 72  0
                 } else if (fname.equals(new QName("http://nothingfornowwf.com", "upper-case", "wf"))) {
 73  0
                         return new UpperCaseFunction();
 74  0
                 } else if (fname.equals(new QName("http://nothingfornowwf.com", "field", "wf"))) {
 75  0
                         return new XPathFunction() {
 76  
                                 public Object evaluate(java.util.List args) {
 77  0
                                         if (args.size() == 1) {
 78  0
                                                 String name = (String) args.get(0);
 79  
                                                 try {
 80  0
                                                         return field(name);
 81  0
                                                 } catch (Exception e) {
 82  0
                                                         throw new WorkflowRuntimeException("Failed to find field to validate.", e);
 83  
                                                 }
 84  
                                         }
 85  0
                                         return "";
 86  
                                 }
 87  
                         };
 88  0
                 } else if (fname.equals(new QName("http://nothingfornowwf.com", "empty", "wf"))) {
 89  0
                         return new XPathFunction() {
 90  
                                 public Object evaluate(java.util.List args) {
 91  0
                                         return empty(args.get(0));
 92  
                                 }
 93  
                         };
 94  
                 } else {
 95  0
                         return null;
 96  
                 }
 97  
         }
 98  
 
 99  
         public String field(String fieldName) throws Exception {
 100  0
             return xpath.evaluate("//edlContent/data/version[@current='true']/field[@name='" + fieldName + "']/value", rootNode);
 101  
         }
 102  
             
 103  
             
 104  
         public boolean empty(Object object) {
 105  0
             if (object instanceof String) {
 106  0
                     return StringUtils.isBlank((String)object);
 107  
             }
 108  0
             return object == null;
 109  
         }
 110  
 
 111  
         public List<RuleExtensionBo> getRuleExtensions() {
 112  0
         return this.ruleExtensions;
 113  
     }
 114  
 
 115  
     public void setRuleExtensions(List<RuleExtensionBo> ruleExtensions) {
 116  0
         this.ruleExtensions = ruleExtensions;
 117  0
     }
 118  
 
 119  
     public Node getRootNode() {
 120  0
                 return rootNode;
 121  
         }
 122  
 
 123  
         public void setRootNode(Node rootNode) {
 124  0
                 this.rootNode = rootNode;
 125  0
         }
 126  
         
 127  
 
 128  
         public XPath getXpath() {
 129  0
                 return xpath;
 130  
         }
 131  
 
 132  
         public void setXpath(XPath xpath) {
 133  0
                 this.xpath = xpath;
 134  0
         }
 135  
 }