package com.dbxml.db.common.scripting;
/*
* dbXML - Native XML Database
* Copyright (c) 1999-2006 The dbXML Group, L.L.C.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
* $Id: PythonTrigger.java,v 1.4 2006/02/02 18:53:52 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
}
}