1 package org.hibernate.engine; 3 4 import java.io.Serializable ; 5 import java.lang.reflect.Constructor ; 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 18 public class UnsavedValueFactory { 19 20 private static Object instantiate(Constructor constructor) { 21 try { 22 return constructor.newInstance(null); 23 } 24 catch (Exception e) { 25 throw new InstantiationException ( "could not instantiate test object", constructor.getDeclaringClass(), e ); 26 } 27 } 28 29 35 public static IdentifierValue getUnsavedIdentifierValue( 36 String unsavedValue, 37 Getter identifierGetter, 38 Type identifierType, 39 Constructor constructor) { 40 41 if ( unsavedValue == null ) { 42 if ( identifierGetter!=null && constructor!=null ) { 43 Serializable defaultValue = (Serializable ) identifierGetter.get( instantiate(constructor) ); 45 return new IdentifierValue( defaultValue ); 46 } 47 else if ( identifierGetter != null && (identifierType instanceof PrimitiveType) ) { 48 Serializable 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 ) ( ( IdentifierType ) identifierType ).stringToObject( unsavedValue ) ); 70 } 71 catch ( ClassCastException cce ) { 72 throw new MappingException( "Bad identifier type: " + identifierType.getName() ); 73 } 74 catch ( Exception e ) { 75 throw new MappingException( "Could not parse identifier unsaved-value: " + unsavedValue ); 76 } 77 } 78 } 79 80 public static VersionValue getUnsavedVersionValue( 81 String versionUnsavedValue, 82 Getter versionGetter, 83 VersionType versionType, 84 Constructor constructor) { 85 86 if ( versionUnsavedValue == null ) { 87 if ( constructor!=null ) { 88 Object defaultValue = versionGetter.get( instantiate(constructor) ); 89 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 throw new MappingException( "Could not parse version unsaved-value: " + versionUnsavedValue ); 111 } 112 113 } 114 115 } 116 | Popular Tags |