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

The following examples show how to use com.orientechnologies.orient.core.record.impl.ODocument#save() . 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: ManageEditorConfigBehavior.java    From Orienteer with Apache License 2.0 6 votes vote down vote up
@Override
protected void respond(AjaxRequestTarget target) {
    if (actionActive)
        return;
    actionActive = true;
    IRequestParameters params = RequestCycle.get().getRequest().getRequestParameters();
    try {
        ODocument document = model.getObject();
        document.field(OArchitectModule.CONFIG_OPROPERTY, params.getParameterValue(CONFIG_VAR));
        document.save();
        target.appendJavaScript("app.executeCallback({save: true});");
    } catch (Exception ex) {
        LOG.error("Can't save editor config to database: {}", ex);
        target.appendJavaScript("app.executeCallback({save: false});");
    }
    actionActive = false;
}
 
Example 2
Source File: ReleaseODocumentCommand.java    From Orienteer with Apache License 2.0 6 votes vote down vote up
@Override
	protected void performMultiAction(AjaxRequestTarget target, List<ODocument> objects) {
		if(objects==null || objects.isEmpty()) return;
		ODocument doc = documentModel.getObject();
		if(doc!=null)
		{
			OProperty property = propertyModel.getObject();
			if(property!=null)
			{
				Collection<ODocument> collection = doc.field(property.getName());
				if(collection!=null) {
					for (ODocument oDocument : objects)
					{
						collection.remove(oDocument);
					}
//					collection.removeAll(objects);
//					doc.field(property.getName(), collection);
					doc.save();
				}
			}
		}
	}
 
Example 3
Source File: DatabaseExternalizerTest.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
private void createSampleDb() {
  try (ODatabaseDocumentTx db = database.getInstance().connect()) {
    OSchema schema = db.getMetadata().getSchema();

    OClass cityType = schema.createClass("City");
    cityType.createProperty("name", OType.STRING);
    cityType.createProperty("country", OType.STRING);
    cityType.createIndex("name_country_idx", INDEX_TYPE.UNIQUE, "name", "country");

    OClass personType = schema.createClass("Person");
    personType.createProperty("name", OType.STRING);
    personType.createProperty("surname", OType.STRING);
    personType.createIndex("name_surname_idx", INDEX_TYPE.UNIQUE, "name", "surname");

    ODocument doc = db.newInstance("Person");
    doc.field("name", "Luke")
        .field("surname", "Skywalker")
        .field("city", new ODocument("City")
            .field("name", "Rome")
            .field("country", "Italy"));
    doc.save();
  }
}
 
Example 4
Source File: TestInAppOrientDBCompatibility.java    From wicket-orientdb with Apache License 2.0 6 votes vote down vote up
@Test
public void testRemovingReadonlyField()
{
	ODatabaseDocument db = wicket.getTester().getDatabase();
	OSchema schema = db.getMetadata().getSchema();
	OClass classA = schema.createClass("TestRemovingField");
	classA.createProperty("name", OType.STRING);
	OProperty property = classA.createProperty("property", OType.STRING);
	property.setReadonly(true);
	
	ODocument doc = new ODocument(classA);
	doc.field("name", "My Name");
	doc.field("property", "value1");
	doc.save();
	
	doc.field("name", "My Name 2");
	doc.field("property", "value2");
	doc.undo("property");
	doc.save();
}
 
Example 5
Source File: DeconflictComponentMetadataTest.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
private boolean tryUpdate(final Component component) {
  try (ODatabaseDocumentTx db = database.getInstance().acquire()) {
    db.begin();

    ODocument copy = initialComponentRecord.copy();
    componentEntityAdapter.writeFields(copy, component);
    copy.save();

    try {
      db.commit();
      return true;
    }
    catch (OConcurrentModificationException e) {
      logger.debug("Update denied due to conflict", e);
      return false;
    }
  }
  finally {
    try (ODatabaseDocumentTx db = database.getInstance().acquire()) {
      componentEntityAdapter.readFields(db.load(componentEntityAdapter.recordIdentity(component)), component);
    }
  }
}
 
Example 6
Source File: DatabaseFreezeServiceImplTest.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Utility method to check the status of the databases by attempting writes.
 * Note: this method only works in non-HA simulations in this test class.
 *
 * @param errorExpected if true, this method will still pass the test when a write fails, and fail if no error is
 *                      encountered. If false, it will fail a test if the write fails.
 */
void verifyWriteFails(boolean errorExpected) {
  for (Provider<DatabaseInstance> provider : providerSet) {
    try (ODatabaseDocumentTx db = provider.get().connect()) {
      db.begin();

      ODocument document = db.newInstance(DB_CLASS);
      document.field(P_NAME, "test");
      document.save();

      try {
        db.commit();
        if (errorExpected) {
          fail("Expected OModificationOperationProhibitedException");
        }
      }
      catch (OModificationOperationProhibitedException e) {
        if (!errorExpected) {
          throw e;
        }
      }
    }
  }
}
 
Example 7
Source File: TestModels.java    From wicket-orientdb with Apache License 2.0 6 votes vote down vote up
@Test
public void testODocumentLinksDataProvider()
{
	ODocument doc1 = new ODocument("ClassA");
	doc1.field("name", "doc1Ext");
	doc1.save();
	ODocument doc2 = new ODocument("ClassA");
	doc2.field("name", "doc2Ext");
	doc2.field("other", Arrays.asList(doc1));
	try {
		ODocumentModel documentModel = new ODocumentModel(doc2);
		OPropertyModel propertyModel = new OPropertyModel("ClassA", "other");
		ODocumentLinksDataProvider provider = new ODocumentLinksDataProvider(documentModel, propertyModel);
		assertEquals(1, provider.size());
		assertEquals(doc1, provider.iterator(0, 1).next());
		doc2.save();
		provider.detach();
		assertEquals(1, provider.size());
		assertEquals(doc1, provider.iterator(0, 1).next());
	} finally {
		doc1.delete();
		doc2.delete();
	}
}
 
Example 8
Source File: OrientDBDocumentAPILiveTest.java    From tutorials with MIT License 5 votes vote down vote up
public void givenDB_whenSavingDocument_thenClassIsAutoCreated() {
    ODocument author = new ODocument("Author");
    author.field("firstName", "Paul");
    author.field("lastName", "Smith");
    author.field("country", "USA");
    author.field("publicProfile", false);
    author.field("level", 7);
    author.save();

    assertEquals("Author", author.getSchemaClass().getName());
}
 
Example 9
Source File: ComponentDatabaseUpgrade_1_6_Test.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@Before
public void setUp() {
  underTest = new ComponentDatabaseUpgrade_1_6(componentDatabase.getInstanceProvider());
  try (ODatabaseDocumentTx db = componentDatabase.getInstance().connect()) {
    OSchema schema = db.getMetadata().getSchema();
    OClass componentType = schema.createClass(DB_CLASS);
    componentType.createProperty(P_ASSET_NAME, OType.STRING);

    for (int i = 0 ; i < 1000 ; i ++) {
      ODocument document = db.newInstance(DB_CLASS);
      document.save();
    }
  }
}
 
Example 10
Source File: WicketOrientDbFilterTesterScope.java    From wicket-orientdb with Apache License 2.0 5 votes vote down vote up
private void createEmbeddedCollectionFieldsForDocuments(List<ODocument> documents, List<List<ODocument>> embeddedList, boolean list) {
    Args.isTrue(documents.size() == embeddedList.size(), "documents.size() == embeddedList.size()");
    String field = list ? EMBEDDED_LIST_FIELD : EMBEDDED_SET_FIELD;
    for (int i = 0; i < documents.size(); i++) {
        ODocument document = documents.get(i);
        document.field(field, embeddedList.get(i), OType.EMBEDDEDLIST);
        document.save();
    }
}
 
Example 11
Source File: OrienteerUsersModule.java    From Orienteer with Apache License 2.0 5 votes vote down vote up
private void updateOrienteerUserRoleDoc(ODatabaseDocument db, ODocument perspective) {
    OSecurity security = db.getMetadata().getSecurity();
    ORole role = security.getRole(ORIENTEER_USER_ROLE);
    if (role == null) {
        ORole reader = security.getRole("reader");
        role = security.createRole(ORIENTEER_USER_ROLE, reader, OSecurityRole.ALLOW_MODES.DENY_ALL_BUT);
    }

    role.grant(ResourceGeneric.CLASS, OWidgetsModule.OCLASS_WIDGET, READ.getPermissionFlag());
    role.grant(ResourceGeneric.CLASS, OWidgetsModule.OCLASS_DASHBOARD, READ.getPermissionFlag());

    // TODO: remove this after release with fix for roles in OrientDB: https://github.com/orientechnologies/orientdb/issues/8338
    role.grant(ResourceGeneric.CLASS, PerspectivesModule.OPerspectiveItem.CLASS_NAME, READ.getPermissionFlag());
    role.grant(ResourceGeneric.CLASS, PerspectivesModule.OPerspective.CLASS_NAME, READ.getPermissionFlag());
    role.grant(ResourceGeneric.CLASS, ORole.CLASS_NAME, READ.getPermissionFlag());
    role.grant(ResourceGeneric.SCHEMA, null, READ.getPermissionFlag());
    role.grant(ResourceGeneric.CLUSTER, "internal", READ.getPermissionFlag());
    role.grant(ResourceGeneric.RECORD_HOOK, "", READ.getPermissionFlag());
    role.grant(ResourceGeneric.DATABASE, null, READ.getPermissionFlag());
    role.grant(ResourceGeneric.DATABASE, "systemclusters", READ.getPermissionFlag());
    role.grant(ResourceGeneric.DATABASE, "function", READ.getPermissionFlag());
    role.grant(ResourceGeneric.DATABASE, "command", READ.getPermissionFlag());

    role.grant(OSecurityHelper.FEATURE_RESOURCE, SearchPage.SEARCH_FEATURE, READ.getPermissionFlag());

    role.grant(ResourceGeneric.CLASS, OrienteerUser.CLASS_NAME, OrientPermission.combinedPermission(READ, UPDATE));
    role.grant(ResourceGeneric.CLASS, OUserSocialNetwork.CLASS_NAME, 11);
    role.grant(ResourceGeneric.DATABASE, "cluster", OrientPermission.combinedPermission(READ, UPDATE));

    role.getDocument().field(ORestrictedOperation.ALLOW_READ.getFieldName(), Collections.singletonList(role.getDocument()));
    role.getDocument().field(PerspectivesModule.PROP_PERSPECTIVE, perspective);
    role.save();

    perspective.field(ORestrictedOperation.ALLOW_READ.getFieldName(), Collections.singletonList(role.getDocument()));
    perspective.save();
}
 
Example 12
Source File: TestInAppOrientDBCompatibility.java    From wicket-orientdb with Apache License 2.0 5 votes vote down vote up
@Test
public void testTransactions() throws Exception {
	ODatabaseDocument db = wicket.getTester().getDatabase();
	try {
		assertFalse(db.getTransaction().isActive());
		OSchema schema = db.getMetadata().getSchema();
		OClass classA = schema.createClass("TransA");
		classA.createProperty("name", OType.STRING);
		ODocument doc = new ODocument(classA);
		doc.field("name", "test1");
		doc.save();
		ORID orid = doc.getIdentity();
		db.begin();
		assertTrue(db.getTransaction().isActive());
		doc = orid.getRecord();
		assertEquals("test1", doc.field("name"));
		doc.field("name", "test2");
		doc = orid.getRecord();
		assertEquals("test2", doc.field("name"));
		//There is NO SAVE!
		db.commit();
		db.getLocalCache().clear();
		/* COMMENT START */
		//db.close();
		//db = wicket.getTester().getDatabase();
		/* COMMENT STOP */
		doc = orid.getRecord();
		assertEquals("test1", doc.field("name"));
		
	} finally {
		db.commit();
	}
}
 
Example 13
Source File: TestPeopleDao.java    From thorntail with Apache License 2.0 5 votes vote down vote up
public ODocument addPerson(String name) {
    try (ODatabaseDocumentTx database = databasePool.acquire()) {
        ODocument document = new ODocument("Person");
        document.field("name", name);
        database.commit();
        return document.save();
    }
}
 
Example 14
Source File: OrientDbDocumentTrial.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
private ODocument createPerson(final ODatabaseDocumentTx db) {
  ODocument doc = db.newInstance("Person");
  doc.field("name", "Luke");
  doc.field("surname", "Skywalker");
  doc.field("city", new ODocument("City")
      .field("name", "Rome")
      .field("country", "Italy"));
  doc.save();
  return doc;
}
 
Example 15
Source File: ExportImportTrial.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
private ODocument createPerson(final ODatabaseDocumentTx db) {
  ODocument doc = db.newInstance("Person");
  doc.field("name", "Luke");
  doc.field("surname", "Skywalker");
  doc.field("city", new ODocument("City")
      .field("name", "Rome")
      .field("country", "Italy"));
  doc.save();
  return doc;
}
 
Example 16
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 17
Source File: OrientDbLoader.java    From nextreports-server with Apache License 2.0 5 votes vote down vote up
@Override
    public void load(Row row) {
        if (row == null) {
            return;
        }

        ODocument document = (ODocument) row.getPayload();

        if (dbAutoCreateProperties) {
            createProperties(row);
        }

        if (className != null) {
            document.setClassName(className);
        }

        if (!documentDatabase.getTransaction().isActive()) {
            // begin the transaction first
            documentDatabase.begin();
        }

//        log.debug("Load document {}", document);
        document.save();

        if (batchCommit > 0) {
            if (batchCounter > batchCommit) {
                documentDatabase.commit();
                documentDatabase.begin();
                batchCounter = 0;
            } else
                batchCounter++;
        }
    }
 
Example 18
Source File: OTasksTest.java    From Orienteer with Apache License 2.0 5 votes vote down vote up
@Test
@Ignore
public void consoleTaskTest() throws Exception{
	assertTrue(OrientDbWebSession.get().signIn("admin", "admin"));
	ODatabaseDocument db = OrientDbWebSession.get().getDatabase();
	assertFalse(db.isClosed());
	db.commit();
	try
	{
		ODocument taskDocument = new ODocument(OConsoleTask.TASK_CLASS);
		taskDocument.field(OTask.Field.AUTODELETE_SESSIONS.fieldName(),false);
		taskDocument.field(OConsoleTask.Field.INPUT.fieldName(),CONSOLE_TEST_COMMAND);
		taskDocument.save();
		db.commit();
		OTask task = OTask.makeFromODocument(taskDocument);

		ODocument taskDocumentAD = new ODocument(OConsoleTask.TASK_CLASS);
		taskDocumentAD.field(OTask.Field.AUTODELETE_SESSIONS.fieldName(),true);
		taskDocumentAD.field(OConsoleTask.Field.INPUT.fieldName(),CONSOLE_TEST_COMMAND);
		taskDocumentAD.save();
		db.commit();
		OTask taskAD = OTask.makeFromODocument(taskDocumentAD);
		
		OTaskSessionRuntime taskSession = task.startNewSession();
		OTaskSessionRuntime taskSessionAD = taskAD.startNewSession();
		Thread.sleep(CONSOLE_TASK_DELAY);
		db.commit();
		ODocument taskSessionDoc = taskSession.getOTaskSessionPersisted().getDocument();
		taskSessionDoc.load();
		assertNull(taskSessionDoc.field(ITaskSession.Field.ERROR.fieldName()));
		assertNotNull(taskSessionDoc.field(ITaskSession.Field.FINISH_TIMESTAMP.fieldName()));
		assertEquals(12L, (Object) taskSessionDoc.field(ITaskSession.Field.PROGRESS_CURRENT.fieldName()));
		assertEquals(ITaskSession.Status.INTERRUPTED,taskSession.getStatus());
	} finally
	{
		OrientDbWebSession.get().signOut();
	}
}
 
Example 19
Source File: ComponentDatabaseUpgrade_1_10.java    From nexus-public with Eclipse Public License 1.0 4 votes vote down vote up
private void moveAssetsToComponent(final List<ODocument> componentsAssets, final ODocument existingComponent) {
  for (ODocument componentsAsset : componentsAssets) {
    componentsAsset.field(P_COMPONENT, existingComponent.getIdentity());
    componentsAsset.save();
  }
}
 
Example 20
Source File: ExportManagerTest.java    From rtc2jira with GNU General Public License v2.0 4 votes vote down vote up
private void createWorkItem(int id) {
  ODocument doc = new ODocument("WorkItem");
  doc.field("ID", id);
  doc.save();
}