package com.dbxml.db.core.transaction;

/*
 * dbXML - Native XML Database
 * Copyright (C) 1999-2004  The dbXML Group, L.L.C.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * $Id: Transaction.java,v 1.5 2004/02/12 00:17:57 bradford Exp $
 */

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import com.dbxml.db.core.DBException;

/**
 * Transaction
 */

public final class Transaction {
   public static final int ACTIVE = 0;
   public static final int COMMITTED = 1;
   public static final int CANCELED = 2;

   private static final String[] STATUS = {"Activated", "Committed", "Canceled"};

   private static long nextID = 0;

   private Set logs = new HashSet();
   private long startTime = System.currentTimeMillis();
   private long endTime;

   private long id = getNextID();
   private int status = ACTIVE;

   public Transaction() {
   }

   public Transaction(TransactionLog log) throws DBException {
      addTransactionLog(log);
   }

   private static synchronized long getNextID() {
      return nextID++;
   }

   /**
    * getID returns the internal ID of this Transaction.
    *
    * @return The Transaction ID
    */
   public long getID() {
      return id;
   }

   /**
    * getStatus returns the status of this Transaction.
    *
    * @return The Transaction status
    */
   public int getStatus() {
      return status;
   }

   /**
    * getStartTime returns the start time of this Transaction.
    * The start time is when this Transaction was instantiated,
    * rather than when the first I/O action took place.
    *
    * @return The start time
    */
   public long getStartTime() {
      return startTime;
   }

   /**
    * getEndTime returns the ending time of this Transaction.
    * The end time may return 0 if the Transaction is still
    * active.
    *
    * @return The end time
    */
   public long getEndTime() {
      return endTime;
   }

   /**
    * commit ends a transaction by commiting all data.  This method,
    * in turn, should call the commit methods of all participating
    * Collections' Transaction Logs.
    */
   public synchronized void commit() throws DBException {
      if ( status != ACTIVE )
         throw new TransactionNotActiveException("Transaction has already been "+STATUS[status]);

      Iterator iter = logs.iterator();
      while ( iter.hasNext() ) {
         TransactionLog log = (TransactionLog)iter.next();
         log.commit(this);
      }
      status = COMMITTED;
      endTime = System.currentTimeMillis();
   }

   /**
    * commit ends a transaction by rolling back all data.  This method,
    * in turn, should call the cancel methods of all participating
    * Collections' Transaction Logs.
    */
   public synchronized void cancel() throws DBException {
      if ( status != ACTIVE )
         throw new TransactionNotActiveException("Transaction has already been "+STATUS[status]);

      Iterator iter = logs.iterator();
      while ( iter.hasNext() ) {
         TransactionLog log = (TransactionLog)iter.next();
         log.cancel(this);
      }
      status = CANCELED;
      endTime = System.currentTimeMillis();
   }

   /**
    * addTransactionLog adds a TransactionLog to the partipation of this
    * Transaction.  This method should invoke the start method of
    * the TransactionLog.
    */
   public synchronized void addTransactionLog(TransactionLog log) throws DBException {
      if ( status != ACTIVE )
         throw new TransactionNotActiveException("Transaction has already been "+STATUS[status]);

      if ( log != null && !logs.contains(log) ) {
         logs.add(log);
         log.start(this);
      }
   }

   public int hashCode() {
      return (int)id;
   }
}
