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