Managing Comments for a Folder or File - Alfresco Content Services - 23.4 - 23.4 - Ready - Alfresco - external

Alfresco Content Services

Platform
Alfresco
Product
Alfresco Content Services
Release
23.4
License

To manage comments for a node, use the CommentsApi and the following methods:

Method Description
createComment Create a comment for a node
deleteComment Delete a comment for a node
listComments List comments for a node
updateComment Update a comment for a node

For more information about this ReST API endpoint, see Manage Comments for a Folder or File.

For a description of the common parameters, such as include, see Common Parameters.

import org.alfresco.core.handler.CommentsApi;
import org.alfresco.core.model.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Component
public class ManageCommentsCmd {
    static final Logger LOGGER = LoggerFactory.getLogger(ManageCommentsCmd.class);

    private Integer skipCount = 0;
    private Integer maxItems = 100;
    private List<String> fields = new ArrayList<>();

    // Fetch only the fields we are interested in
    public ManageCommentsCmd() {
        fields.add("content,createdAt");
    }

    @Autowired
    CommentsApi commentsApi;

    public void execute(String nodeId) throws IOException {
        Comment firstComment = createComment(nodeId, "First comment");
        Comment secondComment = createComment(nodeId, "Second comment");

        LOGGER.info("Listing comments: ");
        CommentPagingList comments = commentsApi.listComments(nodeId, skipCount, maxItems, fields).getBody().getList();
        for (CommentEntry commentEntry: comments.getEntries()) {
            LOGGER.info("    {}", commentEntry.getEntry());
        }
    }

    private Comment createComment(String nodeId, String text) {
        CommentBody commentBody = new CommentBody();
        commentBody.setContent(text);
        Comment comment = commentsApi.createComment(nodeId, commentBody, fields).getBody().getEntry();
        LOGGER.info("{}", comment);
        return comment;
    }
}

Note the use of the fields parameter to limit the number of fields returned with each call, which saves bandwidth.

Executing the code gives a log like follows:

% java -jar target/rest-api-0.0.1-SNAPSHOT.jar manage-comments 0492460b-6269-4ca1-9668-0d934d2f3370

2021-05-03 18:54:48.085  INFO 26804 --- [           main] o.a.tutorial.restapi.RestApiApplication  : Started RestApiApplication in 3.075 seconds (JVM running for 3.55)
2021-05-03 18:54:48.087  INFO 26804 --- [           main] o.a.tutorial.restapi.RestApiApplication  : args[0]: manage-comments
2021-05-03 18:54:48.088  INFO 26804 --- [           main] o.a.tutorial.restapi.RestApiApplication  : args[1]: 0492460b-6269-4ca1-9668-0d934d2f3370
2021-05-03 18:54:48.373  INFO 26804 --- [           main] o.a.tutorial.restapi.ManageCommentsCmd   : class Comment {
    id: null
    title: null
    content: First comment
    createdBy: null
    createdAt: 2021-05-03T17:54:48.224Z
    edited: null
    modifiedBy: null
    modifiedAt: null
    canEdit: null
    canDelete: null
}
2021-05-03 18:54:48.492  INFO 26804 --- [           main] o.a.tutorial.restapi.ManageCommentsCmd   : class Comment {
    id: null
    title: null
    content: Second comment
    createdBy: null
    createdAt: 2021-05-03T17:54:48.417Z
    edited: null
    modifiedBy: null
    modifiedAt: null
    canEdit: null
    canDelete: null
}
2021-05-03 18:54:48.492  INFO 26804 --- [           main] o.a.tutorial.restapi.ManageCommentsCmd   : Listing comments: 
2021-05-03 18:54:48.545  INFO 26804 --- [           main] o.a.tutorial.restapi.ManageCommentsCmd   :     class Comment {
    id: null
    title: null
    content: Second comment
    createdBy: null
    createdAt: 2021-05-03T17:54:48.417Z
    edited: null
    modifiedBy: null
    modifiedAt: null
    canEdit: null
    canDelete: null
}
2021-05-03 18:54:48.545  INFO 26804 --- [           main] o.a.tutorial.restapi.ManageCommentsCmd   :     class Comment {
    id: null
    title: null
    content: First comment
    createdBy: null
    createdAt: 2021-05-03T17:54:48.224Z
    edited: null
    modifiedBy: null
    modifiedAt: null
    canEdit: null
    canDelete: null
}