001    /**
002     * Copyright 2005-2012 The Kuali Foundation
003     *
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.opensource.org/licenses/ecl2.php
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.kuali.rice.devtools.generators.dd;
017    
018    import org.apache.commons.beanutils.PropertyUtils;
019    import org.kuali.rice.krad.bo.BusinessObject;
020    
021    import java.beans.PropertyDescriptor;
022    
023    /**
024     * @deprecated this is for the legacy kns.
025     */
026    @Deprecated
027    public class BeanDDCreator {
028    
029        /**
030         * This method...
031         * @param args
032         */
033        public static void main(String[] args) throws Exception {
034            String className = args[0];
035            Class<? extends BusinessObject> boClass = (Class<? extends BusinessObject>)Class.forName( className );
036            PropertyDescriptor[] props = PropertyUtils.getPropertyDescriptors( boClass );
037            
038            StringBuffer sb = new StringBuffer( 4000 );
039            sb.append( "<beans xmlns=\"http://www.springframework.org/schema/beans\"\r\n" + 
040                            "    xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\r\n" + 
041                            "    xmlns:p=\"http://www.springframework.org/schema/p\"\r\n" + 
042                            "    xsi:schemaLocation=\"http://www.springframework.org/schema/beans\r\n" + 
043                            "        http://www.springframework.org/schema/beans/spring-beans-2.0.xsd\">\r\n" + 
044                            "\r\n" + 
045                            "  <bean id=\"" );
046            sb.append( boClass.getSimpleName() );
047            sb.append( "\" parent=\"" );
048            sb.append( boClass.getSimpleName() );
049            sb.append( "-parentBean\" />\r\n" + 
050                            "\r\n" + 
051                            "  <bean id=\"" );
052            sb.append( boClass.getSimpleName() );
053            sb.append( "-parentBean\" abstract=\"true\" parent=\"BusinessObjectEntry\"\r\n" + 
054                            "        p:businessObjectClass=\"" );
055            sb.append( boClass.getName() );
056            sb.append( "\"\r\n" );
057            sb.append( "        p:titleAttribute=\"" ); 
058            sb.append( "FILL ME IN" );
059            sb.append( "\"\r\n" );
060            sb.append( "        p:objectLabel=\"" ); 
061            sb.append(  camelCaseToString( boClass.getSimpleName() ) );
062            sb.append( "\"\r\n" );
063            sb.append( "        p:inquiryDefinition-ref=\"" ); 
064            sb.append( boClass.getSimpleName() );
065            sb.append( "-inquiryDefinition\"\r\n" );
066            sb.append( "        p:lookupDefinition-ref=\"" ); 
067            sb.append( boClass.getSimpleName() );
068            sb.append( "-lookupDefinition\" >\r\n" );
069            sb.append( "    <property name=\"attributes\" >\r\n" + 
070                            "      <list>\r\n" );
071            for ( PropertyDescriptor p : props ) {
072                if ( isNormalProperty(p) ) {
073                    sb.append( "        <ref bean=\"" ).append( boClass.getSimpleName() ).append( '-' );
074                    sb.append( p.getName() );
075                    sb.append( "\" />\r\n" );
076                }
077            }
078            
079            sb.append( "      </list>\r\n" + 
080                            "    </property>\r\n" +
081                            "  </bean>\r\n" +
082                            "\r\n" );
083            for ( PropertyDescriptor p : props ) {
084                if ( isNormalProperty(p) ) {
085                    
086                    if ( p.getName().equals( "versionNumber" ) ) {
087                        sb.append( getSimpleParentBeanReference( boClass, p.getName() ) );
088                        sb.append( getSimpleAbstractInheritanceBean(boClass, p.getName(), "GenericAttributes-versionNumber" ) );
089                        
090                    } else if ( p.getName().endsWith("chartOfAccountsCode" ) ) {
091                        sb.append( getSimpleParentBeanReference( boClass, p.getName() ) );
092                        sb.append( getSimpleAbstractInheritanceBean(boClass, p.getName(), "Chart-chartOfAccountsCode" ) );
093    
094                    } else if ( p.getName().endsWith("organizationCode" ) ) {
095                        sb.append( getSimpleParentBeanReference( boClass, p.getName() ) );
096                        sb.append( getSimpleAbstractInheritanceBean(boClass, p.getName(), "Org-organizationCode" ) );
097    
098                    } else if ( p.getName().endsWith("accountNumber" ) ) {
099                        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    }