Search in sources :

Example 1 with ODocumentHookAbstract

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

the class TestInAppOrientDBCompatibility method testInHook.

@Test
public void testInHook() throws Exception {
    ODatabaseDocument db = wicket.getTester().getDatabase();
    OSchema schema = db.getMetadata().getSchema();
    OClass oClass = schema.createClass("TestInHook");
    oClass.createProperty("a", OType.INTEGER);
    oClass.createProperty("b", OType.INTEGER);
    oClass.createProperty("c", OType.INTEGER);
    ODocument doc = new ODocument(oClass);
    doc.field("a", 2);
    doc.field("b", 2);
    doc.save();
    doc.reload();
    assertEquals(2, (Object) doc.field("a"));
    assertEquals(2, (Object) doc.field("b"));
    assertNull(doc.field("c"));
    db.registerHook(new ODocumentHookAbstract(db) {

        {
            setIncludeClasses("TestInHook");
        }

        @Override
        public void onRecordAfterCreate(ODocument iDocument) {
            onRecordAfterRead(iDocument);
        }

        @Override
        public void onRecordAfterRead(ODocument iDocument) {
            String script = "select sum(a, b) as value from " + iDocument.getIdentity();
            List<ODocument> calculated = database.query(new OSQLSynchQuery<Object>(script));
            if (calculated != null && !calculated.isEmpty()) {
                iDocument.field("c", (Object) calculated.get(0).field("value"));
            }
        }

        @Override
        public DISTRIBUTED_EXECUTION_MODE getDistributedExecutionMode() {
            return DISTRIBUTED_EXECUTION_MODE.SOURCE_NODE;
        }
    });
    doc.reload();
    assertEquals(2, (Object) doc.field("a"));
    assertEquals(2, (Object) doc.field("b"));
    assertEquals(4, (Object) doc.field("c"));
    doc = new ODocument(oClass);
    doc.field("a", 3);
    doc.field("b", 3);
    doc.save();
    assertEquals(3, (Object) doc.field("a"));
    assertEquals(3, (Object) doc.field("b"));
    assertEquals(6, (Object) doc.field("c"));
}
Also used : OSchema(com.orientechnologies.orient.core.metadata.schema.OSchema) ODocumentHookAbstract(com.orientechnologies.orient.core.hook.ODocumentHookAbstract) ODatabaseDocument(com.orientechnologies.orient.core.db.document.ODatabaseDocument) OSQLSynchQuery(com.orientechnologies.orient.core.sql.query.OSQLSynchQuery) OClass(com.orientechnologies.orient.core.metadata.schema.OClass) List(java.util.List) 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 2 with ODocumentHookAbstract

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

the class CheckHookCallCountTest method testInHook.

@Test
public void testInHook() throws Exception {
    ODatabaseDocument db = new ODatabaseDocumentTx("memory:" + CheckHookCallCountTest.class.getSimpleName());
    db.create();
    try {
        OSchema schema = db.getMetadata().getSchema();
        OClass oClass = schema.createClass("TestInHook");
        oClass.createProperty("a", OType.INTEGER);
        oClass.createProperty("b", OType.INTEGER);
        oClass.createProperty("c", OType.INTEGER);
        ODocument doc = new ODocument(oClass);
        doc.field("a", 2);
        doc.field("b", 2);
        doc.save();
        doc.reload();
        assertEquals(2, doc.field("a"));
        assertEquals(2, doc.field("b"));
        assertNull(doc.field("c"));
        db.registerHook(new ODocumentHookAbstract(db) {

            {
                setIncludeClasses("TestInHook");
            }

            @Override
            public void onRecordAfterCreate(ODocument iDocument) {
                onRecordAfterRead(iDocument);
            }

            @Override
            public void onRecordAfterRead(ODocument iDocument) {
                String script = "select sum(a, b) as value from " + iDocument.getIdentity();
                List<ODocument> calculated = database.query(new OSQLSynchQuery<Object>(script));
                if (calculated != null && !calculated.isEmpty()) {
                    iDocument.field("c", calculated.get(0).field("value"));
                }
            }

            @Override
            public DISTRIBUTED_EXECUTION_MODE getDistributedExecutionMode() {
                return DISTRIBUTED_EXECUTION_MODE.SOURCE_NODE;
            }
        });
        doc.reload();
        assertEquals(2, doc.field("a"));
        assertEquals(2, doc.field("b"));
        assertEquals(4, doc.field("c"));
        doc = new ODocument(oClass);
        doc.field("a", 3);
        doc.field("b", 3);
        // FAILING here: infinite recursion
        doc.save();
        assertEquals(3, doc.field("a"));
        assertEquals(3, doc.field("b"));
        assertEquals(6, doc.field("c"));
    } finally {
        db.drop();
    }
}
Also used : OSchema(com.orientechnologies.orient.core.metadata.schema.OSchema) ODocumentHookAbstract(com.orientechnologies.orient.core.hook.ODocumentHookAbstract) ODatabaseDocument(com.orientechnologies.orient.core.db.document.ODatabaseDocument) OSQLSynchQuery(com.orientechnologies.orient.core.sql.query.OSQLSynchQuery) OClass(com.orientechnologies.orient.core.metadata.schema.OClass) ODatabaseDocumentTx(com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx) List(java.util.List) ODocument(com.orientechnologies.orient.core.record.impl.ODocument) Test(org.testng.annotations.Test)

Example 3 with ODocumentHookAbstract

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

the class HookChangeValidationTest method testHookUpdateChangeTx.

@Test
public void testHookUpdateChangeTx() {
    ODatabaseDocument db = new ODatabaseDocumentTx("memory:" + HookChangeValidationTest.class.getName());
    db.create();
    try {
        OSchema schema = db.getMetadata().getSchema();
        OClass classA = schema.createClass("TestClass");
        classA.createProperty("property1", OType.STRING).setNotNull(true);
        classA.createProperty("property2", OType.STRING).setReadonly(true);
        classA.createProperty("property3", OType.STRING).setMandatory(true);
        db.registerHook(new ODocumentHookAbstract() {

            @Override
            public RESULT onRecordBeforeCreate(ODocument doc) {
                return RESULT.RECORD_NOT_CHANGED;
            }

            @Override
            public RESULT onRecordBeforeUpdate(ODocument doc) {
                doc.removeField("property1");
                doc.removeField("property2");
                doc.removeField("property3");
                return RESULT.RECORD_CHANGED;
            }

            @Override
            public DISTRIBUTED_EXECUTION_MODE getDistributedExecutionMode() {
                return DISTRIBUTED_EXECUTION_MODE.SOURCE_NODE;
            }
        });
        ODocument doc = new ODocument(classA);
        doc.field("property1", "value1-create");
        doc.field("property2", "value2-create");
        doc.field("property3", "value3-create");
        doc.save();
        assertEquals("value1-create", doc.field("property1"));
        assertEquals("value2-create", doc.field("property2"));
        assertEquals("value3-create", doc.field("property3"));
        doc.field("property1", "value1-update");
        doc.field("property2", "value2-update");
        try {
            db.begin();
            doc.save();
            db.commit();
            Assert.fail("The document save should fail for validation exception");
        } catch (OValidationException ex) {
        }
    } finally {
        db.drop();
    }
}
Also used : OValidationException(com.orientechnologies.orient.core.exception.OValidationException) OSchema(com.orientechnologies.orient.core.metadata.schema.OSchema) ODocumentHookAbstract(com.orientechnologies.orient.core.hook.ODocumentHookAbstract) ODatabaseDocument(com.orientechnologies.orient.core.db.document.ODatabaseDocument) OClass(com.orientechnologies.orient.core.metadata.schema.OClass) ODatabaseDocumentTx(com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx) ODocument(com.orientechnologies.orient.core.record.impl.ODocument) Test(org.testng.annotations.Test)

Example 4 with ODocumentHookAbstract

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

the class HookChangeValidationTest method testHookCreateChangeTx.

@Test
public void testHookCreateChangeTx() {
    ODatabaseDocument db = new ODatabaseDocumentTx("memory:" + HookChangeValidationTest.class.getName());
    db.create();
    try {
        OSchema schema = db.getMetadata().getSchema();
        OClass classA = schema.createClass("TestClass");
        classA.createProperty("property1", OType.STRING).setNotNull(true);
        classA.createProperty("property2", OType.STRING).setReadonly(true);
        classA.createProperty("property3", OType.STRING).setMandatory(true);
        db.registerHook(new ODocumentHookAbstract() {

            @Override
            public RESULT onRecordBeforeCreate(ODocument doc) {
                doc.removeField("property1");
                doc.removeField("property2");
                doc.removeField("property3");
                return RESULT.RECORD_CHANGED;
            }

            @Override
            public RESULT onRecordBeforeUpdate(ODocument doc) {
                return RESULT.RECORD_NOT_CHANGED;
            }

            @Override
            public DISTRIBUTED_EXECUTION_MODE getDistributedExecutionMode() {
                return DISTRIBUTED_EXECUTION_MODE.SOURCE_NODE;
            }
        });
        ODocument doc = new ODocument(classA);
        doc.field("property1", "value1-create");
        doc.field("property2", "value2-create");
        doc.field("property3", "value3-create");
        try {
            db.begin();
            doc.save();
            db.commit();
            Assert.fail("The document save should fail for validation exception");
        } catch (OValidationException ex) {
        }
    } finally {
        db.drop();
    }
}
Also used : OValidationException(com.orientechnologies.orient.core.exception.OValidationException) OSchema(com.orientechnologies.orient.core.metadata.schema.OSchema) ODocumentHookAbstract(com.orientechnologies.orient.core.hook.ODocumentHookAbstract) ODatabaseDocument(com.orientechnologies.orient.core.db.document.ODatabaseDocument) OClass(com.orientechnologies.orient.core.metadata.schema.OClass) ODatabaseDocumentTx(com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx) ODocument(com.orientechnologies.orient.core.record.impl.ODocument) Test(org.testng.annotations.Test)

Example 5 with ODocumentHookAbstract

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

the class HookChangeValidationTest method testHookCreateChange.

@Test
public void testHookCreateChange() {
    ODatabaseDocument db = new ODatabaseDocumentTx("memory:" + HookChangeValidationTest.class.getName());
    db.create();
    try {
        OSchema schema = db.getMetadata().getSchema();
        OClass classA = schema.createClass("TestClass");
        classA.createProperty("property1", OType.STRING).setNotNull(true);
        classA.createProperty("property2", OType.STRING).setReadonly(true);
        classA.createProperty("property3", OType.STRING).setMandatory(true);
        db.registerHook(new ODocumentHookAbstract() {

            @Override
            public RESULT onRecordBeforeCreate(ODocument doc) {
                doc.removeField("property1");
                doc.removeField("property2");
                doc.removeField("property3");
                return RESULT.RECORD_CHANGED;
            }

            @Override
            public RESULT onRecordBeforeUpdate(ODocument doc) {
                return RESULT.RECORD_NOT_CHANGED;
            }

            @Override
            public DISTRIBUTED_EXECUTION_MODE getDistributedExecutionMode() {
                return DISTRIBUTED_EXECUTION_MODE.SOURCE_NODE;
            }
        });
        ODocument doc = new ODocument(classA);
        doc.field("property1", "value1-create");
        doc.field("property2", "value2-create");
        doc.field("property3", "value3-create");
        try {
            doc.save();
            Assert.fail("The document save should fail for validation exception");
        } catch (OValidationException ex) {
        }
    } finally {
        db.drop();
    }
}
Also used : OValidationException(com.orientechnologies.orient.core.exception.OValidationException) OSchema(com.orientechnologies.orient.core.metadata.schema.OSchema) ODocumentHookAbstract(com.orientechnologies.orient.core.hook.ODocumentHookAbstract) ODatabaseDocument(com.orientechnologies.orient.core.db.document.ODatabaseDocument) OClass(com.orientechnologies.orient.core.metadata.schema.OClass) ODatabaseDocumentTx(com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx) ODocument(com.orientechnologies.orient.core.record.impl.ODocument) Test(org.testng.annotations.Test)

Aggregations

ODatabaseDocument (com.orientechnologies.orient.core.db.document.ODatabaseDocument)10 ODocumentHookAbstract (com.orientechnologies.orient.core.hook.ODocumentHookAbstract)10 OClass (com.orientechnologies.orient.core.metadata.schema.OClass)10 OSchema (com.orientechnologies.orient.core.metadata.schema.OSchema)10 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)10 ODatabaseDocumentTx (com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx)5 DISTRIBUTED_EXECUTION_MODE (com.orientechnologies.orient.core.hook.ORecordHook.DISTRIBUTED_EXECUTION_MODE)5 Test (org.junit.Test)5 Test (org.testng.annotations.Test)5 OValidationException (com.orientechnologies.orient.core.exception.OValidationException)4 RESULT (com.orientechnologies.orient.core.hook.ORecordHook.RESULT)4 Ignore (org.junit.Ignore)3 OSQLSynchQuery (com.orientechnologies.orient.core.sql.query.OSQLSynchQuery)2 List (java.util.List)2