001: //$Id: Component.java 10119 2006-07-14 00:09:19Z steve.ebersole@jboss.com $
002: package org.hibernate.mapping;
003:
004: import java.util.ArrayList;
005: import java.util.HashMap;
006: import java.util.Iterator;
007: import java.util.Map;
008:
009: import org.hibernate.EntityMode;
010: import org.hibernate.MappingException;
011: import org.hibernate.tuple.component.ComponentMetamodel;
012: import org.hibernate.type.ComponentType;
013: import org.hibernate.type.EmbeddedComponentType;
014: import org.hibernate.type.Type;
015: import org.hibernate.util.JoinedIterator;
016: import org.hibernate.util.ReflectHelper;
017:
018: /**
019: * The mapping for a component, composite element,
020: * composite identifier, etc.
021: * @author Gavin King
022: */
023: public class Component extends SimpleValue implements MetaAttributable {
024:
025: private ArrayList properties = new ArrayList();
026: private String componentClassName;
027: private boolean embedded;
028: private String parentProperty;
029: private PersistentClass owner;
030: private boolean dynamic;
031: private Map metaAttributes;
032: private String nodeName;
033: private boolean isKey;
034: private String roleName;
035:
036: private java.util.Map tuplizerImpls;
037:
038: public Component(PersistentClass owner) throws MappingException {
039: super (owner.getTable());
040: this .owner = owner;
041: }
042:
043: public Component(Component component) throws MappingException {
044: super (component.getTable());
045: this .owner = component.getOwner();
046: }
047:
048: public Component(Join join) throws MappingException {
049: super (join.getTable());
050: this .owner = join.getPersistentClass();
051: }
052:
053: public Component(Collection collection) throws MappingException {
054: super (collection.getCollectionTable());
055: this .owner = collection.getOwner();
056: }
057:
058: public int getPropertySpan() {
059: return properties.size();
060: }
061:
062: public Iterator getPropertyIterator() {
063: return properties.iterator();
064: }
065:
066: public void addProperty(Property p) {
067: properties.add(p);
068: }
069:
070: public void addColumn(Column column) {
071: throw new UnsupportedOperationException(
072: "Cant add a column to a component");
073: }
074:
075: public int getColumnSpan() {
076: int n = 0;
077: Iterator iter = getPropertyIterator();
078: while (iter.hasNext()) {
079: Property p = (Property) iter.next();
080: n += p.getColumnSpan();
081: }
082: return n;
083: }
084:
085: public Iterator getColumnIterator() {
086: Iterator[] iters = new Iterator[getPropertySpan()];
087: Iterator iter = getPropertyIterator();
088: int i = 0;
089: while (iter.hasNext()) {
090: iters[i++] = ((Property) iter.next()).getColumnIterator();
091: }
092: return new JoinedIterator(iters);
093: }
094:
095: public void setTypeByReflection(String propertyClass,
096: String propertyName) {
097: }
098:
099: public boolean isEmbedded() {
100: return embedded;
101: }
102:
103: public String getComponentClassName() {
104: return componentClassName;
105: }
106:
107: public Class getComponentClass() throws MappingException {
108: try {
109: return ReflectHelper.classForName(componentClassName);
110: } catch (ClassNotFoundException cnfe) {
111: throw new MappingException("component class not found: "
112: + componentClassName, cnfe);
113: }
114: }
115:
116: public PersistentClass getOwner() {
117: return owner;
118: }
119:
120: public String getParentProperty() {
121: return parentProperty;
122: }
123:
124: public void setComponentClassName(String componentClass) {
125: this .componentClassName = componentClass;
126: }
127:
128: public void setEmbedded(boolean embedded) {
129: this .embedded = embedded;
130: }
131:
132: public void setOwner(PersistentClass owner) {
133: this .owner = owner;
134: }
135:
136: public void setParentProperty(String parentProperty) {
137: this .parentProperty = parentProperty;
138: }
139:
140: public boolean isDynamic() {
141: return dynamic;
142: }
143:
144: public void setDynamic(boolean dynamic) {
145: this .dynamic = dynamic;
146: }
147:
148: private Type type;
149:
150: public Type getType() throws MappingException {
151: // added this caching as I noticed that getType() is being called multiple times...
152: if (type == null) {
153: type = buildType();
154: }
155: return type;
156: }
157:
158: private Type buildType() {
159: // TODO : temporary initial step towards HHH-1907
160: ComponentMetamodel metamodel = new ComponentMetamodel(this );
161: if (isEmbedded()) {
162: return new EmbeddedComponentType(metamodel);
163: } else {
164: return new ComponentType(metamodel);
165: }
166: }
167:
168: public void setTypeUsingReflection(String className,
169: String propertyName) throws MappingException {
170: }
171:
172: public java.util.Map getMetaAttributes() {
173: return metaAttributes;
174: }
175:
176: public MetaAttribute getMetaAttribute(String attributeName) {
177: return metaAttributes == null ? null
178: : (MetaAttribute) metaAttributes.get(attributeName);
179: }
180:
181: public void setMetaAttributes(java.util.Map metas) {
182: this .metaAttributes = metas;
183: }
184:
185: public Object accept(ValueVisitor visitor) {
186: return visitor.accept(this );
187: }
188:
189: public boolean[] getColumnInsertability() {
190: boolean[] result = new boolean[getColumnSpan()];
191: Iterator iter = getPropertyIterator();
192: int i = 0;
193: while (iter.hasNext()) {
194: Property prop = (Property) iter.next();
195: boolean[] chunk = prop.getValue().getColumnInsertability();
196: if (prop.isInsertable()) {
197: System.arraycopy(chunk, 0, result, i, chunk.length);
198: }
199: i += chunk.length;
200: }
201: return result;
202: }
203:
204: public boolean[] getColumnUpdateability() {
205: boolean[] result = new boolean[getColumnSpan()];
206: Iterator iter = getPropertyIterator();
207: int i = 0;
208: while (iter.hasNext()) {
209: Property prop = (Property) iter.next();
210: boolean[] chunk = prop.getValue().getColumnUpdateability();
211: if (prop.isUpdateable()) {
212: System.arraycopy(chunk, 0, result, i, chunk.length);
213: }
214: i += chunk.length;
215: }
216: return result;
217: }
218:
219: public String getNodeName() {
220: return nodeName;
221: }
222:
223: public void setNodeName(String nodeName) {
224: this .nodeName = nodeName;
225: }
226:
227: public boolean isKey() {
228: return isKey;
229: }
230:
231: public void setKey(boolean isKey) {
232: this .isKey = isKey;
233: }
234:
235: public boolean hasPojoRepresentation() {
236: return componentClassName != null;
237: }
238:
239: public void addTuplizer(EntityMode entityMode, String implClassName) {
240: if (tuplizerImpls == null) {
241: tuplizerImpls = new HashMap();
242: }
243: tuplizerImpls.put(entityMode, implClassName);
244: }
245:
246: public String getTuplizerImplClassName(EntityMode mode) {
247: // todo : remove this once ComponentMetamodel is complete and merged
248: if (tuplizerImpls == null) {
249: return null;
250: }
251: return (String) tuplizerImpls.get(mode);
252: }
253:
254: public Map getTuplizerMap() {
255: if (tuplizerImpls == null) {
256: return null;
257: }
258: return java.util.Collections.unmodifiableMap(tuplizerImpls);
259: }
260:
261: public Property getProperty(String propertyName)
262: throws MappingException {
263: Iterator iter = getPropertyIterator();
264: while (iter.hasNext()) {
265: Property prop = (Property) iter.next();
266: if (prop.getName().equals(propertyName)) {
267: return prop;
268: }
269: }
270: throw new MappingException("component property not found: "
271: + propertyName);
272: }
273:
274: public String getRoleName() {
275: return roleName;
276: }
277:
278: public void setRoleName(String roleName) {
279: this .roleName = roleName;
280: }
281:
282: public String toString() {
283: return getClass().getName() + '(' + properties.toString() + ')';
284: }
285:
286: }
|