1 package org.hibernate.type; 3 4 import java.io.Serializable ; 5 import java.math.BigDecimal ; 6 import java.math.BigInteger ; 7 import java.sql.Blob ; 8 import java.sql.Clob ; 9 import java.sql.Time ; 10 import java.sql.Timestamp ; 11 import java.util.Calendar ; 12 import java.util.Collections ; 13 import java.util.Comparator ; 14 import java.util.GregorianCalendar ; 15 import java.util.HashMap ; 16 import java.util.Locale ; 17 import java.util.Map ; 18 import java.util.Properties ; 19 import java.util.TimeZone ; 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 41 public final class TypeFactory { 42 43 private static final Map BASIC_TYPES; 44 45 static { 46 HashMap basics = new HashMap (); 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 .class.getName(), Hibernate.BOOLEAN ); 78 basics.put( Long .class.getName(), Hibernate.LONG ); 79 basics.put( Short .class.getName(), Hibernate.SHORT ); 80 basics.put( Integer .class.getName(), Hibernate.INTEGER ); 81 basics.put( Byte .class.getName(), Hibernate.BYTE ); 82 basics.put( Float .class.getName(), Hibernate.FLOAT ); 83 basics.put( Double .class.getName(), Hibernate.DOUBLE ); 84 basics.put( Character .class.getName(), Hibernate.CHARACTER ); 85 basics.put( String .class.getName(), Hibernate.STRING ); 86 basics.put( java.util.Date .class.getName(), Hibernate.TIMESTAMP ); 87 basics.put( Time .class.getName(), Hibernate.TIME ); 88 basics.put( Timestamp .class.getName(), Hibernate.TIMESTAMP ); 89 basics.put( java.sql.Date .class.getName(), Hibernate.DATE ); 90 basics.put( BigDecimal .class.getName(), Hibernate.BIG_DECIMAL ); 91 basics.put( BigInteger .class.getName(), Hibernate.BIG_INTEGER ); 92 basics.put( Locale .class.getName(), Hibernate.LOCALE ); 93 basics.put( Calendar .class.getName(), Hibernate.CALENDAR ); 94 basics.put( GregorianCalendar .class.getName(), Hibernate.CALENDAR ); 95 if ( CurrencyType.CURRENCY_CLASS != null ) { 96 basics.put( CurrencyType.CURRENCY_CLASS.getName(), Hibernate.CURRENCY ); 97 } 98 basics.put( TimeZone .class.getName(), Hibernate.TIMEZONE ); 99 basics.put( Object .class.getName(), Hibernate.OBJECT ); 100 basics.put( Class .class.getName(), Hibernate.CLASS ); 101 basics.put( byte[].class.getName(), Hibernate.BINARY ); 102 basics.put( "byte[]", Hibernate.BINARY ); 103 basics.put( Blob .class.getName(), Hibernate.BLOB ); 104 basics.put( Clob .class.getName(), Hibernate.CLOB ); 105 basics.put( Serializable .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 (); 127 } 128 129 132 public static EntityType oneToOne( 133 String persistentClass, 134 ForeignKeyDirection foreignKeyType, 135 String uniqueKeyPropertyName, 136 boolean lazy, 137 boolean unwrapProxy, 138 boolean isEmbeddedInXML, 139 String entityName, 140 String 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 157 public static EntityType manyToOne(String persistentClass) { 158 return new ManyToOneType( persistentClass ); 159 } 160 161 164 public static EntityType manyToOne(String persistentClass, boolean lazy) { 165 return new ManyToOneType( persistentClass, lazy ); 166 } 167 168 171 public static EntityType manyToOne( 172 String persistentClass, 173 String 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 193 public static Type basic(String name) { 194 return (Type) BASIC_TYPES.get( name ); 195 } 196 197 201 public static Type heuristicType(String typeName) throws MappingException { 202 return heuristicType( typeName, null ); 203 } 204 205 209 public static Type heuristicType(String typeName, Properties parameters) 210 throws MappingException { 211 Type type = TypeFactory.basic( typeName ); 212 if ( type == null ) { 213 Class typeClass; 214 try { 215 typeClass = ReflectHelper.classForName( typeName ); 216 } 217 catch (ClassNotFoundException 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 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 .class.isAssignableFrom( typeClass ) ) { 243 type = Hibernate.serializable( typeClass ); 244 } 245 } 246 } 247 return type; 248 249 } 250 251 public static CollectionType customCollection(String typeName, String role, String propertyRef, 252 boolean embedded) { 253 Class typeClass; 254 try { 255 typeClass = ReflectHelper.classForName( typeName ); 256 } 257 catch (ClassNotFoundException 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 265 public static CollectionType array(String role, String propertyRef, boolean embedded, 266 Class elementClass) { 267 return new ArrayType( role, propertyRef, elementClass, embedded ); 268 } 269 270 public static CollectionType list(String role, String propertyRef, boolean embedded) { 271 return new ListType( role, propertyRef, embedded ); 272 } 273 274 public static CollectionType bag(String role, String propertyRef, boolean embedded) { 275 return new BagType( role, propertyRef, embedded ); 276 } 277 278 public static CollectionType idbag(String role, String propertyRef, boolean embedded) { 279 return new IdentifierBagType( role, propertyRef, embedded ); 280 } 281 282 public static CollectionType map(String role, String propertyRef, boolean embedded) { 283 return new MapType( role, propertyRef, embedded ); 284 } 285 286 public static CollectionType set(String role, String propertyRef, boolean embedded) { 287 return new SetType( role, propertyRef, embedded ); 288 } 289 290 public static CollectionType sortedMap(String role, String propertyRef, boolean embedded, 291 Comparator comparator) { 292 return new SortedMapType( role, propertyRef, comparator, embedded ); 293 } 294 295 public static CollectionType sortedSet(String role, String propertyRef, boolean embedded, 296 Comparator comparator) { 297 return new SortedSetType( role, propertyRef, comparator, embedded ); 298 } 299 300 303 public static void deepCopy(Object [] values, Type[] types, boolean[] copy, Object [] 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 323 342 343 347 367 368 public static Object [] assemble(Serializable [] row, Type[] types, SessionImplementor session, 369 Object owner) throws HibernateException { 370 Object [] assembled = new Object [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 [] disassemble(Object [] row, Type[] types, SessionImplementor session, 384 Object owner) throws HibernateException { 385 Serializable [] disassembled = new Serializable [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 ) 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 [] replace(Object [] original, Object [] target, Type[] types, 399 SessionImplementor session, Object owner, Map copyCache) throws HibernateException { 400 Object [] copied = new Object [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 [] replace(Object [] original, Object [] target, Type[] types, 414 SessionImplementor session, Object owner, Map copyCache, 415 ForeignKeyDirection foreignKeyDirection) throws HibernateException { 416 Object [] copied = new Object [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 437 public static int[] findDirty( 438 final StandardProperty[] properties, 439 final Object [] x, 440 final Object [] 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 && 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 480 public static int[] findModified( 481 final StandardProperty[] properties, 482 final Object [] x, 483 final Object [] 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 && 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 type, Properties 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 |