Coverage Report - org.kuali.rice.devtools.generators.jpa.JpaToOjbMetadata
 
Classes in this File Line Coverage Branch Coverage Complexity
JpaToOjbMetadata
0%
0/113
0%
0/76
9.6
 
 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.ManyToOne;
 28  
 import javax.persistence.OneToMany;
 29  
 import javax.persistence.OneToOne;
 30  
 import javax.persistence.Table;
 31  
 import java.lang.reflect.Field;
 32  
 import java.util.ArrayList;
 33  
 import java.util.HashMap;
 34  
 import java.util.List;
 35  
 import java.util.Map;
 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 JpaToOjbMetadata {
 44  
 
 45  
         public static void main( String[] args ) throws ClassNotFoundException {
 46  
                 
 47  0
                 Class<? extends PersistableBusinessObjectBase> clazz =
 48  
                 (Class<? extends PersistableBusinessObjectBase>) Class.forName(args[0]);
 49  
 
 50  
                 
 51  0
                 StringBuffer sb = new StringBuffer( 1000 );
 52  0
                 Table tableAnnotation = (Table)clazz.getAnnotation( Table.class );
 53  
                 
 54  0
                 sb.append( "        <class-descriptor class=\"" ).append( clazz.getName() ).append( "\" table=\"" );
 55  0
                 sb.append( tableAnnotation.name() ).append( "\">\r\n" );
 56  
 
 57  0
                 getClassFields( clazz, sb, null );
 58  0
                 getReferences( clazz, sb );
 59  0
                 sb.append( "        </class-descriptor>\r\n" );
 60  
                 
 61  0
                 System.out.println( sb.toString() );
 62  0
         }
 63  
 
 64  
         
 65  
         private static String javaToOjbDataType( Class<? extends Object> dataType ) {
 66  0
                 if ( dataType.equals( String.class ) ) {
 67  0
                         return "VARCHAR";
 68  0
                 } else if (dataType.equals(Long.class) || dataType.equals(Integer.class)) {
 69  0
                         return "BIGINT";
 70  0
                 } else if (dataType.equals(java.util.Date.class) || dataType.equals(java.sql.Date.class)) {
 71  0
                         return "DATE";
 72  0
                 } else if (dataType.equals(java.sql.Timestamp.class)) {
 73  0
                         return "TIMESTAMP";
 74  
                 }
 75  0
                 return "VARCHAR";
 76  
         }
 77  
         
 78  
         private static void getClassFields( Class<? extends Object> clazz, StringBuffer sb, Map<String,AttributeOverride> overrides ) {
 79  
                 // first get annotation overrides
 80  0
                 if ( overrides == null ) {
 81  0
                         overrides = new HashMap<String,AttributeOverride>();
 82  
                 }
 83  0
                 if ( clazz.getAnnotation( AttributeOverride.class ) != null ) {
 84  0
                         AttributeOverride ao = (AttributeOverride)clazz.getAnnotation( AttributeOverride.class );
 85  0
                         if ( !overrides.containsKey(ao.name() ) ) {
 86  0
                                 overrides.put(ao.name(), ao);
 87  
                         }
 88  
                 }
 89  0
                 if ( clazz.getAnnotation( AttributeOverrides.class ) != null ) {
 90  0
                         for ( AttributeOverride ao : ((AttributeOverrides)clazz.getAnnotation( AttributeOverrides.class )).value() ) {
 91  0
                                 if ( !overrides.containsKey(ao.name() ) ) {
 92  0
                                         overrides.put(ao.name(), ao);
 93  
                                 }
 94  0
                                 overrides.put(ao.name(),ao);
 95  
                         }
 96  
                 }
 97  0
                 for ( Field field : clazz.getDeclaredFields() ) {
 98  0
                         Id id = (Id)field.getAnnotation( Id.class );
 99  0
                         Column column = (Column)field.getAnnotation( Column.class );
 100  0
                         if ( column != null ) {
 101  0
                                 sb.append( "                <field-descriptor name=\"" );
 102  0
                                 sb.append( field.getName() );
 103  0
                                 sb.append( "\" column=\"" );
 104  0
                                 if ( overrides.containsKey(field.getName() ) ) {
 105  0
                                         sb.append( overrides.get(field.getName()).column().name() );
 106  
                                 } else {
 107  0
                                         sb.append( column.name() );
 108  
                                 }
 109  0
                                 sb.append( "\" jdbc-type=\"" );
 110  0
                                 sb.append( javaToOjbDataType( field.getType() ) );
 111  0
                                 sb.append( "\" " );
 112  0
                                 if ( id != null ) {
 113  0
                                         sb.append( "primarykey=\"true\" " );
 114  
                                 }
 115  0
                                 if ( field.getName().equals( "objectId" ) ) {
 116  0
                                         sb.append( "index=\"true\" " );
 117  
                                 }
 118  0
                                 if ( field.getType() == boolean.class ) {
 119  0
                                         sb.append( "conversion=\"org.kuali.rice.krad.util.OjbCharBooleanConversion3\" " );
 120  
                                 }
 121  0
                                 if ( field.getName().equals( "versionNumber" ) ) {
 122  0
                                         sb.append( "locking=\"true\" " );
 123  
                                 }
 124  0
                                 sb.append( "/>\r\n" );
 125  
                         }
 126  
                 }
 127  0
                 if ( !clazz.equals( PersistableBusinessObject.class ) && clazz.getSuperclass() != null ) {
 128  0
                         getClassFields( clazz.getSuperclass(), sb, overrides );
 129  
                 }
 130  0
         }
 131  
 
 132  
         private static void getReferences( Class<? extends Object> clazz, StringBuffer sb ) {
 133  0
                 for ( Field field : clazz.getDeclaredFields() ) {
 134  0
                         JoinColumns multiKey = (JoinColumns)field.getAnnotation( JoinColumns.class );
 135  0
                         JoinColumn singleKey = (JoinColumn)field.getAnnotation( JoinColumn.class );
 136  0
                         if ( multiKey != null || singleKey != null ) {
 137  0
                                 List<JoinColumn> keys = new ArrayList<JoinColumn>();
 138  0
                                 if ( singleKey != null ) {
 139  0
                                         keys.add( singleKey );
 140  
                                 }
 141  0
                                 if ( multiKey != null ) {
 142  0
                                         for ( JoinColumn col : multiKey.value() ) {
 143  0
                                                 keys.add( col );
 144  
                                         }
 145  
                                 }
 146  0
                                 OneToOne oneToOne = field.getAnnotation( OneToOne.class );
 147  0
                                 if ( oneToOne != null ) {
 148  0
                                         sb.append( "                <reference-descriptor name=\"" );
 149  0
                                         sb.append( field.getName() );
 150  0
                                         sb.append( "\" class-ref=\"" );
 151  0
                                         if ( !oneToOne.targetEntity().getName().equals( "void" ) ) {
 152  0
                                                 sb.append( oneToOne.targetEntity().getName() );
 153  
                                         } else {
 154  0
                                                 sb.append( field.getType().getName() );
 155  
                                         }
 156  0
                                         sb.append( "\" auto-retrieve=\"true\" auto-update=\"none\" auto-delete=\"none\" proxy=\"true\">\r\n" );
 157  0
                                         for ( JoinColumn col : keys ) {
 158  0
                                                 sb.append( "                        <foreignkey field-ref=\"" );
 159  0
                                                 sb.append( getPropertyFromField( clazz, col.name() ) );
 160  0
                                                 sb.append( "\" />\r\n" );
 161  
                                         }
 162  0
                                         sb.append( "                </reference-descriptor>\r\n" );
 163  
                                 }
 164  0
                                 ManyToOne manyToOne = field.getAnnotation( ManyToOne.class );
 165  0
                                 if ( manyToOne != null ) {
 166  0
                                         sb.append( "                <reference-descriptor name=\"" );
 167  0
                                         sb.append( field.getName() );
 168  0
                                         sb.append( "\" class-ref=\"" );
 169  0
                                         if ( !manyToOne.targetEntity().getName().equals( "void" ) ) {
 170  0
                                                 sb.append( manyToOne.targetEntity().getName() );
 171  
                                         } else {
 172  0
                                                 sb.append( field.getType().getName() );
 173  
                                         }
 174  0
                                         sb.append( "\" auto-retrieve=\"true\" auto-update=\"none\" auto-delete=\"none\" proxy=\"true\">\r\n" );
 175  0
                                         for ( JoinColumn col : keys ) {
 176  0
                                                 sb.append( "                        <foreignkey field-ref=\"" );
 177  0
                                                 sb.append( getPropertyFromField( clazz, col.name() ) );
 178  0
                                                 sb.append( "\" />\r\n" );
 179  
                                         }
 180  0
                                         sb.append( "                </reference-descriptor>\r\n" );
 181  
                                 }
 182  0
                                 OneToMany oneToMany = field.getAnnotation( OneToMany.class );
 183  0
                                 if ( oneToMany != null ) {
 184  0
                                         sb.append( "                <collection-descriptor name=\"" );
 185  0
                                         sb.append( field.getName() );
 186  0
                                         sb.append( "\" element-class-ref=\"" );
 187  0
                                         sb.append( oneToMany.targetEntity().getName() );
 188  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" );
 189  0
                                         for ( JoinColumn col : keys ) {
 190  0
                                                 sb.append( "                        <inverse-foreignkey field-ref=\"" );
 191  0
                                                 sb.append( getPropertyFromField( clazz, col.name() ) );
 192  0
                                                 sb.append( "\" />\r\n" );
 193  
                                         }
 194  0
                                         sb.append( "                </collection-descriptor>\r\n" );
 195  
                                 }
 196  
                         }
 197  
                 }
 198  0
         }
 199  
         
 200  
         private static String getPropertyFromField( Class<? extends Object> clazz, String colName ) {
 201  0
                 for ( Field field : clazz.getDeclaredFields() ) {
 202  0
                         Column column = (Column)field.getAnnotation( Column.class );
 203  0
                         if ( column != null ) {
 204  0
                                 if ( column.name().equals( colName ) ) {
 205  0
                                         return field.getName();
 206  
                                 }
 207  
                         }
 208  
                 }
 209  0
                 return "";
 210  
         }
 211  
 }