View Javadoc
1   /*
2    * Copyright 2008 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.ole.sys.spring.datadictionary;
17  
18  import org.apache.log4j.Logger;
19  import org.kuali.ole.sys.OLEPropertyConstants;
20  import org.springframework.beans.factory.config.BeanDefinition;
21  import org.springframework.beans.factory.support.BeanDefinitionBuilder;
22  import org.springframework.beans.factory.xml.ParserContext;
23  import org.springframework.util.StringUtils;
24  import org.w3c.dom.Element;
25  import org.w3c.dom.Node;
26  import org.w3c.dom.NodeList;
27  
28  public class AttributeBeanDefinitionParser extends KualiBeanDefinitionParserBase {
29  
30      private static Logger LOG = Logger.getLogger(AttributeBeanDefinitionParser.class);
31      
32      
33      @Override
34      protected String getBaseBeanTypeParent(Element element) {
35          return "AttributeDefinition";
36      }    
37      
38      
39      @Override
40      protected void doParse(Element element, ParserContext context, BeanDefinitionBuilder bean) {
41          // get all attributes
42          handleAbstractAttribute( element, bean );
43          processAttributeAttributes(element, bean);
44          // parse inner tags
45          NodeList children = element.getChildNodes();
46          processChildNodes( children, bean );
47          parseEmbeddedPropertyElements(element, bean);
48      }
49      
50      protected void processAttributeAttributes( Element element, BeanDefinitionBuilder bean ) {
51          String attributeName = element.getAttribute("attributeName");
52          String label = element.getAttribute("label");
53          String shortLabel = element.getAttribute("shortLabel");
54          String required = element.getAttribute("required");
55          String forceUppercase = element.getAttribute("forceUppercase");
56          String maxLength = element.getAttribute("maxLength");
57          String displayLabelAttribute = element.getAttribute("displayLabelAttribute");
58          String formatterClass = element.getAttribute("formatterClass");
59  
60          if ( StringUtils.hasText( attributeName ) ) {
61              bean.addPropertyValue("name", attributeName);
62          }
63          if ( StringUtils.hasText( label ) ) {
64              bean.addPropertyValue("label", label);
65          }
66          if ( StringUtils.hasText( shortLabel ) ) {
67              bean.addPropertyValue("shortLabel", shortLabel);
68          }
69          if ( StringUtils.hasText( displayLabelAttribute ) ) {
70              bean.addPropertyValue("displayLabelAttribute", displayLabelAttribute);
71          }
72          if ( StringUtils.hasText( formatterClass ) ) {
73              bean.addPropertyValue("formatterClass", formatterClass);
74          }
75          if ( StringUtils.hasText( required ) ) {
76              bean.addPropertyValue("required", Boolean.valueOf(required));
77          }
78          if ( StringUtils.hasText( forceUppercase ) ) {
79              bean.addPropertyValue("forceUppercase", Boolean.valueOf(forceUppercase));
80          }
81          if ( StringUtils.hasText( maxLength ) ) {
82              bean.addPropertyValue("maxLength", Integer.valueOf(maxLength));
83          }
84      }
85  
86      protected void processChildNodes( NodeList children, BeanDefinitionBuilder bean ) {
87          for ( int i = 0; i < children.getLength(); i++ ) {
88              Node child = children.item(i);
89              if ( child.getNodeType() != Node.ELEMENT_NODE ) continue;
90              Element ele = (Element)child;
91              String elementName = ele.getLocalName();
92              if ( elementName == null ) continue;
93              if ( elementName.equals( "text" ) ) {
94                  bean.addPropertyValue("control", processTextControlElement(ele) );
95              } else if ( elementName.equals( "textarea" ) ) {
96                  bean.addPropertyValue("control", processTextareaControlElement(ele) );
97              } else if ( elementName.equals( "select" ) ) {
98                  bean.addPropertyValue("control", processSelectControlElement(ele) );
99              } else if ( elementName.equals( "radio" ) ) {
100                 bean.addPropertyValue("control", processRadioControlElement(ele) );
101             } else if ( elementName.equals( "hidden" ) ) {
102                 bean.addPropertyValue("control", processHiddenControlElement(ele) );
103             } else if ( elementName.equals( "user" ) ) {
104                 bean.addPropertyValue("control", processUserControlElement(ele) );
105             } else if ( elementName.equals( "checkbox" ) ) {
106                 bean.addPropertyValue("control", processCheckboxControlElement(ele) );
107             } else if ( elementName.equals( "validationPattern") ) {
108                 bean.addPropertyValue("validationPattern", processValidationPatternElement(ele) );
109             }
110         }
111     }
112     
113     protected BeanDefinition processTextControlElement( Element ele ) {
114         BeanDefinitionBuilder controlBean = BeanDefinitionBuilder.childBeanDefinition( "TextControlDefinition" );
115         
116         String size = ele.getAttribute("size");
117         if ( StringUtils.hasText(size) ) {
118             controlBean.addPropertyValue("size", Integer.valueOf(size) );
119         }
120         parseEmbeddedPropertyElements(ele, controlBean);
121         
122         return controlBean.getBeanDefinition();
123     }
124 
125     protected BeanDefinition processUserControlElement( Element ele ) {
126         BeanDefinitionBuilder controlBean = BeanDefinitionBuilder.childBeanDefinition( "KualiUserControlDefinition" );
127         
128         String universalIdAttribute = ele.getAttribute("universalIdAttribute");
129         if ( StringUtils.hasText(universalIdAttribute) ) {
130             controlBean.addPropertyValue("universalIdAttributeName", universalIdAttribute );
131         }
132         String userObjectAttribute = ele.getAttribute("userObjectAttribute");
133         if ( StringUtils.hasText(userObjectAttribute) ) {
134             controlBean.addPropertyValue("userIdAttributeName", userObjectAttribute + "." + OLEPropertyConstants.PERSON_USER_ID );
135             controlBean.addPropertyValue("personNameAttributeName", userObjectAttribute + "." + OLEPropertyConstants.PERSON_NAME );
136         }
137         parseEmbeddedPropertyElements(ele, controlBean);
138         
139         return controlBean.getBeanDefinition();
140     }
141 
142     protected BeanDefinition processTextareaControlElement( Element ele ) {
143         BeanDefinitionBuilder controlBean = BeanDefinitionBuilder.childBeanDefinition( "TextareaControlDefinition" );
144         
145         String rows = ele.getAttribute("rows");
146         if ( StringUtils.hasText(rows) ) {
147             controlBean.addPropertyValue("rows", Integer.valueOf(rows) );
148         }
149         String cols = ele.getAttribute("rows");
150         if ( StringUtils.hasText(cols) ) {
151             controlBean.addPropertyValue("cols", Integer.valueOf(cols) );
152         }
153         parseEmbeddedPropertyElements(ele, controlBean);
154         
155         return controlBean.getBeanDefinition();
156     }
157 
158     protected BeanDefinition processHiddenControlElement( Element ele ) {
159         BeanDefinitionBuilder controlBean = BeanDefinitionBuilder.childBeanDefinition( "HiddenControlDefinition" );
160         parseEmbeddedPropertyElements(ele, controlBean);
161         return controlBean.getBeanDefinition();
162     }
163 
164     protected BeanDefinition processCheckboxControlElement( Element ele ) {
165         BeanDefinitionBuilder controlBean = BeanDefinitionBuilder.childBeanDefinition( "CheckboxControlDefinition" );
166         parseEmbeddedPropertyElements(ele, controlBean);
167         return controlBean.getBeanDefinition();
168     }
169     
170     protected void setMultiValueControlAttributes( Element ele, BeanDefinitionBuilder controlBean ) {
171         String valuesFinderClass = ele.getAttribute("valuesFinderClass");
172         if ( StringUtils.hasText(valuesFinderClass) ) {
173             controlBean.addPropertyValue("valuesFinderClass", valuesFinderClass );
174         }
175         String boClass = ele.getAttribute("boClass");
176         if ( StringUtils.hasText(boClass) ) {
177             controlBean.addPropertyValue("boClass", boClass );
178         }
179         String keyAttribute = ele.getAttribute("keyAttribute");
180         if ( StringUtils.hasText(keyAttribute) ) {
181             controlBean.addPropertyValue("keyAttribute", keyAttribute );
182         }
183         String labelAttribute = ele.getAttribute("labelAttribute");
184         if ( StringUtils.hasText(labelAttribute) ) {
185             controlBean.addPropertyValue("labelAttribute", labelAttribute );
186         }
187         String includeKeyInLabel = ele.getAttribute("includeKeyInLabel");
188         if ( StringUtils.hasText(includeKeyInLabel) ) {
189             controlBean.addPropertyValue("includeKeyInLabel", Boolean.valueOf(includeKeyInLabel) );
190         }
191     }
192     
193     protected BeanDefinition processSelectControlElement( Element ele ) {
194         BeanDefinitionBuilder controlBean = BeanDefinitionBuilder.childBeanDefinition( "SelectControlDefinition" );
195         
196         setMultiValueControlAttributes( ele, controlBean );
197         parseEmbeddedPropertyElements(ele, controlBean);
198                   
199         return controlBean.getBeanDefinition();
200     }
201 
202     protected BeanDefinition processRadioControlElement( Element ele ) {
203         BeanDefinitionBuilder controlBean = BeanDefinitionBuilder.childBeanDefinition( "RadioControlDefinition" );
204 
205         setMultiValueControlAttributes( ele, controlBean );
206         parseEmbeddedPropertyElements(ele, controlBean);
207         
208         return controlBean.getBeanDefinition();
209     }
210 
211     protected BeanDefinition processValidationPatternElement( Element ele ) {
212         BeanDefinitionBuilder validatorBean = null;
213         String parent = ele.getAttribute( "parent" );
214         String validationPatternClass = ele.getAttribute( "validationPatternClass" );
215         if ( StringUtils.hasText(parent) ) {
216             validatorBean = BeanDefinitionBuilder.childBeanDefinition( parent );
217         } else if ( StringUtils.hasText(validationPatternClass)) {
218             try {
219                 validatorBean = BeanDefinitionBuilder.rootBeanDefinition(Class.forName(validationPatternClass));
220             } catch ( ClassNotFoundException ex ) {
221                 LOG.fatal( "Invalid class name given for validationPattern bean: " + validationPatternClass );
222                 throw new RuntimeException( "Invalid class name given for validationPattern bean: " + validationPatternClass, ex );
223             }
224         }
225         
226         parseEmbeddedPropertyElements(ele, validatorBean);
227                    
228         return validatorBean.getBeanDefinition();
229     }
230 }