/*
 * The contents of this file are subject to the Mozilla Public License
 * Version 1.1 (the "License");  you may not use this file except in 
 * compliance with the License.  You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
 * the specific language governing rights and limitations under the License.
 *
 * The Original Code is Protege-2000.
 *
 * The Initial Developer of the Original Code is Stanford University. Portions
 * created by Stanford University are Copyright (C) 2007.  All Rights Reserved.
 *
 * Protege was developed by Stanford Medical Informatics
 * (http://www.smi.stanford.edu) at the Stanford University School of Medicine
 * with support from the National Library of Medicine, the National Science
 * Foundation, and the Defense Advanced Research Projects Agency.  Current
 * information about Protege can be obtained at http://protege.stanford.edu.
 *
 */

package edu.stanford.smi.protege.util.transaction;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import edu.stanford.smi.protege.exception.TransactionException;
import edu.stanford.smi.protege.server.RemoteSession;
import edu.stanford.smi.protege.server.framestore.ServerFrameStore;
import edu.stanford.smi.protege.util.Log;


/**
 * This is a simple class that keeps track of running transactions and their nesting.
 * @author tredmond
 *
 */
public abstract class TransactionMonitor {
  
  private Map<RemoteSession,Integer> transactionsInProgress = new HashMap<RemoteSession, Integer>();

  public synchronized void beginTransaction() {
    RemoteSession session = ServerFrameStore.getCurrentSession();
    int nesting = getNesting();
    transactionsInProgress.put(session, nesting + 1);
  }

  public synchronized void rollbackTransaction() {
    decrementTransaction();
  }
  
  public synchronized void commitTransaction() {
    decrementTransaction();
  }
  
  private void decrementTransaction() {
    RemoteSession session = ServerFrameStore.getCurrentSession();
    int nesting = getNesting();
    if (nesting <= 0) {
      Log.getLogger().warning("Exiting a transaction when no transaction is in progress");
    } else if (nesting == 1) {
      transactionsInProgress.remove(session);
    } else {
      transactionsInProgress.put(session, nesting - 1);
    }
  }
  
  public synchronized boolean inTransaction() {
    RemoteSession session = ServerFrameStore.getCurrentSession();
    Integer nesting = transactionsInProgress.get(session);
    return nesting != null;
  }
  
  public synchronized boolean existsTransaction() {
    return !transactionsInProgress.isEmpty();
  }
  
  public synchronized boolean exclusiveTransaction() {
    RemoteSession mySession = ServerFrameStore.getCurrentSession();
    for (RemoteSession session : transactionsInProgress.keySet()) {
      int nesting = transactionsInProgress.get(session);
      if (session.equals(mySession) && nesting <= 0) {
        return false;
      }
      if (!session.equals(mySession) && nesting > 0) {
        return false;
      }
    }
    return true;
  }

  public synchronized int getNesting() {
    RemoteSession session = ServerFrameStore.getCurrentSession();
    return getNesting(session);
  }
  
  public synchronized int getNesting(RemoteSession session) {
    Integer nesting = transactionsInProgress.get(session);
    if (nesting == null) {
      return 0;
    } else {
      return nesting;
    }
  }
  
  public static boolean updatesSeenByUntransactedClients(TransactionMonitor tm) {
      return tm == null || tm.updatesSeenByUntransactedClients();
  }
  
  public static boolean updatesSeenByUntransactedClients(TransactionMonitor tm,
                                                         TransactionIsolationLevel level) {
      return tm == null || tm.updatesSeenByUntransactedClients(level);
  }
  
  public boolean updatesSeenByUntransactedClients() {
      TransactionIsolationLevel level = getTransationIsolationLevel();
      return updatesSeenByUntransactedClients(level);
  }
  
  public boolean updatesSeenByUntransactedClients(TransactionIsolationLevel level) {
    return !inTransaction() || 
      (level != null && level.compareTo(TransactionIsolationLevel.READ_UNCOMMITTED) <= 0);
  }
  


  public synchronized Set<RemoteSession> getSessions() {
    return transactionsInProgress.keySet();
  }

  public abstract TransactionIsolationLevel getTransationIsolationLevel() 
  throws TransactionException;

  /**
   * Sets the transaction isolation level for the current connection.
   * 
   * @param level The desired TransactionIsolationLevel
   * @throws TransactionException 
   */
  public abstract void setTransactionIsolationLevel(TransactionIsolationLevel level)
    throws TransactionException;
}
