Clover Coverage Report - Kuali Student 1.2-M1-SNAPSHOT (Aggregated)
Coverage timestamp: Fri Mar 4 2011 04:03:38 EST
../../../../../../../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  5340 toggle @Override
51    protected Class<?> getBeanClass(Element element) {
52   
53  5340 if (element.getLocalName().equals("fieldDescriptor")) {
54  0 return FieldDescriptor.class;
55    }
56  5340 if (element.getLocalName().equals("searchCriteriaTypeInfo")) {
57  694 return SearchCriteriaTypeInfo.class;
58    }
59  4646 if (element.getLocalName().equals("queryParam")) {
60  1236 return QueryParamInfo.class;
61    }
62  3410 if (element.getLocalName().equals("searchType")) {
63  716 return SearchTypeInfo.class;
64    }
65  2694 if (element.getLocalName().equals("resultColumn")) {
66  1292 return ResultColumnInfo.class;
67    }
68  1402 if (element.getLocalName().equals("searchResultTypeInfo")) {
69  590 return SearchResultTypeInfo.class;
70    }
71   
72   
73  812 if (element.getLocalName().equals("crossSearchType")) {
74  26 return CrossSearchTypeInfo.class;
75    }
76  786 if (element.getLocalName().equals("subSearch")) {
77  52 return SubSearchInfo.class;
78    }
79  734 if (element.getLocalName().equals("subSearchParamMappings")) {
80  328 return SubSearchParamMappingInfo.class;
81    }
82  406 if (element.getLocalName().equals("joinCriteria")) {
83  26 return JoinCriteriaInfo.class;
84    }
85  380 if (element.getLocalName().equals("comparison")) {
86  8 return JoinComparisonInfo.class;
87    }
88  372 if (element.getLocalName().equals("leftHandSide")) {
89  8 return ComparisonParamInfo.class;
90    }
91  364 if (element.getLocalName().equals("rightHandSide")) {
92  8 return ComparisonParamInfo.class;
93    }
94  356 if (element.getLocalName().equals("joinResultMapping")) {
95  356 return JoinResultMappingInfo.class;
96    }
97   
98  0 return super.getBeanClass(element);
99    }
100   
 
101  5340 toggle @Override
102    protected void doParse(Element element, ParserContext pc, BeanDefinitionBuilder builder) {
103    //Set the key
104  5340 if(!"fieldDescriptor".equals(element.getLocalName())&&element.hasAttribute("id")){
105  4522 builder.addPropertyValue("key", element.getAttributes().getNamedItem("id").getTextContent());
106    }
107   
108  5340 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  5340 if("queryParam".equals(element.getLocalName())&&element.hasAttribute("optional")){
114  460 builder.addPropertyValue("optional", "true".equals(element.getAttribute("optional"))?true:false);
115    }
116   
117    //Copy Attributes
118  5340 if(element.hasAttributes()){
119  16668 for(int i = 0;i<element.getAttributes().getLength();i++){
120  11360 Attr attr = (Attr) element.getAttributes().item(i);
121  11360 if("abstract".equals(attr.getName())){
122  2202 builder.setAbstract(true);
123  9158 }else if(!"id".equals(attr.getName())&&!"parent".equals(attr.getName())){
124  2354 String fieldName = resolveFieldName(element.getLocalName(),attr.getName());
125  2354 builder.addPropertyValue(fieldName, attr.getValue());
126    }
127    }
128    }
129   
130   
131  5340 HashSet<String> visitedNodes = new HashSet<String>();
132    //Parse the children
133  21098 for(int i = 0;i<element.getChildNodes().getLength();i++){
134  15758 Node node = element.getChildNodes().item(i);
135   
136  15758 if(Node.ELEMENT_NODE == node.getNodeType()){
137  6682 String localName=node.getLocalName();
138  6682 if(!visitedNodes.contains(localName)){
139  6042 if(isWrappedList(node.getLocalName())){
140  622 List<?> refList = pc.getDelegate().parseListElement((Element) node, pc.getContainingBeanDefinition());
141  622 builder.addPropertyValue(node.getLocalName(),refList);
142  5420 }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  5316 Element childElement = getFirstChildElement(node);
152  5316 if(childElement!=null){
153  944 if("ref".equals(childElement.getLocalName())){
154  728 Object childBean = pc.getDelegate().parsePropertySubElement(childElement, pc.getContainingBeanDefinition());
155  728 builder.addPropertyValue(node.getLocalName(), childBean);
156    }else{
157  216 Object childBean = pc.getDelegate().parsePropertySubElement((Element)node, pc.getContainingBeanDefinition());
158  216 builder.addPropertyValue(node.getLocalName(), childBean);
159    }
160    }else{
161  4372 if(("ref".equals(node.getLocalName())&&"queryParam".equals(element.getLocalName()))){
162  422 Object childBean = pc.getDelegate().parsePropertySubElement((Element)node, pc.getContainingBeanDefinition());
163  422 builder.addPropertyValue("fieldDescriptor", childBean);
164  3950 }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  3916 builder.addPropertyValue(node.getLocalName(), node.getTextContent());
171    }
172    }
173    }
174    }
175    }
176    }
177    }
178   
 
179  5420 toggle private boolean isUnwrappedList(String localName) {
180  5420 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  2458 toggle private String resolveFieldName(String parentName, String nodeName) {
188  2458 if("comparison".equals(nodeName)){
189  8 return "comparisons";
190    }
191  2450 if("subSearch".equals(nodeName)){
192  26 return "subSearches";
193    }
194  2424 if("joinResultMapping".equals(nodeName)){
195  26 return "joinResultMappings";
196    }
197  2398 if("joinCriteria".equals(parentName)&&"type".equals(nodeName)){
198  26 return "joinType";
199    }
200   
201  2372 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  3064 for(int i = 0;i<element.getChildNodes().getLength();i++){
216  2960 Node node = element.getChildNodes().item(i);
217  2960 if(Node.ELEMENT_NODE == node.getNodeType() && localName.equals(node.getLocalName())){
218   
219    //Copy the node from the spring config to our list
220  744 Node copied = doc.importNode(node, true);
221  744 root.appendChild(copied);
222    }
223  2960 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  5316 toggle private Element getFirstChildElement(Node node) {
240  10304 for(int i = 0;i<node.getChildNodes().getLength();i++){
241  5932 Node childNode = node.getChildNodes().item(i);
242  5932 if(Node.ELEMENT_NODE == childNode.getNodeType()){
243  944 return (Element) childNode;
244    }
245    }
246   
247  4372 return null;
248    }
249   
 
250  6042 toggle private boolean isWrappedList(String localName) {
251  6042 return localName.equals("queryParams")||
252    localName.equals("resultColumns");
253    }
254   
 
255  5340 toggle @Override
256    protected String getParentName(Element element) {
257  5340 if(element.hasAttribute("parent")){
258  2282 return element.getAttribute("parent");
259    }
260  3058 return super.getParentName(element);
261    }
262   
 
263  818 toggle @Override
264    protected boolean shouldGenerateIdAsFallback() {
265  818 return true;
266    }
267    }