jm.pascal

Alfresco Java Client SDK - Usage Part 4 - Managing Nodes

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

We will now follow  Gavin Cornwell  v1 REST API - Part 4 - Managing Nodes  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 ReleaseIn our case we will consider Alfresco is available at http://localhost:8080/alfresco and the "admin" user is available and has "admin" as password.

 

Part 4 - Managing Nodes

 

Create Empty File

//Create Empty Node with Properties
LinkedTreeMap<String, Object> properties = new LinkedTreeMap<>();
properties.put(ContentModel.PROP_TITLE, "The Title");

NodeBodyCreate emptyFileBody = new NodeBodyCreate("my-file.txt", ContentModel.TYPE_CONTENT, properties, null);

Response<NodeRepresentation> emptyNodeResponse = nodesAPI.createNodeCall(NodesAPI.FOLDER_MY, emptyFileBody).execute();

NodeRepresentation emptyNode = emptyNodeResponse.body();
Assert.assertEquals(emptyNode.getContent().getSizeInBytes(), 0);

Get Node Information

//Get Node Information
String nodeId = emptyNode.getId();
NodeRepresentation node = nodesAPI.getNodeCall(nodeId).execute().body();
Assert.assertNotNull(node.getProperties());
Assert.assertNotNull(node.getAspects());
Assert.assertTrue(node.isFile());
Assert.assertFalse(node.isFolder());
Assert.assertNull(node.isLink());

Get Node Information with isLink and path

//Get node information with isLink and path
node = nodesAPI.getNodeCall(nodeId, new IncludeParam(Arrays.asList("isLink", "path")), null, null).execute().body();
Assert.assertFalse(node.isLink());
Assert.assertNotNull(node.getPath());

Update Node Information

//Update Node Information
LinkedTreeMap<String, Object> props = new LinkedTreeMap<>();
props.put(ContentModel.PROP_DESCRIPTION, "The Description");
props.put(ContentModel.PROP_MANUFACTURER, "Canon");
NodeRepresentation updatedNode = nodesAPI.updateNodeCall(nodeId, new NodeBodyUpdate(props)).execute().body();
Assert.assertEquals(updatedNode.getProperties().get(ContentModel.PROP_DESCRIPTION), "The Description");
Assert.assertEquals(updatedNode.getProperties().get(ContentModel.PROP_MANUFACTURER), "Canon");

 

Rename Node

//Rename Node
NodeRepresentation renamedNode = nodesAPI.updateNodeCall(nodeId, new NodeBodyUpdate("renamed-file.txt")).execute().body();
Assert.assertEquals(renamedNode.getName(), "renamed-file.txt");

 

Change Owner

//Change owner
props = new LinkedTreeMap<>();
props.put("cm:owner", "gavinc");
NodeRepresentation ownerNode = nodesAPI.updateNodeCall(nodeId, new NodeBodyUpdate(props)).execute().body();
Assert.assertEquals(((LinkedTreeMap)ownerNode.getProperties().get("cm:owner")).get("id"), "gavinc");

 

Remove EXIF Aspect

//Remove Exif Aspects
List<String> aspectNames = new ArrayList<>();
aspectNames.add(ContentModel.ASPECT_TITLED);
aspectNames.add(ContentModel.ASPECT_AUDITABLE);
aspectNames.add("cm:ownable");
NodeRepresentation aspectNode = nodesAPI.updateNodeCall(nodeId, new NodeBodyUpdate(null, null, aspectNames)).execute().body();
Assert.assertTrue(aspectNode.getAspects().contains(ContentModel.ASPECT_TITLED));
Assert.assertTrue(aspectNode.getAspects().contains(ContentModel.ASPECT_AUDITABLE));
Assert.assertTrue(aspectNode.getAspects().contains("cm:ownable"));
Assert.assertFalse(aspectNode.getAspects().contains(ContentModel.ASPECT_EXIF));

 

Change Node type

//Change Node Type
NodeRepresentation changedTypeNode = nodesAPI.updateNodeCall(nodeId, new NodeBodyUpdate(null, "cm:savedquery", null, null)).execute().body();
Assert.assertEquals(changedTypeNode.getNodeType(), "cm:savedquery");

 

Update Content

//Update Content
RequestBody requestBody = RequestBody.create(MediaType.parse("text/plain"), "This is the initial content for the file.");
NodeRepresentation updatedContentNode = nodesAPI.updateNodeContentCall(nodeId, requestBody).execute().body();
Assert.assertEquals(updatedContentNode.getContent().getSizeInBytes(), 41);

 

Get Content

//Get Content
Call<ResponseBody> downloadCall = nodesAPI.getNodeContentCall(nodeId);
File dlFile = new File("W:\\", "dl.txt");
IOUtils.copyFile(downloadCall.execute().body().byteStream(), dlFile);

 

Delete Node

//Delete Node
Response<Void> deleteCall = nodesAPI.deleteNodeCall(nodeId).execute();
Assert.assertTrue(deleteCall.isSuccessful());

 

Permanently delete node

//Delete Node Permanently
Response<Void> deleteCall = nodesAPI.deleteNodeCall(nodeId, true).execute();
Assert.assertTrue(deleteCall.isSuccessful());

 

Alfresco Java Client SDK Series

Outcomes