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: PythonTrigger.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.xml.dtsm.DTSMException;
import com.dbxml.xml.dtsm.DTSMHelper;
import com.dbxml.xml.dtsm.DocumentTable;

import org.python.util.PythonInterpreter;

/**
 * PythonTrigger
 */

public final class PythonTrigger extends ScriptTrigger {
   protected Object execute(int type, Key key, Object oldObj, Object newObj) throws DBException {
      PythonInterpreter pi = new PythonInterpreter();

      // Standard Imports
      pi.exec("from com.dbxml.db.core import Database");
      pi.exec("from com.dbxml.db.core import Collection");
      pi.exec("from com.dbxml.db.core import Container");
      pi.exec("from com.dbxml.db.core.data import Record");
      pi.exec("from com.dbxml.db.common.scripting import *");
      pi.exec("from org.w3c.dom import *");

      // Standard Variables
      pi.set("this", this);
      pi.set("database", collection.getDatabase());
      pi.set("collection", collection);

      if ( key != null )
         pi.set("key", key);

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

      // Now run the actual script
      pi.exec(script);

      /** @todo Populate result and return as a Record/DocumentTable */
      return newObj; // for now
   }

}
