1 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 ; 9 import java.util.Set ; 10 import java.util.Iterator ; 11 import java.util.HashSet ; 12 import java.util.ArrayList ; 13 14 19 public class BulkOperationCleanupAction implements Executable, Serializable { 20 21 private final SessionImplementor session; 22 23 private final Set affectedEntityNames = new HashSet (); 24 private final Set affectedCollectionRoles = new HashSet (); 25 private final Serializable [] spaces; 26 27 public BulkOperationCleanupAction(SessionImplementor session, Queryable[] affectedQueryables) { 28 this.session = session; 29 ArrayList tmpSpaces = new ArrayList (); 31 for ( int i = 0; i < affectedQueryables.length; i++ ) { 32 if ( affectedQueryables[i].hasCache() ) { 33 affectedEntityNames.add( affectedQueryables[i].getEntityName() ); 34 } 35 Set 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 [ tmpSpaces.size() ]; 44 for ( int i = 0; i < tmpSpaces.size(); i++ ) { 45 this.spaces[i] = ( Serializable ) 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 [] getPropertySpaces() { 64 return spaces; 65 } 66 67 public void beforeExecutions() throws HibernateException { 68 } 70 71 public void execute() throws HibernateException { 72 } 74 75 private void evictEntityRegions() { 76 if ( affectedEntityNames != null ) { 77 Iterator itr = affectedEntityNames.iterator(); 78 while ( itr.hasNext() ) { 79 final String entityName = ( String ) itr.next(); 80 session.getFactory().evictEntity( entityName ); 81 } 82 } 83 } 84 85 private void evictCollectionRegions() { 86 if ( affectedCollectionRoles != null ) { 87 Iterator itr = affectedCollectionRoles.iterator(); 88 while ( itr.hasNext() ) { 89 final String roleName = ( String ) itr.next(); 90 session.getFactory().evictCollection( roleName ); 91 } 92 } 93 } 94 } 95 | Popular Tags |