We will now follow Gavin Cornwell https://community.alfresco.com/community/ecm/blog/2016/12/14/v1-rest-api-part-7-collaboration 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 Content
NodeBodyCreate emptyFileBody = new NodeBodyCreate("collaboration.txt", ContentModel.TYPE_CONTENT);
Response<NodeRepresentation> emptyNodeResponse = nodesAPI.createNodeCall(NodesAPI.FOLDER_MY, emptyFileBody).execute();
NodeRepresentation emptyNode = emptyNodeResponse.body();
Assert.assertEquals(emptyNode.getContent().getSizeInBytes(), 0);
//Setting up Comments API
CommentsAPI commentsAPI = client.getCommentsAPI();
//Add a comment
String commentValue = "This is my comment";
CommentBody commentBody = new CommentBody(commentValue);
Response<CommentRepresentation> commentResponse = commentsAPI.createCommentCall(emptyNode.getId(), commentBody).execute();
CommentRepresentation commentRepresentation = commentResponse.body();
Assert.assertEquals(commentRepresentation.getContent(), commentValue);
Assert.assertEquals(commentRepresentation.getCreatedBy().getId(), "admin");
//Retrieve Comment
Response<ResultPaging<CommentRepresentation>> commentsListResponse = commentsAPI.listCommentsCall(emptyNode.getId()).execute();
ResultPaging<CommentRepresentation> commentListing = commentsListResponse.body();
Assert.assertEquals(commentListing.getCount(), 1);
Assert.assertEquals(commentListing.getList().get(0), commentRepresentation);
Assert.assertEquals(commentListing.getList().get(0).getCanEdit(), Boolean.TRUE);
Assert.assertEquals(commentListing.getList().get(0).getCanDelete(), Boolean.TRUE);
//Update Comment
String updatedCommentValue = "Updated comment";
CommentBody updatedCommentBody = new CommentBody(updatedCommentValue);
Response<CommentRepresentation> updatedCommentResponse = commentsAPI.updateCommentCall(emptyNode.getId(), commentRepresentation.getId(), updatedCommentBody).execute();
CommentRepresentation updatedCommentRepresentation = updatedCommentResponse.body();
Assert.assertEquals(updatedCommentRepresentation.getContent(), updatedCommentValue);
//Delete Comment
Response<Void> deleteResponse = commentsAPI.deleteCommentCall(emptyNode.getId(), commentRepresentation.getId()).execute();
Assert.assertEquals(deleteResponse.isSuccessful(), true);
Assert.assertEquals(commentsAPI.listCommentsCall(emptyNode.getId()).execute().body().getCount(), 0);
//Setting up Ratings API
RatingsAPI ratingsAPI = client.getRatingsAPI();
//Like a node
RatingBody likeBody = new RatingBody(RatingsAPI.LIKES, true);
Response<RatingRepresentation> likeResponse = ratingsAPI.rateNodeCall(emptyNode.getId(), likeBody).execute();
Assert.assertEquals(likeResponse.body().getId(), RatingsAPI.LIKES);
Assert.assertEquals(likeResponse.body().getMyRating(), Boolean.TRUE);
Assert.assertEquals(likeResponse.body().getAggregate().getNumberOfRatings(), (Integer) 1);
Response<ResultPaging<RatingRepresentation>> ratingListingResponse = ratingsAPI.listRatingsCall(emptyNode.getId()).execute();
ResultPaging<RatingRepresentation> ratingListing = ratingListingResponse.body();
Assert.assertEquals(ratingListing.getCount(), 2);
Assert.assertEquals(ratingListing.getList().get(0).getId(),RatingsAPI.FIVE_STAR);
Assert.assertEquals(ratingListing.getList().get(1).getId(),RatingsAPI.LIKES);
//Unlike a node
Response<Void> unlikeResponse = ratingsAPI.deleteRatingCall(emptyNode.getId(), RatingsAPI.LIKES).execute();
Assert.assertEquals(unlikeResponse.isSuccessful(), true);
//Retrieve Like Rating
Response<RatingRepresentation> ratingRepresentationResponse = ratingsAPI.getRatingCall(emptyNode.getId(), RatingsAPI.LIKES).execute();
Assert.assertNull(ratingRepresentationResponse.body().getMyRating());
Assert.assertEquals(ratingRepresentationResponse.body().getAggregate().getNumberOfRatings(), (Integer) 0);
//Setting up Tagging API
TagsAPI tagsApi = client.getTagsAPI();
//Add Blog Tag
String blog = "blog";
TagBody tagBody = new TagBody(blog);
Response<TagRepresentation> tagRepresentationResponse = tagsApi.createTagForNodeCall(emptyNode.getId(), tagBody).execute();
TagRepresentation tagBlog = tagRepresentationResponse.body();
Assert.assertEquals(tagBlog.getTag(), blog);
//Add Post Tag
String post = "post";
TagBody tag2Body = new TagBody(post);
Response<TagRepresentation> tag2RepresentationResponse = tagsApi.createTagForNodeCall(emptyNode.getId(), tag2Body).execute();
Assert.assertEquals(tag2RepresentationResponse.body().getTag(), post);
//Retrieve node Tags
Response<ResultPaging<TagRepresentation>> tagListResponse = tagsApi.listTagsForNodeCall(emptyNode.getId()).execute();
ResultPaging<TagRepresentation> tagList = tagListResponse.body();
Assert.assertEquals(tagList.getCount(), 2);
Assert.assertEquals(tagList.getList().get(0).getTag(), blog);
Assert.assertEquals(tagList.getList().get(1).getTag(), post);
//Retrieve Repository Tags
Response<ResultPaging<TagRepresentation>> tagRepoListResponse = tagsApi.listTagsCall().execute();
ResultPaging<TagRepresentation> tagRepoList = tagRepoListResponse.body();
Assert.assertEquals(tagRepoList.getCount(), 2);
Assert.assertEquals(tagRepoList.getList().get(0).getTag(), blog);
Assert.assertEquals(tagRepoList.getList().get(1).getTag(), post);
//Delete Tag from Node
Response<Void> deleteTagResponse = tagsApi.deleteTagFromNodeCall(emptyNode.getId(), tagBlog.getId()).execute();
Assert.assertEquals(deleteTagResponse.isSuccessful(), true);
Assert.assertEquals(tagsApi.listTagsForNodeCall(emptyNode.getId()).execute().body().getCount(), 1);
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.