jm.pascal

Alfresco Java Client SDK - Usage Part 5 - Versioning & Locking

Blog Post created by jm.pascal Employee on Nov 17, 2016

We will now follow Gavin Cornwell v1 REST API - Part 5 - Versioning & Locking examples and see how we can achieve the same experience using the SDK

 

To make the exercise more concise we will execute each request in a synchronous way.

Important Notice

Alfresco Java Client is currently in Early Access mode. It evolves as you use them, as you give feedback, and as the developers update and add file. We like to think app & lib development as services that grow and evolve with the involvement of the community.

 

Prerequisites

In order to follow along you'll need an environment to do so, firstly download and install the 5.2.c Early Access Community Release. In our case we will consider Alfresco is available at http://localhost:8080/alfresco and the "admin" user is available and has "admin" as password.

 

Create Empty File

// Select NodesAPI
NodesAPI nodesAPI = client.getNodesAPI();

//Create Empty Node
NodeBodyCreate emptyFileBody = new NodeBodyCreate("version.txt", ContentModel.TYPE_CONTENT);
Response<NodeRepresentation> initialNodeResponse = nodesAPI.createNodeCall(NodesAPI.FOLDER_MY, emptyFileBody).execute();

 

Update content as major version

//Update content as Major Version
String nodeId = initialNodeResponse.body().getId();
RequestBody requestBody = RequestBody.create(MediaType.parse("text/plain"), "This is the initial content for the file.");
NodeRepresentation updatedContentNode = nodesAPI.updateNodeContentCall(nodeId, requestBody, true, "First version").execute().body();
Assert.assertEquals(updatedContentNode.getContent().getSizeInBytes(), 41);
Assert.assertEquals(updatedContentNode.getProperties().get(ContentModel.PROP_VERSION_LABEL), "1.0");

 

Retrieve version history

//Retrieve version history
VersionAPI versionAPI = client.getVersionAPI();
ResultPaging<VersionRepresentation> versions = versionAPI.listVersionHistoryCall(nodeId).execute().body();
Assert.assertEquals(versions.getCount(), 1);
Assert.assertEquals(versions.getPagination().getCount(), 1);

 

 

Update content as minor version

//Update content as minor version
RequestBody minorRequestBody = RequestBody.create(MediaType.parse("text/plain"), "This is the second version of the content, v1.1.");
NodeRepresentation minorupdatedContentNode = nodesAPI.updateNodeContentCall(nodeId, minorRequestBody, false, "Second version").execute().body();
Assert.assertEquals(updatedContentNode.getContent().getSizeInBytes(), 50);
Assert.assertEquals(updatedContentNode.getProperties().get(ContentModel.PROP_VERSION_LABEL), "1.1");

 

Get content

//Get Content
Call<ResponseBody> downloadCall = nodesAPI.getNodeContentCall(nodeId);
File dlFile = new File("W:\\", "version-1.1.txt");
IOUtils.copyFile(downloadCall.execute().body().byteStream(), dlFile);
Assert.assertEquals(dlFile.length(), 48);

 

 

Retrieve version content

//Retrieve Version Content
Call<ResponseBody> downloadOriginalCall = versionAPI.getVersionContentCall(nodeId, "1.0");
File originalFile = new File("W:\\", "version-1.0.txt");
IOUtils.copyFile(downloadOriginalCall.execute().body().byteStream(), originalFile);
Assert.assertEquals(originalFile.length(), 41);

 

Revert version

//Revert Version
RevertBody revertBody = new RevertBody("Reverted to original", true);
VersionRepresentation versionReverted = versionAPI.revertVersionCall(nodeId, "1.0", revertBody).execute().body();
Assert.assertEquals(versionReverted.getContent().getSizeInBytes(), 41);
Assert.assertEquals(versionReverted.getProperties().get(ContentModel.PROP_VERSION_LABEL), "2.0");

 

Lock file

//Lock File
NodeRepresentation lockedNode = nodesAPI.lockNodeCall(nodeId, new NodeBodyLock(), new IncludeParam(Arrays.asList("isLocked")), null).execute().body();
Assert.assertTrue(lockedNode.isLocked());
Assert.assertTrue(lockedNode.getAspects().contains(ContentModel.ASPECT_LOCKABLE));
Assert.assertEquals(lockedNode.getProperties().get(ContentModel.PROP_LOCK_TYPE), "WRITE_LOCK");
Assert.assertEquals(lockedNode.getProperties().get(ContentModel.PROP_LOCK_LIFETIME), NodeBodyLock.LifetimeEnum.PERSISTENT.toString());

 

Unlock file

//UnLock File
NodeRepresentation unlockedNode = nodesAPI.unlockNodeCall(nodeId, new NodeBodyUnLock(), new IncludeParam(Arrays.asList("isLocked")), null).execute().body();
Assert.assertFalse(unlockedNode.isLocked());
Assert.assertFalse(unlockedNode.getAspects().contains(ContentModel.ASPECT_LOCKABLE));

 

Alfresco Java Client SDK Series

Outcomes