View Javadoc

1   /*
2    * Copyright 2005-2007 The Kuali Foundation
3    *
4    *
5    * Licensed under the Educational Community License, Version 2.0 (the "License");
6    * you may not use this file except in compliance with the License.
7    * You may obtain a copy of the License at
8    *
9    * http://www.opensource.org/licenses/ecl2.php
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.kuali.rice.kew.docsearch.xml;
18  
19  import org.kuali.rice.kew.docsearch.StandardDocumentSearchResultProcessor;
20  import org.kuali.rice.kew.rule.bo.RuleAttribute;
21  import org.kuali.rice.kew.rule.xmlrouting.XPathHelper;
22  import org.kuali.rice.kns.web.ui.Column;
23  import org.w3c.dom.Element;
24  import org.w3c.dom.NamedNodeMap;
25  import org.w3c.dom.Node;
26  import org.w3c.dom.NodeList;
27  import org.xml.sax.InputSource;
28  
29  import javax.xml.parsers.DocumentBuilderFactory;
30  import javax.xml.xpath.XPath;
31  import javax.xml.xpath.XPathConstants;
32  import javax.xml.xpath.XPathExpressionException;
33  import java.io.BufferedReader;
34  import java.io.StringReader;
35  import java.util.ArrayList;
36  import java.util.List;
37  
38  
39  /**
40   *
41   * @author Kuali Rice Team (rice.collab@kuali.org)
42   */
43  public class DocumentSearchXMLResultProcessorImpl extends StandardDocumentSearchResultProcessor implements DocumentSearchXMLResultProcessor {
44  	private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(DocumentSearchXMLResultProcessorImpl.class);
45  
46  	private RuleAttribute ruleAttribute;
47  	private List<Column> customDisplayColumns = new ArrayList<Column>();
48  
49  	public void setRuleAttribute(RuleAttribute ruleAttribute) {
50  		this.ruleAttribute = ruleAttribute;
51  	}
52  
53  	@Override
54  	public List<Column> getCustomDisplayColumns() {
55  		List<Column> displayColumns = new ArrayList<Column>();
56  		if (customDisplayColumns.isEmpty()) {
57  			XPath xpath = XPathHelper.newXPath();
58  			String xPathExpression = "//searchResultConfig/column";
59  			try {
60  				NodeList nodes = (NodeList) xpath.evaluate(xPathExpression, getConfigXML(), XPathConstants.NODESET);
61  	            if (nodes == null) {
62  	                LOG.error("Could not find searching configuration columns (<searchResultConfig><column>) for this DocumentSearchXMLResultProcessor");
63  	            } else {
64  	    			for (int i = 0; i < nodes.getLength(); i++) {
65  	    				Node field = nodes.item(i);
66  	    				NamedNodeMap fieldAttributes = field.getAttributes();
67  	    				String key = (fieldAttributes.getNamedItem("name") != null) ? fieldAttributes.getNamedItem("name").getNodeValue().trim() : null;
68  	    				String title = (fieldAttributes.getNamedItem("title") != null) ? fieldAttributes.getNamedItem("title").getNodeValue().trim() : null;
69  	    				String sortable = (fieldAttributes.getNamedItem("sortable") != null) ? fieldAttributes.getNamedItem("sortable").getNodeValue().trim() : null;
70  	    				Column currentColumn = new Column(title,sortable,key);
71  	    				displayColumns.add(currentColumn);
72  	    			}
73  	    			customDisplayColumns = displayColumns;
74  	            }
75  			} catch (XPathExpressionException e) {
76  				LOG.error("error in getCustomDisplayColumns ", e);
77  				throw new RuntimeException("Error trying to find xml content with xpath expression: " + xPathExpression, e);
78  			} catch (Exception e) {
79  				LOG.error("error in getCustomDisplayColumns attempting to find xml custon columns", e);
80  				throw new RuntimeException("Error trying to get xml custom columns.", e);
81  			}
82  		}
83  		return customDisplayColumns;
84  	}
85  
86  	@Override
87  	public boolean getShowAllStandardFields() {
88  		boolean returnValue = DEFAULT_SHOW_ALL_STANDARD_FIELDS_VALUE;
89  		XPath xpath = XPathHelper.newXPath();
90  		String findXpathExpressionPrefix = "//searchResultConfig";
91  		Node searchResultConfig;
92  		try {
93  			searchResultConfig = (Node) xpath.evaluate(findXpathExpressionPrefix, getConfigXML(), XPathConstants.NODE);
94  			if (searchResultConfig != null) {
95  				NamedNodeMap fieldAttributes = searchResultConfig.getAttributes();
96  				if (fieldAttributes.getNamedItem("showStandardSearchFields") != null) {
97  					returnValue = Boolean.valueOf(fieldAttributes.getNamedItem("showStandardSearchFields").getNodeValue());
98  				}
99  			}
100 		} catch (XPathExpressionException e) {
101 			LOG.error("error in getSearchContent ", e);
102 			throw new RuntimeException("Error trying to find xml content with xpath expression", e);
103 		}
104 		return returnValue;
105 	}
106 
107 	@Override
108 	public boolean getOverrideSearchableAttributes() {
109 		boolean returnValue = DEFAULT_OVERRIDE_SEARCHABLE_ATTRIBUTES_VALUE;
110 		XPath xpath = XPathHelper.newXPath();
111 		String findXpathExpressionPrefix = "//searchResultConfig";
112 		Node searchResultConfig;
113 		try {
114 			searchResultConfig = (Node) xpath.evaluate(findXpathExpressionPrefix, getConfigXML(), XPathConstants.NODE);
115 			if (searchResultConfig != null) {
116 				NamedNodeMap fieldAttributes = searchResultConfig.getAttributes();
117 				if (fieldAttributes.getNamedItem("overrideSearchableAttributes") != null) {
118 					returnValue = Boolean.valueOf(fieldAttributes.getNamedItem("overrideSearchableAttributes").getNodeValue());
119 				}
120 			}
121 		} catch (XPathExpressionException e) {
122 			LOG.error("error in getSearchContent ", e);
123 			throw new RuntimeException("Error trying to find xml content with xpath expression", e);
124 		}
125 		return returnValue;
126 	}
127 
128 	public Element getConfigXML() {
129 		try {
130 			return DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new InputSource(new BufferedReader(new StringReader(ruleAttribute.getXmlConfigData())))).getDocumentElement();
131 		} catch (Exception e) {
132 			String ruleAttrStr = (ruleAttribute == null ? null : ruleAttribute.getName());
133 			LOG.error("error parsing xml data from search processor attribute: " + ruleAttrStr, e);
134 			throw new RuntimeException("error parsing xml data from search processor attribute: " + ruleAttrStr, e);
135 		}
136 	}
137 }