View Javadoc

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