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