01: package org.hibernate.tuple.component;
02:
03: import org.hibernate.engine.SessionFactoryImplementor;
04: import org.hibernate.mapping.Component;
05: import org.hibernate.mapping.Property;
06: import org.hibernate.HibernateException;
07: import org.hibernate.tuple.StandardProperty;
08: import org.hibernate.tuple.PropertyFactory;
09:
10: import java.io.Serializable;
11: import java.util.Iterator;
12: import java.util.Map;
13: import java.util.HashMap;
14:
15: /**
16: * Centralizes metamodel information about a component.
17: *
18: * @author Steve Ebersole
19: */
20: public class ComponentMetamodel implements Serializable {
21:
22: // TODO : will need reference to session factory to fully complete HHH-1907
23:
24: // private final SessionFactoryImplementor sessionFactory;
25: private final String role;
26: private final boolean isKey;
27: private final StandardProperty[] properties;
28: private final ComponentEntityModeToTuplizerMapping tuplizerMapping;
29:
30: // cached for efficiency...
31: private final int propertySpan;
32: private final Map propertyIndexes = new HashMap();
33:
34: // public ComponentMetamodel(Component component, SessionFactoryImplementor sessionFactory) {
35: public ComponentMetamodel(Component component) {
36: // this.sessionFactory = sessionFactory;
37: this .role = component.getRoleName();
38: this .isKey = component.isKey();
39: propertySpan = component.getPropertySpan();
40: properties = new StandardProperty[propertySpan];
41: Iterator itr = component.getPropertyIterator();
42: int i = 0;
43: while (itr.hasNext()) {
44: Property property = (Property) itr.next();
45: properties[i] = PropertyFactory.buildStandardProperty(
46: property, false);
47: propertyIndexes.put(property.getName(), new Integer(i));
48: i++;
49: }
50:
51: tuplizerMapping = new ComponentEntityModeToTuplizerMapping(
52: component);
53: }
54:
55: public boolean isKey() {
56: return isKey;
57: }
58:
59: public int getPropertySpan() {
60: return propertySpan;
61: }
62:
63: public StandardProperty[] getProperties() {
64: return properties;
65: }
66:
67: public StandardProperty getProperty(int index) {
68: if (index < 0 || index >= propertySpan) {
69: throw new IllegalArgumentException(
70: "illegal index value for component property access [request="
71: + index + ", span=" + propertySpan + "]");
72: }
73: return properties[index];
74: }
75:
76: public int getPropertyIndex(String propertyName) {
77: Integer index = (Integer) propertyIndexes.get(propertyName);
78: if (index == null) {
79: throw new HibernateException(
80: "component does not contain such a property ["
81: + propertyName + "]");
82: }
83: return index.intValue();
84: }
85:
86: public StandardProperty getProperty(String propertyName) {
87: return getProperty(getPropertyIndex(propertyName));
88: }
89:
90: public ComponentEntityModeToTuplizerMapping getTuplizerMapping() {
91: return tuplizerMapping;
92: }
93:
94: }
|