KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > tuple > Dom4jEntityTuplizer


1 // $Id: Dom4jEntityTuplizer.java,v 1.1 2005/07/11 17:31:50 steveebersole Exp $
2
package org.hibernate.tuple;
3
4 import org.hibernate.mapping.PersistentClass;
5 import org.hibernate.mapping.Property;
6 import org.hibernate.proxy.ProxyFactory;
7 import org.hibernate.proxy.HibernateProxy;
8 import org.hibernate.proxy.Dom4jProxyFactory;
9 import org.hibernate.property.PropertyAccessor;
10 import org.hibernate.property.PropertyAccessorFactory;
11 import org.hibernate.property.Getter;
12 import org.hibernate.property.Setter;
13 import org.hibernate.EntityMode;
14 import org.hibernate.HibernateException;
15 import org.hibernate.type.AbstractComponentType;
16 import org.dom4j.Element;
17 import org.apache.commons.logging.Log;
18 import org.apache.commons.logging.LogFactory;
19
20 import java.io.Serializable JavaDoc;
21 import java.util.HashSet JavaDoc;
22 import java.util.Set JavaDoc;
23 import java.util.Iterator JavaDoc;
24
25 /**
26  * Implementation of Dom4jEntityTuplizer.
27  *
28  * @author Steve Ebersole
29  */

30 public class Dom4jEntityTuplizer extends AbstractEntityTuplizer {
31
32     static final Log log = LogFactory.getLog( Dom4jEntityTuplizer.class );
33
34     private Set JavaDoc subclassNodeNames = new HashSet JavaDoc();
35
36     Dom4jEntityTuplizer(EntityMetamodel entityMetamodel, PersistentClass mappedEntity) {
37         super(entityMetamodel, mappedEntity);
38         Iterator JavaDoc itr = mappedEntity.getSubclassClosureIterator();
39         while( itr.hasNext() ) {
40             final PersistentClass mapping = ( PersistentClass ) itr.next();
41             subclassNodeNames.add( mapping.getNodeName() );
42         }
43     }
44     
45     public EntityMode getEntityMode() {
46         return EntityMode.DOM4J;
47     }
48
49     private PropertyAccessor buildPropertyAccessor(Property mappedProperty) {
50         if ( mappedProperty.isBackRef() ) {
51             return mappedProperty.getPropertyAccessor(null);
52         }
53         else {
54             return PropertyAccessorFactory.getDom4jPropertyAccessor(
55                     mappedProperty.getNodeName(),
56                     mappedProperty.getType(),
57                     getEntityMetamodel().getSessionFactory()
58                 );
59         }
60     }
61
62     protected Getter buildPropertyGetter(Property mappedProperty, PersistentClass mappedEntity) {
63         return buildPropertyAccessor(mappedProperty).getGetter( null, mappedProperty.getName() );
64     }
65
66     protected Setter buildPropertySetter(Property mappedProperty, PersistentClass mappedEntity) {
67         return buildPropertyAccessor(mappedProperty).getSetter( null, mappedProperty.getName() );
68     }
69
70     protected Instantiator buildInstantiator(PersistentClass persistentClass) {
71         return new Dom4jInstantiator( persistentClass );
72     }
73
74     public Serializable JavaDoc getIdentifier(Object JavaDoc entityOrId) throws HibernateException {
75         if (entityOrId instanceof Element) {
76             return super.getIdentifier(entityOrId);
77         }
78         else {
79             //it was not embedded, so the argument is just an id
80
return (Serializable JavaDoc) entityOrId;
81         }
82     }
83     
84     protected ProxyFactory buildProxyFactory(PersistentClass mappingInfo, Getter idGetter, Setter idSetter) {
85         HashSet JavaDoc proxyInterfaces = new HashSet JavaDoc();
86         proxyInterfaces.add( HibernateProxy.class );
87         proxyInterfaces.add( Element.class );
88
89         ProxyFactory pf = new Dom4jProxyFactory();
90         try {
91             pf.postInstantiate(
92                     getEntityName(),
93                     Element.class,
94                     proxyInterfaces,
95                     null,
96                     null,
97                     mappingInfo.hasEmbeddedIdentifier() ?
98                             (AbstractComponentType) mappingInfo.getIdentifier().getType() :
99                             null
100             );
101         }
102         catch ( HibernateException he ) {
103             log.warn( "could not create proxy factory for:" + getEntityName(), he );
104             pf = null;
105         }
106         return pf;
107     }
108
109     public Class JavaDoc getMappedClass() {
110         return Element.class;
111     }
112
113     public Class JavaDoc getConcreteProxyClass() {
114         return Element.class;
115     }
116
117     public boolean isInstrumented() {
118         return false;
119     }
120 }
121
Popular Tags