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: JavaScriptTrigger.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.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();
}
}
}