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.rice.kns.util;
17  
18  import java.beans.PropertyDescriptor;
19  
20  import org.apache.commons.beanutils.PropertyUtils;
21  import org.kuali.rice.kns.bo.BusinessObject;
22  
23  public class BeanDDCreator {
24  
25      /**
26       * This method...
27       * @param args
28       */
29      public static void main(String[] args) throws Exception {
30          String className = args[0];
31          Class<? extends BusinessObject> boClass = (Class<? extends BusinessObject>)Class.forName( className );
32          PropertyDescriptor[] props = PropertyUtils.getPropertyDescriptors( boClass );
33          
34          StringBuffer sb = new StringBuffer( 4000 );
35          sb.append( "<beans xmlns=\"http://www.springframework.org/schema/beans\"\r\n" + 
36          		"    xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\r\n" + 
37          		"    xmlns:p=\"http://www.springframework.org/schema/p\"\r\n" + 
38          		"    xsi:schemaLocation=\"http://www.springframework.org/schema/beans\r\n" + 
39          		"        http://www.springframework.org/schema/beans/spring-beans-2.0.xsd\">\r\n" + 
40          		"\r\n" + 
41          		"  <bean id=\"" );
42          sb.append( boClass.getSimpleName() );
43          sb.append( "\" parent=\"" );
44          sb.append( boClass.getSimpleName() );
45          sb.append( "-parentBean\" />\r\n" + 
46          		"\r\n" + 
47          		"  <bean id=\"" );
48          sb.append( boClass.getSimpleName() );
49          sb.append( "-parentBean\" abstract=\"true\" parent=\"BusinessObjectEntry\"\r\n" + 
50          		"        p:businessObjectClass=\"" );
51          sb.append( boClass.getName() );
52          sb.append( "\"\r\n" );
53          sb.append( "        p:titleAttribute=\"" ); 
54          sb.append( "FILL ME IN" );
55          sb.append( "\"\r\n" );
56          sb.append( "        p:objectLabel=\"" ); 
57          sb.append(  camelCaseToString( boClass.getSimpleName() ) );
58          sb.append( "\"\r\n" );
59          sb.append( "        p:inquiryDefinition-ref=\"" ); 
60          sb.append( boClass.getSimpleName() );
61          sb.append( "-inquiryDefinition\"\r\n" );
62          sb.append( "        p:lookupDefinition-ref=\"" ); 
63          sb.append( boClass.getSimpleName() );
64          sb.append( "-lookupDefinition\" >\r\n" );
65          sb.append( "    <property name=\"attributes\" >\r\n" + 
66          		"      <list>\r\n" );
67          for ( PropertyDescriptor p : props ) {
68              if ( isNormalProperty(p) ) {
69                  sb.append( "        <ref bean=\"" ).append( boClass.getSimpleName() ).append( '-' );
70                  sb.append( p.getName() );
71                  sb.append( "\" />\r\n" );
72              }
73          }
74          
75          sb.append( "      </list>\r\n" + 
76          		"    </property>\r\n" +
77          		"  </bean>\r\n" +
78          		"\r\n" );
79          for ( PropertyDescriptor p : props ) {
80              if ( isNormalProperty(p) ) {
81                  
82                  if ( p.getName().equals( "versionNumber" ) ) {
83                      sb.append( getSimpleParentBeanReference( boClass, p.getName() ) );
84                      sb.append( getSimpleAbstractInheritanceBean(boClass, p.getName(), "GenericAttributes-versionNumber" ) );
85                      
86                  } else if ( p.getName().endsWith("chartOfAccountsCode" ) ) {
87                      sb.append( getSimpleParentBeanReference( boClass, p.getName() ) );
88                      sb.append( getSimpleAbstractInheritanceBean(boClass, p.getName(), "Chart-chartOfAccountsCode" ) );
89  
90                  } else if ( p.getName().endsWith("organizationCode" ) ) {
91                      sb.append( getSimpleParentBeanReference( boClass, p.getName() ) );
92                      sb.append( getSimpleAbstractInheritanceBean(boClass, p.getName(), "Org-organizationCode" ) );
93  
94                  } else if ( p.getName().endsWith("accountNumber" ) ) {
95                      sb.append( getSimpleParentBeanReference( boClass, p.getName() ) );
96                      sb.append( getSimpleAbstractInheritanceBean(boClass, p.getName(), "Account-accountNumber" ) );
97  
98                  } else if ( p.getName().equals("active" ) ) {
99                      sb.append( getSimpleParentBeanReference( boClass, p.getName() ) );
100                     sb.append( getSimpleAbstractInheritanceBean(boClass, p.getName(), "GenericAttributes-activeIndicator" ) );
101 
102                 } else if ( p.getName().equals("codeAndDescription" ) ) {
103                     sb.append( getSimpleParentBeanReference( boClass, p.getName() ) );
104                     sb.append( getSimpleAbstractInheritanceBeanWithLabel(boClass, p.getName(), "CommonField-CodeAndDescription", camelCaseToString(boClass.getSimpleName()) ) );
105 
106                 } else if ( p.getPropertyType() == Boolean.TYPE ) {
107                     sb.append( getSimpleParentBeanReference( boClass, p.getName() ) );
108                     sb.append( getSimpleAbstractInheritanceBean(boClass, p.getName(), "GenericAttributes-genericBoolean" ) );
109 
110                 } else {
111                     // attribute bean
112                     sb.append( getSimpleParentBeanReference( boClass, p.getName() ) );
113                     // attribute parent bean
114                     sb.append( "  <bean id=\"" ).append( boClass.getSimpleName() ).append( '-' );
115                     sb.append( p.getName() ).append( "-parentBean\" parent=\"AttributeDefinition\" abstract=\"true\"\r\n" );
116                     sb.append( "        p:name=\"" ).append( p.getName() ).append( "\"\r\n" );
117                     sb.append( "        p:forceUppercase=\"false\"\r\n" );
118                     sb.append( "        p:label=\"" ).append( camelCaseToString(p.getName()) ).append( "\"\r\n" );
119                     sb.append( "        p:shortLabel=\"" ).append( camelCaseToString(p.getName()) ).append( "\"\r\n" );
120                     sb.append( "        p:maxLength=\"10\"\r\n" );
121                     sb.append( "        p:required=\"false\" >\r\n" );
122                     sb.append( "    <property name=\"validationPattern\" >\r\n" + 
123                             "      <bean parent=\"AnyCharacterValidationPattern\"\r\n" + 
124                             "            p:allowWhitespace=\"true\" />\r\n" + 
125                             "    </property>\r\n" + 
126                             "    <property name=\"control\" >\r\n" + 
127                             "      <bean parent=\"TextControlDefinition\"\r\n" + 
128                             "            p:size=\"10\" />\r\n" + 
129                             "    </property>\r\n" + 
130                             "  </bean>\r\n" );
131                     
132                 }
133                 sb.append( "\r\n" );
134             }
135         }
136         // inquiry definition
137         
138         sb.append( getSimpleParentBeanReference( boClass, "inquiryDefinition" ) );
139         sb.append( "\r\n" );
140         sb.append( "  <bean id=\"" );
141         sb.append( boClass.getSimpleName() );
142         sb.append( "-inquiryDefinition-parentBean\" abstract=\"true\" parent=\"InquiryDefinition\"\r\n" + 
143         		"        p:title=\"" );
144         sb.append( camelCaseToString(boClass.getSimpleName() ) );
145         sb.append( " Inquiry\" >\r\n" + 
146         		"    <property name=\"inquirySections\" >\r\n" + 
147         		"      <list>\r\n" + 
148         		"        <bean parent=\"InquirySectionDefinition\"\r\n" + 
149         		"              p:title=\"" );
150         sb.append( camelCaseToString(boClass.getSimpleName() ) );
151         sb.append( " Attributes\"\r\n" + 
152         		"              p:numberOfColumns=\"1\" >\r\n" + 
153         		"          <property name=\"inquiryFields\" >\r\n" + 
154         		"            <list>\r\n" );
155         for ( PropertyDescriptor p : props ) {
156             if ( isNormalProperty(p) ) {
157                 sb.append("              <bean parent=\"FieldDefinition\" p:attributeName=\"" );
158                 sb.append( p.getName() ).append( "\" />\r\n" );
159             }
160         }
161         sb.append( "            </list>\r\n" + 
162         		"          </property>\r\n" + 
163         		"        </bean>\r\n" + 
164         		"      </list>\r\n" + 
165         		"    </property>\r\n" + 
166         		"  </bean>\r\n" + 
167         		"\r\n" );
168 
169         sb.append( getSimpleParentBeanReference( boClass, "lookupDefinition" ) );
170         sb.append( "\r\n" );
171         sb.append( "  <bean id=\"" );
172         sb.append( boClass.getSimpleName() );
173         sb.append( "-lookupDefinition-parentBean\" abstract=\"true\" parent=\"LookupDefinition\"\r\n" + 
174                 "        p:title=\"" );
175         sb.append( camelCaseToString(boClass.getSimpleName() ) );
176         sb.append( " Lookup\" \r\n" );
177         //sb.append( "        p:instructions=\"Lookup a " );
178         //sb.append( camelCaseToString(boClass.getSimpleName() ) );
179         sb.append( "\" >\r\n" );
180         sb.append( "    <property name=\"defaultSort\" >\r\n" + 
181         		"      <bean parent=\"SortDefinition\">\r\n" + 
182         		"        <property name=\"attributeNames\" >\r\n" + 
183         		"          <list>\r\n" + 
184         		"            <value>FILL ME IN</value>\r\n" + 
185         		"          </list>\r\n" + 
186         		"        </property>\r\n" + 
187         		"        <property name=\"sortAscending\" value=\"true\" />\r\n" + 
188         		"      </bean>\r\n" + 
189         		"    </property>\r\n" + 
190         		"    <property name=\"lookupFields\" >\r\n" + 
191         		"      <list>\r\n" );
192         for ( PropertyDescriptor p : props ) {
193             if ( isNormalProperty(p) ) {
194                 sb.append("        <bean parent=\"FieldDefinition\" p:attributeName=\"" );
195                 sb.append( p.getName() ).append( "\" />\r\n" );
196             }
197         }
198         sb.append( "      </list>\r\n" + 
199         		"    </property>\r\n" + 
200         		"    <property name=\"resultFields\" >\r\n" + 
201         		"      <list>\r\n" );
202         for ( PropertyDescriptor p : props ) {
203             if ( isNormalProperty(p) ) {
204                 sb.append("        <bean parent=\"FieldDefinition\" p:attributeName=\"" );
205                 sb.append( p.getName() ).append( "\" />\r\n" );
206             }
207         }
208         sb.append( "      </list>\r\n" + 
209         		"    </property>\r\n" + 
210         		"  </bean>\r\n" );
211         sb.append( "\r\n</beans>" );
212         System.out.println( sb.toString() );
213     }
214 
215     public static String getSimpleAbstractInheritanceBean( Class<? extends BusinessObject> boClass, String propertyName, String parentBean ) {
216         StringBuffer sb = new StringBuffer( 100 );
217         sb.append( "  <bean id=\"" );
218         sb.append( boClass.getSimpleName() );                    
219         sb.append( "-" ).append( propertyName ).append( "-parentBean\" abstract=\"true\" parent=\"" );
220         sb.append( parentBean ).append( "\" \r\n" );        
221         sb.append( "        p:name=\"" ).append( propertyName ).append( "\"\r\n" );
222         sb.append( "        p:required=\"false\" />\r\n" );
223         return sb.toString();
224     }
225     
226     public static String getSimpleAbstractInheritanceBeanWithLabel( Class<? extends BusinessObject> boClass, String propertyName, String parentBean, String label ) {
227         StringBuffer sb = new StringBuffer( 100 );
228         sb.append( "  <bean id=\"" );
229         sb.append( boClass.getSimpleName() );                    
230         sb.append( "-" ).append( propertyName ).append( "-parentBean\" abstract=\"true\" parent=\"" );
231         sb.append( parentBean ).append( "\" \r\n" );        
232         sb.append( "        p:name=\"" ).append( propertyName ).append( "\"\r\n" );
233         sb.append( "        p:label=\"" ).append( label ).append( "\"\r\n" );
234         sb.append( "        p:required=\"false\" />\r\n" );
235         return sb.toString();
236     }
237     
238     public static String getSimpleParentBeanReference( Class<? extends BusinessObject> boClass, String propertyName ) {
239         StringBuffer sb = new StringBuffer( 100 );
240         sb.append( "  <bean id=\"" );
241         sb.append( boClass.getSimpleName() );                    
242         sb.append( "-" ).append( propertyName ).append( "\" parent=\"" );
243         sb.append( boClass.getSimpleName() );                    
244         sb.append( "-" ).append( propertyName ).append( "-parentBean\" />\r\n" );
245         return sb.toString();
246     }
247     
248     public static boolean isNormalProperty( PropertyDescriptor p ) {
249         return p.getPropertyType()!= null 
250                 && !BusinessObject.class.isAssignableFrom( p.getPropertyType() )
251                 && !p.getName().equals( "objectId" )
252                 && !p.getName().equals( "class" )
253                 && !p.getName().startsWith( "boNote" )
254                 && !p.getName().startsWith( "autoIncrementSet" )
255                 && !p.getName().equals( "newCollectionRecord" );
256     }
257     
258     public static String camelCaseToString( String className ) {
259         StringBuffer newName = new StringBuffer( className );
260         // upper case the 1st letter
261         newName.replace(0, 1, newName.substring(0, 1).toUpperCase());
262         // loop through, inserting spaces when cap
263         for ( int i = 0; i < newName.length(); i++ ) {
264             if ( Character.isUpperCase(newName.charAt(i)) ) {
265                 newName.insert(i, ' ');
266                 i++;
267             }
268         }
269 
270         return newName.toString().trim().replace( "Uc", "UC" );
271     }
272 }