Search in sources :

Example 21 with ODatabaseSession

use of com.orientechnologies.orient.core.db.ODatabaseSession in project wicket-orientdb by OrienteerBAP.

the class TestInAppOrientDBCompatibility method testValidationAndHooksOrder.

@Test
@Ignore
public void testValidationAndHooksOrder() {
    ODatabaseSession db = wicket.getTester().getDatabaseSession();
    OSchema schema = db.getMetadata().getSchema();
    OClass classA = schema.createClass(TEST_VALIDATION_AND_HOOKS_CLASS);
    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(db) {

        @Override
        public RESULT onRecordBeforeCreate(ODocument doc) {
            doc.field("property1", "value1-create");
            doc.field("property2", "value2-create");
            doc.field("property3", "value3-create");
            return RESULT.RECORD_CHANGED;
        }

        @Override
        public RESULT onRecordBeforeUpdate(ODocument doc) {
            doc.undo("property2");
            if (doc.field("property3") == null)
                doc.field("property3", "value3-update");
            return RESULT.RECORD_CHANGED;
        }

        @Override
        public DISTRIBUTED_EXECUTION_MODE getDistributedExecutionMode() {
            return DISTRIBUTED_EXECUTION_MODE.SOURCE_NODE;
        }
    });
    ODocument doc = new ODocument(classA);
    // doc.field("property3", "value3-create"); //Magic line #1
    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");
    doc.removeField("property3");
    // doc.undo("property2"); //Magic line #2
    // doc.field("property3", "value3-update"); //Magic line #3
    doc.save();
    assertEquals("value1-update", doc.field("property1"));
    assertEquals("value2-create", doc.field("property2"));
    assertEquals("value3-update", doc.field("property3"));
}
Also used : OSchema(com.orientechnologies.orient.core.metadata.schema.OSchema) ODocumentHookAbstract(com.orientechnologies.orient.core.hook.ODocumentHookAbstract) OClass(com.orientechnologies.orient.core.metadata.schema.OClass) ODatabaseSession(com.orientechnologies.orient.core.db.ODatabaseSession) ODocument(com.orientechnologies.orient.core.record.impl.ODocument) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 22 with ODatabaseSession

use of com.orientechnologies.orient.core.db.ODatabaseSession in project wicket-orientdb by OrienteerBAP.

the class TestInAppOrientDBCompatibility method testUpdatesInHooks.

@Test
@Ignore
public void testUpdatesInHooks() {
    final String className = "TestUpdatesInHook";
    ODatabaseSession db = wicket.getTester().getDatabaseSession();
    db.registerHook(new ODocumentHookAbstract(db) {

        {
            setIncludeClasses(className);
        }

        @Override
        public DISTRIBUTED_EXECUTION_MODE getDistributedExecutionMode() {
            return DISTRIBUTED_EXECUTION_MODE.SOURCE_NODE;
        }

        @Override
        public RESULT onRecordBeforeCreate(ODocument iDocument) {
            return onRecordBeforeUpdate(iDocument);
        }

        @Override
        public RESULT onRecordBeforeUpdate(ODocument iDocument) {
            iDocument.undo("b");
            return RESULT.RECORD_CHANGED;
        }

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

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

        @Override
        public void onRecordAfterRead(ODocument iDocument) {
            if (!iDocument.containsField("b"))
                iDocument.field("b", (Object) iDocument.field("a"));
        }
    });
    OSchema schema = db.getMetadata().getSchema();
    OClass classA = schema.createClass(className);
    classA.createProperty("a", OType.STRING);
    classA.createProperty("b", OType.STRING);
    db.commit();
    ODocument doc = new ODocument(classA);
    /*doc.field("a", "test1");
		doc.save();
		doc.reload();
		assertEquals(doc.field("a"), doc.field("b"));
		doc.field("a", "test2");
		doc.save();
		doc.reload();
		assertEquals(doc.field("a"), doc.field("b"));*/
    db.begin();
    doc.field("a", "test3");
    doc.save();
    doc.reload();
    // doc = db.reload(doc, null, true);
    assertEquals((Object) doc.field("a"), (Object) doc.field("b"));
    doc.field("a", "test4");
    doc.save();
    doc.reload();
    // doc = db.reload(doc, null, true);
    assertEquals((Object) doc.field("a"), (Object) doc.field("b"));
    db.commit();
}
Also used : OSchema(com.orientechnologies.orient.core.metadata.schema.OSchema) ODocumentHookAbstract(com.orientechnologies.orient.core.hook.ODocumentHookAbstract) OClass(com.orientechnologies.orient.core.metadata.schema.OClass) ODatabaseSession(com.orientechnologies.orient.core.db.ODatabaseSession) ODocument(com.orientechnologies.orient.core.record.impl.ODocument) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 23 with ODatabaseSession

use of com.orientechnologies.orient.core.db.ODatabaseSession in project wicket-orientdb by OrienteerBAP.

the class TestInAppOrientDBCompatibility method testLoosingLinkedClass.

@Test
@Ignore
public void testLoosingLinkedClass() throws Exception {
    ODatabaseSession db = wicket.getTester().getDatabaseSession();
    OSchema schema = wicket.getTester().getSchema();
    OClass mainClass = schema.createClass("LMainClass");
    OClass embeddedClass = schema.createClass("LEmbeddedClass");
    mainClass.createProperty("name", OType.STRING);
    mainClass.createProperty("embedded", OType.EMBEDDED).setLinkedClass(embeddedClass);
    embeddedClass.createProperty("name", OType.STRING);
    db.begin();
    ODocument main = new ODocument(mainClass);
    main.field("name", "main");
    ODocument embedded = new ODocument(embeddedClass);
    // embedded.field("name", "embedded");
    main.field("embedded", embedded);
    // NO Save here!
    db.commit();
    db.close();
    main.fromStream(main.toStream());
    db = wicket.getTester().getDatabaseSession();
    db.begin();
    assertEmbeddedIsCorrect(main);
    main.save();
    ORID recordId = main.getIdentity();
    db.commit();
    db.close();
    db = wicket.getTester().getDatabaseSession();
    db.begin();
    main = recordId.getRecord();
    assertEmbeddedIsCorrect(main);
    db.commit();
}
Also used : OSchema(com.orientechnologies.orient.core.metadata.schema.OSchema) OClass(com.orientechnologies.orient.core.metadata.schema.OClass) ORID(com.orientechnologies.orient.core.id.ORID) ODatabaseSession(com.orientechnologies.orient.core.db.ODatabaseSession) ODocument(com.orientechnologies.orient.core.record.impl.ODocument) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 24 with ODatabaseSession

use of com.orientechnologies.orient.core.db.ODatabaseSession in project wicket-orientdb by OrienteerBAP.

the class TestInAppOrientDBCompatibility method testClassChange.

@Test
@Ignore
public void testClassChange() {
    ODatabaseSession db = wicket.getTester().getDatabaseSession();
    OSchema schema = db.getMetadata().getSchema();
    OClass classA = schema.createClass("TestClassChangeA");
    OClass classB = schema.createClass("TestClassChangeB");
    ODocument doc = new ODocument(classA);
    doc.save();
    doc = doc.getIdentity().getRecord();
    doc.setClassName(classB.getName());
    assertEquals(classB.getName(), doc.getClassName());
    doc = doc.getIdentity().getRecord();
    assertEquals(classB.getName(), doc.getClassName());
    ORID id = doc.getIdentity();
    db.commit(true);
    db.close();
    db = wicket.getTester().getDatabaseSession();
    doc = id.getRecord();
    assertEquals(classB.getName(), doc.getClassName());
}
Also used : OSchema(com.orientechnologies.orient.core.metadata.schema.OSchema) OClass(com.orientechnologies.orient.core.metadata.schema.OClass) ORID(com.orientechnologies.orient.core.id.ORID) ODatabaseSession(com.orientechnologies.orient.core.db.ODatabaseSession) ODocument(com.orientechnologies.orient.core.record.impl.ODocument) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 25 with ODatabaseSession

use of com.orientechnologies.orient.core.db.ODatabaseSession in project wicket-orientdb by OrienteerBAP.

the class OrientDbWebSession method authenticate.

@Override
public boolean authenticate(String username, String password) {
    ODatabaseSession currentDB = getDatabaseSession();
    try {
        boolean inTransaction = currentDB.getTransaction().isActive();
        IOrientDbSettings settings = OrientDbWebApplication.get().getOrientDbSettings();
        ODatabaseSession newDB = settings.getContext().cachedPool(settings.getDbName(), username, password).acquire();
        if (newDB != currentDB) {
            currentDB.activateOnCurrentThread();
            currentDB.commit();
            currentDB.close();
            newDB.activateOnCurrentThread();
        }
        setUser(username, password);
        userModel.setObject(newDB.getUser().getDocument());
        if (inTransaction && !newDB.getTransaction().isActive()) {
            newDB.begin();
        }
        return true;
    } catch (OSecurityAccessException e) {
        currentDB.activateOnCurrentThread();
        return false;
    }
}
Also used : OSecurityAccessException(com.orientechnologies.orient.core.exception.OSecurityAccessException) ODatabaseSession(com.orientechnologies.orient.core.db.ODatabaseSession)

Aggregations

ODatabaseSession (com.orientechnologies.orient.core.db.ODatabaseSession)31 Test (org.junit.Test)23 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)21 OClass (com.orientechnologies.orient.core.metadata.schema.OClass)19 OSchema (com.orientechnologies.orient.core.metadata.schema.OSchema)17 Ignore (org.junit.Ignore)10 ORID (com.orientechnologies.orient.core.id.ORID)6 ODocumentHookAbstract (com.orientechnologies.orient.core.hook.ODocumentHookAbstract)5 OResultSet (com.orientechnologies.orient.core.sql.executor.OResultSet)4 OProperty (com.orientechnologies.orient.core.metadata.schema.OProperty)3 ODatabaseDocument (com.orientechnologies.orient.core.db.document.ODatabaseDocument)2 ORecordId (com.orientechnologies.orient.core.id.ORecordId)2 ORecord (com.orientechnologies.orient.core.record.ORecord)2 OSchemaHelper (ru.ydn.wicket.wicketorientdb.utils.OSchemaHelper)2 Function (com.google.common.base.Function)1 Maps (com.google.common.collect.Maps)1 OResettable (com.orientechnologies.common.util.OResettable)1 ODatabaseDocumentInternal (com.orientechnologies.orient.core.db.ODatabaseDocumentInternal)1 ODatabaseRecordThreadLocal (com.orientechnologies.orient.core.db.ODatabaseRecordThreadLocal)1 OrientDB (com.orientechnologies.orient.core.db.OrientDB)1