001: package org.hibernate.tuple.component;
002:
003: import org.hibernate.tuple.EntityModeToTuplizerMapping;
004: import org.hibernate.tuple.Tuplizer;
005: import org.hibernate.mapping.Component;
006: import org.hibernate.mapping.PersistentClass;
007: import org.hibernate.EntityMode;
008: import org.hibernate.HibernateException;
009: import org.hibernate.util.ReflectHelper;
010:
011: import java.util.Map;
012: import java.util.HashMap;
013: import java.util.Iterator;
014: import java.io.Serializable;
015:
016: /**
017: * Handles mapping {@link EntityMode}s to {@link ComponentTuplizer}s.
018: * <p/>
019: * Most of the handling is really in the super class; here we just create
020: * the tuplizers and add them to the superclass
021: *
022: * @author Steve Ebersole
023: */
024: class ComponentEntityModeToTuplizerMapping extends
025: EntityModeToTuplizerMapping implements Serializable {
026:
027: private static final Class[] COMPONENT_TUP_CTOR_SIG = new Class[] { Component.class };
028:
029: public ComponentEntityModeToTuplizerMapping(Component component) {
030: PersistentClass owner = component.getOwner();
031:
032: // create our own copy of the user-supplied tuplizer impl map
033: Map userSuppliedTuplizerImpls = new HashMap();
034: if (component.getTuplizerMap() != null) {
035: userSuppliedTuplizerImpls
036: .putAll(component.getTuplizerMap());
037: }
038:
039: // Build the dynamic-map tuplizer...
040: Tuplizer dynamicMapTuplizer = null;
041: String tuplizerImpl = (String) userSuppliedTuplizerImpls
042: .remove(EntityMode.MAP);
043: if (tuplizerImpl == null) {
044: dynamicMapTuplizer = new DynamicMapComponentTuplizer(
045: component);
046: } else {
047: dynamicMapTuplizer = buildComponentTuplizer(tuplizerImpl,
048: component);
049: }
050:
051: // then the pojo tuplizer, using the dynamic-map tuplizer if no pojo representation is available
052: tuplizerImpl = (String) userSuppliedTuplizerImpls
053: .remove(EntityMode.POJO);
054: Tuplizer pojoTuplizer = null;
055: if (owner.hasPojoRepresentation()
056: && component.hasPojoRepresentation()) {
057: if (tuplizerImpl == null) {
058: pojoTuplizer = new PojoComponentTuplizer(component);
059: } else {
060: pojoTuplizer = buildComponentTuplizer(tuplizerImpl,
061: component);
062: }
063: } else {
064: pojoTuplizer = dynamicMapTuplizer;
065: }
066:
067: // then dom4j tuplizer, if dom4j representation is available
068: Tuplizer dom4jTuplizer = null;
069: tuplizerImpl = (String) userSuppliedTuplizerImpls
070: .remove(EntityMode.DOM4J);
071: if (owner.hasDom4jRepresentation()) {
072: if (tuplizerImpl == null) {
073: dom4jTuplizer = new Dom4jComponentTuplizer(component);
074: } else {
075: dom4jTuplizer = buildComponentTuplizer(tuplizerImpl,
076: component);
077: }
078: } else {
079: dom4jTuplizer = null;
080: }
081:
082: // put the "standard" tuplizers into the tuplizer map first
083: if (pojoTuplizer != null) {
084: addTuplizer(EntityMode.POJO, pojoTuplizer);
085: }
086: if (dynamicMapTuplizer != null) {
087: addTuplizer(EntityMode.MAP, dynamicMapTuplizer);
088: }
089: if (dom4jTuplizer != null) {
090: addTuplizer(EntityMode.DOM4J, dom4jTuplizer);
091: }
092:
093: // then handle any user-defined entity modes...
094: if (!userSuppliedTuplizerImpls.isEmpty()) {
095: Iterator itr = userSuppliedTuplizerImpls.entrySet()
096: .iterator();
097: while (itr.hasNext()) {
098: Map.Entry entry = (Map.Entry) itr.next();
099: EntityMode entityMode = (EntityMode) entry.getKey();
100: ComponentTuplizer tuplizer = buildComponentTuplizer(
101: (String) entry.getValue(), component);
102: addTuplizer(entityMode, tuplizer);
103: }
104: }
105: }
106:
107: private ComponentTuplizer buildComponentTuplizer(
108: String tuplizerImpl, Component component) {
109: try {
110: Class implClass = ReflectHelper.classForName(tuplizerImpl);
111: return (ComponentTuplizer) implClass.getConstructor(
112: COMPONENT_TUP_CTOR_SIG).newInstance(
113: new Object[] { component });
114: } catch (Throwable t) {
115: throw new HibernateException("Could not build tuplizer ["
116: + tuplizerImpl + "]", t);
117: }
118: }
119: }
|