KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > mapping > SimpleValue


1 //$Id: SimpleValue.java,v 1.20 2005/04/26 06:37:53 oneovthafew Exp $
2
package org.hibernate.mapping;
3
4 import java.util.ArrayList JavaDoc;
5 import java.util.Iterator JavaDoc;
6 import java.util.List JavaDoc;
7 import java.util.Properties JavaDoc;
8
9 import org.hibernate.FetchMode;
10 import org.hibernate.MappingException;
11 import org.hibernate.dialect.Dialect;
12 import org.hibernate.engine.Mapping;
13 import org.hibernate.id.IdentifierGenerator;
14 import org.hibernate.id.IdentifierGeneratorFactory;
15 import org.hibernate.id.IdentityGenerator;
16 import org.hibernate.id.PersistentIdentifierGenerator;
17 import org.hibernate.type.Type;
18 import org.hibernate.type.TypeFactory;
19 import org.hibernate.util.ReflectHelper;
20
21 /**
22  * Any value that maps to columns.
23  * @author Gavin King
24  */

25 public class SimpleValue implements KeyValue {
26
27     private final List columns = new ArrayList JavaDoc();
28     private String JavaDoc typeName;
29     private Properties JavaDoc identifierGeneratorProperties;
30     private String JavaDoc identifierGeneratorStrategy = "assigned";
31     private String JavaDoc nullValue;
32     private Table table;
33     private String JavaDoc foreignKeyName;
34     private boolean alternateUniqueKey;
35     private Properties JavaDoc typeParameters;
36     private boolean cascadeDeleteEnabled;
37
38     public boolean isCascadeDeleteEnabled() {
39         return cascadeDeleteEnabled;
40     }
41
42     public void setCascadeDeleteEnabled(boolean cascadeDeleteEnabled) {
43         this.cascadeDeleteEnabled = cascadeDeleteEnabled;
44     }
45     
46     public void addColumn(Column column) {
47         if ( !columns.contains(column) ) columns.add(column);
48         column.setValue(this);
49         column.setTypeIndex( columns.size()-1 );
50     }
51     
52     public void addFormula(Formula formula) {
53         columns.add(formula);
54     }
55     
56     public boolean hasFormula() {
57         Iterator JavaDoc iter = getColumnIterator();
58         while ( iter.hasNext() ) {
59             Object JavaDoc o = iter.next();
60             if (o instanceof Formula) return true;
61         }
62         return false;
63     }
64
65     public int getColumnSpan() {
66         return columns.size();
67     }
68     public Iterator JavaDoc getColumnIterator() {
69         return columns.iterator();
70     }
71     public List getConstraintColumns() {
72         return columns;
73     }
74     public String JavaDoc getTypeName() {
75         return typeName;
76     }
77     public void setTypeName(String JavaDoc type) {
78         this.typeName = type;
79     }
80     public void setTable(Table table) {
81         this.table = table;
82     }
83     
84     public SimpleValue(Table table) {
85         this.table = table;
86     }
87
88     public SimpleValue() {}
89
90     public void createForeignKey() throws MappingException {}
91
92     public void createForeignKeyOfEntity(String JavaDoc entityName) {
93         if ( !hasFormula() && !"none".equals(getForeignKeyName())) {
94             ForeignKey fk = table.createForeignKey( getForeignKeyName(), getConstraintColumns(), entityName );
95             fk.setCascadeDeleteEnabled(cascadeDeleteEnabled);
96         }
97     }
98
99     public IdentifierGenerator createIdentifierGenerator(
100             Dialect dialect,
101             String JavaDoc defaultCatalog,
102             String JavaDoc defaultSchema,
103             RootClass rootClass)
104     throws MappingException {
105         
106         Properties JavaDoc params = new Properties JavaDoc();
107         
108         //if the hibernate-mapping did not specify a schema/catalog, use the defaults
109
//specified by properties - but note that if the schema/catalog were specified
110
//in hibernate-mapping, or as params, they will already be initialized and
111
//will override the values set here (they are in identifierGeneratorProperties)
112
if ( defaultSchema!=null ) {
113             params.setProperty(PersistentIdentifierGenerator.SCHEMA, defaultSchema);
114         }
115         if ( defaultCatalog!=null ) {
116             params.setProperty(PersistentIdentifierGenerator.CATALOG, defaultCatalog);
117         }
118         
119         //pass the entity-name, if not a collection-id
120
if (rootClass!=null) {
121             params.setProperty( IdentifierGenerator.ENTITY_NAME, rootClass.getEntityName() );
122         }
123         
124         //init the table here instead of earlier, so that we can get a quoted table name
125
//TODO: would it be better to simply pass the qualified table name, instead of
126
// splitting it up into schema/catalog/table names
127
String JavaDoc tableName = getTable().getQuotedName(dialect);
128         params.setProperty( PersistentIdentifierGenerator.TABLE, tableName );
129         
130         //pass the column name (a generated id almost always has a single column)
131
params.setProperty( PersistentIdentifierGenerator.PK, ( (Column) getColumnIterator().next() ).getName() );
132         
133         if (rootClass!=null) {
134             StringBuffer JavaDoc tables = new StringBuffer JavaDoc();
135             Iterator JavaDoc iter = rootClass.getIdentityTables().iterator();
136             while ( iter.hasNext() ) {
137                 Table table= (Table) iter.next();
138                 tables.append( table.getQuotedName(dialect) );
139                 if ( iter.hasNext() ) tables.append(", ");
140             }
141             params.setProperty( PersistentIdentifierGenerator.TABLES, tables.toString() );
142         }
143         else {
144             params.setProperty( PersistentIdentifierGenerator.TABLES, tableName );
145         }
146
147         if (identifierGeneratorProperties!=null) {
148             params.putAll(identifierGeneratorProperties);
149         }
150         
151         return IdentifierGeneratorFactory.create(
152             identifierGeneratorStrategy,
153             getType(),
154             params,
155             dialect
156         );
157         
158     }
159
160     public boolean isUpdateable() {
161         //needed to satisfy KeyValue
162
return true;
163     }
164     
165     public FetchMode getFetchMode() {
166         return FetchMode.SELECT;
167     }
168
169     public Properties JavaDoc getIdentifierGeneratorProperties() {
170         return identifierGeneratorProperties;
171     }
172
173     public String JavaDoc getNullValue() {
174         return nullValue;
175     }
176
177     public Table getTable() {
178         return table;
179     }
180
181     /**
182      * Returns the identifierGeneratorStrategy.
183      * @return String
184      */

185     public String JavaDoc getIdentifierGeneratorStrategy() {
186         return identifierGeneratorStrategy;
187     }
188     
189     public boolean isIdentityColumn(Dialect dialect) {
190         return IdentifierGeneratorFactory.getIdentifierGeneratorClass(identifierGeneratorStrategy, dialect)
191                 .equals(IdentityGenerator.class);
192     }
193
194     /**
195      * Sets the identifierGeneratorProperties.
196      * @param identifierGeneratorProperties The identifierGeneratorProperties to set
197      */

198     public void setIdentifierGeneratorProperties(Properties JavaDoc identifierGeneratorProperties) {
199         this.identifierGeneratorProperties = identifierGeneratorProperties;
200     }
201
202     /**
203      * Sets the identifierGeneratorStrategy.
204      * @param identifierGeneratorStrategy The identifierGeneratorStrategy to set
205      */

206     public void setIdentifierGeneratorStrategy(String JavaDoc identifierGeneratorStrategy) {
207         this.identifierGeneratorStrategy = identifierGeneratorStrategy;
208     }
209
210     /**
211      * Sets the nullValue.
212      * @param nullValue The nullValue to set
213      */

214     public void setNullValue(String JavaDoc nullValue) {
215         this.nullValue = nullValue;
216     }
217
218     public String JavaDoc getForeignKeyName() {
219         return foreignKeyName;
220     }
221
222     public void setForeignKeyName(String JavaDoc foreignKeyName) {
223         this.foreignKeyName = foreignKeyName;
224     }
225
226     public boolean isAlternateUniqueKey() {
227         return alternateUniqueKey;
228     }
229
230     public void setAlternateUniqueKey(boolean unique) {
231         this.alternateUniqueKey = unique;
232     }
233
234     public boolean isNullable() {
235         if ( hasFormula() ) return true;
236         boolean nullable = true;
237         Iterator JavaDoc iter = getColumnIterator();
238         while ( iter.hasNext() ) {
239             if ( !( (Column) iter.next() ).isNullable() ) {
240                 nullable = false;
241                 return nullable; //shortcut
242
}
243         }
244         return nullable;
245     }
246
247     public boolean isSimpleValue() {
248         return true;
249     }
250
251     public boolean isValid(Mapping mapping) throws MappingException {
252         return getColumnSpan()==getType().getColumnSpan(mapping);
253     }
254
255     public Type getType() throws MappingException {
256         if (typeName==null) {
257             throw new MappingException("No type name");
258         }
259         Type result = TypeFactory.heuristicType(typeName, typeParameters);
260         if (result==null) {
261             String JavaDoc msg = "Could not determine type for: " + typeName;
262             if(columns!=null && columns.size()>0) {
263                 msg += ", for columns: " + columns;
264             }
265             throw new MappingException(msg);
266         }
267         return result;
268     }
269
270     public void setTypeUsingReflection(String JavaDoc className, String JavaDoc propertyName) throws MappingException {
271         if (typeName==null) {
272             if (className==null) {
273                 throw new MappingException("you must specify types for a dynamic entity: " + propertyName);
274             }
275             typeName = ReflectHelper.reflectedPropertyClass(className, propertyName).getName();
276         }
277     }
278
279     public boolean isTypeSpecified() {
280         return typeName!=null;
281     }
282
283     public void setTypeParameters(Properties JavaDoc parameterMap) {
284         this.typeParameters = parameterMap;
285     }
286     
287     public Properties JavaDoc getTypeParameters() {
288         return typeParameters;
289     }
290
291     public String JavaDoc toString() {
292         return getClass().getName() + '(' + columns.toString() + ')';
293     }
294
295     public Object JavaDoc accept(ValueVisitor visitor) {
296         return visitor.accept(this);
297     }
298     
299     public boolean[] getColumnInsertability() {
300         boolean[] result = new boolean[ getColumnSpan() ];
301         int i = 0;
302         Iterator JavaDoc iter = getColumnIterator();
303         while ( iter.hasNext() ) {
304             Selectable s = (Selectable) iter.next();
305             result[i++] = !s.isFormula();
306         }
307         return result;
308     }
309     
310     public boolean[] getColumnUpdateability() {
311         return getColumnInsertability();
312     }
313 }
314
Popular Tags