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