1 package org.hibernate.cfg.annotations; 3 4 import java.util.Properties ; 5 6 import org.apache.commons.logging.Log; 7 import org.apache.commons.logging.LogFactory; 8 import org.hibernate.annotations.Parameter; 9 import org.hibernate.annotations.Type; 10 import org.hibernate.cfg.AnnotationBinder; 11 import org.hibernate.cfg.Ejb3Column; 12 import org.hibernate.cfg.Mappings; 13 import org.hibernate.mapping.SimpleValue; 14 import org.hibernate.mapping.Table; 15 16 19 public class SimpleValueBinder { 20 private static Log log = LogFactory.getLog(SimpleValueBinder.class); 21 private String propertyName; 22 private String returnedClassName; 23 private Ejb3Column[] columns; 24 private String persistentClassName; 25 private String explicitType = ""; 26 private Properties typeParameters = new Properties (); 27 private Mappings mappings; 28 29 public void setPropertyName(String propertyName) { 30 this.propertyName = propertyName; 31 } 32 33 public void setReturnedClassName(String returnedClassName) { 34 this.returnedClassName = returnedClassName; 35 } 36 37 public void setColumns(Ejb3Column[] columns) { 38 this.columns = columns; 39 } 40 41 42 public void setPersistentClassName(String persistentClassName) { 43 this.persistentClassName = persistentClassName; 44 } 45 46 public void setExplicitType(String explicitType) { 47 this.explicitType = explicitType; 48 } 49 public void setExplicitType(Type typeAnn) { 51 if (typeAnn!= null) { 52 explicitType = typeAnn.type(); 53 for ( Parameter param : typeAnn.parameters() ) { 54 typeParameters.setProperty( param.name(), param.value() ); 55 } 56 } 57 } 58 59 public void setTypeParameters(Properties typeParameters) { 60 this.typeParameters = typeParameters; 61 } 62 63 public void setMappings(Mappings mappings) { 64 this.mappings = mappings; 65 } 66 67 private void validate() { 68 Ejb3Column.checkPropertyConsistency(columns, propertyName); 70 } 71 72 public SimpleValue make() { 73 validate(); 74 log.debug("building SimpleValue for " + propertyName); 75 Table table = columns[0].getTable(); 76 SimpleValue simpleValue = new SimpleValue(table); 77 return fillSimpleValue(simpleValue); 78 } 79 80 private SimpleValue fillSimpleValue(SimpleValue simpleValue) { 81 String type = AnnotationBinder.isDefault( explicitType ) ? returnedClassName : explicitType; 82 org.hibernate.mapping.TypeDef typeDef = mappings.getTypeDef(type); 83 if (typeDef != null) { 84 type = typeDef.getTypeClass(); 85 simpleValue.setTypeParameters( typeDef.getParameters() ); 86 } 87 if ( typeParameters != null && typeParameters.size() != 0) { 88 simpleValue.setTypeParameters( typeParameters ); 90 } 91 simpleValue.setTypeName(type); 92 if (persistentClassName != null) { 93 simpleValue.setTypeUsingReflection( persistentClassName, propertyName ); 94 } 95 for (Ejb3Column column : columns) { 96 column.linkWithValue(simpleValue); 97 } 98 return simpleValue; 99 } 100 } 101 | Popular Tags |