Skip to main content
Version: 24.3

Updating Assets

Updating assets through the Java SDK involves calling the Save method of the Services object. The process is that you first have to query for the asset to update, make the update in memory, then save the asset back to VersionOne.

Prior to updating an asset in VersionOne, you must first instantiate a V1Connector and Services object:

V1Connector connector = V1Connector
.withInstanceUrl("<Server Base URI>")
.withUserAgentHeader("AppName", "1.0")
.withAccessToken("1.rWM8lKLk+PnyFxkEWVX5Kl2u6Jk=")
.build();

IServices services = new Services(connector);

In versions of the SDK prior to the 15.0.0.0 release, you would also have to instantiate a connector for theMetaModelobject. However, starting with the 15.0.0.0 release, that is no longer necessary. TheMetaModelobject is now available from thegetMetamethod of theServicesobject. For more advancedServicesconstructor overloads, see the Creating a Services Object topic.

Updating a Scalar ValueAttribute

This example shows that updating a scalar attribute on an asset is accomplished by calling the setAttributeValue method on an asset, specifying the attribute definition of the attribute you wish to change and the new scalar value. This example updates the Name attribute on the Story with ID 7617:

Oid storyId = services.getOid("Story:7617");

Query query = new Query(storyId);
IAssetType storyType = services.getMeta().getAssetType("Story");
IAttributeDefinition nameAttribute = storyType.getAttributeDefinition("Name");
query.getSelection().add(nameAttribute);
QueryResult result = services.retrieve(query);
Asset story = result.getAssets()[0];
String oldName = story.getAttribute(nameAttribute).getValue().toString();
story.setAttributeValue(nameAttribute, "New Name");
services.save(story);

System.out.println(story.getOid().getToken());
System.out.println(oldName);
System.out.println(story.getAttribute(nameAttribute).getValue());

/***** OUTPUT *****
Story:7617:9244
My New Story
New Name
******************/

Updating a Single-Value Relation Attribute

This example shows that updating a single-value relation is accomplished by calling the setAttributeValue method on an asset, specifying the attribute definition of the attribute you wish to change and the ID for the new relation. This example updates the source of the Story with ID 7617:


Oid storyId = services.getOid("Story:7617");

Query query = new Query(storyId);
IAssetType storyType = services.getMeta().getAssetType("Story");
IAttributeDefinition sourceAttribute = storyType.getAttributeDefinition("Source");
query.getSelection().add(sourceAttribute);
QueryResult result = services.retrieve(query);
Asset story = result.getAssets()[0];
String oldSource = story.getAttribute(sourceAttribute).getValue().toString();
story.setAttributeValue(sourceAttribute, "StorySource:149");
services.save(story);

System.out.println(story.getOid().getToken());
System.out.println(oldSource);
System.out.println(story.getAttribute(sourceAttribute).getValue());

/***** OUTPUT *****
Story:7617:9245
NULL
StorySource:149
******************/

Updating a Multi-Value Relation Attribute

This example shows that updating a multi-value relation is accomplished by calling either the removeAttributeValue or addAttributeValue methods on an asset, specifying the attribute definition of the attribute you wish to change and the ID of the relation you wish to add or remove. This example updatesone Member and removes another Member from the Story with ID 7617:


Oid storyId = services.getOid("Story:7617");

Query query = new Query(storyId);
IAssetType storyType = services.getMeta().getAssetType("Story");
IAttributeDefinition ownersAttribute = storyType.getAttributeDefinition("Owners");
query.getSelection().add(ownersAttribute);
QueryResult result = services.retrieve(query);
Asset story = result.getAssets()[0];

List<Object> oldOwners = new ArrayList<Object>();
oldOwners.addAll(Arrays.asList(story.getAttribute(ownersAttribute).getValues()));
story.removeAttributeValue(ownersAttribute, "Member:20");
story.addAttributeValue(ownersAttribute, "Member:2024");
services.save(story);
System.out.println(story.getOid().getToken());
Iterator<Object> iter = oldOwners.iterator();

while (iter.hasNext()) {
Oid oid = (Oid) iter.next();
System.out.println(oid.getToken());
}

for (Object o : story.getAttribute(ownersAttribute).getValues()) {
Oid oid = (Oid) o;
System.out.println(oid.getToken());
}

/***** OUTPUT *****
Story:7617:9247
Member:20
Member:2024
******************/