package com.dbxml.db.core.trigger;

/*
 * 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: Trigger.java,v 1.2 2004/02/12 00:17:57 bradford Exp $
 */

import com.dbxml.db.core.Collection;
import com.dbxml.db.core.DBException;
import com.dbxml.db.core.data.Key;
import com.dbxml.db.core.transaction.Transaction;
import com.dbxml.util.Configurable;

/**
 * Trigger represents a dbXML trigger.  Triggers are class implementations
 * that are capable of responding to Collection level events.  Triggerrs
 * are a fairly low level interface, and use Object instances to represent
 * the data that is being reported.  Depending on the type of Collection
 * that is being monitored, the actual type of Object will be a
 * DocumentTable (for Document collections) or a byte array (for Binary
 * collections).
 */

public interface Trigger extends Configurable {
   /**
    * getName returns the name of this Trigger.
    *
    * @return the Trigger name
    */
   String getName();

   /**
    * setCollection sets the Collection context for this Trigger.
    *
    * @param collection The Collection
    */
   void setCollection(Collection collection);

   /**
    * getCollection returns the Collection context for this Trigger.
    *
    * @return The Collection
    */
   Collection getCollection();

   /**
    * beforeInsert is fired before a new Object is inserted into
    * the Collection.  Throw a CancelOperationException to cancel
    * the Object insertion.
    *
    * @param tx The controlling Transaction
    * @param key The Object Key
    * @param newObj The new Object
    * @return The modified or original new Object
    */
   Object beforeInsert(Transaction tx, Key key, Object newObj) throws DBException;

   /**
    * afterInsert is fired after a new Object is inserted  into
    * the Collection.
    *
    * @param tx The controlling Transaction
    * @param key The Object Key
    * @param newObj The new Object
    */
   void afterInsert(Transaction tx, Key key, Object newObj) throws DBException;

   /**
    * beforeUpdate is fired before a Object is updated in the
    * Collection.  Throw a CancelOperationException to cancel the
    * Object update.
    *
    * @param tx The controlling Transaction
    * @param key The Object Key
    * @param oldObj The old Object
    * @param newObj The new Object
    * @return The modified or original new Object
    */
   Object beforeUpdate(Transaction tx, Key key, Object oldObj, Object newObj) throws DBException;

   /**
    * afterUpdate is fired after a Object is updated in the
    * Collection.
    *
    * @param tx The controlling Transaction
    * @param key The Object Key
    * @param oldObj The old Object
    * @param newObj The new Object
    */
   void afterUpdate(Transaction tx, Key key, Object oldObj, Object newObj) throws DBException;

   /**
    * beforeDelete is fired before a Object is deleted from the
    * Collection.  Throw a CancelOperationException to cancel the
    * delete operation.
    *
    * @param tx The controlling Transaction
    * @param key The Object Key
    * @param oldObj The Object to be deleted
    */
   void beforeDelete(Transaction tx, Key key, Object oldObj) throws DBException;

   /**
    * afterDelete is fired after a Object is deleted from the
    * Collection.
    *
    * @param tx The controlling Transaction
    * @param key The Object Key
    * @param oldObj The Object that was deleted
    */
   void afterDelete(Transaction tx, Key key, Object oldObj) throws DBException;

   /**
    * beforeGet is fired before a Object is retrieved from the
    * Collection.  At this point, the Object hasn't even been
    * loaded, so there is no reference to the actual Object
    * Node.
    *
    * @param tx The controlling Transaction
    * @param key The Object Key
    */
   void beforeGet(Transaction tx, Key key) throws DBException;

   /**
    * afterGet is fired after a Object is retrieved from the
    * Collection, but before it is actually returned.
    *
    * @param tx The controlling Transaction
    * @param key The Object Key
    * @param doc The Object that was retrieved
    */
   void afterGet(Transaction tx, Key key, Object obj) throws DBException;
}
