
- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
We will now follow Gavin Cornwell https://community.alfresco.com/community/ecm/blog/2016/11/02/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 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.
Part 4 - Managing Nodes
Create Empty File
//Create Empty Node with PropertiesLinkedTreeMap<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 InformationString 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 pathnode = 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 InformationLinkedTreeMap<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 NodeNodeRepresentation renamedNode = nodesAPI.updateNodeCall(nodeId, new NodeBodyUpdate("renamed-file.txt")).execute().body();Assert.assertEquals(renamedNode.getName(), "renamed-file.txt");
Change Owner
//Change ownerprops = 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 AspectsList<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 TypeNodeRepresentation changedTypeNode = nodesAPI.updateNodeCall(nodeId, new NodeBodyUpdate(null, "cm:savedquery", null, null)).execute().body();Assert.assertEquals(changedTypeNode.getNodeType(), "cm:savedquery");
Update Content
//Update ContentRequestBody 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 ContentCall<ResponseBody> downloadCall = nodesAPI.getNodeContentCall(nodeId);File dlFile = new File("W:\\", "dl.txt");IOUtils.copyFile(downloadCall.execute().body().byteStream(), dlFile);
Delete Node
//Delete NodeResponse<Void> deleteCall = nodesAPI.deleteNodeCall(nodeId).execute();Assert.assertTrue(deleteCall.isSuccessful());
Permanently delete node
//Delete Node PermanentlyResponse<Void> deleteCall = nodesAPI.deleteNodeCall(nodeId, true).execute();Assert.assertTrue(deleteCall.isSuccessful());
Alfresco Java Client SDK Series
- https://community.alfresco.com/community/ecm/blog/2016/11/17/alfresco-java-client-sdk
- https://community.alfresco.com/community/ecm/blog/2016/11/16/alfresco-java-client-sdk-usage
- https://community.alfresco.com/community/ecm/blog/2016/11/16/alfresco-java-client-sdk-usage-part-3
- https://community.alfresco.com/community/ecm/blog/2016/11/16/alfresco-java-client-sdk-usage-part-4
- https://community.alfresco.com/community/ecm/blog/2016/11/16/alfresco-java-client-sdk-usage-part-5
- https://community.alfresco.com/community/ecm/blog/2016/11/25/alfresco-java-client-sdk-usage-part-6
- https://community.alfresco.com/community/ecm/blog/2017/04/26/alfresco-java-client-sdk-usage-part-7
- https://community.alfresco.com/community/ecm/blog/2017/04/26/alfresco-java-client-sdk-usage-part-8
- https://community.alfresco.com/community/ecm/blog/2017/04/26/alfresco-java-client-sdk-usage-part-9
- https://community.alfresco.com/community/ecm/blog/2017/04/26/alfresco-java-client-sdk-usage-part-10
- https://community.alfresco.com/community/ecm/blog/2017/04/26/alfresco-java-client-sdk-usage-part-11
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.