KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > engine > UnsavedValueFactory


1 //$Id: UnsavedValueFactory.java,v 1.5 2005/06/05 04:31:33 oneovthafew Exp $
2
package org.hibernate.engine;
3
4 import java.io.Serializable JavaDoc;
5 import java.lang.reflect.Constructor JavaDoc;
6
7 import org.hibernate.InstantiationException;
8 import org.hibernate.MappingException;
9 import org.hibernate.property.Getter;
10 import org.hibernate.type.IdentifierType;
11 import org.hibernate.type.PrimitiveType;
12 import org.hibernate.type.Type;
13 import org.hibernate.type.VersionType;
14
15 /**
16  * @author Gavin King
17  */

18 public class UnsavedValueFactory {
19     
20     private static Object JavaDoc instantiate(Constructor JavaDoc constructor) {
21         try {
22             return constructor.newInstance(null);
23         }
24         catch (Exception JavaDoc e) {
25             throw new InstantiationException JavaDoc( "could not instantiate test object", constructor.getDeclaringClass(), e );
26         }
27     }
28     
29     /**
30      * Return an IdentifierValue for the specified unsaved-value. If none is specified,
31      * guess the unsaved value by instantiating a test instance of the class and
32      * reading it's id property, or if that is not possible, using the java default
33      * value for the type
34      */

35     public static IdentifierValue getUnsavedIdentifierValue(
36             String JavaDoc unsavedValue,
37             Getter identifierGetter,
38             Type identifierType,
39             Constructor JavaDoc constructor) {
40         
41         if ( unsavedValue == null ) {
42             if ( identifierGetter!=null && constructor!=null ) {
43                 // use the id value of a newly instantiated instance as the unsaved-value
44
Serializable JavaDoc defaultValue = (Serializable JavaDoc) identifierGetter.get( instantiate(constructor) );
45                 return new IdentifierValue( defaultValue );
46             }
47             else if ( identifierGetter != null && (identifierType instanceof PrimitiveType) ) {
48                 Serializable JavaDoc defaultValue = ( ( PrimitiveType ) identifierType ).getDefaultValue();
49                 return new IdentifierValue( defaultValue );
50             }
51             else {
52                 return IdentifierValue.NULL;
53             }
54         }
55         else if ( "null".equals( unsavedValue ) ) {
56             return IdentifierValue.NULL;
57         }
58         else if ( "undefined".equals( unsavedValue ) ) {
59             return IdentifierValue.UNDEFINED;
60         }
61         else if ( "none".equals( unsavedValue ) ) {
62             return IdentifierValue.NONE;
63         }
64         else if ( "any".equals( unsavedValue ) ) {
65             return IdentifierValue.ANY;
66         }
67         else {
68             try {
69                 return new IdentifierValue( ( Serializable JavaDoc ) ( ( IdentifierType ) identifierType ).stringToObject( unsavedValue ) );
70             }
71             catch ( ClassCastException JavaDoc cce ) {
72                 throw new MappingException( "Bad identifier type: " + identifierType.getName() );
73             }
74             catch ( Exception JavaDoc e ) {
75                 throw new MappingException( "Could not parse identifier unsaved-value: " + unsavedValue );
76             }
77         }
78     }
79
80     public static VersionValue getUnsavedVersionValue(
81             String JavaDoc versionUnsavedValue,
82             Getter versionGetter,
83             VersionType versionType,
84             Constructor JavaDoc constructor) {
85         
86         if ( versionUnsavedValue == null ) {
87             if ( constructor!=null ) {
88                 Object JavaDoc defaultValue = versionGetter.get( instantiate(constructor) );
89                 // if the version of a newly instantiated object is not the same
90
// as the version seed value, use that as the unsaved-value
91
return versionType.isEqual( versionType.seed(), defaultValue ) ?
92                         VersionValue.UNDEFINED :
93                         new VersionValue( defaultValue );
94             }
95             else {
96                 return VersionValue.UNDEFINED;
97             }
98         }
99         else if ( "undefined".equals( versionUnsavedValue ) ) {
100             return VersionValue.UNDEFINED;
101         }
102         else if ( "null".equals( versionUnsavedValue ) ) {
103             return VersionValue.NULL;
104         }
105         else if ( "negative".equals( versionUnsavedValue ) ) {
106             return VersionValue.NEGATIVE;
107         }
108         else {
109             // this should not happen since the DTD prevents it
110
throw new MappingException( "Could not parse version unsaved-value: " + versionUnsavedValue );
111         }
112         
113     }
114
115 }
116
Popular Tags