package com.dbxml.db.common.scripting;

/*
 * 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: ScriptTrigger.java,v 1.5 2004/08/22 04:18:41 bradford Exp $
 */

import com.dbxml.db.core.DBException;
import com.dbxml.db.core.data.Key;
import com.dbxml.db.core.transaction.Transaction;
import com.dbxml.db.core.trigger.SimpleTrigger;
import com.dbxml.util.Configuration;
import com.dbxml.util.dbXMLException;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;

/**
 * ScriptTrigger is the base class for all script-interpreted Triggers.
 */

public abstract class ScriptTrigger extends SimpleTrigger {
   private static final String FILENAME = "filename";
   private static final String TYPE = "type";

   private static final String TYPE_BEFORE_INSERT = "beforeInsert";
   private static final String TYPE_AFTER_INSERT = "afterInsert";
   private static final String TYPE_BEFORE_UPDATE = "beforeUpdate";
   private static final String TYPE_AFTER_UPDATE = "afterUpdate";
   private static final String TYPE_BEFORE_DELETE = "beforeDelete";
   private static final String TYPE_AFTER_DELETE = "afterDelete";
   private static final String TYPE_BEFORE_GET = "beforeGet";
   private static final String TYPE_AFTER_GET = "afterGet";

   protected static final int UNKNOWN = -1;
   protected static final int BEFORE_INSERT = 0;
   protected static final int AFTER_INSERT = 1;
   protected static final int BEFORE_UPDATE = 2;
   protected static final int AFTER_UPDATE = 3;
   protected static final int BEFORE_DELETE = 4;
   protected static final int AFTER_DELETE = 5;
   protected static final int BEFORE_GET = 6;
   protected static final int AFTER_GET = 7;

   protected String filename;
   protected String script;
   protected int type = UNKNOWN;

   public void setConfig(Configuration config) throws dbXMLException {
      super.setConfig(config);
      try {
         String t = config.getAttribute(TYPE);
         if ( t.equalsIgnoreCase(TYPE_BEFORE_INSERT) )
            type = BEFORE_INSERT;
         else if ( t.equalsIgnoreCase(TYPE_AFTER_INSERT) )
            type = AFTER_INSERT;
         else if ( t.equalsIgnoreCase(TYPE_BEFORE_UPDATE) )
            type = BEFORE_UPDATE;
         else if ( t.equalsIgnoreCase(TYPE_AFTER_UPDATE) )
            type = AFTER_UPDATE;
         else if ( t.equalsIgnoreCase(TYPE_BEFORE_DELETE) )
            type = BEFORE_DELETE;
         else if ( t.equalsIgnoreCase(TYPE_AFTER_DELETE) )
            type = AFTER_DELETE;
         else if ( t.equalsIgnoreCase(TYPE_BEFORE_GET) )
            type = BEFORE_GET;
         else if ( t.equalsIgnoreCase(TYPE_AFTER_GET) )
            type = AFTER_GET;

         filename = config.getAttribute(FILENAME);
         if ( filename != null && filename.length() > 0 ) {
            File f = new File(filename);
            FileInputStream fis = new FileInputStream(f);
            BufferedInputStream bis = new BufferedInputStream(fis, 4096);
            InputStreamReader isr = new InputStreamReader(bis, "UTF8");
            char[] c = new char[(int)f.length()];
            int size = isr.read(c);
            isr.close();
            script = new String(c, 0, size);
         }
         else
            script = config.getValue();
      }
      catch ( Exception e ) {
         e.printStackTrace(System.err);
      }
   }

   /**
    * getType returns the type of Trigger that's being executed as
    * defined by constants such as BEFORE_DELETE.
    *
    * @return The Trigger type
    */
   protected int getType() {
      return type;
   }

   /**
    * execute instructs the ScriptTrigger implementation to interpret
    * and/or execute the Trigger script.
    *
    * @param type The Trigger type
    * @param key The affected Object Key (if any)
    * @param oldObj The old (or retrieved) Object (if any)
    * @param newObj The new (or updated) Object (if any)
    * @return The new or modified new Object (if any)
    */
   protected abstract Object execute(int type, Key key, Object oldObj, Object newObj) throws DBException;

   public Object beforeInsert(Transaction tx, Key key, Object newObj) throws DBException {
      if ( type == BEFORE_INSERT )
         return execute(BEFORE_INSERT, key, null, newObj);
      else
         return newObj;
   }

   public void afterInsert(Transaction tx, Key key, Object newObj) throws DBException {
      if ( type == AFTER_INSERT )
         execute(AFTER_INSERT, key, null, newObj);
   }

   public Object beforeUpdate(Transaction tx, Key key, Object oldObj, Object newObj) throws DBException {
      if ( type == BEFORE_UPDATE )
         return execute(BEFORE_UPDATE, key, oldObj, newObj);
      else
         return newObj;
   }

   public void afterUpdate(Transaction tx, Key key, Object oldObj, Object newObj) throws DBException {
      if ( type == AFTER_UPDATE )
         execute(AFTER_UPDATE, key, oldObj, newObj);
   }

   public void beforeDelete(Transaction tx, Key key, Object oldObj) throws DBException {
      if ( type == BEFORE_DELETE )
         execute(BEFORE_DELETE, key, oldObj, null);
   }

   public void afterDelete(Transaction tx, Key key, Object oldObj) throws DBException {
      if ( type == AFTER_DELETE )
         execute(AFTER_DELETE, key, oldObj, null);
   }

   public void beforeGet(Transaction tx, Key key) throws DBException {
      if ( type == BEFORE_GET )
         execute(BEFORE_GET, key, null, null);
   }

   public void afterGet(Transaction tx, Key key, Object obj) throws DBException {
      if ( type == AFTER_GET )
         execute(AFTER_GET, key, null, obj);
   }
}
