Groups keyboard shortcuts have been updated
Dismiss
See shortcuts

it is not possible register hook to remote grap database with adding lib file to server folder?

26 views
Skip to first unread message

Saltık Buğra Avcı

unread,
Feb 21, 2016, 6:18:59 PM2/21/16
to OrientDB
I want  to create a Hook to handler create delete update  of command execution .

for example if i call  graph.command(OSqlCommand("create edge has_notification from .. to ... ").execute();
do some operations  ( something like listener for has_notifcation)
i tried this example  connecting my local db server  like on the example..

but couldnt register the hook to the server ..


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);

 
} }


SavioL

unread,
Feb 22, 2016, 5:23:31 AM2/22/16
to orient-...@googlegroups.com

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:


I hope will be of help.









Saltık Buğra Avcı

unread,
Feb 22, 2016, 11:15:39 AM2/22/16
to OrientDB
First of all ..
Thank you for answer
i thought that  hooks work like triggers
but  it semes that it only works with save...  not works with command...



Reply all
Reply to author
Forward
0 new messages