public class HookTest extends ORecordHookAbstract { public saveProfile(){ ODatabaseObjectTx database = new ODatabaseObjectTx("remote:localhost/demo"); database.open("writer", "writer");
// REGISTER MYSELF AS HOOK
database.registerHook(this);
//didnt try this will work or not ...
p = new Profile("Luca"); p.setAge(10000); database.save(p);
// but i want something like this
database.registerHook(this);
graph.command(OSqlCommand("create edge has_notification from .. to ... ").execute();
database.unRegisterHook(this);
} }
hi,
I write you a working example that I had done so you can adapt it to your purposes:
MAIN CLASS
import java.io.IOException;
public class Hook {
static final String REMOTE = "remote:localhost/";
static final String NOMEDB = "hook";
static final String CURRENTPATH = REMOTE + NOMEDB;
public static void main(String[] args) throws IOException {
HookTest hookTest = new HookTest(CURRENTPATH);
}
}
CLASS HOOKTEST
import com.orientechnologies.orient.core.hook.ORecordHookAbstract;
import com.orientechnologies.orient.core.record.ORecord;
import com.orientechnologies.orient.core.record.impl.ODocument;
import com.orientechnologies.orient.object.db.OObjectDatabaseTx;
import altreClassi.ObjectHook;
public class HookTest extends ORecordHookAbstract {
private OObjectDatabaseTx database = null;
public HookTest(String currentPath){
System.out.println("Class HookTest instantiated...");
this.database = new OObjectDatabaseTx (currentPath);
database.open("root","root");
saveMyObject();
}
public void saveMyObject() {
// REGISTER MYSELF AS HOOK
database.registerHook(this);
//myobject
database.getEntityManager().registerEntityClass(ObjectHook.class);
ObjectHook objectHook = new ObjectHook();
objectHook.setScore(100);
System.out.println("save objectHook...");
database.save(objectHook);
}
/**
* Custom validation rules
*/
@Override
public RESULT onRecordBeforeCreate(ORecord iRecord) {
System.out.println("start hook");
if( iRecord instanceof ODocument ){
ODocument doc = (ODocument) iRecord;
Integer score = doc .field( "score" );
if( score > 99 ){
System.out.println("Score > 99");
}
}
return super.onRecordBeforeCreate(iRecord);
}
@Override
public DISTRIBUTED_EXECUTION_MODE getDistributedExecutionMode() {
// TODO Auto-generated method stub
return null;
}
}
public class ObjectHook {
private int score = 0;
public void setScore (int value) {
this.score = value;
}
public int getScore () {
return this.score;
}
}