Alfresco Java Client SDK - Usage Part 7 - Collaboration

cancel
Showing results for 
Search instead for 
Did you mean: 

Alfresco Java Client SDK - Usage Part 7 - Collaboration

jm_pascal
Active Member
1 0 1,776

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.

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.

Create Content

// 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);‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

Add a comment

//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 comments

//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 comments

//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

//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);‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

Like node

//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);‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

Retrieve ratings

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 Nodes

//Unlike a node
Response<Void> unlikeResponse = ratingsAPI.deleteRatingCall(emptyNode.getId(), RatingsAPI.LIKES).execute();
Assert.assertEquals(unlikeResponse.isSuccessful(), true);‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

Retrieve Like ratings

//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);
‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

Add Blog Tag

//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

//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

//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

//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

//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);‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

Alfresco Java Client SDK Series