/*
 * JBoss, the OpenSource J2EE webOS
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */
package org.jboss.cache;


import org.jboss.cache.lock.IdentityLock;
import org.jgroups.blocks.MethodCall;

import javax.transaction.Transaction;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;


/**
 * This is the value (key being the {@link GlobalTransaction}) in the transaction table
 * of TreeCache.
 * <br>A TransactionEntry maintains
 * <ul>
 * <li>Transaction handle
 * <li>List of modifications (Modification)
 * <li>List of nodes that were created as part of lock acquisition. These nodes can be
 * safely deleted when a transaction is rolled back
 * <li>List of locks ({@link IdentityLock}) that have been acquired by
 * this transaction so far
 * </ul>
 *
 * @author <a href="mailto:bela@jboss.org">Bela Ban</a> Apr 14, 2003
 * @version $Revision: 1.10.2.5 $
 */
public class TransactionEntry {

   /**
    * Backpointer to the LocalTx. Needed to remove entry from LocalTx-GlobalTx table (if non-null)
    */
   protected Transaction tx=null;

   /**
    * List<MethodCall> of modifications ({@link MethodCall}). They will be replicated on TX commit
    */
   protected List modification_list=new LinkedList();

   /**
    * List of compensating {@link org.jgroups.blocks.MethodCall} objects
    * which revert the ones in <tt>modification_list</tt>. For each entry in the
    * modification list, we have a corresponding entry in this list. A rollback
    * will simply iterate over this list in reverse to undo the modifications.
    * Note that these undo-ops will never be replicated.
    */
   protected List undo_list=new LinkedList();

   /**
    * List of nodes created. Elements are fully qualified names (Fqns)
    */
   protected List nodes=new LinkedList();

   /**
    * List of locks acquired by the transaction ({@link IdentityLock})
    */
   protected List locks=new LinkedList();


   public TransactionEntry() {
      ;
   }

   public void addModification(MethodCall m) {
      if(m == null) return;
      modification_list.add(m);
   }

   public List getModifications() {
      return modification_list;
   }

   public void addUndoOperation(MethodCall m) {
      undo_list.add(m);
   }

   public List getUndoOperations() {
      return undo_list;
   }

   public void addNode(Fqn fqn) {
      if(fqn != null)
         nodes.add(fqn);
   }

   public List getNodes() {
      return nodes;
   }

   public void setTransaction(Transaction tx) {
      this.tx=tx;
   }

   public Transaction getTransaction() {
      return tx;
   }

   public void addLock(IdentityLock l) {
      if(l != null) {
         synchronized(locks) {
            if(!locks.contains(l))
               locks.add(l);
         }
      }
   }

   public List getLocks() {
      return locks;
   }

   public void releaseAllLocks(Object owner) {
      IdentityLock lock;
      synchronized(locks) {
         for(Iterator it=locks.iterator(); it.hasNext();) {
            lock=(IdentityLock)it.next();
            lock.release(owner);
         }
         locks.clear();
      }
   }


   public String toString() {
      StringBuffer sb=new StringBuffer();
      if(modification_list != null)
         sb.append("\nmodification_list: ").append(modification_list);
      if(undo_list != null)
         sb.append("\nundo_list: ").append(undo_list);
      if(locks != null)
         sb.append("\nlocks: ").append(locks);
      if(nodes != null)
         sb.append("\nnodes: ").append(nodes);
      return sb.toString();
   }


}
