KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > engine > CascadingAction


1 //$Id: CascadingAction.java,v 1.2 2005/07/15 03:35:17 oneovthafew Exp $
2
package org.hibernate.engine;
3
4 import java.util.Iterator JavaDoc;
5 import java.util.Map JavaDoc;
6
7 import org.apache.commons.logging.Log;
8 import org.apache.commons.logging.LogFactory;
9 import org.hibernate.HibernateException;
10 import org.hibernate.LockMode;
11 import org.hibernate.ReplicationMode;
12 import org.hibernate.collection.PersistentCollection;
13 import org.hibernate.event.EventSource;
14 import org.hibernate.type.CollectionType;
15
16 /**
17  * A session action that may be cascaded from parent entity to its children
18  *
19  * @author Gavin King
20  */

21 public abstract class CascadingAction {
22     
23     private static final Log log = LogFactory.getLog(CascadingAction.class);
24
25     /**
26      * cascade the action to the child object
27      */

28     public abstract void cascade(EventSource session, Object JavaDoc child, String JavaDoc entityName, Object JavaDoc anything, boolean isCascadeDeleteEnabled)
29     throws HibernateException;
30     /**
31      * Should this action be cascaded to the given (possibly uninitialized) collection?
32      */

33     public abstract Iterator JavaDoc getCascadableChildrenIterator(EventSource session, CollectionType collectionType, Object JavaDoc collection);
34     /**
35      * Do we need to handle orphan delete for this action?
36      */

37     public abstract boolean deleteOrphans();
38
39     /**
40      * @see org.hibernate.Session#delete(Object)
41      */

42     public static final CascadingAction DELETE = new CascadingAction() {
43         public void cascade(EventSource session, Object JavaDoc child, String JavaDoc entityName, Object JavaDoc anything, boolean isCascadeDeleteEnabled)
44         throws HibernateException {
45             if ( log.isTraceEnabled() ) log.trace("cascading to delete: " + entityName);
46             if ( ForeignKeys.isNotTransient(entityName, child, null, session) ) {
47                 session.delete(entityName, child, isCascadeDeleteEnabled);
48             }
49         }
50         public Iterator JavaDoc getCascadableChildrenIterator(EventSource session, CollectionType collectionType, Object JavaDoc collection) {
51             // delete does cascade to uninitialized collections
52
return CascadingAction.getAllElementsIterator(session, collectionType, collection);
53         }
54         public boolean deleteOrphans() {
55             // orphans should be deleted during delete
56
return true;
57         }
58         public String JavaDoc toString() {
59             return "ACTION_DELETE";
60         }
61     };
62     
63     /**
64      * @see org.hibernate.Session#lock(Object, LockMode)
65      */

66     public static final CascadingAction LOCK = new CascadingAction() {
67         public void cascade(EventSource session, Object JavaDoc child, String JavaDoc entityName, Object JavaDoc anything, boolean isCascadeDeleteEnabled)
68         throws HibernateException {
69             if ( log.isTraceEnabled() ) log.trace("cascading to lock: " + entityName);
70             session.lock( entityName, child, LockMode.NONE/*(LockMode) anything*/ );
71         }
72         public Iterator JavaDoc getCascadableChildrenIterator(EventSource session, CollectionType collectionType, Object JavaDoc collection) {
73             // lock doesn't cascade to uninitialized collections
74
return getLoadedElementsIterator(session, collectionType, collection);
75         }
76         public boolean deleteOrphans() {
77             //TODO: should orphans really be deleted during lock???
78
return false;
79         }
80         public String JavaDoc toString() {
81             return "ACTION_LOCK";
82         }
83     };
84     
85     /**
86      * @see org.hibernate.Session#refresh(Object)
87      */

88     public static final CascadingAction REFRESH = new CascadingAction() {
89         public void cascade(EventSource session, Object JavaDoc child, String JavaDoc entityName, Object JavaDoc anything, boolean isCascadeDeleteEnabled)
90         throws HibernateException {
91             if ( log.isTraceEnabled() ) log.trace("cascading to refresh: " + entityName);
92             session.refresh( child, (Map JavaDoc) anything );
93         }
94         public Iterator JavaDoc getCascadableChildrenIterator(EventSource session, CollectionType collectionType, Object JavaDoc collection) {
95             // refresh doesn't cascade to uninitialized collections
96
return getLoadedElementsIterator(session, collectionType, collection);
97         }
98         public boolean deleteOrphans() {
99             return false;
100         }
101         public String JavaDoc toString() {
102             return "ACTION_REFRESH";
103         }
104     };
105     
106     /**
107      * @see org.hibernate.Session#evict(Object)
108      */

109     public static final CascadingAction EVICT = new CascadingAction() {
110         public void cascade(EventSource session, Object JavaDoc child, String JavaDoc entityName, Object JavaDoc anything, boolean isCascadeDeleteEnabled)
111         throws HibernateException {
112             if ( log.isTraceEnabled() ) log.trace("cascading to evict: " + entityName);
113             session.evict(child);
114         }
115         public Iterator JavaDoc getCascadableChildrenIterator(EventSource session, CollectionType collectionType, Object JavaDoc collection) {
116             // evicts don't cascade to uninitialized collections
117
return getLoadedElementsIterator(session, collectionType, collection);
118         }
119         public boolean deleteOrphans() {
120             return false;
121         }
122         public String JavaDoc toString() {
123             return "ACTION_EVICT";
124         }
125     };
126     
127     /**
128      * @see org.hibernate.Session#saveOrUpdate(Object)
129      */

130     public static final CascadingAction SAVE_UPDATE = new CascadingAction() {
131         public void cascade(EventSource session, Object JavaDoc child, String JavaDoc entityName, Object JavaDoc anything, boolean isCascadeDeleteEnabled)
132         throws HibernateException {
133             if ( log.isTraceEnabled() ) log.trace("cascading to saveOrUpdate: " + entityName);
134             session.saveOrUpdate(entityName, child);
135         }
136         public Iterator JavaDoc getCascadableChildrenIterator(EventSource session, CollectionType collectionType, Object JavaDoc collection) {
137             // saves / updates don't cascade to uninitialized collections
138
return getLoadedElementsIterator(session, collectionType, collection);
139         }
140         public boolean deleteOrphans() {
141             // orphans should be deleted during save/update
142
return true;
143         }
144         public String JavaDoc toString() {
145             return "ACTION_SAVE_UPDATE";
146         }
147     };
148     
149     /**
150      * @see org.hibernate.Session#merge(Object)
151      */

152     public static final CascadingAction MERGE = new CascadingAction() {
153         public void cascade(EventSource session, Object JavaDoc child, String JavaDoc entityName, Object JavaDoc anything, boolean isCascadeDeleteEnabled)
154         throws HibernateException {
155             if ( log.isTraceEnabled() ) log.trace("cascading to merge: " + entityName);
156             session.merge( entityName, child, (Map JavaDoc) anything );
157         }
158         public Iterator JavaDoc getCascadableChildrenIterator(EventSource session, CollectionType collectionType, Object JavaDoc collection) {
159             // saves / updates don't cascade to uninitialized collections
160
return getLoadedElementsIterator(session, collectionType, collection);
161         }
162         public boolean deleteOrphans() {
163             // orphans should not be deleted during copy??
164
return false;
165         }
166         public String JavaDoc toString() {
167             return "ACTION_MERGE";
168         }
169     };
170     
171     /**
172      * @see org.hibernate.classic.Session#saveOrUpdateCopy(Object)
173      */

174     public static final CascadingAction SAVE_UPDATE_COPY = new CascadingAction() {
175         // for deprecated saveOrUpdateCopy()
176
public void cascade(EventSource session, Object JavaDoc child, String JavaDoc entityName, Object JavaDoc anything, boolean isCascadeDeleteEnabled)
177         throws HibernateException {
178             if ( log.isTraceEnabled() ) log.trace("cascading to saveOrUpdateCopy: " + entityName);
179             session.saveOrUpdateCopy( entityName, child, (Map JavaDoc) anything );
180         }
181         public Iterator JavaDoc getCascadableChildrenIterator(EventSource session, CollectionType collectionType, Object JavaDoc collection) {
182             // saves / updates don't cascade to uninitialized collections
183
return getLoadedElementsIterator(session, collectionType, collection);
184         }
185         public boolean deleteOrphans() {
186             // orphans should not be deleted during copy??
187
return false;
188         }
189         public String JavaDoc toString() {
190             return "ACTION_SAVE_UPDATE_COPY";
191         }
192     };
193     
194     /**
195      * @see org.hibernate.Session#persist(Object)
196      */

197     public static final CascadingAction PERSIST = new CascadingAction() {
198         public void cascade(EventSource session, Object JavaDoc child, String JavaDoc entityName, Object JavaDoc anything, boolean isCascadeDeleteEnabled)
199         throws HibernateException {
200             if ( log.isTraceEnabled() ) log.trace("cascading to persist: " + entityName);
201             session.persist( entityName, child, (Map JavaDoc) anything );
202         }
203         public Iterator JavaDoc getCascadableChildrenIterator(EventSource session, CollectionType collectionType, Object JavaDoc collection) {
204             // saves / updates don't cascade to uninitialized collections
205
return CascadingAction.getAllElementsIterator(session, collectionType, collection);
206         }
207         public boolean deleteOrphans() {
208             // orphans should not be deleted during create
209
return false;
210         }
211         public String JavaDoc toString() {
212             return "ACTION_PERSIST";
213         }
214     };
215     
216     /**
217      * @see org.hibernate.Session#replicate(Object, org.hibernate.ReplicationMode)
218      */

219     public static final CascadingAction REPLICATE = new CascadingAction() {
220         public void cascade(EventSource session, Object JavaDoc child, String JavaDoc entityName, Object JavaDoc anything, boolean isCascadeDeleteEnabled)
221         throws HibernateException {
222             if ( log.isTraceEnabled() ) log.trace("cascading to replicate: " + entityName);
223             session.replicate( entityName, child, (ReplicationMode) anything );
224         }
225         public Iterator JavaDoc getCascadableChildrenIterator(EventSource session, CollectionType collectionType, Object JavaDoc collection) {
226             // replicate does cascade to uninitialized collections
227
return getLoadedElementsIterator(session, collectionType, collection);
228         }
229         public boolean deleteOrphans() {
230             return false; //I suppose?
231
}
232         public String JavaDoc toString() {
233             return "ACTION_REPLICATE";
234         }
235     };
236     
237     CascadingAction() {}
238
239     /**
240      * Iterate all the collection elements, loading them from the database if necessary.
241      */

242     private static Iterator JavaDoc getAllElementsIterator(EventSource session, CollectionType collectionType, Object JavaDoc collection) {
243         return collectionType.getElementsIterator(collection, session);
244     }
245     /**
246      * Iterate just the elements of the collection that are already there. Don't load
247      * any new elements from the database.
248      */

249     public static Iterator JavaDoc getLoadedElementsIterator(SessionImplementor session, CollectionType collectionType, Object JavaDoc collection) {
250         if ( collectionIsInitialized(collection) ) {
251             // handles arrays and newly instantiated collections
252
return collectionType.getElementsIterator(collection, session);
253         }
254         else {
255             // does not handle arrays (thats ok, cos they can't be lazy)
256
// or newly instantiated collections, so we can do the cast
257
return ( (PersistentCollection) collection ).queuedAdditionIterator();
258         }
259     }
260     
261     private static boolean collectionIsInitialized(Object JavaDoc collection) {
262         return !(collection instanceof PersistentCollection) || ( (PersistentCollection) collection ).wasInitialized();
263     }
264 }
Popular Tags