Search in sources :

Example 1 with ORecordHook

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

the class OrientDbWebApplication method init.

@Override
protected void init() {
    super.init();
    Orient.instance().registerThreadDatabaseFactory(new DefaultODatabaseThreadLocalFactory(this));
    Orient.instance().addDbLifecycleListener(new ODatabaseLifecycleListener() {

        private ORecordHook createHook(Class<? extends ORecordHook> clazz, ODatabaseInternal iDatabase) {
            if (!(iDatabase instanceof ODatabaseDocument))
                return null;
            try {
                return (ORecordHook) clazz.getConstructor(ODatabaseDocument.class).newInstance(iDatabase);
            } catch (Exception e) {
                try {
                    return (ORecordHook) clazz.newInstance();
                } catch (Exception e1) {
                    throw new IllegalStateException("Can't initialize hook " + clazz.getName(), e);
                }
            }
        }

        @Override
        public void onOpen(ODatabaseInternal iDatabase) {
            registerHooks(iDatabase);
        }

        @Override
        public void onCreate(ODatabaseInternal iDatabase) {
            registerHooks(iDatabase);
            // Fix for "feature" appeared in OrientDB 2.1.1
            // Issue: https://github.com/orientechnologies/orientdb/issues/4906
            fixOrientDBRights(iDatabase);
        }

        public void registerHooks(ODatabaseInternal iDatabase) {
            Set<ORecordHook> hooks = iDatabase.getHooks().keySet();
            List<Class<? extends ORecordHook>> hooksToRegister = new ArrayList<Class<? extends ORecordHook>>(getOrientDbSettings().getORecordHooks());
            for (ORecordHook hook : hooks) {
                if (hooksToRegister.contains(hook.getClass()))
                    hooksToRegister.remove(hook.getClass());
            }
            for (Class<? extends ORecordHook> oRecordHookClass : hooksToRegister) {
                ORecordHook hook = createHook(oRecordHookClass, iDatabase);
                if (hook != null) {
                    if (hook instanceof IHookPosition) {
                        iDatabase.registerHook(hook, ((IHookPosition) hook).getPosition());
                    } else {
                        iDatabase.registerHook(hook);
                    }
                }
            }
        }

        @Override
        public void onClose(ODatabaseInternal iDatabase) {
        /*NOP*/
        }

        @Override
        public void onDrop(ODatabaseInternal iDatabase) {
        /*NOP*/
        }

        public PRIORITY getPriority() {
            return PRIORITY.REGULAR;
        }

        @Override
        public void onCreateClass(ODatabaseInternal iDatabase, OClass iClass) {
        /*NOP*/
        }

        @Override
        public void onDropClass(ODatabaseInternal iDatabase, OClass iClass) {
        /*NOP*/
        }

        @Override
        public void onLocalNodeConfigurationRequest(ODocument arg0) {
        /*NOP*/
        }
    });
    getRequestCycleListeners().add(newTransactionRequestCycleListener());
    getRequestCycleListeners().add(new OrientDefaultExceptionsHandlingListener());
    getSecuritySettings().setAuthorizationStrategy(new WicketOrientDbAuthorizationStrategy(this));
    getApplicationListeners().add(new IApplicationListener() {

        @Override
        public void onAfterInitialized(Application application) {
            Orient.instance().startup();
            Orient.instance().removeShutdownHook();
        }

        @Override
        public void onBeforeDestroyed(Application application) {
            Orient.instance().shutdown();
        }
    });
    getAjaxRequestTargetListeners().add(new FixFormEncTypeListener());
    // workaround to support changing system users passwords in web interface
    getOrientDbSettings().getORecordHooks().add(OUserCatchPasswordHook.class);
    PropertyResolver.setLocator(this, new ODocumentPropertyLocator(new PropertyResolver.CachingPropertyLocator(new PropertyResolver.DefaultPropertyLocator())));
}
Also used : Set(java.util.Set) ODocumentPropertyLocator(ru.ydn.wicket.wicketorientdb.utils.ODocumentPropertyLocator) IHookPosition(ru.ydn.wicket.wicketorientdb.components.IHookPosition) PropertyResolver(org.apache.wicket.core.util.lang.PropertyResolver) FixFormEncTypeListener(ru.ydn.wicket.wicketorientdb.utils.FixFormEncTypeListener) OClass(com.orientechnologies.orient.core.metadata.schema.OClass) ArrayList(java.util.ArrayList) List(java.util.List) ODatabaseLifecycleListener(com.orientechnologies.orient.core.db.ODatabaseLifecycleListener) ODocument(com.orientechnologies.orient.core.record.impl.ODocument) ORecordHook(com.orientechnologies.orient.core.hook.ORecordHook) InvocationTargetException(java.lang.reflect.InvocationTargetException) ODatabaseInternal(com.orientechnologies.orient.core.db.ODatabaseInternal) ODatabaseDocument(com.orientechnologies.orient.core.db.document.ODatabaseDocument) WicketOrientDbAuthorizationStrategy(ru.ydn.wicket.wicketorientdb.security.WicketOrientDbAuthorizationStrategy) IApplicationListener(org.apache.wicket.IApplicationListener) OClass(com.orientechnologies.orient.core.metadata.schema.OClass) AuthenticatedWebApplication(org.apache.wicket.authroles.authentication.AuthenticatedWebApplication) Application(org.apache.wicket.Application) WebApplication(org.apache.wicket.protocol.http.WebApplication)

Example 2 with ORecordHook

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

the class HookReadTest method testSelectChangedInHook.

@Test
public void testSelectChangedInHook() {
    database.registerHook(new ORecordHook() {

        @Override
        public void onUnregister() {
        }

        @Override
        public RESULT onTrigger(TYPE iType, ORecord iRecord) {
            if (iType == TYPE.AFTER_READ)
                ((ODocument) iRecord).field("read", "test");
            return RESULT.RECORD_CHANGED;
        }

        @Override
        public DISTRIBUTED_EXECUTION_MODE getDistributedExecutionMode() {
            return null;
        }
    });
    database.save(new ODocument("TestClass"));
    List<ODocument> res = database.query(new OSQLSynchQuery<Object>("select from TestClass"));
    assertEquals(res.get(0).field("read"), "test");
}
Also used : ORecord(com.orientechnologies.orient.core.record.ORecord) ORecordHook(com.orientechnologies.orient.core.hook.ORecordHook) ODocument(com.orientechnologies.orient.core.record.impl.ODocument) Test(org.junit.Test)

Example 3 with ORecordHook

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

the class HookSaveTest method testCreatedLinkedInHook.

@Test
public void testCreatedLinkedInHook() {
    database.registerHook(new ORecordHook() {

        @Override
        public void onUnregister() {
        }

        @Override
        public RESULT onTrigger(TYPE iType, ORecord iRecord) {
            if (iType != TYPE.BEFORE_CREATE)
                return RESULT.RECORD_NOT_CHANGED;
            ODocument doc = (ODocument) iRecord;
            if (doc.containsField("test"))
                return RESULT.RECORD_NOT_CHANGED;
            ODocument doc1 = new ODocument();
            doc1.field("test", "value");
            doc.field("testNewLinkedRecord", doc1);
            return RESULT.RECORD_CHANGED;
        }

        @Override
        public DISTRIBUTED_EXECUTION_MODE getDistributedExecutionMode() {
            return null;
        }
    });
    ODocument doc = database.save(new ODocument("test"));
    ODocument newRef = doc.field("testNewLinkedRecord");
    assertNotNull(newRef);
    assertNotNull(newRef.getIdentity().isPersistent());
}
Also used : ORecord(com.orientechnologies.orient.core.record.ORecord) ORecordHook(com.orientechnologies.orient.core.hook.ORecordHook) ODocument(com.orientechnologies.orient.core.record.impl.ODocument) Test(org.junit.Test)

Example 4 with ORecordHook

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

the class HookSaveTest method testCreatedBackLinkedInHook.

@Test
public void testCreatedBackLinkedInHook() {
    database.registerHook(new ORecordHook() {

        @Override
        public void onUnregister() {
        }

        @Override
        public RESULT onTrigger(TYPE iType, ORecord iRecord) {
            if (iType != TYPE.BEFORE_CREATE)
                return RESULT.RECORD_NOT_CHANGED;
            ODocument doc = (ODocument) iRecord;
            if (doc.containsField("test"))
                return RESULT.RECORD_NOT_CHANGED;
            ODocument doc1 = new ODocument();
            doc1.field("test", "value");
            doc.field("testNewLinkedRecord", doc1);
            doc1.field("backLink", doc);
            return RESULT.RECORD_CHANGED;
        }

        @Override
        public DISTRIBUTED_EXECUTION_MODE getDistributedExecutionMode() {
            return null;
        }
    });
    ODocument doc = database.save(new ODocument("test"));
    ODocument newRef = doc.field("testNewLinkedRecord");
    assertNotNull(newRef);
    assertNotNull(newRef.getIdentity().isPersistent());
}
Also used : ORecord(com.orientechnologies.orient.core.record.ORecord) ORecordHook(com.orientechnologies.orient.core.hook.ORecordHook) ODocument(com.orientechnologies.orient.core.record.impl.ODocument) Test(org.junit.Test)

Example 5 with ORecordHook

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

the class DbImportExportTest method testDbImport.

@Test(dependsOnMethods = "testDbExport")
public void testDbImport() throws IOException {
    final File importDir = new File(testPath + "/" + NEW_DB_PATH);
    if (importDir.exists())
        for (File f : importDir.listFiles()) f.delete();
    else
        importDir.mkdir();
    ODatabaseDocumentTx database = new ODatabaseDocumentTx(getStorageType() + ":" + testPath + "/" + NEW_DB_URL);
    database.create();
    ODatabaseImport dbImport = new ODatabaseImport(database, testPath + "/" + exportFilePath, this);
    // UNREGISTER ALL THE HOOKS
    for (ORecordHook hook : new ArrayList<ORecordHook>(database.getHooks().keySet())) {
        database.unregisterHook(hook);
    }
    dbImport.setPreserveRids(true);
    dbImport.setDeleteRIDMapping(false);
    dbImport.importDatabase();
    dbImport.close();
    database.close();
}
Also used : ODatabaseImport(com.orientechnologies.orient.core.db.tool.ODatabaseImport) ArrayList(java.util.ArrayList) ODatabaseDocumentTx(com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx) File(java.io.File) ORecordHook(com.orientechnologies.orient.core.hook.ORecordHook) Test(org.testng.annotations.Test)

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