We will now follow Gavin Cornwell https://community.alfresco.com/community/ecm/blog/2016/11/11/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.
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.
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.
// 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
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
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
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
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
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
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
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
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));
Ask for and offer help to other Alfresco Content Services Users and members of the Alfresco team.
Related links:
By using this site, you are agreeing to allow us to collect and use cookies as outlined in Alfresco’s Cookie Statement and Terms of Use (and you have a legitimate interest in Alfresco and our products, authorizing us to contact you in such methods). If you are not ok with these terms, please do not use this website.