Java Code Examples for com.orientechnologies.orient.core.record.impl.ODocument#reload()

The following examples show how to use com.orientechnologies.orient.core.record.impl.ODocument#reload() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TestInAppOrientDBCompatibility.java    From wicket-orientdb with Apache License 2.0 6 votes vote down vote up
@Test
public void testGettingFields()
{
	OSchema schema = wicket.getTester().getSchema();
	OClass classA = schema.createClass("GettingFields");
	classA.createProperty("title", OType.STRING);
	classA.createProperty("link", OType.LINK).setLinkedClass(classA);
	classA.createProperty("multi", OType.LINKLIST).setLinkedClass(classA);
	ODocument doc = new ODocument(classA);
	doc.field("title", "test");
	doc.save();
	doc.field("link", doc);
	doc.field("multi", Arrays.asList(doc));
	doc.save();
	doc.reload();
	assertNotNull(doc.field("title"));
	assertNotNull(doc.field("link"));
	assertNotNull(doc.field("multi"));
}
 
Example 2
Source File: OLuceneClassIndexManager.java    From orientdb-lucene with Apache License 2.0 6 votes vote down vote up
@Override
public RESULT onRecordBeforeDelete(final ODocument iDocument) {
  final ORecordVersion version = iDocument.getRecordVersion(); // Cache the transaction-provided value
  if (iDocument.fields() == 0) {
    // FORCE LOADING OF CLASS+FIELDS TO USE IT AFTER ON onRecordAfterDelete METHOD
    iDocument.reload();
    if (version.getCounter() > -1 && iDocument.getRecordVersion().compareTo(version) != 0) // check for record version errors
      if (OFastConcurrentModificationException.enabled())
        throw OFastConcurrentModificationException.instance();
      else
        throw new OConcurrentModificationException(iDocument.getIdentity(), iDocument.getRecordVersion(), version,
            ORecordOperation.DELETED);
  }

  return RESULT.RECORD_NOT_CHANGED;
}
 
Example 3
Source File: TestRestApi.java    From wicket-orientdb with Apache License 2.0 5 votes vote down vote up
@Test
public void testQueryAndUpdate() throws Exception
{
	ODocument doc = (ODocument) wicket.getTester().getDatabase().browseClass(TEST_REST_CLASS).current();
	String ret = wicket.getTester().executeUrl("orientdb/query/db/sql/select+from+"+TEST_REST_CLASS, "GET", null);
	assertTrue(ret.contains(doc.toJSON()));
	
	int nextB = RANDOM.nextInt();
	ret = wicket.getTester().executeUrl("orientdb/command/db/sql", "POST", "update "+TEST_REST_CLASS+" set b = "+nextB, "admin", "admin");
	doc.reload();
	assertEquals(nextB, (Object) doc.field("b"));
	wicket.getTester().signOut();
}
 
Example 4
Source File: OLoggerRepository.java    From Orienteer with Apache License 2.0 5 votes vote down vote up
public static OLoggerEventModel storeOLoggerEvent(ODatabaseDocument db, String eventJson) {
    ODocument doc = new ODocument();
    doc.fromJSON(eventJson);
    Long dateTime = doc.field(OLoggerEventModel.PROP_DATE_TIME, Long.class);
    doc.field(OLoggerEventModel.PROP_DATE_TIME, new Date(dateTime));
    doc.setClassName(OLoggerEventModel.CLASS_NAME);
    doc.save();
    doc.reload();
    return new OLoggerEventModel(doc);
}
 
Example 5
Source File: HooksTest.java    From Orienteer with Apache License 2.0 5 votes vote down vote up
@Test
@Sudo
public void testCallbackHook() throws Exception {
	ODatabaseDocument db = OrientDbWebSession.get().getDatabase();
	OSchema schema = db.getMetadata().getSchema();
	OClass oClass = schema.createClass(TEST_CLASS_CALLBACK);
	oClass.createProperty("name", OType.STRING);
	try {
		ODocument doc = new ODocument(oClass);
		doc.field("name", "testname");
		TestCallback callback = new TestCallback();
		CallbackHook.registerCallback(doc, TYPE.AFTER_CREATE, callback);
		CallbackHook.registerCallback(doc, TYPE.BEFORE_CREATE, callback);
		doc.save();
		assertEquals("executed", doc.field("callback"+TYPE.AFTER_CREATE));
		assertEquals("executed", doc.field("callback"+TYPE.BEFORE_CREATE));
		assertFalse(doc.containsField("__callbacks__"));
		doc.reload();
		assertFalse(doc.containsField("__callbacks__"));
		assertFalse(doc.containsField("callback"+TYPE.AFTER_READ)); 
		CallbackHook.registerCallback(doc, TYPE.AFTER_READ, callback);
		doc.reload();
		assertEquals("executed", doc.field("callback"+TYPE.AFTER_READ));
	} finally {
		schema.dropClass(TEST_CLASS_CALLBACK);
	}
}
 
Example 6
Source File: HooksTest.java    From Orienteer with Apache License 2.0 4 votes vote down vote up
@Test
@Sudo
public void testCalculableHook() throws Exception
{
	ODatabaseDocument db = OrientDbWebSession.get().getDatabase();
	OSchema schema = db.getMetadata().getSchema();
	
	assertFalse(db.isClosed());
	db.commit();
	if(schema.existsClass(TEST_CLASS_A)) schema.dropClass(TEST_CLASS_A);
	OClass oClass = schema.createClass(TEST_CLASS_A);
	try
	{
		oClass.createProperty("a", OType.INTEGER);
		oClass.createProperty("b", OType.INTEGER);
		OProperty cProperty = oClass.createProperty("c", OType.INTEGER);
		OProperty dProperty = oClass.createProperty("d", OType.INTEGER);
		CustomAttribute.CALCULABLE.setValue(cProperty, true);
		CustomAttribute.CALCULABLE.setValue(dProperty, true);
		CustomAttribute.CALC_SCRIPT.setValue(cProperty, "select sum(a, b) as value from TestClassA where @rid = ?");
		CustomAttribute.CALC_SCRIPT.setValue(dProperty, "sum(a, b)");
		
		ODocument doc = new ODocument(oClass);
		doc.field("a", 2);
		doc.field("b", 2);
		doc.save();
		doc.reload();
		assertEquals(4, (Object) doc.field("c"));
		assertEquals(4, (Object) doc.field("d"));
		doc.field("a", 3);
		doc.field("b", 3);
		doc.save();
		doc.reload();
		assertEquals(6, (Object) doc.field("c"));
		assertEquals(6, (Object) doc.field("d"));
		db.begin();
		doc.field("a", 4);
		doc.field("b", 4);
		doc.save();
		doc.reload();
		assertEquals(8, (Object) doc.field("c"));
		assertEquals(8, (Object) doc.field("d"));
		db.commit();
		db.begin();
		doc.field("a", 5);
		doc.field("b", 5);
		doc.save();
		doc.reload();
		assertEquals(10, (Object) doc.field("c"));
		assertEquals(10, (Object) doc.field("d"));
		db.commit();
	} finally
	{
		if(db.getTransaction().isActive()) db.commit();
		schema.dropClass(TEST_CLASS_A);
	}
}
 
Example 7
Source File: HooksTest.java    From Orienteer with Apache License 2.0 4 votes vote down vote up
@Test
@Sudo
public void testReferencesHookDeepCase() throws Exception
{
	ODatabaseDocument db = OrientDbWebSession.get().getDatabase();
	OSchema schema = db.getMetadata().getSchema();
	
	assertFalse(db.isClosed());
	db.commit();
	if(schema.existsClass(TEST_CLASS_C)) schema.dropClass(TEST_CLASS_C);
	OClass classC = schema.createClass(TEST_CLASS_C);
	try
	{
		OProperty parent = classC.createProperty("parent", OType.LINK);
		OProperty child = classC.createProperty("child", OType.LINKLIST);
		CustomAttribute.PROP_INVERSE.setValue(parent, child);
		CustomAttribute.PROP_INVERSE.setValue(child, parent);
		
		ODocument doc1 = new ODocument(classC).save();
		ODocument doc2 = new ODocument(classC).save();
		ODocument doc3 = new ODocument(classC).save();
		
		doc1.field("child", Arrays.asList(doc2));
		doc1.save();
		doc2.reload();
		assertEquals(doc1, doc2.field("parent"));
		
		doc3.field("child", Arrays.asList(doc2));
		doc3.save();
		doc2.reload();
		assertEquals(doc3, doc2.field("parent"));
		doc1.reload();
		List<ODocument> childs = doc1.field("child");
		assertNotNull(childs);
		assertArrayEquals(new ODocument[0], childs.toArray(new ODocument[0]));
		
	} finally
	{
		schema.dropClass(TEST_CLASS_C);
	}
}
 
Example 8
Source File: HooksTest.java    From Orienteer with Apache License 2.0 4 votes vote down vote up
@Test
@Sudo
public void testReferencesHookChangeParent() throws Exception
{
	ODatabaseDocument db = OrientDbWebSession.get().getDatabase();
	OSchema schema = db.getMetadata().getSchema();
	
	assertFalse(db.isClosed());
	db.commit();
	if(schema.existsClass(TEST_CLASS_C)) schema.dropClass(TEST_CLASS_C);
	OClass classC = schema.createClass(TEST_CLASS_C);
	try
	{
		OProperty parent = classC.createProperty("parent", OType.LINK);
		OProperty child = classC.createProperty("child", OType.LINKLIST);
		CustomAttribute.PROP_INVERSE.setValue(parent, child);
		CustomAttribute.PROP_INVERSE.setValue(child, parent);
		
		ODocument doc1 = new ODocument(classC).save();
		ODocument doc2 = new ODocument(classC).save();
		ODocument doc3 = new ODocument(classC).save();
		
		doc1.field("child", Arrays.asList(doc2));
		doc1.save();
		doc2.reload();
		assertEquals(doc1, doc2.field("parent"));
		
		doc2.field("parent", doc3);
		doc2.save();
		
		doc1.reload();
		doc3.reload();
		List<ODocument> childs = doc1.field("child");
		assertNotNull(childs);
		assertArrayEquals(new ODocument[0], childs.toArray(new ODocument[0]));
		childs = doc3.field("child");
		assertNotNull(childs);
		assertArrayEquals(new ODocument[]{doc2}, childs.toArray(new ODocument[0]));
		
	} finally
	{
		schema.dropClass(TEST_CLASS_C);
	}
}