Search in sources :

Example 76 with OSchema

use of com.orientechnologies.orient.core.metadata.schema.OSchema 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 77 with OSchema

use of com.orientechnologies.orient.core.metadata.schema.OSchema 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)

Example 78 with OSchema

use of com.orientechnologies.orient.core.metadata.schema.OSchema 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 79 with OSchema

use of com.orientechnologies.orient.core.metadata.schema.OSchema in project orientdb by orientechnologies.

the class AbstractServerClusterInsertTest method onAfterDatabaseCreation.

/**
   * Event called right after the database has been created and right before to be replicated to the X servers
   *
   * @param db Current database
   */
@Override
protected void onAfterDatabaseCreation(final OrientBaseGraph db) {
    System.out.println("Creating database schema...");
    // CREATE BASIC SCHEMA
    OClass personClass = db.getRawGraph().getMetadata().getSchema().createClass("Person");
    personClass.createProperty("id", OType.STRING);
    personClass.createProperty("name", OType.STRING);
    personClass.createProperty("birthday", OType.DATE);
    personClass.createProperty("children", OType.STRING);
    final OSchema schema = db.getRawGraph().getMetadata().getSchema();
    OClass person = schema.getClass("Person");
    idx = person.createIndex("Person.name", INDEX_TYPE.UNIQUE, "name");
    OClass customer = schema.createClass("Customer", person);
    customer.createProperty("totalSold", OType.DECIMAL);
    OClass provider = schema.createClass("Provider", person);
    provider.createProperty("totalPurchased", OType.DECIMAL);
}
Also used : OSchema(com.orientechnologies.orient.core.metadata.schema.OSchema) OClass(com.orientechnologies.orient.core.metadata.schema.OClass)

Example 80 with OSchema

use of com.orientechnologies.orient.core.metadata.schema.OSchema in project orientdb by orientechnologies.

the class OrientDbCreationHelper method createSchemaDB.

public static void createSchemaDB(ODatabaseDocumentTx db) {
    OSchema schema = db.getMetadata().getSchema();
    // item
    OClass item = schema.createClass("Item");
    item.createProperty("stringKey", OType.STRING).createIndex(INDEX_TYPE.UNIQUE);
    item.createProperty("intKey", OType.INTEGER).createIndex(INDEX_TYPE.UNIQUE);
    item.createProperty("date", OType.DATE).createIndex(INDEX_TYPE.NOTUNIQUE);
    item.createProperty("time", OType.DATETIME).createIndex(INDEX_TYPE.NOTUNIQUE);
    item.createProperty("text", OType.STRING);
    item.createProperty("score", OType.DECIMAL);
    item.createProperty("length", OType.LONG).createIndex(INDEX_TYPE.NOTUNIQUE);
    item.createProperty("published", OType.BOOLEAN).createIndex(INDEX_TYPE.NOTUNIQUE);
    item.createProperty("title", OType.STRING).createIndex(INDEX_TYPE.NOTUNIQUE);
    item.createProperty("author", OType.STRING).createIndex(INDEX_TYPE.NOTUNIQUE);
    item.createProperty("tags", OType.EMBEDDEDLIST);
    // class Article
    OClass article = schema.createClass("Article");
    article.createProperty("uuid", OType.INTEGER).createIndex(INDEX_TYPE.UNIQUE);
    article.createProperty("date", OType.DATE).createIndex(INDEX_TYPE.NOTUNIQUE);
    article.createProperty("title", OType.STRING);
    article.createProperty("content", OType.STRING);
    // article.createProperty("attachment", OType.LINK);
    // author
    OClass author = schema.createClass("Author");
    author.createProperty("uuid", OType.LONG).createIndex(INDEX_TYPE.UNIQUE);
    author.createProperty("name", OType.STRING).setMin("3");
    author.createProperty("rating", OType.DOUBLE);
    author.createProperty("articles", OType.LINKLIST, article);
    // link article-->author
    article.createProperty("author", OType.LINK, author);
    //Graph
    OrientGraphNoTx graph = new OrientGraphNoTx(db);
    OrientVertexType post = graph.createVertexType("Post");
    post.createProperty("uuid", OType.LONG);
    post.createProperty("title", OType.STRING);
    post.createProperty("date", OType.DATE).createIndex(INDEX_TYPE.NOTUNIQUE);
    post.createProperty("content", OType.STRING);
    OrientVertexType writer = graph.createVertexType("Writer");
    writer.createProperty("uuid", OType.LONG).createIndex(INDEX_TYPE.UNIQUE);
    writer.createProperty("name", OType.STRING);
    writer.createProperty("is_active", OType.BOOLEAN);
    writer.createProperty("isActive", OType.BOOLEAN);
    graph.createEdgeType("Writes");
    schema.reload();
}
Also used : OSchema(com.orientechnologies.orient.core.metadata.schema.OSchema) OClass(com.orientechnologies.orient.core.metadata.schema.OClass) OrientGraphNoTx(com.tinkerpop.blueprints.impls.orient.OrientGraphNoTx) OrientVertexType(com.tinkerpop.blueprints.impls.orient.OrientVertexType)

Aggregations

OSchema (com.orientechnologies.orient.core.metadata.schema.OSchema)203 OClass (com.orientechnologies.orient.core.metadata.schema.OClass)165 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)122 OCommandSQL (com.orientechnologies.orient.core.sql.OCommandSQL)54 ODatabaseDocumentTx (com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx)46 Test (org.testng.annotations.Test)35 OSQLSynchQuery (com.orientechnologies.orient.core.sql.query.OSQLSynchQuery)31 OProperty (com.orientechnologies.orient.core.metadata.schema.OProperty)23 Test (org.junit.Test)19 Before (org.junit.Before)16 ORID (com.orientechnologies.orient.core.id.ORID)15 OIndex (com.orientechnologies.orient.core.index.OIndex)15 Collection (java.util.Collection)15 Set (java.util.Set)15 ODatabaseDocument (com.orientechnologies.orient.core.db.document.ODatabaseDocument)9 OIdentifiable (com.orientechnologies.orient.core.db.record.OIdentifiable)9 BeforeClass (org.testng.annotations.BeforeClass)9 OStorage (com.orientechnologies.orient.core.storage.OStorage)6 ArrayList (java.util.ArrayList)5 OCommandExecutionException (com.orientechnologies.orient.core.exception.OCommandExecutionException)4