KickJava   Java API By Example, From Geeks To Geeks.

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


1 //$Id: Component.java,v 1.18 2005/07/21 01:11:51 oneovthafew Exp $
2
package org.hibernate.mapping;
3
4 import java.util.ArrayList JavaDoc;
5 import java.util.HashMap JavaDoc;
6 import java.util.Iterator JavaDoc;
7 import java.util.Map JavaDoc;
8
9 import org.hibernate.EntityMode;
10 import org.hibernate.FetchMode;
11 import org.hibernate.MappingException;
12 import org.hibernate.engine.CascadeStyle;
13 import org.hibernate.tuple.TuplizerLookup;
14 import org.hibernate.type.ComponentType;
15 import org.hibernate.type.EmbeddedComponentType;
16 import org.hibernate.type.Type;
17 import org.hibernate.util.JoinedIterator;
18 import org.hibernate.util.ReflectHelper;
19
20 /**
21  * The mapping for a component, composite element,
22  * composite identifier, etc.
23  * @author Gavin King
24  */

25 public class Component extends SimpleValue implements MetaAttributable {
26
27     private ArrayList JavaDoc properties = new ArrayList JavaDoc();
28     private String JavaDoc componentClassName;
29     private boolean embedded;
30     private String JavaDoc parentProperty;
31     private PersistentClass owner;
32     private boolean dynamic;
33     private Map metaAttributes;
34     private String JavaDoc nodeName;
35     private boolean isKey;
36
37     private java.util.Map JavaDoc tuplizerImpls;
38
39     public int getPropertySpan() {
40         return properties.size();
41     }
42     public Iterator JavaDoc getPropertyIterator() {
43         return properties.iterator();
44     }
45     public void addProperty(Property p) {
46         properties.add(p);
47     }
48     public void addColumn(Column column) {
49         throw new UnsupportedOperationException JavaDoc("Cant add a column to a component");
50     }
51     public int getColumnSpan() {
52         int n=0;
53         Iterator JavaDoc iter = getPropertyIterator();
54         while ( iter.hasNext() ) {
55             Property p = (Property) iter.next();
56             n+= p.getColumnSpan();
57         }
58         return n;
59     }
60     public Iterator JavaDoc getColumnIterator() {
61         Iterator JavaDoc[] iters = new Iterator JavaDoc[ getPropertySpan() ];
62         Iterator JavaDoc iter = getPropertyIterator();
63         int i=0;
64         while ( iter.hasNext() ) {
65             iters[i++] = ( (Property) iter.next() ).getColumnIterator();
66         }
67         return new JoinedIterator(iters);
68     }
69
70     public Component(PersistentClass owner) throws MappingException {
71         super( owner.getTable() );
72         this.owner = owner;
73     }
74
75     public Component(Component component) throws MappingException {
76         super( component.getTable() );
77         this.owner = component.getOwner();
78     }
79
80     public Component(Join join) throws MappingException {
81         super( join.getTable() );
82         this.owner = join.getPersistentClass();
83     }
84
85     public Component(Collection collection) throws MappingException {
86         super( collection.getCollectionTable() );
87         this.owner = collection.getOwner();
88     }
89
90     public void setTypeByReflection(String JavaDoc propertyClass, String JavaDoc propertyName) {}
91
92     public boolean isEmbedded() {
93         return embedded;
94     }
95
96     public String JavaDoc getComponentClassName() {
97         return componentClassName;
98     }
99
100     public Class JavaDoc getComponentClass() throws MappingException {
101         try {
102             return ReflectHelper.classForName(componentClassName);
103         }
104         catch (ClassNotFoundException JavaDoc cnfe) {
105             throw new MappingException("component class not found: " + componentClassName, cnfe);
106         }
107     }
108
109     public PersistentClass getOwner() {
110         return owner;
111     }
112
113     public String JavaDoc getParentProperty() {
114         return parentProperty;
115     }
116
117     public void setComponentClassName(String JavaDoc componentClass) {
118         this.componentClassName = componentClass;
119     }
120
121     public void setEmbedded(boolean embedded) {
122         this.embedded = embedded;
123     }
124
125     public void setOwner(PersistentClass owner) {
126         this.owner = owner;
127     }
128
129     public void setParentProperty(String JavaDoc parentProperty) {
130         this.parentProperty = parentProperty;
131     }
132
133     public boolean isDynamic() {
134         return dynamic;
135     }
136
137     public void setDynamic(boolean dynamic) {
138         this.dynamic = dynamic;
139     }
140
141     public Type getType() throws MappingException {
142         final int span = getPropertySpan();
143         String JavaDoc[] names = new String JavaDoc[span];
144         org.hibernate.type.Type[] types = new org.hibernate.type.Type[span];
145         boolean[] nullabilities = new boolean[span];
146         CascadeStyle[] cascade = new CascadeStyle[span];
147         FetchMode[] joinedFetch = new FetchMode[span];
148         Iterator JavaDoc props = getPropertyIterator();
149         int j=0;
150         while ( props.hasNext() ) {
151             Property prop = (Property) props.next();
152             names[j] = prop.getName();
153             types[j] = prop.getType();
154             cascade[j] = prop.getCascadeStyle();
155             joinedFetch[j] = prop.getValue().getFetchMode();
156             nullabilities[j] = prop.isNullable();
157             j++;
158         }
159             
160         TuplizerLookup tuplizers = TuplizerLookup.create(this);
161         
162         if ( isEmbedded() ) {
163             return new EmbeddedComponentType(
164                     names,
165                     types,
166                     nullabilities,
167                     joinedFetch,
168                     cascade,
169                     isKey,
170                     tuplizers
171             );
172         }
173         else {
174             return new ComponentType(
175                 names,
176                 types,
177                 nullabilities,
178                 joinedFetch,
179                 cascade,
180                 isKey,
181                 tuplizers
182             );
183         }
184     
185     }
186
187     public void setTypeUsingReflection(String JavaDoc className, String JavaDoc propertyName)
188         throws MappingException {
189     }
190     
191     public java.util.Map JavaDoc getMetaAttributes() {
192         return metaAttributes;
193     }
194     public MetaAttribute getMetaAttribute(String JavaDoc attributeName) {
195         return (MetaAttribute) metaAttributes.get(attributeName);
196     }
197
198     public void setMetaAttributes(java.util.Map JavaDoc metas) {
199         this.metaAttributes = metas;
200     }
201     
202     public Object JavaDoc accept(ValueVisitor visitor) {
203         return visitor.accept(this);
204     }
205     
206     public boolean[] getColumnInsertability() {
207         boolean[] result = new boolean[ getColumnSpan() ];
208         Iterator JavaDoc iter = getPropertyIterator();
209         int i=0;
210         while ( iter.hasNext() ) {
211             Property prop = (Property) iter.next();
212             boolean[] chunk = prop.getValue().getColumnInsertability();
213             if ( prop.isInsertable() ) {
214                 System.arraycopy(chunk, 0, result, i, chunk.length);
215             }
216             i+=chunk.length;
217         }
218         return result;
219     }
220
221     public boolean[] getColumnUpdateability() {
222         boolean[] result = new boolean[ getColumnSpan() ];
223         Iterator JavaDoc iter = getPropertyIterator();
224         int i=0;
225         while ( iter.hasNext() ) {
226             Property prop = (Property) iter.next();
227             boolean[] chunk = prop.getValue().getColumnUpdateability();
228             if ( prop.isUpdateable() ) {
229                 System.arraycopy(chunk, 0, result, i, chunk.length);
230             }
231             i+=chunk.length;
232         }
233         return result;
234     }
235     
236     public String JavaDoc getNodeName() {
237         return nodeName;
238     }
239     
240     public void setNodeName(String JavaDoc nodeName) {
241         this.nodeName = nodeName;
242     }
243     
244     public boolean isKey() {
245         return isKey;
246     }
247     
248     public void setKey(boolean isKey) {
249         this.isKey = isKey;
250     }
251     
252     public boolean hasPojoRepresentation() {
253         return componentClassName!=null;
254     }
255
256     public void addTuplizer(EntityMode entityMode, String JavaDoc implClassName) {
257         if ( tuplizerImpls == null ) {
258             tuplizerImpls = new HashMap JavaDoc();
259         }
260         tuplizerImpls.put( entityMode, implClassName );
261     }
262
263     public String JavaDoc getTuplizerImplClassName(EntityMode mode) {
264         if ( tuplizerImpls == null ) return null;
265         return ( String JavaDoc ) tuplizerImpls.get( mode );
266     }
267
268     public Property getProperty(String JavaDoc propertyName) throws MappingException {
269         Iterator JavaDoc iter = getPropertyIterator();
270         while ( iter.hasNext() ) {
271             Property prop = (Property) iter.next();
272             if ( prop.getName().equals(propertyName) ) {
273                 return prop;
274             }
275         }
276         throw new MappingException("component property not found: " + propertyName);
277     }
278
279     public String JavaDoc toString() {
280         return getClass().getName() + '(' + properties.toString() + ')';
281     }
282
283 }
284
Popular Tags