Search in sources :

Example 6 with ORecordHook

use of com.orientechnologies.orient.core.hook.ORecordHook in project orientdb by orientechnologies.

the class ODatabaseDocumentTx method resetInitialization.

@Override
public void resetInitialization() {
    for (ORecordHook h : hooks.keySet()) h.onUnregister();
    hooks.clear();
    compileHooks();
    close();
    initialized = false;
}
Also used : ORecordHook(com.orientechnologies.orient.core.hook.ORecordHook)

Example 7 with ORecordHook

use of com.orientechnologies.orient.core.hook.ORecordHook in project orientdb by orientechnologies.

the class CRUDObjectInheritanceTestSchemaFull method beforeClass.

@BeforeClass
public void beforeClass() throws Exception {
    super.beforeClass();
    database.close();
    database = new OObjectDatabaseTx(url + "_objectschema");
    ODatabaseHelper.dropDatabase(database, getStorageType());
    ODatabaseHelper.createDatabase(database, url + "_objectschema", getStorageType());
    try {
        ODatabaseDocumentTx exportDatabase = new ODatabaseDocumentTx(url);
        exportDatabase.open("admin", "admin");
        OCommandOutputListener listener = new OCommandOutputListener() {

            @Override
            public void onMessage(String iText) {
            }
        };
        ODatabaseExport export = new ODatabaseExport(exportDatabase, EXPORT_DIR, listener);
        export.exportDatabase();
        export.close();
        exportDatabase.close();
        ODatabaseDocumentTx importDatabase = new ODatabaseDocumentTx(url + "_objectschema");
        if (url.startsWith("remote")) {
            importDatabase.open("root", ODatabaseHelper.getServerRootPassword());
        } else {
            importDatabase.open("admin", "admin");
        }
        ODatabaseImport impor = new ODatabaseImport(importDatabase, EXPORT_DIR, listener);
        // UNREGISTER ALL THE HOOKS
        for (ORecordHook hook : new ArrayList<ORecordHook>(importDatabase.getHooks().keySet())) {
            importDatabase.unregisterHook(hook);
        }
        impor.setDeleteRIDMapping(true);
        impor.importDatabase();
        impor.close();
        importDatabase.close();
        final File importDir = new File(EXPORT_DIR);
        importDir.delete();
    } catch (IOException e) {
        Assert.fail("Export import didn't go as expected", e);
    }
    database.open("admin", "admin");
    if (database.getMetadata().getSchema().existsClass("Company"))
        database.command(new OCommandSQL("delete from Company")).execute();
    if (database.getMetadata().getSchema().existsClass("Account"))
        database.command(new OCommandSQL("delete from Account")).execute();
    if (database.getMetadata().getSchema().existsClass("JavaComplexTestClass"))
        database.command(new OCommandSQL("delete from JavaComplexTestClass")).execute();
    if (database.getMetadata().getSchema().existsClass("Profile"))
        database.command(new OCommandSQL("delete from Profile")).execute();
    if (database.getMetadata().getSchema().existsClass("IdentityChild"))
        database.command(new OCommandSQL("delete from IdentityChild")).execute();
    database.close();
}
Also used : OCommandSQL(com.orientechnologies.orient.core.sql.OCommandSQL) ODatabaseImport(com.orientechnologies.orient.core.db.tool.ODatabaseImport) OObjectDatabaseTx(com.orientechnologies.orient.object.db.OObjectDatabaseTx) ArrayList(java.util.ArrayList) ODatabaseDocumentTx(com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx) IOException(java.io.IOException) OCommandOutputListener(com.orientechnologies.orient.core.command.OCommandOutputListener) File(java.io.File) ORecordHook(com.orientechnologies.orient.core.hook.ORecordHook) ODatabaseExport(com.orientechnologies.orient.core.db.tool.ODatabaseExport) BeforeClass(org.testng.annotations.BeforeClass)

Example 8 with ORecordHook

use of com.orientechnologies.orient.core.hook.ORecordHook in project wicket-orientdb by OrienteerBAP.

the class TestInAppOrientDBCompatibility method testDocumentTrackingSimple.

@Test
public void testDocumentTrackingSimple() {
    final String className = "TestDocumentTrackingSimple";
    ODatabaseDocument db = wicket.getTester().getDatabase();
    OSchema schema = db.getMetadata().getSchema();
    final OClass classA = schema.createClass(className);
    classA.createProperty("a", OType.STRING);
    db.registerHook(new ORecordHook() {

        @Override
        public void onUnregister() {
        // NOP
        }

        @Override
        public RESULT onTrigger(TYPE iType, ORecord iRecord) {
            if (iRecord instanceof ODocument) {
                ODocument doc = (ODocument) iRecord;
                if (classA.isSuperClassOf(doc.getSchemaClass())) {
                    // System.out.println("During "+iType+" document should track changes: "+doc.isTrackingChanges());
                    assertTrue("During " + iType + " document should track changes", doc.isTrackingChanges());
                }
            }
            return RESULT.RECORD_NOT_CHANGED;
        }

        @Override
        public DISTRIBUTED_EXECUTION_MODE getDistributedExecutionMode() {
            return DISTRIBUTED_EXECUTION_MODE.SOURCE_NODE;
        }
    });
    db.commit();
    ODocument doc = new ODocument(classA);
    doc.field("a", "test");
    doc.save();
    doc.reload();
    doc.field("a", "test2");
    doc.save();
    doc.reload();
    doc.delete();
}
Also used : OSchema(com.orientechnologies.orient.core.metadata.schema.OSchema) ODatabaseDocument(com.orientechnologies.orient.core.db.document.ODatabaseDocument) ORecord(com.orientechnologies.orient.core.record.ORecord) OClass(com.orientechnologies.orient.core.metadata.schema.OClass) RESULT(com.orientechnologies.orient.core.hook.ORecordHook.RESULT) ORecordHook(com.orientechnologies.orient.core.hook.ORecordHook) ODocument(com.orientechnologies.orient.core.record.impl.ODocument) DISTRIBUTED_EXECUTION_MODE(com.orientechnologies.orient.core.hook.ORecordHook.DISTRIBUTED_EXECUTION_MODE) Test(org.junit.Test)

Example 9 with ORecordHook

use of com.orientechnologies.orient.core.hook.ORecordHook in project orientdb by orientechnologies.

the class HookRegisterRemoveTest method addAndRemoveHookTest.

@Test
public void addAndRemoveHookTest() {
    ODatabaseDocumentTx db = new ODatabaseDocumentTx("memory:" + HookRegisterRemoveTest.class.getSimpleName());
    db.create();
    final AtomicInteger integer = new AtomicInteger(0);
    ORecordHook iHookImpl = new ORecordHook() {

        @Override
        public void onUnregister() {
        }

        @Override
        public RESULT onTrigger(TYPE iType, ORecord iRecord) {
            integer.incrementAndGet();
            return null;
        }

        @Override
        public DISTRIBUTED_EXECUTION_MODE getDistributedExecutionMode() {
            return null;
        }
    };
    db.registerHook(iHookImpl);
    db.save(new ODocument().field("test", "test"));
    assertEquals(3, integer.get());
    db.unregisterHook(iHookImpl);
    db.save(new ODocument());
    assertEquals(3, integer.get());
    db.drop();
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ORecord(com.orientechnologies.orient.core.record.ORecord) ODatabaseDocumentTx(com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx) ORecordHook(com.orientechnologies.orient.core.hook.ORecordHook) ODocument(com.orientechnologies.orient.core.record.impl.ODocument) Test(org.testng.annotations.Test)

Example 10 with ORecordHook

use of com.orientechnologies.orient.core.hook.ORecordHook in project orientdb by orientechnologies.

the class OConfigurableHooksManager method onOpen.

public void onOpen(ODatabaseInternal iDatabase) {
    if (!iDatabase.getStorage().isRemote()) {
        final ODatabase<?> db = (ODatabase<?>) iDatabase;
        for (OServerHookConfiguration hook : configuredHooks) {
            try {
                final ORecordHook.HOOK_POSITION pos = ORecordHook.HOOK_POSITION.valueOf(hook.position);
                final ORecordHook h = (ORecordHook) Class.forName(hook.clazz).newInstance();
                if (hook.parameters != null && hook.parameters.length > 0)
                    try {
                        final Method m = h.getClass().getDeclaredMethod("config", new Class[] { OServerParameterConfiguration[].class });
                        m.invoke(h, new Object[] { hook.parameters });
                    } catch (Exception e) {
                        OLogManager.instance().warn(this, "[configure] Failed to configure hook '%s'. Parameters specified but hook don support parameters. Should have a method config with parameters OServerParameterConfiguration[] ", hook.clazz);
                    }
                db.registerHook(h, pos);
            } catch (Exception e) {
                OLogManager.instance().error(this, "[configure] Failed to configure hook '%s' due to the an error : ", e, hook.clazz, e.getMessage());
            }
        }
    }
}
Also used : OServerHookConfiguration(com.orientechnologies.orient.server.config.OServerHookConfiguration) OClass(com.orientechnologies.orient.core.metadata.schema.OClass) ODatabase(com.orientechnologies.orient.core.db.ODatabase) Method(java.lang.reflect.Method) ORecordHook(com.orientechnologies.orient.core.hook.ORecordHook)

Aggregations

ORecordHook (com.orientechnologies.orient.core.hook.ORecordHook)13 ORecord (com.orientechnologies.orient.core.record.ORecord)7 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)6 Test (org.junit.Test)5 ODatabaseDocumentTx (com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx)3 OClass (com.orientechnologies.orient.core.metadata.schema.OClass)3 ArrayList (java.util.ArrayList)3 ODatabaseDocument (com.orientechnologies.orient.core.db.document.ODatabaseDocument)2 ODatabaseImport (com.orientechnologies.orient.core.db.tool.ODatabaseImport)2 File (java.io.File)2 Test (org.testng.annotations.Test)2 OCommandOutputListener (com.orientechnologies.orient.core.command.OCommandOutputListener)1 ODatabase (com.orientechnologies.orient.core.db.ODatabase)1 ODatabaseInternal (com.orientechnologies.orient.core.db.ODatabaseInternal)1 ODatabaseLifecycleListener (com.orientechnologies.orient.core.db.ODatabaseLifecycleListener)1 ODatabaseExport (com.orientechnologies.orient.core.db.tool.ODatabaseExport)1 DISTRIBUTED_EXECUTION_MODE (com.orientechnologies.orient.core.hook.ORecordHook.DISTRIBUTED_EXECUTION_MODE)1 RESULT (com.orientechnologies.orient.core.hook.ORecordHook.RESULT)1 ORID (com.orientechnologies.orient.core.id.ORID)1 OSchema (com.orientechnologies.orient.core.metadata.schema.OSchema)1