Clover Coverage Report - Kuali Student 1.2-M4-SNAPSHOT (Aggregated)
Coverage timestamp: Wed Jul 20 2011 11:14:35 EDT
../../../../../../../img/srcFileCovDistChart10.png 0% of files have more coverage
110   267   63   12.22
84   212   0.57   9
9     7  
1    
 
  SearchBeanDefinitionParser       Line # 46 110 0% 63 13 93.6% 0.9359606
 
  (6)
 
1    /**
2    * Copyright 2010 The Kuali Foundation Licensed under the
3    * Educational Community License, Version 2.0 (the "License"); you may
4    * not use this file except in compliance with the License. You may
5    * obtain a copy of the License at
6    *
7    * http://www.osedu.org/licenses/ECL-2.0
8    *
9    * Unless required by applicable law or agreed to in writing,
10    * software distributed under the License is distributed on an "AS IS"
11    * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12    * or implied. See the License for the specific language governing
13    * permissions and limitations under the License.
14    */
15   
16    package org.kuali.student.common.search.service.impl;
17   
18    import java.util.HashSet;
19    import java.util.List;
20   
21    import javax.xml.parsers.DocumentBuilder;
22    import javax.xml.parsers.DocumentBuilderFactory;
23   
24    import org.apache.log4j.Logger;
25    import org.kuali.student.common.dictionary.old.dto.FieldDescriptor;
26    import org.kuali.student.common.search.dto.ComparisonParamInfo;
27    import org.kuali.student.common.search.dto.CrossSearchTypeInfo;
28    import org.kuali.student.common.search.dto.JoinComparisonInfo;
29    import org.kuali.student.common.search.dto.JoinCriteriaInfo;
30    import org.kuali.student.common.search.dto.JoinResultMappingInfo;
31    import org.kuali.student.common.search.dto.QueryParamInfo;
32    import org.kuali.student.common.search.dto.ResultColumnInfo;
33    import org.kuali.student.common.search.dto.SearchCriteriaTypeInfo;
34    import org.kuali.student.common.search.dto.SearchResultTypeInfo;
35    import org.kuali.student.common.search.dto.SearchTypeInfo;
36    import org.kuali.student.common.search.dto.SubSearchInfo;
37    import org.kuali.student.common.search.dto.SubSearchParamMappingInfo;
38    import org.springframework.beans.factory.support.BeanDefinitionBuilder;
39    import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;
40    import org.springframework.beans.factory.xml.ParserContext;
41    import org.w3c.dom.Attr;
42    import org.w3c.dom.Document;
43    import org.w3c.dom.Element;
44    import org.w3c.dom.Node;
45   
 
46    public class SearchBeanDefinitionParser extends
47    AbstractSingleBeanDefinitionParser {
48    private static final Logger logger = Logger.getLogger(SearchBeanDefinitionParser.class);
49   
 
50  6060 toggle @Override
51    protected Class<?> getBeanClass(Element element) {
52   
53  6060 if (element.getLocalName().equals("fieldDescriptor")) {
54  0 return FieldDescriptor.class;
55    }
56  6060 if (element.getLocalName().equals("searchCriteriaTypeInfo")) {
57  800 return SearchCriteriaTypeInfo.class;
58    }
59  5260 if (element.getLocalName().equals("queryParam")) {
60  1388 return QueryParamInfo.class;
61    }
62  3872 if (element.getLocalName().equals("searchType")) {
63  782 return SearchTypeInfo.class;
64    }
65  3090 if (element.getLocalName().equals("resultColumn")) {
66  1578 return ResultColumnInfo.class;
67    }
68  1512 if (element.getLocalName().equals("searchResultTypeInfo")) {
69  696 return SearchResultTypeInfo.class;
70    }
71   
72   
73  816 if (element.getLocalName().equals("crossSearchType")) {
74  26 return CrossSearchTypeInfo.class;
75    }
76  790 if (element.getLocalName().equals("subSearch")) {
77  52 return SubSearchInfo.class;
78    }
79  738 if (element.getLocalName().equals("subSearchParamMappings")) {
80  328 return SubSearchParamMappingInfo.class;
81    }
82  410 if (element.getLocalName().equals("joinCriteria")) {
83  26 return JoinCriteriaInfo.class;
84    }
85  384 if (element.getLocalName().equals("comparison")) {
86  8 return JoinComparisonInfo.class;
87    }
88  376 if (element.getLocalName().equals("leftHandSide")) {
89  8 return ComparisonParamInfo.class;
90    }
91  368 if (element.getLocalName().equals("rightHandSide")) {
92  8 return ComparisonParamInfo.class;
93    }
94  360 if (element.getLocalName().equals("joinResultMapping")) {
95  360 return JoinResultMappingInfo.class;
96    }
97   
98  0 return super.getBeanClass(element);
99    }
100   
 
101  6060 toggle @Override
102    protected void doParse(Element element, ParserContext pc, BeanDefinitionBuilder builder) {
103    //Set the key
104  6060 if(!"fieldDescriptor".equals(element.getLocalName())&&element.hasAttribute("id")){
105  5238 builder.addPropertyValue("key", element.getAttributes().getNamedItem("id").getTextContent());
106    }
107   
108  6060 if("fieldDescriptor".equals(element.getLocalName())&&element.hasAttribute("parent")){
109  0 builder.setParentName(element.getAttributes().getNamedItem("parent").getTextContent());
110    }
111   
112    //Set optional if its a queryParam
113  6060 if("queryParam".equals(element.getLocalName())&&element.hasAttribute("optional")){
114  536 builder.addPropertyValue("optional", "true".equals(element.getAttribute("optional"))?true:false);
115    }
116   
117    //Copy Attributes
118  6060 if(element.hasAttributes()){
119  18868 for(int i = 0;i<element.getAttributes().getLength();i++){
120  12840 Attr attr = (Attr) element.getAttributes().item(i);
121  12840 if("abstract".equals(attr.getName())){
122  2528 builder.setAbstract(true);
123  10312 }else if(!"id".equals(attr.getName())&&!"parent".equals(attr.getName())){
124  2442 String fieldName = resolveFieldName(element.getLocalName(),attr.getName());
125  2442 builder.addPropertyValue(fieldName, attr.getValue());
126    }
127    }
128    }
129   
130   
131  6060 HashSet<String> visitedNodes = new HashSet<String>();
132    //Parse the children
133  24222 for(int i = 0;i<element.getChildNodes().getLength();i++){
134  18162 Node node = element.getChildNodes().item(i);
135   
136  18162 if(Node.ELEMENT_NODE == node.getNodeType()){
137  7701 String localName=node.getLocalName();
138  7701 if(!visitedNodes.contains(localName)){
139  7057 if(isWrappedList(node.getLocalName())){
140  728 List<?> refList = pc.getDelegate().parseListElement((Element) node, pc.getContainingBeanDefinition());
141  728 builder.addPropertyValue(node.getLocalName(),refList);
142  6329 }else if(isUnwrappedList(node.getLocalName())){
143  104 Element childList=getChildList(element,node.getLocalName());
144  104 visitedNodes.add(node.getLocalName());
145  104 List<?> refList = pc.getDelegate().parseListElement(childList, pc.getContainingBeanDefinition());
146  104 if(refList!=null&&!refList.isEmpty()){
147  104 String fieldName=resolveFieldName(element.getLocalName(),node.getLocalName());
148  104 builder.addPropertyValue(fieldName,refList);
149    }
150    }else{
151  6225 Element childElement = getFirstChildElement(node);
152  6225 if(childElement!=null){
153  1092 if("ref".equals(childElement.getLocalName())){
154  834 Object childBean = pc.getDelegate().parsePropertySubElement(childElement, pc.getContainingBeanDefinition());
155  834 builder.addPropertyValue(node.getLocalName(), childBean);
156    }else{
157  258 Object childBean = pc.getDelegate().parsePropertySubElement((Element)node, pc.getContainingBeanDefinition());
158  258 builder.addPropertyValue(node.getLocalName(), childBean);
159    }
160    }else{
161  5133 if(("ref".equals(node.getLocalName())&&"queryParam".equals(element.getLocalName()))){
162  448 Object childBean = pc.getDelegate().parsePropertySubElement((Element)node, pc.getContainingBeanDefinition());
163  448 builder.addPropertyValue("fieldDescriptor", childBean);
164  4685 }else if("leftHandSide".equals(node.getLocalName())
165    ||"rightHandSide".equals(node.getLocalName())
166    ||"joinCriteria".equals(node.getLocalName())){
167  34 Object childBean = pc.getDelegate().parsePropertySubElement((Element)node, pc.getContainingBeanDefinition());
168  34 builder.addPropertyValue(node.getLocalName(), childBean);
169    }else{
170  4651 builder.addPropertyValue(node.getLocalName(), node.getTextContent());
171    }
172    }
173    }
174    }
175    }
176    }
177    }
178   
 
179  6329 toggle private boolean isUnwrappedList(String localName) {
180  6329 return localName.equals("subSearchParamMappings")||
181    localName.equals("comparison")||
182    localName.equals("subSearch")||
183    localName.equals("joinResultMapping");
184    }
185   
186    //This is called to resolve tag names to field names based on the element and parent element local names
 
187  2546 toggle private String resolveFieldName(String parentName, String nodeName) {
188  2546 if("comparison".equals(nodeName)){
189  8 return "comparisons";
190    }
191  2538 if("subSearch".equals(nodeName)){
192  26 return "subSearches";
193    }
194  2512 if("joinResultMapping".equals(nodeName)){
195  26 return "joinResultMappings";
196    }
197  2486 if("joinCriteria".equals(parentName)&&"type".equals(nodeName)){
198  26 return "joinType";
199    }
200   
201  2460 return nodeName;
202    }
203   
204    //This builds up a list of the child nodes so that the spring parseListElement can be used
205    //it also translates <fooRef> elements into straight spring <ref> elements
 
206  104 toggle private Element getChildList(Element element, String localName) {
207  104 try{
208    //Create a new document to contain our list of elements
209  104 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
210  104 DocumentBuilder builder = dbf.newDocumentBuilder();
211  104 Document doc = builder.newDocument();
212   
213  104 Element root = doc.createElement("listRoot");
214   
215  3080 for(int i = 0;i<element.getChildNodes().getLength();i++){
216  2976 Node node = element.getChildNodes().item(i);
217  2976 if(Node.ELEMENT_NODE == node.getNodeType() && localName.equals(node.getLocalName())){
218   
219    //Copy the node from the spring config to our list
220  748 Node copied = doc.importNode(node, true);
221  748 root.appendChild(copied);
222    }
223  2976 if(Node.ELEMENT_NODE == node.getNodeType() && (localName+"Ref").equals(node.getLocalName())){
224   
225    //Create a new spring ref element and copy the bean attribute
226  0 Element ref = doc.createElement("ref");
227  0 ref.setAttribute("bean", ((Element)node).getAttribute("bean"));
228  0 root.appendChild(ref);
229    }
230    }
231   
232  104 return root;
233    }catch(Exception e){
234  0 logger.error("Exception occured: ", e);
235    }
236  0 return null;
237    }
238   
 
239  6225 toggle private Element getFirstChildElement(Node node) {
240  12096 for(int i = 0;i<node.getChildNodes().getLength();i++){
241  6963 Node childNode = node.getChildNodes().item(i);
242  6963 if(Node.ELEMENT_NODE == childNode.getNodeType()){
243  1092 return (Element) childNode;
244    }
245    }
246   
247  5133 return null;
248    }
249   
 
250  7057 toggle private boolean isWrappedList(String localName) {
251  7057 return localName.equals("queryParams")||
252    localName.equals("resultColumns");
253    }
254   
 
255  6060 toggle @Override
256    protected String getParentName(Element element) {
257  6060 if(element.hasAttribute("parent")){
258  2632 return element.getAttribute("parent");
259    }
260  3428 return super.getParentName(element);
261    }
262   
 
263  822 toggle @Override
264    protected boolean shouldGenerateIdAsFallback() {
265  822 return true;
266    }
267    }