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;
}
}
CLASS MY OBJECT (ObjectHook)
public class ObjectHook {
private int score = 0;
public void setScore (int value) {
this.score = value;
}
public int getScore () {
return this.score;
}
}
The result is:
<https://lh3.googleusercontent.com/-VARWxmHM6aQ/Vsrg-4n4KzI/AAAAAAAAAIA/qdN9W3eq_Qc/s1600/Schermata%2Bda%2B2016-02-22%2B11-17-44.png>
I hope will be of help.
--
---
You received this message because you are subscribed to the Google Groups
"OrientDB" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.