Alfresco Java Client SDK - Usage Part 6 - Associations

cancel
Showing results for 
Search instead for 
Did you mean: 

Alfresco Java Client SDK - Usage Part 6 - Associations

jm_pascal
Active Member
1 0 1,487

We will now follow  Gavin Cornwell  https://community.alfresco.com/community/ecm/blog/2016/11/23/v1-rest-api-part-6-associations 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.

Attempt creation of fdk:gadget node

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

//Create Empty Node
NodeBodyCreate emptyFileBody = new NodeBodyCreate("fdk.txt", "fdk:gadget");
Response<NodeRepresentation> initialNodeResponse = nodesAPI.createNodeCall(NodesAPI.FOLDER_MY, emptyFileBody).execute();‍‍‍‍‍‍
Assert.assertFalse(initialNodeResponse.isSuccessful());
Assert.assertEquals(initialNodeResponse.code(), 422);‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

Create Images folder

//Create Images Folder
NodeBodyCreate creationBody = new NodeBodyCreate("Images", ContentModel.TYPE_FOLDER);
Response<NodeRepresentation> folderCreationResponse = nodesAPI.createNodeCall(NodesAPI.FOLDER_MY, creationBody).execute();
Assert.assertTrue(folderCreationResponse.isSuccessful());
Assert.assertEquals(folderCreationResponse.body().getName(), "Images");‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

Upload first image

//Upload First Image
File file = new File("W:\\image.png");
RequestBody requestBody = RequestBody.create(MediaType.parse("image/png"), file);
MultipartBody.Builder multipartBuilder = new MultipartBody.Builder();
multipartBuilder.addFormDataPart("filedata", "image.png", requestBody);
RequestBody fileRequestBody = multipartBuilder.build();

//Create Content
Response<NodeRepresentation> firstImageResponse = nodesAPI.createNodeCall(folderCreationResponse.body().getId(), fileRequestBody).execute();‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

Upload second image

//Upload Second Image
file = new File("W:\\image.png");
requestBody = RequestBody.create(MediaType.parse("image/png"), file);
multipartBuilder = new MultipartBody.Builder();
multipartBuilder.addFormDataPart("filedata", "image2.png", requestBody);
fileRequestBody = multipartBuilder.build();

//Create Content
Response<NodeRepresentation> secondImageResponse = nodesAPI.createNodeCall(folderCreationResponse.body().getId(), fileRequestBody).execute();‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

Upload review text

//Upload review text
file = new File("W:\\test.txt");
requestBody = RequestBody.create(MediaType.parse("text/plain"), file);
multipartBuilder = new MultipartBody.Builder();
multipartBuilder.addFormDataPart("filedata", "test.txt", requestBody);
fileRequestBody = multipartBuilder.build();

HashMap<String, RequestBody> map = new HashMap<>();
map.put("filedata", fileRequestBody);
map.put("name", RequestBody.create(MediaType.parse("multipart/form-data"), "review-text.txt"));

//Create Content
Response<NodeRepresentation> reviewTextResponse = nodesAPI.createNodeCall(NodesAPI.FOLDER_MY, map).execute();‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

Create Amazon company node

//Create Amazon company node
Map<String, Object> props = new HashMap<>();
props.put("fdk:email", "info@amazon.com");
props.put("fdk:url", "http://www.amazon.com");
props.put("fdk:city", "Seattle");

NodeBodyCreate amazonNode = new NodeBodyCreate("Amazon", "fdk:company", props, null);
Response<NodeRepresentation> amazonNodeResponse = nodesAPI.createNodeCall(NodesAPI.FOLDER_MY, amazonNode).execute();
Assert.assertTrue(amazonNodeResponse.isSuccessful());‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

Create fdk:gadget node

//Create fdk:gadget node
//Secondary Children
ChildAssociationBody firstImage = new ChildAssociationBody(firstImageResponse.body().getId(), "fdk:images");
ChildAssociationBody secondImage = new ChildAssociationBody(secondImageResponse.body().getId(), "fdk:images");
List<ChildAssociationBody> secondaryChildren = Arrays.asList(firstImage, secondImage);

//Target
AssociationBody review = new AssociationBody(reviewTextResponse.body().getId(), "fdk:reviews");
AssociationBody company = new AssociationBody(amazonNodeResponse.body().getId(), "fdk:company");
List<AssociationBody> targets = Arrays.asList(review, company);

//Create Gadget
NodeBodyCreate gadgetNode = new NodeBodyCreate("Amazon Echo", "fdk:gadget", null, null, null, null, secondaryChildren, targets);
Response<NodeRepresentation> amazonEchoResponse = nodesAPI.createNodeCall(NodesAPI.FOLDER_MY, gadgetNode).execute();
Assert.assertTrue(amazonEchoResponse.isSuccessful());‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

Get fdk:gadget peer associations

//Get fdk:gadget peer associations
Response<ResultPaging<NodeRepresentation>> targetsAssocsResponse = nodesAPI.listTargetAssociationsCall(amazonEchoResponse.body().getId()).execute();
Assert.assertTrue(targetsAssocsResponse.isSuccessful());
Assert.assertEquals(targetsAssocsResponse.body().getCount(), 2);‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

Get fdk:reviews association with path and properties

//Get fdk:gadget peer associations
Response<ResultPaging<NodeRepresentation>> targetsAssocsResponse2 = nodesAPI.listTargetAssociationsCall(
amazonEchoResponse.body().getId(),
"(assocType='fdk:reviews')",
new IncludeParam(Arrays.asList("properties","path")), null).execute();
Assert.assertTrue(targetsAssocsResponse2.isSuccessful());
Assert.assertEquals(targetsAssocsResponse2.body().getCount(), 1);‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

Get fdk:gadget secondary child associations

//Get fdk:gadget secondary child associations
Response<ResultPaging<NodeRepresentation>> childAssocsResponse = nodesAPI.listSecondaryChildrenCall(amazonEchoResponse.body().getId()).execute();
Assert.assertTrue(childAssocsResponse.isSuccessful());
Assert.assertEquals(childAssocsResponse.body().getCount(), 2);‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

Get review text sources

Response<ResultPaging<NodeRepresentation>> reviewAssocsResponse = nodesAPI.listSourceAssociationsCall(reviewTextResponse.body().getId()).execute();
Assert.assertTrue(reviewAssocsResponse.isSuccessful());
Assert.assertEquals(reviewAssocsResponse.body().getCount(), 1);‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

Get first image parents

Response<ResultPaging<NodeRepresentation>> firstImageParentResponse = nodesAPI.listParentsCall(firstImageResponse.body().getId()).execute();
Assert.assertTrue(firstImageParentResponse.isSuccessful());
Assert.assertEquals(firstImageParentResponse.body().getCount(), 2);‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

Upload third image

//Upload Third image
file = new File("W:\\image.png");
requestBody = RequestBody.create(MediaType.parse("image/png"), file);
multipartBuilder = new MultipartBody.Builder();
multipartBuilder.addFormDataPart("filedata", "image3.png", requestBody);
fileRequestBody = multipartBuilder.build();

//Create Content
Response<NodeRepresentation> thirdImageResponse = nodesAPI.createNodeCall(folderCreationResponse.body().getId(), fileRequestBody).execute();‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

Upload second review text

file = new File("W:\\test.txt");
requestBody = RequestBody.create(MediaType.parse("text/plain"), file);
multipartBuilder = new MultipartBody.Builder();
multipartBuilder.addFormDataPart("filedata", "test.txt", requestBody);
fileRequestBody = multipartBuilder.build();

HashMap<String, RequestBody> map2 = new HashMap<>();
map2.put("filedata", fileRequestBody);
map2.put("name", RequestBody.create(MediaType.parse("multipart/form-data"), "2nd-review-text.txt"));

//Create Content
Response<NodeRepresentation> secondReviewTextResponse = nodesAPI.createNodeCall(NodesAPI.FOLDER_MY, map2).execute();‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

Create fdk:images child association

ChildAssociationBody thirdImage = new ChildAssociationBody(thirdImageResponse.body().getId(), "fdk:images");
Response<ChildAssociationRepresentation> imageAssoc = nodesAPI.createSecondaryChildAssocationCall(amazonEchoResponse.body().getId(), thirdImage, null).execute();
Assert.assertTrue(imageAssoc.isSuccessful());‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

Create fdk:reviews peer association

AssociationBody secondReview = new AssociationBody(secondReviewTextResponse.body().getId(), "fdk:reviews");
Response<AssociationRepresentation> reviewAssoc = nodesAPI.createAssocationCall(amazonEchoResponse.body().getId(), secondReview, null).execute();
Assert.assertTrue(reviewAssoc.isSuccessful());‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

Multi-file review text

ChildAssociationBody reviewChild = new ChildAssociationBody(reviewTextResponse.body().getId(), "cm:contains");
Response<ChildAssociationRepresentation> reviewChildAssoc = nodesAPI.createSecondaryChildAssocationCall(folderCreationResponse.body().getId(), reviewChild, null).execute();
Assert.assertTrue(reviewChildAssoc.isSuccessful());‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

Get images folder children

Response<ResultPaging<NodeRepresentation>> imageFolderChildrenResponse =
nodesAPI.listNodeChildrenCall(folderCreationResponse.body().getId(), null, null, null, null, new IncludeParam(Arrays.asList("association")), null, null, null).execute();
Assert.assertTrue(imageFolderChildrenResponse.isSuccessful());
Assert.assertEquals(imageFolderChildrenResponse.body().getCount(), 4);‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

Get images folder primary children only

Response<ResultPaging<NodeRepresentation>> imageFolderPrimaryChildrenResponse =
nodesAPI.listNodeChildrenCall(folderCreationResponse.body().getId(), null, null, null, "(isPrimary=true)", null, null, null, null).execute();
Assert.assertTrue(imageFolderPrimaryChildrenResponse.isSuccessful());
Assert.assertEquals(imageFolderPrimaryChildrenResponse.body().getCount(), 3);‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

Delete second review text peer association

Response<Void> deleteAssocResponse = nodesAPI.deleteAssocationCall(amazonEchoResponse.body().getId(), secondReviewTextResponse.body().getId(), "fdk:reviews").execute();
Assert.assertTrue(deleteAssocResponse.isSuccessful());‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

Delete third image child association

Response<Void> deleteChildAssocResponse = nodesAPI.deleteSecondaryChildAssocationCall(amazonEchoResponse.body().getId(), thirdImageResponse.body().getId(), "fdk:images").execute();
Assert.assertTrue(deleteChildAssocResponse.isSuccessful());‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

Alfresco Java Client SDK Series