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