View Javadoc

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