KickJava   Java API By Example, From Geeks To Geeks.

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


1 //$Id: AbstractVisitor.java,v 1.8 2005/07/19 18:17:12 oneovthafew Exp $
2
package org.hibernate.event.def;
3
4 import org.hibernate.HibernateException;
5 import org.hibernate.event.EventSource;
6 import org.hibernate.intercept.LazyPropertyInitializer;
7 import org.hibernate.persister.entity.EntityPersister;
8 import org.hibernate.type.AbstractComponentType;
9 import org.hibernate.type.CollectionType;
10 import org.hibernate.type.EntityType;
11 import org.hibernate.type.Type;
12
13
14 /**
15  * Abstract superclass of algorithms that walk
16  * a tree of property values of an entity, and
17  * perform specific functionality for collections,
18  * components and associated entities.
19  *
20  * @author Gavin King
21  */

22 public abstract class AbstractVisitor {
23
24     private final EventSource session;
25
26     AbstractVisitor(EventSource session) {
27         this.session = session;
28     }
29
30     /**
31      * Dispatch each property value to processValue().
32      *
33      * @param values
34      * @param types
35      * @throws HibernateException
36      */

37     void processValues(Object JavaDoc[] values, Type[] types) throws HibernateException {
38         for ( int i=0; i<types.length; i++ ) {
39             if ( includeProperty(values, i) ) {
40                 processValue( i, values, types );
41             }
42         }
43     }
44     
45     /**
46      * Dispatch each property value to processValue().
47      *
48      * @param values
49      * @param types
50      * @throws HibernateException
51      */

52     public void processEntityPropertyValues(Object JavaDoc[] values, Type[] types) throws HibernateException {
53         for ( int i=0; i<types.length; i++ ) {
54             if ( includeEntityProperty(values, i) ) {
55                 processValue( i, values, types );
56             }
57         }
58     }
59     
60     void processValue(int i, Object JavaDoc[] values, Type[] types) {
61         processValue( values[i], types[i] );
62     }
63     
64     boolean includeEntityProperty(Object JavaDoc[] values, int i) {
65         return includeProperty(values, i);
66     }
67     
68     boolean includeProperty(Object JavaDoc[] values, int i) {
69         return values[i]!=LazyPropertyInitializer.UNFETCHED_PROPERTY;
70     }
71
72     /**
73      * Visit a component. Dispatch each property
74      * to processValue().
75      * @param component
76      * @param componentType
77      * @throws HibernateException
78      */

79     Object JavaDoc processComponent(Object JavaDoc component, AbstractComponentType componentType)
80     throws HibernateException {
81         if (component!=null) {
82             processValues(
83                 componentType.getPropertyValues(component, session),
84                 componentType.getSubtypes()
85             );
86         }
87         return null;
88     }
89
90     /**
91      * Visit a property value. Dispatch to the
92      * correct handler for the property type.
93      * @param value
94      * @param type
95      * @throws HibernateException
96      */

97     final Object JavaDoc processValue(Object JavaDoc value, Type type) throws HibernateException {
98
99         if ( type.isCollectionType() ) {
100             //even process null collections
101
return processCollection( value, (CollectionType) type );
102         }
103         else if ( type.isEntityType() ) {
104             return processEntity( value, (EntityType) type );
105         }
106         else if ( type.isComponentType() ) {
107             return processComponent( value, (AbstractComponentType) type );
108         }
109         else {
110             return null;
111         }
112     }
113
114     /**
115      * Walk the tree starting from the given entity.
116      *
117      * @param object
118      * @param persister
119      * @throws HibernateException
120      */

121     void process(Object JavaDoc object, EntityPersister persister)
122     throws HibernateException {
123         processEntityPropertyValues(
124             persister.getPropertyValues( object, getSession().getEntityMode() ),
125             persister.getPropertyTypes()
126         );
127     }
128
129     /**
130      * Visit a collection. Default superclass
131      * implementation is a no-op.
132      * @param collection
133      * @param type
134      * @throws HibernateException
135      */

136     Object JavaDoc processCollection(Object JavaDoc collection, CollectionType type)
137     throws HibernateException {
138         return null;
139     }
140
141     /**
142      * Visit a many-to-one or one-to-one associated
143      * entity. Default superclass implementation is
144      * a no-op.
145      * @param value
146      * @param entityType
147      * @throws HibernateException
148      */

149     Object JavaDoc processEntity(Object JavaDoc value, EntityType entityType)
150     throws HibernateException {
151         return null;
152     }
153
154     final EventSource getSession() {
155         return session;
156     }
157 }
158
Popular Tags