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