KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > event > def > WrapVisitor


1 //$Id: WrapVisitor.java,v 1.12 2005/06/17 19:36:08 oneovthafew Exp $
2
package org.hibernate.event.def;
3
4 import org.apache.commons.logging.Log;
5 import org.apache.commons.logging.LogFactory;
6 import org.hibernate.EntityMode;
7 import org.hibernate.HibernateException;
8 import org.hibernate.collection.PersistentCollection;
9 import org.hibernate.engine.PersistenceContext;
10 import org.hibernate.engine.SessionImplementor;
11 import org.hibernate.event.EventSource;
12 import org.hibernate.persister.collection.CollectionPersister;
13 import org.hibernate.persister.entity.EntityPersister;
14 import org.hibernate.type.AbstractComponentType;
15 import org.hibernate.type.CollectionType;
16 import org.hibernate.type.Type;
17
18 /**
19  * Wrap collections in a Hibernate collection
20  * wrapper.
21  * @author Gavin King
22  */

23 public class WrapVisitor extends ProxyVisitor {
24
25     private static final Log log = LogFactory.getLog(WrapVisitor.class);
26
27     boolean substitute = false;
28
29     boolean isSubstitutionRequired() {
30         return substitute;
31     }
32
33     WrapVisitor(EventSource session) {
34         super(session);
35     }
36
37     Object JavaDoc processCollection(Object JavaDoc collection, CollectionType collectionType)
38     throws HibernateException {
39
40         if ( collection!=null && (collection instanceof PersistentCollection) ) {
41
42             final SessionImplementor session = getSession();
43             PersistentCollection coll = (PersistentCollection) collection;
44             if ( coll.setCurrentSession(session) ) {
45                 reattachCollection( coll, collectionType );
46             }
47             return null;
48
49         }
50         else {
51             return processArrayOrNewCollection(collection, collectionType);
52         }
53
54     }
55
56     final Object JavaDoc processArrayOrNewCollection(Object JavaDoc collection, CollectionType collectionType)
57     throws HibernateException {
58
59         final SessionImplementor session = getSession();
60
61         if (collection==null) {
62             //do nothing
63
return null;
64         }
65         else {
66             CollectionPersister persister = session.getFactory().getCollectionPersister( collectionType.getRole() );
67
68             final PersistenceContext persistenceContext = session.getPersistenceContext();
69             //TODO: move into collection type, so we can use polymorphism!
70
if ( collectionType.hasHolder( session.getEntityMode() ) ) {
71                 
72                 if (collection==CollectionType.UNFETCHED_COLLECTION) return null;
73
74                 PersistentCollection ah = persistenceContext.getCollectionHolder(collection);
75                 if (ah==null) {
76                     ah = collectionType.wrap(session, collection);
77                     persistenceContext.addNewCollection( persister, ah );
78                     persistenceContext.addCollectionHolder(ah);
79                 }
80                 return null;
81             }
82             else {
83
84                 PersistentCollection persistentCollection = collectionType.wrap(session, collection);
85                 persistenceContext.addNewCollection( persister, persistentCollection );
86
87                 if ( log.isTraceEnabled() ) log.trace( "Wrapped collection in role: " + collectionType.getRole() );
88
89                 return persistentCollection; //Force a substitution!
90

91             }
92
93         }
94
95     }
96
97     void processValue(int i, Object JavaDoc[] values, Type[] types) {
98         Object JavaDoc result = processValue( values[i], types[i] );
99         if (result!=null) {
100             substitute = true;
101             values[i] = result;
102         }
103     }
104
105     Object JavaDoc processComponent(Object JavaDoc component, AbstractComponentType componentType)
106     throws HibernateException {
107
108         if (component!=null) {
109             Object JavaDoc[] values = componentType.getPropertyValues( component, getSession() );
110             Type[] types = componentType.getSubtypes();
111             boolean substituteComponent = false;
112             for ( int i=0; i<types.length; i++ ) {
113                 Object JavaDoc result = processValue( values[i], types[i] );
114                 if (result!=null) {
115                     values[i] = result;
116                     substituteComponent = true;
117                 }
118             }
119             if (substituteComponent) {
120                 componentType.setPropertyValues( component, values, getSession().getEntityMode() );
121             }
122         }
123
124         return null;
125     }
126
127     void process(Object JavaDoc object, EntityPersister persister) throws HibernateException {
128         EntityMode entityMode = getSession().getEntityMode();
129         Object JavaDoc[] values = persister.getPropertyValues( object, entityMode );
130         Type[] types = persister.getPropertyTypes();
131         processEntityPropertyValues(values, types);
132         if ( isSubstitutionRequired() ) {
133             persister.setPropertyValues( object, values, entityMode );
134         }
135     }
136
137 }
138
Popular Tags