Open Source Repository

Home /hibernate/hibernate-3.2.4.ga | Repository Home



org/hibernate/action/BulkOperationCleanupAction.java
// $Id: BulkOperationCleanupAction.java 11375 2007-03-29 19:36:33Z [email protected] $
package org.hibernate.action;

import org.hibernate.HibernateException;
import org.hibernate.persister.entity.EntityPersister;
import org.hibernate.persister.entity.Queryable;
import org.hibernate.engine.SessionFactoryImplementor;
import org.hibernate.engine.SessionImplementor;

import java.io.Serializable;
import java.util.Map;
import java.util.Set;
import java.util.Iterator;
import java.util.HashSet;
import java.util.ArrayList;

/**
 * Implementation of BulkOperationCleanupAction.
 *
 @author Steve Ebersole
 */
public class BulkOperationCleanupAction implements Executable, Serializable {

  private final SessionImplementor session;

  private final Set affectedEntityNames = new HashSet();
  private final Set affectedCollectionRoles = new HashSet();
  private final Serializable[] spaces;

  public BulkOperationCleanupAction(SessionImplementor session, Queryable[] affectedQueryables) {
    this.session = session;
    // TODO : probably better to calculate these and pass them in, as it'll be more performant
    ArrayList tmpSpaces = new ArrayList();
    for int i = 0; i < affectedQueryables.length; i++ ) {
      if affectedQueryables[i].hasCache() ) {
        affectedEntityNames.addaffectedQueryables[i].getEntityName() );
      }
      Set roles = session.getFactory().getCollectionRolesByEntityParticipantaffectedQueryables[i].getEntityName() );
      if roles != null ) {
        affectedCollectionRoles.addAllroles );
      }
      for int y = 0; y < affectedQueryables[i].getQuerySpaces().length; y++ ) {
        tmpSpaces.addaffectedQueryables[i].getQuerySpaces()[y] );
      }
    }
    this.spaces = new SerializabletmpSpaces.size() ];
    for int i = 0; i < tmpSpaces.size(); i++ ) {
      this.spaces[iSerializable tmpSpaces.get);
    }
  }
  
  /** Create an action that will evict collection and entity regions based on queryspaces (table names).
   *  TODO: cache the autodetected information and pass it in instead.
   **/
  public BulkOperationCleanupAction(SessionImplementor session, Set querySpaces) {
    this.session = session;

    Set tmpSpaces = new HashSet(querySpaces);
    SessionFactoryImplementor factory = session.getFactory();
    Iterator iterator = factory.getAllClassMetadata().entrySet().iterator();
    while iterator.hasNext() ) {
      Map.Entry entry = (Map.Entryiterator.next();
      String entityName = (Stringentry.getKey();
      EntityPersister persister = factory.getEntityPersisterentityName );
      Serializable[] entitySpaces = persister.getQuerySpaces();

      if (affectedEntityquerySpaces, entitySpaces )) {
        if persister.hasCache() ) {
          affectedEntityNames.addpersister.getEntityName() );
        }
        Set roles = session.getFactory().getCollectionRolesByEntityParticipantpersister.getEntityName() );
        if roles != null ) {
          affectedCollectionRoles.addAllroles );
        }
        for int y = 0; y < entitySpaces.length; y++ ) {
          tmpSpaces.addentitySpaces[y] );
        }
      }

    }
    this.spaces = (Serializable[]) tmpSpaces.toArraynew Serializable[tmpSpaces.size()] );    
  }


  /** returns true if no queryspaces or if there are a match */
  private boolean affectedEntity(Set querySpaces, Serializable[] entitySpaces) {
    if(querySpaces==null || querySpaces.isEmpty()) {
      return true;
    }
    
    for int i = 0; i < entitySpaces.length; i++ ) {
      if querySpaces.containsentitySpaces[i] ) ) {
        return true;
      }
    }
    return false;
  }

  public void init() {
    evictEntityRegions();
    evictCollectionRegions();
  }

  public boolean hasAfterTransactionCompletion() {
    return true;
  }

  public void afterTransactionCompletion(boolean successthrows HibernateException {
    ///////////////////////////////////////////////////////////////////////
    // HACK ALERT!!!!!
    if session.getFactory().getSettings().getCacheProvider() instanceof org.hibernate.cache.OptimisticTreeCacheProvider
        || session.getFactory().getSettings().getCacheProvider() instanceof org.hibernate.cache.TreeCacheProvider ) {
      return;
    }
    ///////////////////////////////////////////////////////////////////////
    evictEntityRegions();
    evictCollectionRegions();
  }

  public Serializable[] getPropertySpaces() {
    return spaces;
  }

  public void beforeExecutions() throws HibernateException {
    // nothing to do
  }

  public void execute() throws HibernateException {
    // nothing to do    
  }

  private void evictEntityRegions() {
    if affectedEntityNames != null ) {
      Iterator itr = affectedEntityNames.iterator();
      while itr.hasNext() ) {
        final String entityName = String itr.next();
        session.getFactory().evictEntityentityName );
      }
    }
  }

  private void evictCollectionRegions() {
    if affectedCollectionRoles != null ) {
      Iterator itr = affectedCollectionRoles.iterator();
      while itr.hasNext() ) {
        final String roleName = String itr.next();
        session.getFactory().evictCollectionroleName );
      }
    }
  }
}