KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > action > BulkOperationCleanupAction


1 // $Id: BulkOperationCleanupAction.java,v 1.1 2005/07/07 19:52:07 steveebersole Exp $
2
package org.hibernate.action;
3
4 import org.hibernate.HibernateException;
5 import org.hibernate.persister.entity.Queryable;
6 import org.hibernate.engine.SessionImplementor;
7
8 import java.io.Serializable JavaDoc;
9 import java.util.Set JavaDoc;
10 import java.util.Iterator JavaDoc;
11 import java.util.HashSet JavaDoc;
12 import java.util.ArrayList JavaDoc;
13
14 /**
15  * Implementation of BulkOperationCleanupAction.
16  *
17  * @author Steve Ebersole
18  */

19 public class BulkOperationCleanupAction implements Executable, Serializable JavaDoc {
20
21     private final SessionImplementor session;
22
23     private final Set JavaDoc affectedEntityNames = new HashSet JavaDoc();
24     private final Set JavaDoc affectedCollectionRoles = new HashSet JavaDoc();
25     private final Serializable JavaDoc[] spaces;
26
27     public BulkOperationCleanupAction(SessionImplementor session, Queryable[] affectedQueryables) {
28         this.session = session;
29         // TODO : probably better to calculate these and pass them in, as it'll be more performant
30
ArrayList JavaDoc tmpSpaces = new ArrayList JavaDoc();
31         for ( int i = 0; i < affectedQueryables.length; i++ ) {
32             if ( affectedQueryables[i].hasCache() ) {
33                 affectedEntityNames.add( affectedQueryables[i].getEntityName() );
34             }
35             Set JavaDoc roles = session.getFactory().getCollectionRolesByEntityParticipant( affectedQueryables[i].getEntityName() );
36             if ( roles != null ) {
37                 affectedCollectionRoles.addAll( roles );
38             }
39             for ( int y = 0; y < affectedQueryables[i].getQuerySpaces().length; y++ ) {
40                 tmpSpaces.add( affectedQueryables[i].getQuerySpaces()[y] );
41             }
42         }
43         this.spaces = new Serializable JavaDoc[ tmpSpaces.size() ];
44         for ( int i = 0; i < tmpSpaces.size(); i++ ) {
45             this.spaces[i] = ( Serializable JavaDoc ) tmpSpaces.get( i );
46         }
47     }
48
49     public void init() {
50         evictEntityRegions();
51         evictCollectionRegions();
52     }
53
54     public boolean hasAfterTransactionCompletion() {
55         return true;
56     }
57
58     public void afterTransactionCompletion(boolean success) throws HibernateException {
59         evictEntityRegions();
60         evictCollectionRegions();
61     }
62
63     public Serializable JavaDoc[] getPropertySpaces() {
64         return spaces;
65     }
66
67     public void beforeExecutions() throws HibernateException {
68         // nothing to do
69
}
70
71     public void execute() throws HibernateException {
72         // nothing to do
73
}
74
75     private void evictEntityRegions() {
76         if ( affectedEntityNames != null ) {
77             Iterator JavaDoc itr = affectedEntityNames.iterator();
78             while ( itr.hasNext() ) {
79                 final String JavaDoc entityName = ( String JavaDoc ) itr.next();
80                 session.getFactory().evictEntity( entityName );
81             }
82         }
83     }
84
85     private void evictCollectionRegions() {
86         if ( affectedCollectionRoles != null ) {
87             Iterator JavaDoc itr = affectedCollectionRoles.iterator();
88             while ( itr.hasNext() ) {
89                 final String JavaDoc roleName = ( String JavaDoc ) itr.next();
90                 session.getFactory().evictCollection( roleName );
91             }
92         }
93     }
94 }
95
Popular Tags