KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > persister > entity > AbstractPropertyMapping


1 //$Id: AbstractPropertyMapping.java,v 1.4 2005/07/21 01:11:51 oneovthafew Exp $
2
package org.hibernate.persister.entity;
3
4 import java.util.HashMap JavaDoc;
5 import java.util.Map JavaDoc;
6
7 import org.hibernate.MappingException;
8 import org.hibernate.QueryException;
9 import org.hibernate.engine.Mapping;
10 import org.hibernate.sql.Template;
11 import org.hibernate.type.AbstractComponentType;
12 import org.hibernate.type.AssociationType;
13 import org.hibernate.type.EntityType;
14 import org.hibernate.type.Type;
15 import org.hibernate.util.ArrayHelper;
16 import org.hibernate.util.StringHelper;
17
18 /**
19  * Base implementation of a <tt>PropertyMapping</tt>
20  *
21  * @author Gavin King
22  */

23 public abstract class AbstractPropertyMapping implements PropertyMapping {
24
25     private final Map JavaDoc typesByPropertyPath = new HashMap JavaDoc();
26     private final Map JavaDoc columnsByPropertyPath = new HashMap JavaDoc();
27     private final Map JavaDoc formulaTemplatesByPropertyPath = new HashMap JavaDoc();
28
29     public String JavaDoc[] getIdentifierColumnNames() {
30         throw new UnsupportedOperationException JavaDoc("one-to-one is not supported here");
31     }
32
33     protected abstract String JavaDoc getEntityName();
34
35     public Type toType(String JavaDoc propertyName) throws QueryException {
36         Type type = (Type) typesByPropertyPath.get(propertyName);
37         if (type==null) throwPropertyException(propertyName);
38         return type;
39     }
40
41     protected final void throwPropertyException(String JavaDoc propertyName)
42     throws QueryException {
43         throw new QueryException(
44                 "could not resolve property: " +
45                 propertyName +
46                 " of: " +
47                 getEntityName()
48             );
49     }
50     
51     public String JavaDoc[] getColumnNames(String JavaDoc propertyName) {
52         String JavaDoc[] cols = (String JavaDoc[]) columnsByPropertyPath.get(propertyName);
53         if (cols==null) {
54             throw new MappingException("unknown property: " + propertyName);
55         }
56         return cols;
57     }
58
59     public String JavaDoc[] toColumns(String JavaDoc alias, String JavaDoc propertyName)
60     throws QueryException {
61         //TODO: *two* hashmap lookups here is one too many...
62
String JavaDoc[] columns = (String JavaDoc[]) columnsByPropertyPath.get(propertyName);
63         if (columns==null) throwPropertyException(propertyName);
64         String JavaDoc[] templates = (String JavaDoc[]) formulaTemplatesByPropertyPath.get(propertyName);
65         String JavaDoc[] result = new String JavaDoc[columns.length];
66         for ( int i=0; i<columns.length; i++ ) {
67             if ( columns[i]==null ) {
68                 result[i] = StringHelper.replace( templates[i], Template.TEMPLATE, alias );
69             }
70             else {
71                 result[i] = StringHelper.qualify( alias, columns[i] );
72             }
73         }
74         return result;
75     }
76
77     public String JavaDoc[] toColumns(String JavaDoc propertyName)
78     throws QueryException {
79         String JavaDoc[] columns = (String JavaDoc[]) columnsByPropertyPath.get(propertyName);
80         if (columns==null) throwPropertyException(propertyName);
81         String JavaDoc[] templates = (String JavaDoc[]) formulaTemplatesByPropertyPath.get(propertyName);
82         String JavaDoc[] result = new String JavaDoc[columns.length];
83         for ( int i=0; i<columns.length; i++ ) {
84             if ( columns[i]==null ) {
85                 result[i] = StringHelper.replace( templates[i], Template.TEMPLATE, "" );
86             }
87             else {
88                 result[i] = columns[i];
89             }
90         }
91         return result;
92     }
93
94     protected void addPropertyPath(String JavaDoc path, Type type, String JavaDoc[] columns, String JavaDoc[] formulaTemplates) {
95         typesByPropertyPath.put(path, type);
96         columnsByPropertyPath.put(path, columns);
97         if (formulaTemplates!=null) {
98             formulaTemplatesByPropertyPath.put(path, formulaTemplates);
99         }
100     }
101
102     /*protected void initPropertyPaths(
103             final String path,
104             final Type type,
105             final String[] columns,
106             final String[] formulaTemplates,
107             final Mapping factory)
108     throws MappingException {
109         //addFormulaPropertyPath(path, type, formulaTemplates);
110         initPropertyPaths(path, type, columns, formulaTemplates, factory);
111     }*/

112     
113     protected void initPropertyPaths(
114             final String JavaDoc path,
115             final Type type,
116             String JavaDoc[] columns,
117             String JavaDoc[] formulaTemplates, final Mapping factory)
118     throws MappingException {
119         
120         if ( columns.length!=type.getColumnSpan(factory) ) {
121             throw new MappingException( "broken column mapping for: " + path + " of: " + getEntityName() );
122         }
123
124         if ( type.isAssociationType() ) {
125             AssociationType actype = (AssociationType) type;
126             if ( actype.useLHSPrimaryKey() ) {
127                 columns = getIdentifierColumnNames();
128             }
129             else {
130                 String JavaDoc foreignKeyProperty = actype.getLHSPropertyName();
131                 if ( foreignKeyProperty!=null && !path.equals(foreignKeyProperty) ) {
132                     //TODO: this requires that the collection is defined after the
133
// referenced property in the mapping file (ok?)
134
columns = (String JavaDoc[]) columnsByPropertyPath.get(foreignKeyProperty);
135                     if (columns==null) return; //get em on the second pass!
136
}
137             }
138         }
139
140         if (path!=null) addPropertyPath(path, type, columns, formulaTemplates);
141
142         if ( type.isComponentType() ) {
143             initComponentPropertyPaths( path, (AbstractComponentType) type, columns, formulaTemplates, factory );
144         }
145         else if ( type.isEntityType() ) {
146             initIdentifierPropertyPaths( path, (EntityType) type, columns, factory );
147         }
148     }
149
150     protected void initIdentifierPropertyPaths(
151             final String JavaDoc path,
152             final EntityType etype,
153             final String JavaDoc[] columns,
154             final Mapping factory)
155     throws MappingException {
156
157         Type idtype = etype.getIdentifierOrUniqueKeyType(factory);
158
159         if ( etype.isReferenceToPrimaryKey() ) {
160             String JavaDoc idpath1 = extendPath(path, EntityPersister.ENTITY_ID);
161             addPropertyPath(idpath1, idtype, columns, null);
162             initPropertyPaths(idpath1, idtype, columns, null, factory);
163         }
164
165         String JavaDoc idPropName = etype.getIdentifierOrUniqueKeyPropertyName(factory);
166         if (idPropName!=null) {
167             String JavaDoc idpath2 = extendPath(path, idPropName);
168             addPropertyPath(idpath2, idtype, columns, null);
169             initPropertyPaths(idpath2, idtype, columns, null, factory);
170         }
171     }
172
173     protected void initComponentPropertyPaths(
174             final String JavaDoc path,
175             final AbstractComponentType type,
176             final String JavaDoc[] columns,
177             String JavaDoc[] formulaTemplates, final Mapping factory)
178     throws MappingException {
179
180         Type[] types = type.getSubtypes();
181         String JavaDoc[] properties = type.getPropertyNames();
182         int begin=0;
183         for ( int i=0; i<properties.length; i++ ) {
184             String JavaDoc subpath = extendPath( path, properties[i] );
185             try {
186                 int length = types[i].getColumnSpan(factory);
187                 String JavaDoc[] columnSlice = ArrayHelper.slice(columns, begin, length);
188                 String JavaDoc[] formulaSlice = formulaTemplates==null ?
189                         null : ArrayHelper.slice(formulaTemplates, begin, length);
190                 initPropertyPaths(subpath, types[i], columnSlice, formulaSlice, factory);
191                 begin+=length;
192             }
193             catch (Exception JavaDoc e) {
194                 throw new MappingException("bug in initComponentPropertyPaths", e);
195             }
196         }
197     }
198
199     private static String JavaDoc extendPath(String JavaDoc path, String JavaDoc property) {
200         if (path==null) {
201             return property;
202         }
203         else {
204             return StringHelper.qualify(path, property);
205         }
206     }
207
208 }
209
Popular Tags