Hi all,

Since we cannot directly retrieve comments with nested replies using SQL, I
retrieved the comments as usual. After, that I used a Map to map the
comments using the commentId as the key. So I needed just only one loop for
this task.
Following is the code segment which I modified in *APIDAOImpl.java* file.
(Also, I had to change the definition of the Comment resource in
*publisher-api.yaml* and *store-api.yaml* and modified *Comment.java* in
models and *CommentMappingUtil.java* files in both Publisher and Store
components)

@Override

public List<Comment> getCommentsForApi(String apiId) throws APIMgtDAOException {
    List<Comment> commentList;
    Map commentListMap = new HashMap();
    final String getCommentsQuery = "SELECT UUID, COMMENT_TEXT,
USER_IDENTIFIER, API_ID, CATEGORY,"
            + " PARENT_COMMENT_ID, ENTRY_POINT, CREATED_BY,
CREATED_TIME, UPDATED_BY, LAST_UPDATED_TIME "
            + "FROM AM_API_COMMENTS WHERE API_ID = ?";
    try (Connection connection = DAOUtil.getConnection();
         PreparedStatement statement =
connection.prepareStatement(getCommentsQuery)) {
        try {
            statement.setString(1, apiId);
            statement.execute();

            try (ResultSet rs = statement.getResultSet()) {
                while (rs.next()) {
                    Comment comment = constructCommentFromResultSet(rs);
                    String commentId = comment.getUuid();
                    String parentCommentId = comment.getParentCommentId();
                    if (commentListMap.containsKey(parentCommentId)) {
                        Comment existingComment = (Comment)
commentListMap.get(parentCommentId);
                        existingComment.getReplies().add(comment);
                        commentListMap.put(parentCommentId, existingComment);
                    } else {
                        commentListMap.put(commentId, comment);
                    }
                }
                commentList = new ArrayList<Comment>(commentListMap.values());
            }
        } catch (SQLException | IOException e) {
            connection.rollback();
            String errorMessage = "getting all comments for API " + apiId;
            throw new APIMgtDAOException(DAOUtil.DAO_ERROR_PREFIX +
errorMessage, e);
        }
    } catch (SQLException e) {
        String errorMessage = "getting all comments for API " + apiId;
        throw new APIMgtDAOException(DAOUtil.DAO_ERROR_PREFIX +
errorMessage, e);
    }
    return commentList;
}

Thanks,
Wasura


On Mon, Oct 29, 2018 at 8:31 AM Wasura Wattearachchi <[email protected]>
wrote:

> Hi all,
>
> Will move the logic to the backend as Bhathiya has suggested, and will get
> the response similar to the dummy comments json in the UI.
>
> Thanks,
> Wasura
>
> On Mon, Oct 29, 2018 at 6:29 AM Chanaka Jayasena <[email protected]> wrote:
>
>> The dummy comments json in the UI is matching to what Bhathiya has
>> suggested. It allows to load the comment react component recessively.
>>
>> thanks,
>> Chanaka
>>
>> On Sun, Oct 28, 2018 at 10:11 PM Bhathiya Jayasekara <[email protected]>
>> wrote:
>>
>>> Hi Wasura,
>>>
>>> IMO, the response for the GET should be intuitive.
>>>
>>> For example:
>>>
>>> [
>>>    {
>>>       "id":"12434",
>>>       "comment":"this is a parenth comment",
>>>       "timestamp":"xxxxx01",
>>>       "replies":[
>>>          {
>>>             "id":"12435",
>>>             "comment":"this is a reply",
>>>             "timestamp":"xxxxx02"
>>>          },
>>>          {
>>>             "id":"12436",
>>>             "comment":"this is another reply",
>>>             "timestamp":"xxxxx03"
>>>          }
>>>       ]
>>>    },
>>>    {
>>>       "id":"12438",
>>>       "comment":"this is another parent comment",
>>>       "timestamp":"xxxxx02"
>>>    }
>>> ]
>>>
>>> So you will have to write the logic to compose this response at the
>>> backend.
>>>
>>> On Sat, Oct 27, 2018 at 12:05 PM Wasura Wattearachchi <[email protected]>
>>> wrote:
>>>
>>>> Hi all,
>>>>
>>>> Sorry for the inconvenience. The methods that I have already
>>>> implemented are not visible in the above mail. Check below for the methods.
>>>>
>>>>
>>>> Thanks!
>>>>
>>>> On Sat, Oct 27, 2018 at 12:00 PM Wasura Wattearachchi <[email protected]>
>>>> wrote:
>>>>
>>>>>
>>>>> If I use above GET method, I will get an array of comments as the
>>>>> response and each array element will consist a comment (or a reply) with
>>>>> the fields (column names as attributes) from the database table.
>>>>> So, to list down the comments I used a loop and to list down the
>>>>> replies by checking whether what are the comments which have the
>>>>> parentCommentId of the parent comment.
>>>>>
>>>>> *Pseudocode*
>>>>>
>>>>>> commentList = call GET /apis/{apiId}/comments
>>>>>> For each comment in commentList{
>>>>>> Display comment;
>>>>>> If (comment[parentCommentId] != null){
>>>>>>           For each commentReply in commentList{
>>>>>>               If (comment[parentCommentId] ==
>>>>>> commentReply[parentCommentId]){
>>>>>>                    Display commentReply;
>>>>>>                       }
>>>>>>                   }
>>>>>>         }
>>>>>> }
>>>>>
>>>>>
>>>>> IMHO, I think above approach of using 2 nested loops is not efficient.
>>>>> So, how about we use another GET method to get the comments which have a
>>>>> particular parentCommentId and use it instead of the second loop? (check
>>>>> the method in red colour)
>>>>>
>>>>> *Pseudocode*
>>>>>
>>>>>> commentList = call GET /apis/{apiId}/comments
>>>>>> For each comment in commentList{
>>>>>> Display comment;
>>>>>> If (comment[parentCommentId] != null){
>>>>>> replyList = call GET /apis/{apiId}/comments/{parentCommentId}
>>>>>>         For each commentReply in replyList{
>>>>>>        Display commentReply;
>>>>>>                 }
>>>>>>         }
>>>>>> }
>>>>>
>>>>>
>>>>> But the drawback in here is that there will be two database calls when
>>>>> listing a single comment with replies.
>>>>>
>>>>
>>> This also has 2 network calls which is costlier than the 1st case.
>>>
>>> Anyway, this will be easiler when you move the logic to the backend as I
>>> suggested above.
>>>
>>> Thanks,
>>> Bhathiya
>>>
>>>
>>>> What do you think about this?
>>>>>
>>>>> Thank you!
>>>>>
>>>>> --
>>>>> Wasura Wattearachchi | Software Engineer Intern | WSO2 Inc.
>>>>> (m) +94775396038 | (e) [email protected]
>>>>> <http://wso2.com/signature>
>>>>>
>>>>
>>>>
>>>> --
>>>> Wasura Wattearachchi | Software Engineer Intern | WSO2 Inc.
>>>> (m) +94775396038 | (e) [email protected]
>>>> <http://wso2.com/signature>
>>>>
>>>
>>>
>>> --
>>> *Bhathiya Jayasekara*
>>> *Technical Lead,*
>>> *WSO2 inc., http://wso2.com <http://wso2.com>*
>>>
>>> *Phone: +94715478185*
>>> *LinkedIn: http://www.linkedin.com/in/bhathiyaj
>>> <http://www.linkedin.com/in/bhathiyaj>*
>>> *Twitter: https://twitter.com/bhathiyax <https://twitter.com/bhathiyax>*
>>> *Blog: http://movingaheadblog.blogspot.com
>>> <http://movingaheadblog.blogspot.com/>*
>>>
>>
>>
>> --
>> Chanaka Jayasena
>> Associate Tech Lead,
>> email: [email protected]; cell: +94 77 4464006
>> blog: http://chanaka3d.blogspot.com
>>
>
>
> --
> Wasura Wattearachchi | Software Engineer Intern | WSO2 Inc.
> (m) +94775396038 | (e) [email protected]
> <http://wso2.com/signature>
>


-- 
Wasura Wattearachchi | Software Engineer Intern | WSO2 Inc.
(m) +94775396038 | (e) [email protected]
<http://wso2.com/signature>
_______________________________________________
Dev mailing list
[email protected]
http://wso2.org/cgi-bin/mailman/listinfo/dev

Reply via email to