Coverage Report - org.kuali.rice.devtools.generators.jpa.JpaToDdl
 
Classes in this File Line Coverage Branch Coverage Complexity
JpaToDdl
0%
0/92
0%
0/68
8.8
 
 1  
 /*
 2  
  * Copyright 2007-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.devtools.generators.jpa;
 17  
 
 18  
 import org.kuali.rice.krad.bo.PersistableBusinessObject;
 19  
 import org.kuali.rice.krad.bo.PersistableBusinessObjectBase;
 20  
 
 21  
 import javax.persistence.AttributeOverride;
 22  
 import javax.persistence.AttributeOverrides;
 23  
 import javax.persistence.Column;
 24  
 import javax.persistence.Id;
 25  
 import javax.persistence.JoinColumn;
 26  
 import javax.persistence.JoinColumns;
 27  
 import javax.persistence.OneToMany;
 28  
 import javax.persistence.Table;
 29  
 import java.lang.reflect.Field;
 30  
 import java.util.ArrayList;
 31  
 import java.util.HashMap;
 32  
 import java.util.List;
 33  
 import java.util.Map;
 34  
 
 35  
 /**
 36  
  * This is a description of what this class does - kellerj don't forget to fill this in. 
 37  
  * 
 38  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 39  
  *
 40  
  */
 41  0
 public class JpaToDdl {
 42  
 
 43  
         public static void main( String[] args ) throws ClassNotFoundException {
 44  
                 
 45  0
                 Class<? extends PersistableBusinessObjectBase> clazz =
 46  
                 (Class<? extends PersistableBusinessObjectBase>) Class.forName(args[0]);
 47  
 
 48  
                 
 49  0
                 StringBuffer sb = new StringBuffer( 1000 );
 50  0
                 StringBuffer pk = new  StringBuffer();
 51  0
                 Table tableAnnotation = (Table)clazz.getAnnotation( Table.class );
 52  
                 
 53  0
                 sb.append( "CREATE TABLE " ).append( tableAnnotation.name().toLowerCase() ).append( " (\r\n" );
 54  
 
 55  0
                 getClassFields( tableAnnotation.name().toLowerCase(), clazz, sb, pk, null );
 56  0
                 pk.append( " )\r\n" );
 57  0
                 sb.append( pk );
 58  0
                 sb.append( ")\r\n" );
 59  0
                 sb.append( "/\r\n" );
 60  0
                 System.out.println( sb.toString() );
 61  0
                 sb.setLength( 0 );
 62  0
                 getReferences( clazz, sb );
 63  0
                 System.out.println( sb.toString() );
 64  0
         }
 65  
 
 66  
         
 67  
         private static String javaToSqlDataType( Class<? extends Object> dataType ) {
 68  0
                 if ( dataType.equals( String.class ) ) {
 69  0
                         return "VARCHAR(40)";
 70  0
                 } else if (dataType.equals(Long.class) || dataType.equals(Integer.class)) {
 71  0
                         return "NUMBER(?,?)";
 72  0
                 } else if (dataType.equals(java.util.Date.class) || dataType.equals(java.sql.Date.class)) {
 73  0
                         return "DATE";
 74  0
                 } else if (dataType.equals(java.sql.Timestamp.class)) {
 75  0
                         return "TIMESTAMP";
 76  
                 }
 77  0
                 return "VARCHAR(40)";
 78  
         }
 79  
         
 80  
         private static void getClassFields( String tableName, Class<? extends Object> clazz, StringBuffer sb, StringBuffer pk, Map<String,AttributeOverride> overrides ) {
 81  
                 // first get annotation overrides
 82  0
                 if ( overrides == null ) {
 83  0
                         overrides = new HashMap<String,AttributeOverride>();
 84  
                 }
 85  0
                 if ( clazz.getAnnotation( AttributeOverride.class ) != null ) {
 86  0
                         AttributeOverride ao = (AttributeOverride)clazz.getAnnotation( AttributeOverride.class );
 87  0
                         if ( !overrides.containsKey(ao.name() ) ) {
 88  0
                                 overrides.put(ao.name(), ao);
 89  
                         }
 90  
                 }
 91  0
                 if ( clazz.getAnnotation( AttributeOverrides.class ) != null ) {
 92  0
                         for ( AttributeOverride ao : ((AttributeOverrides)clazz.getAnnotation( AttributeOverrides.class )).value() ) {
 93  0
                                 if ( !overrides.containsKey(ao.name() ) ) {
 94  0
                                         overrides.put(ao.name(), ao);
 95  
                                 }
 96  0
                                 overrides.put(ao.name(),ao);
 97  
                         }
 98  
                 }
 99  0
                 for ( Field field : clazz.getDeclaredFields() ) {
 100  0
                         Id id = (Id)field.getAnnotation( Id.class );
 101  0
                         Column column = (Column)field.getAnnotation( Column.class );
 102  0
                         if ( column != null ) {
 103  0
                                 sb.append( "\t" );
 104  0
                                 if ( field.getName().equals( "objectId" ) ) {
 105  0
                                         sb.append( "obj_id\t\tVARCHAR2(36) NOT NULL" );
 106  0
                                 } else if ( field.getName().equals( "versionNumber" ) ) {
 107  0
                                         sb.append( "ver_nbr\t\tNUMBER(8,0)" );
 108  
                                 } else {
 109  0
                                         if ( overrides.containsKey(field.getName() ) ) {
 110  0
                                                 sb.append( overrides.get(field.getName()).column().name().toLowerCase() );
 111  
                                         } else {
 112  0
                                                 sb.append( column.name().toLowerCase() );
 113  
                                         }
 114  0
                                         sb.append( "\t\t" );
 115  0
                                         if ( field.getType() == boolean.class ) {
 116  0
                                                 sb.append( "VARCHAR2(1) DEFAULT 'Y'" );
 117  
                                         } else {
 118  0
                                                 sb.append( javaToSqlDataType( field.getType() ) );
 119  
                                         }
 120  0
                                         if ( id != null ) {
 121  0
                                                 if ( pk.length() == 0 ) {
 122  0
                                                         pk.append( "\tCONSTRAINT " + tableName + "p1 PRIMARY KEY ( " );
 123  
                                                 } else {
 124  0
                                                         pk.append( ", " );
 125  
                                                 }
 126  0
                                                 if ( overrides.containsKey(field.getName() ) ) {
 127  0
                                                         pk.append( overrides.get(field.getName()).column().name().toLowerCase() );
 128  
                                                 } else {
 129  0
                                                         pk.append( column.name().toLowerCase() );
 130  
                                                 }
 131  
                                         }
 132  
                                 }
 133  0
                                 sb.append( ",\r\n" );
 134  
                         }
 135  
                 }
 136  0
                 if ( !clazz.equals( PersistableBusinessObject.class ) && clazz.getSuperclass() != null ) {
 137  0
                         getClassFields( tableName, clazz.getSuperclass(), sb, pk, overrides );
 138  
                 }
 139  0
         }
 140  
 
 141  
         private static void getReferences( Class<? extends Object> clazz, StringBuffer sb ) {
 142  0
                 for ( Field field : clazz.getDeclaredFields() ) {
 143  0
                         JoinColumns multiKey = (JoinColumns)field.getAnnotation( JoinColumns.class );
 144  0
                         JoinColumn singleKey = (JoinColumn)field.getAnnotation( JoinColumn.class );
 145  0
                         if ( multiKey != null || singleKey != null ) {
 146  0
                                 List<JoinColumn> keys = new ArrayList<JoinColumn>();
 147  0
                                 if ( singleKey != null ) {
 148  0
                                         keys.add( singleKey );
 149  
                                 }
 150  0
                                 if ( multiKey != null ) {
 151  0
                                         for ( JoinColumn col : multiKey.value() ) {
 152  0
                                                 keys.add( col );
 153  
                                         }
 154  
                                 }
 155  0
                                 OneToMany oneToMany = field.getAnnotation( OneToMany.class );
 156  0
                                 if ( oneToMany != null ) {
 157  0
                                         sb.append( "ALTER TABLE " );
 158  0
                                         sb.append( field.getName() );
 159  0
                                         sb.append( "\" element-class-ref=\"" );
 160  0
                                         sb.append( oneToMany.targetEntity().getName() );
 161  0
                                         sb.append( "\" collection-class=\"org.apache.ojb.broker.util.collections.ManageableArrayList\" auto-retrieve=\"true\" auto-update=\"object\" auto-delete=\"object\" proxy=\"true\">\r\n" );
 162  0
                                         for ( JoinColumn col : keys ) {
 163  0
                                                 sb.append( "                        <inverse-foreignkey field-ref=\"" );
 164  0
                                                 sb.append( getPropertyFromField( clazz, col.name() ) );
 165  0
                                                 sb.append( "\" />\r\n" );
 166  
                                         }
 167  0
                                         sb.append( "                </collection-descriptor>\r\n" );
 168  
                                 }
 169  
                         }
 170  
                 }
 171  0
         }
 172  
         
 173  
         private static String getPropertyFromField( Class<? extends Object> clazz, String colName ) {
 174  0
                 for ( Field field : clazz.getDeclaredFields() ) {
 175  0
                         Column column = (Column)field.getAnnotation( Column.class );
 176  0
                         if ( column != null ) {
 177  0
                                 if ( column.name().equals( colName ) ) {
 178  0
                                         return field.getName();
 179  
                                 }
 180  
                         }
 181  
                 }
 182  0
                 return "";
 183  
         }
 184  
 }