public class Hooks {
public static void main(String[] args) {
ODatabaseDocument db = new ODatabaseDocumentTx("remote:localhost/New");
MyHook hook = new MyHook();
try {
db.open("admin", "admin");
db.registerHook(hook);
db.command(new OCommandSQL("create class Account")).execute();
db.getMetadata().getSchema().reload();
ODocument accountR = new ODocument("Account");
accountR.field("type", "facebook");
accountR.save();
db.command(new OCommandSQL("insert into Account set type = 'twitter'")).execute();
} finally {
db.unregisterHook(hook);
db.close();
}
}
private static class MyHook extends ODocumentHookAbstract {
public MyHook() {
setIncludeClasses("Account");
}
@Override
public ORecordHook.DISTRIBUTED_EXECUTION_MODE getDistributedExecutionMode() {
return ORecordHook.DISTRIBUTED_EXECUTION_MODE.SOURCE_NODE;
}
@Override
public ORecordHook.RESULT onRecordBeforeCreate(ODocument iDocument) {
System.out.println("before create : " + iDocument.field("type"));
return ORecordHook.RESULT.RECORD_NOT_CHANGED;
}
}
}
The output is:
before create : facebook
This means that the hook was only triggered in doc.save().
Is there a way to also trigger it with db.command(...) ?
(I have the same problem when trying to delete. Only doc.delete() will trigger the hook)
Thanks
The OCommandSQL
command is executed by the sever. If you use a plocal
connection you will see that it works as you expected.
To execute a (java) hook server side, it has to be registered there, see documentation. Also note you can use Dynamic Hooks.
BTW Welcome at stackoverflow :)