KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > cfg > annotations > SimpleValueBinder


1 //$Id: SimpleValueBinder.java,v 1.2 2005/05/16 17:43:07 epbernard Exp $
2
package org.hibernate.cfg.annotations;
3
4 import java.util.Properties JavaDoc;
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 /**
17  * @author Emmanuel Bernard
18  */

19 public class SimpleValueBinder {
20     private static Log log = LogFactory.getLog(SimpleValueBinder.class);
21     private String JavaDoc propertyName;
22     private String JavaDoc returnedClassName;
23     private Ejb3Column[] columns;
24     private String JavaDoc persistentClassName;
25     private String JavaDoc explicitType = "";
26     private Properties JavaDoc typeParameters = new Properties JavaDoc();
27     private Mappings mappings;
28
29     public void setPropertyName(String JavaDoc propertyName) {
30         this.propertyName = propertyName;
31     }
32
33     public void setReturnedClassName(String JavaDoc 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 JavaDoc persistentClassName) {
43         this.persistentClassName = persistentClassName;
44     }
45
46     public void setExplicitType(String JavaDoc explicitType) {
47         this.explicitType = explicitType;
48     }
49     //FIXME raise an assertion failure if setExplicitType(String) and setExplicitType(Type) are use at the same time
50
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 JavaDoc typeParameters) {
60         this.typeParameters = typeParameters;
61     }
62
63     public void setMappings(Mappings mappings) {
64         this.mappings = mappings;
65     }
66
67     private void validate() {
68         //TODO check necessary params
69
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 JavaDoc 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             //explicit type params takes precedence over type def params
89
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