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();
}
}
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();
}
}
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();
}
}
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);
}
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();
}
Aggregations