KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > type > TypeFactory


1 // $Id: TypeFactory.java,v 1.35 2005/07/13 02:11:43 oneovthafew Exp $
2
package org.hibernate.type;
3
4 import java.io.Serializable JavaDoc;
5 import java.math.BigDecimal JavaDoc;
6 import java.math.BigInteger JavaDoc;
7 import java.sql.Blob JavaDoc;
8 import java.sql.Clob JavaDoc;
9 import java.sql.Time JavaDoc;
10 import java.sql.Timestamp JavaDoc;
11 import java.util.Calendar JavaDoc;
12 import java.util.Collections JavaDoc;
13 import java.util.Comparator JavaDoc;
14 import java.util.GregorianCalendar JavaDoc;
15 import java.util.HashMap JavaDoc;
16 import java.util.Locale JavaDoc;
17 import java.util.Map JavaDoc;
18 import java.util.Properties JavaDoc;
19 import java.util.TimeZone JavaDoc;
20
21 import org.hibernate.Hibernate;
22 import org.hibernate.HibernateException;
23 import org.hibernate.MappingException;
24 import org.hibernate.classic.Lifecycle;
25 import org.hibernate.engine.SessionImplementor;
26 import org.hibernate.intercept.LazyPropertyInitializer;
27 import org.hibernate.property.BackrefPropertyAccessor;
28 import org.hibernate.tuple.StandardProperty;
29 import org.hibernate.usertype.CompositeUserType;
30 import org.hibernate.usertype.UserType;
31 import org.hibernate.usertype.ParameterizedType;
32 import org.hibernate.util.ReflectHelper;
33
34 /**
35  * Used internally to obtain instances of <tt>Type</tt>. Applications should use static methods
36  * and constants on <tt>org.hibernate.Hibernate</tt>.
37  *
38  * @see org.hibernate.Hibernate
39  * @author Gavin King
40  */

41 public final class TypeFactory {
42
43     private static final Map JavaDoc BASIC_TYPES;
44
45     static {
46         HashMap JavaDoc basics = new HashMap JavaDoc();
47         basics.put( boolean.class.getName(), Hibernate.BOOLEAN );
48         basics.put( long.class.getName(), Hibernate.LONG );
49         basics.put( short.class.getName(), Hibernate.SHORT );
50         basics.put( int.class.getName(), Hibernate.INTEGER );
51         basics.put( byte.class.getName(), Hibernate.BYTE );
52         basics.put( float.class.getName(), Hibernate.FLOAT );
53         basics.put( double.class.getName(), Hibernate.DOUBLE );
54         basics.put( char.class.getName(), Hibernate.CHARACTER );
55         basics.put( Hibernate.CHARACTER.getName(), Hibernate.CHARACTER );
56         basics.put( Hibernate.INTEGER.getName(), Hibernate.INTEGER );
57         basics.put( Hibernate.STRING.getName(), Hibernate.STRING );
58         basics.put( Hibernate.DATE.getName(), Hibernate.DATE );
59         basics.put( Hibernate.TIME.getName(), Hibernate.TIME );
60         basics.put( Hibernate.TIMESTAMP.getName(), Hibernate.TIMESTAMP );
61         basics.put( Hibernate.LOCALE.getName(), Hibernate.LOCALE );
62         basics.put( Hibernate.CALENDAR.getName(), Hibernate.CALENDAR );
63         basics.put( Hibernate.CALENDAR_DATE.getName(), Hibernate.CALENDAR_DATE );
64         basics.put( Hibernate.CURRENCY.getName(), Hibernate.CURRENCY );
65         basics.put( Hibernate.TIMEZONE.getName(), Hibernate.TIMEZONE );
66         basics.put( Hibernate.CLASS.getName(), Hibernate.CLASS );
67         basics.put( Hibernate.TRUE_FALSE.getName(), Hibernate.TRUE_FALSE );
68         basics.put( Hibernate.YES_NO.getName(), Hibernate.YES_NO );
69         basics.put( Hibernate.BINARY.getName(), Hibernate.BINARY );
70         basics.put( Hibernate.TEXT.getName(), Hibernate.TEXT );
71         basics.put( Hibernate.BLOB.getName(), Hibernate.BLOB );
72         basics.put( Hibernate.CLOB.getName(), Hibernate.CLOB );
73         basics.put( Hibernate.BIG_DECIMAL.getName(), Hibernate.BIG_DECIMAL );
74         basics.put( Hibernate.BIG_INTEGER.getName(), Hibernate.BIG_INTEGER );
75         basics.put( Hibernate.SERIALIZABLE.getName(), Hibernate.SERIALIZABLE );
76         basics.put( Hibernate.OBJECT.getName(), Hibernate.OBJECT );
77         basics.put( Boolean JavaDoc.class.getName(), Hibernate.BOOLEAN );
78         basics.put( Long JavaDoc.class.getName(), Hibernate.LONG );
79         basics.put( Short JavaDoc.class.getName(), Hibernate.SHORT );
80         basics.put( Integer JavaDoc.class.getName(), Hibernate.INTEGER );
81         basics.put( Byte JavaDoc.class.getName(), Hibernate.BYTE );
82         basics.put( Float JavaDoc.class.getName(), Hibernate.FLOAT );
83         basics.put( Double JavaDoc.class.getName(), Hibernate.DOUBLE );
84         basics.put( Character JavaDoc.class.getName(), Hibernate.CHARACTER );
85         basics.put( String JavaDoc.class.getName(), Hibernate.STRING );
86         basics.put( java.util.Date JavaDoc.class.getName(), Hibernate.TIMESTAMP );
87         basics.put( Time JavaDoc.class.getName(), Hibernate.TIME );
88         basics.put( Timestamp JavaDoc.class.getName(), Hibernate.TIMESTAMP );
89         basics.put( java.sql.Date JavaDoc.class.getName(), Hibernate.DATE );
90         basics.put( BigDecimal JavaDoc.class.getName(), Hibernate.BIG_DECIMAL );
91         basics.put( BigInteger JavaDoc.class.getName(), Hibernate.BIG_INTEGER );
92         basics.put( Locale JavaDoc.class.getName(), Hibernate.LOCALE );
93         basics.put( Calendar JavaDoc.class.getName(), Hibernate.CALENDAR );
94         basics.put( GregorianCalendar JavaDoc.class.getName(), Hibernate.CALENDAR );
95         if ( CurrencyType.CURRENCY_CLASS != null ) {
96             basics.put( CurrencyType.CURRENCY_CLASS.getName(), Hibernate.CURRENCY );
97         }
98         basics.put( TimeZone JavaDoc.class.getName(), Hibernate.TIMEZONE );
99         basics.put( Object JavaDoc.class.getName(), Hibernate.OBJECT );
100         basics.put( Class JavaDoc.class.getName(), Hibernate.CLASS );
101         basics.put( byte[].class.getName(), Hibernate.BINARY );
102         basics.put( "byte[]", Hibernate.BINARY );
103         basics.put( Blob JavaDoc.class.getName(), Hibernate.BLOB );
104         basics.put( Clob JavaDoc.class.getName(), Hibernate.CLOB );
105         basics.put( Serializable JavaDoc.class.getName(), Hibernate.SERIALIZABLE );
106         
107         Type type = new AdaptedImmutableType(Hibernate.DATE);
108         basics.put( type.getName(), type );
109         type = new AdaptedImmutableType(Hibernate.TIME);
110         basics.put( type.getName(), type );
111         type = new AdaptedImmutableType(Hibernate.TIMESTAMP);
112         basics.put( type.getName(), type );
113         type = new AdaptedImmutableType(Hibernate.CALENDAR);
114         basics.put( type.getName(), type );
115         type = new AdaptedImmutableType(Hibernate.CALENDAR_DATE);
116         basics.put( type.getName(), type );
117         type = new AdaptedImmutableType(Hibernate.SERIALIZABLE);
118         basics.put( type.getName(), type );
119         type = new AdaptedImmutableType(Hibernate.BINARY);
120         basics.put( type.getName(), type );
121         
122         BASIC_TYPES = Collections.unmodifiableMap( basics );
123     }
124
125     private TypeFactory() {
126         throw new UnsupportedOperationException JavaDoc();
127     }
128
129     /**
130      * A one-to-one association type for the given class
131      */

132     public static EntityType oneToOne(
133             String JavaDoc persistentClass,
134             ForeignKeyDirection foreignKeyType,
135             String JavaDoc uniqueKeyPropertyName,
136             boolean lazy,
137             boolean unwrapProxy,
138             boolean isEmbeddedInXML,
139             String JavaDoc entityName,
140             String JavaDoc propertyName
141     ) {
142         return new OneToOneType(
143                 persistentClass,
144                 foreignKeyType,
145                 uniqueKeyPropertyName,
146                 lazy,
147                 unwrapProxy,
148                 isEmbeddedInXML,
149                 entityName,
150                 propertyName
151             );
152     }
153
154     /**
155      * A many-to-one association type for the given class
156      */

157     public static EntityType manyToOne(String JavaDoc persistentClass) {
158         return new ManyToOneType( persistentClass );
159     }
160
161     /**
162      * A many-to-one association type for the given class
163      */

164     public static EntityType manyToOne(String JavaDoc persistentClass, boolean lazy) {
165         return new ManyToOneType( persistentClass, lazy );
166     }
167
168     /**
169      * A many-to-one association type for the given class
170      */

171     public static EntityType manyToOne(
172             String JavaDoc persistentClass,
173             String JavaDoc uniqueKeyPropertyName,
174             boolean lazy,
175             boolean unwrapProxy,
176             boolean isEmbeddedInXML,
177             boolean ignoreNotFound
178     ) {
179         return new ManyToOneType(
180                 persistentClass,
181                 uniqueKeyPropertyName,
182                 lazy,
183                 unwrapProxy,
184                 isEmbeddedInXML,
185                 ignoreNotFound
186             );
187     }
188
189     /**
190      * Given the name of a Hibernate basic type, return an instance of
191      * <tt>org.hibernate.type.Type</tt>.
192      */

193     public static Type basic(String JavaDoc name) {
194         return (Type) BASIC_TYPES.get( name );
195     }
196
197     /**
198      * Uses heuristics to deduce a Hibernate type given a string naming the type or Java class.
199      * Return an instance of <tt>org.hibernate.type.Type</tt>.
200      */

201     public static Type heuristicType(String JavaDoc typeName) throws MappingException {
202         return heuristicType( typeName, null );
203     }
204
205     /**
206      * Uses heuristics to deduce a Hibernate type given a string naming the type or Java class.
207      * Return an instance of <tt>org.hibernate.type.Type</tt>.
208      */

209     public static Type heuristicType(String JavaDoc typeName, Properties JavaDoc parameters)
210             throws MappingException {
211         Type type = TypeFactory.basic( typeName );
212         if ( type == null ) {
213             Class JavaDoc typeClass;
214             try {
215                 typeClass = ReflectHelper.classForName( typeName );
216             }
217             catch (ClassNotFoundException JavaDoc cnfe) {
218                 typeClass = null;
219             }
220             if ( typeClass != null ) {
221                 if ( Type.class.isAssignableFrom( typeClass ) ) {
222                     try {
223                         type = (Type) typeClass.newInstance();
224                     }
225                     catch (Exception JavaDoc e) {
226                         throw new MappingException(
227                                 "Could not instantiate Type: " + typeClass.getName(),
228                                 e
229                             );
230                     }
231                     injectParameters(type, parameters);
232                 }
233                 else if ( CompositeUserType.class.isAssignableFrom( typeClass ) ) {
234                     type = new CompositeCustomType( typeClass, parameters );
235                 }
236                 else if ( UserType.class.isAssignableFrom( typeClass ) ) {
237                     type = new CustomType( typeClass, parameters );
238                 }
239                 else if ( Lifecycle.class.isAssignableFrom( typeClass ) ) {
240                     type = Hibernate.entity( typeClass );
241                 }
242                 else if ( Serializable JavaDoc.class.isAssignableFrom( typeClass ) ) {
243                     type = Hibernate.serializable( typeClass );
244                 }
245             }
246         }
247         return type;
248
249     }
250
251     public static CollectionType customCollection(String JavaDoc typeName, String JavaDoc role, String JavaDoc propertyRef,
252             boolean embedded) {
253         Class JavaDoc typeClass;
254         try {
255             typeClass = ReflectHelper.classForName( typeName );
256         }
257         catch (ClassNotFoundException JavaDoc cnfe) {
258             throw new MappingException( "user colllection type class not found: " + typeName, cnfe );
259         }
260         return new CustomCollectionType( typeClass, role, propertyRef, embedded );
261     }
262
263     // Collection Types:
264

265     public static CollectionType array(String JavaDoc role, String JavaDoc propertyRef, boolean embedded,
266             Class JavaDoc elementClass) {
267         return new ArrayType( role, propertyRef, elementClass, embedded );
268     }
269
270     public static CollectionType list(String JavaDoc role, String JavaDoc propertyRef, boolean embedded) {
271         return new ListType( role, propertyRef, embedded );
272     }
273
274     public static CollectionType bag(String JavaDoc role, String JavaDoc propertyRef, boolean embedded) {
275         return new BagType( role, propertyRef, embedded );
276     }
277
278     public static CollectionType idbag(String JavaDoc role, String JavaDoc propertyRef, boolean embedded) {
279         return new IdentifierBagType( role, propertyRef, embedded );
280     }
281
282     public static CollectionType map(String JavaDoc role, String JavaDoc propertyRef, boolean embedded) {
283         return new MapType( role, propertyRef, embedded );
284     }
285
286     public static CollectionType set(String JavaDoc role, String JavaDoc propertyRef, boolean embedded) {
287         return new SetType( role, propertyRef, embedded );
288     }
289
290     public static CollectionType sortedMap(String JavaDoc role, String JavaDoc propertyRef, boolean embedded,
291             Comparator JavaDoc comparator) {
292         return new SortedMapType( role, propertyRef, comparator, embedded );
293     }
294
295     public static CollectionType sortedSet(String JavaDoc role, String JavaDoc propertyRef, boolean embedded,
296             Comparator JavaDoc comparator) {
297         return new SortedSetType( role, propertyRef, comparator, embedded );
298     }
299
300     /**
301      * Deep copy values in the first array into the second
302      */

303     public static void deepCopy(Object JavaDoc[] values, Type[] types, boolean[] copy, Object JavaDoc[] target,
304             SessionImplementor session) throws HibernateException {
305         for ( int i = 0; i < types.length; i++ ) {
306             if ( copy[i] ) {
307                 if ( values[i] == LazyPropertyInitializer.UNFETCHED_PROPERTY
308                     || values[i] == BackrefPropertyAccessor.UNKNOWN ) {
309                     target[i] = values[i];
310                 }
311                 else {
312                     target[i] = types[i].deepCopy( values[i], session.getEntityMode(), session
313                         .getFactory() );
314                 }
315             }
316         }
317     }
318
319     /**
320      * Determine if any of the given field values are dirty, returning an array containing indexes
321      * of the dirty fields or <tt>null</tt> if no fields are dirty.
322      */

323     /*public static int[] findDirty(Type[] types, Object[] x, Object[] y, boolean[] check,
324             SessionImplementor session) throws HibernateException {
325         int[] results = null;
326         int count = 0;
327         for ( int i = 0; i < types.length; i++ ) {
328             if ( check[i] && types[i].isDirty( x[i], y[i], session ) ) {
329                 if ( results == null ) results = new int[types.length];
330                 results[count++] = i;
331             }
332         }
333         if ( count == 0 ) {
334             return null;
335         }
336         else {
337             int[] trimmed = new int[count];
338             System.arraycopy( results, 0, trimmed, 0, count );
339             return trimmed;
340         }
341     }*/

342
343     /**
344      * Determine if any of the given field values are modified, returning an array containing
345      * indexes of the dirty fields or <tt>null</tt> if no fields are dirty.
346      */

347     /*public static int[] findModified(Type[] types, Object[] old, Object[] current, boolean[] check,
348             SessionImplementor session) throws HibernateException {
349         int[] results = null;
350         int count = 0;
351         for ( int i = 0; i < types.length; i++ ) {
352             if ( check[i]
353                 && types[i].isModified( old[i], current[i], session ) ) {
354                 if ( results == null ) results = new int[types.length];
355                 results[count++] = i;
356             }
357         }
358         if ( count == 0 ) {
359             return null;
360         }
361         else {
362             int[] trimmed = new int[count];
363             System.arraycopy( results, 0, trimmed, 0, count );
364             return trimmed;
365         }
366     }*/

367
368     public static Object JavaDoc[] assemble(Serializable JavaDoc[] row, Type[] types, SessionImplementor session,
369             Object JavaDoc owner) throws HibernateException {
370         Object JavaDoc[] assembled = new Object JavaDoc[row.length];
371         for ( int i = 0; i < types.length; i++ ) {
372             if ( row[i] == LazyPropertyInitializer.UNFETCHED_PROPERTY
373                 || row[i] == BackrefPropertyAccessor.UNKNOWN ) {
374                 assembled[i] = row[i];
375             }
376             else {
377                 assembled[i] = types[i].assemble( row[i], session, owner );
378             }
379         }
380         return assembled;
381     }
382
383     public static Serializable JavaDoc[] disassemble(Object JavaDoc[] row, Type[] types, SessionImplementor session,
384             Object JavaDoc owner) throws HibernateException {
385         Serializable JavaDoc[] disassembled = new Serializable JavaDoc[row.length];
386         for ( int i = 0; i < row.length; i++ ) {
387             if ( row[i] == LazyPropertyInitializer.UNFETCHED_PROPERTY
388                 || row[i] == BackrefPropertyAccessor.UNKNOWN ) {
389                 disassembled[i] = (Serializable JavaDoc) row[i];
390             }
391             else {
392                 disassembled[i] = types[i].disassemble( row[i], session, owner );
393             }
394         }
395         return disassembled;
396     }
397
398     public static Object JavaDoc[] replace(Object JavaDoc[] original, Object JavaDoc[] target, Type[] types,
399             SessionImplementor session, Object JavaDoc owner, Map JavaDoc copyCache) throws HibernateException {
400         Object JavaDoc[] copied = new Object JavaDoc[original.length];
401         for ( int i = 0; i < types.length; i++ ) {
402             if ( original[i] == LazyPropertyInitializer.UNFETCHED_PROPERTY
403                 || original[i] == BackrefPropertyAccessor.UNKNOWN ) {
404                 copied[i] = target[i];
405             }
406             else {
407                 copied[i] = types[i].replace( original[i], target[i], session, owner, copyCache );
408             }
409         }
410         return copied;
411     }
412
413     public static Object JavaDoc[] replace(Object JavaDoc[] original, Object JavaDoc[] target, Type[] types,
414             SessionImplementor session, Object JavaDoc owner, Map JavaDoc copyCache,
415             ForeignKeyDirection foreignKeyDirection) throws HibernateException {
416         Object JavaDoc[] copied = new Object JavaDoc[original.length];
417         for ( int i = 0; i < types.length; i++ ) {
418             if ( original[i] == LazyPropertyInitializer.UNFETCHED_PROPERTY
419                 || original[i] == BackrefPropertyAccessor.UNKNOWN ) {
420                 copied[i] = target[i];
421             }
422             else {
423                 copied[i] = types[i].replace( original[i], target[i], session, owner, copyCache, foreignKeyDirection );
424             }
425         }
426         return copied;
427     }
428
429
430
431     /**
432      * Determine if any of the given field values are dirty, returning an array containing
433      * indexes of the dirty fields or <tt>null</tt> if no fields are dirty.
434      * @param x the current state of the entity
435      * @param y the snapshot state from the time the object was loaded
436      */

437     public static int[] findDirty(
438             final StandardProperty[] properties,
439             final Object JavaDoc[] x,
440             final Object JavaDoc[] y,
441             final boolean anyUninitializedProperties,
442             final SessionImplementor session)
443     throws HibernateException {
444
445         int[] results = null;
446         int count = 0;
447         int span = properties.length;
448
449         for ( int i = 0; i < span; i++ ) {
450
451             final boolean dirty = x[i]!=LazyPropertyInitializer.UNFETCHED_PROPERTY //x is the "current" state
452
&& properties[i].isDirtyCheckable(anyUninitializedProperties)
453                     && properties[i].getType().isDirty( y[i], x[i], session );
454
455             if ( dirty ) {
456                 if ( results == null ) {
457                     results = new int[span];
458                 }
459                 results[count++] = i;
460             }
461
462         }
463
464         if ( count == 0 ) {
465             return null;
466         }
467         else {
468             int[] trimmed = new int[count];
469             System.arraycopy( results, 0, trimmed, 0, count );
470             return trimmed;
471         }
472     }
473
474     /**
475      * Determine if any of the given field values are modified, returning an array containing
476      * indexes of the dirty fields or <tt>null</tt> if no fields are dirty.
477      * @param x the current state of the entity
478      * @param y the snapshot state just retrieved from the database
479      */

480     public static int[] findModified(
481             final StandardProperty[] properties,
482             final Object JavaDoc[] x,
483             final Object JavaDoc[] y,
484             final boolean anyUninitializedProperties,
485             final SessionImplementor session)
486             throws HibernateException {
487
488         int[] results = null;
489         int count = 0;
490         int span = properties.length;
491
492         for ( int i = 0; i < span; i++ ) {
493
494             final boolean modified = x[i]!=LazyPropertyInitializer.UNFETCHED_PROPERTY //x is the "current" state
495
&& properties[i].isDirtyCheckable(anyUninitializedProperties)
496                     && properties[i].getType().isModified( y[i], x[i], session );
497
498             if ( modified ) {
499                 if ( results == null ) {
500                     results = new int[span];
501                 }
502                 results[count++] = i;
503             }
504
505         }
506
507         if ( count == 0 ) {
508             return null;
509         }
510         else {
511             int[] trimmed = new int[count];
512             System.arraycopy( results, 0, trimmed, 0, count );
513             return trimmed;
514         }
515     }
516
517     public static void injectParameters(Object JavaDoc type, Properties JavaDoc parameters) {
518         if (type instanceof ParameterizedType) {
519             ( (ParameterizedType) type ).setParameterValues(parameters);
520         }
521         else if ( parameters!=null && !parameters.isEmpty() ) {
522             throw new MappingException(
523                     "type is not parameterized: " +
524                     type.getClass().getName()
525                 );
526         }
527     }
528
529 }
530
Popular Tags