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: JavaScriptTrigger.java,v 1.3 2004/02/12 00:17:56 bradford Exp $
 */

import org.w3c.dom.Node;

import com.dbxml.db.core.DBException;
import com.dbxml.db.core.FaultCodes;
import com.dbxml.db.core.data.Key;
import com.dbxml.db.core.trigger.TriggerException;
import com.dbxml.xml.dtsm.DTSMException;
import com.dbxml.xml.dtsm.DTSMHelper;
import com.dbxml.xml.dtsm.DocumentTable;

import org.mozilla.javascript.Context;
import org.mozilla.javascript.Scriptable;

/**
 * JavaScriptTrigger
 */

public final class JavaScriptTrigger extends ScriptTrigger {
   protected Object execute(int type, Key key, Object oldObj, Object newObj) throws DBException {
      Context ctx = Context.enter();
      ctx.setLanguageVersion(Context.VERSION_1_5);
      Scriptable scope = ctx.initStandardObjects(null);

      // Standard Variables
      scope.put("this", scope, Context.toObject(this, scope));
      scope.put("database", scope, Context.toObject(collection.getDatabase(), scope));
      scope.put("collection", scope, Context.toObject(collection, scope));

      if ( key != null )
         scope.put("key", scope, Context.toObject(key, scope));

      try {
         if ( oldObj != null ) {
            if ( oldObj instanceof DocumentTable ) {
               Node oldDoc = DTSMHelper.tableToDocument((DocumentTable)oldObj);
               if ( getType() != AFTER_GET )
                  scope.put("oldDoc", scope, Context.toObject(oldDoc, scope));
               else
                  scope.put("doc", scope, Context.toObject(oldDoc, scope));
            }
            else {
               if ( getType() != AFTER_GET )
                  scope.put("oldBin", scope, Context.toObject(oldObj, scope));
               else
                  scope.put("bin", scope, Context.toObject(oldObj, scope));
            }
         }
         if ( newObj != null ) {
            if ( newObj instanceof DocumentTable ) {
               Node newDoc = DTSMHelper.tableToDocument((DocumentTable)newObj);
               scope.put("newDoc", scope, Context.toObject(newDoc, scope));
            }
            else
               scope.put("newBin", scope, Context.toObject(newObj, scope));
         }
      }
      catch ( DTSMException e ) {
         throw new DBException(FaultCodes.SCR_RUNTIME_ERROR, e);
      }


      // Now run the actual script and populate the result
      try {
         ctx.evaluateString(scope, script, "<cmd>", 1, null);
         /** @todo Populate result and return as a Record/DocumentTable */
         return newObj; // for now
      }
      catch ( Exception e ) {
         throw new TriggerException(FaultCodes.SCR_RUNTIME_ERROR, e);
      }
      finally {
         Context.exit();
      }
   }
}
