Search in sources :

Example 86 with Comment

use of org.wso2.carbon.apimgt.core.models.Comment in project carbon-apimgt by wso2.

the class ApiMgtDAO method deleteComment.

/**
 * Delete a comment
 *
 * @param uuid API uuid
 * @param commentId  Comment ID
 * @throws APIManagementException
 */
public void deleteComment(String uuid, String commentId) throws APIManagementException {
    try (Connection connection = APIMgtDBUtil.getConnection()) {
        int id = -1;
        id = getAPIID(uuid, connection);
        if (id == -1) {
            String msg = "Could not load API record for API with UUID: " + uuid;
            throw new APIManagementException(msg);
        }
        String deleteCommentQuery = SQLConstants.DELETE_COMMENT_SQL;
        connection.setAutoCommit(false);
        try (PreparedStatement prepStmt = connection.prepareStatement(deleteCommentQuery)) {
            prepStmt.setInt(1, id);
            prepStmt.setString(2, commentId);
            prepStmt.execute();
            connection.commit();
        }
    } catch (SQLException e) {
        handleException("Error while deleting comment " + commentId + " from the database", e);
    }
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) SQLException(java.sql.SQLException) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement)

Example 87 with Comment

use of org.wso2.carbon.apimgt.core.models.Comment in project carbon-apimgt by wso2.

the class ApiMgtDAO method getComments.

/**
 **************************************
 * Returns all the Comments on an API
 *
 * @param uuid API UUID
 * @param parentCommentID Parent Comment ID
 * @param limit           The limit
 * @param offset          The offset
 * @param connection Database connection
 * @return Comment Array
 * @throws APIManagementException
 */
private CommentList getComments(String uuid, String parentCommentID, Integer limit, Integer offset, Connection connection) throws APIManagementException {
    List<Comment> list = new ArrayList<Comment>();
    CommentList commentList = new CommentList();
    Pagination pagination = new Pagination();
    commentList.setPagination(pagination);
    int total = 0;
    String sqlQuery;
    String sqlQueryForCount;
    if (parentCommentID == null) {
        sqlQueryForCount = SQLConstants.GET_ROOT_COMMENTS_COUNT_SQL;
    } else {
        sqlQueryForCount = SQLConstants.GET_REPLIES_COUNT_SQL;
    }
    try (PreparedStatement prepStmtForCount = connection.prepareStatement(sqlQueryForCount)) {
        prepStmtForCount.setString(1, uuid);
        if (parentCommentID != null) {
            prepStmtForCount.setString(2, parentCommentID);
        }
        try (ResultSet resultSetForCount = prepStmtForCount.executeQuery()) {
            while (resultSetForCount.next()) {
                total = resultSetForCount.getInt("COMMENT_COUNT");
            }
            if (total > 0 && limit > 0) {
                if (parentCommentID == null) {
                    sqlQuery = SQLConstantManagerFactory.getSQlString("GET_ROOT_COMMENTS_SQL");
                } else {
                    sqlQuery = SQLConstantManagerFactory.getSQlString("GET_REPLIES_SQL");
                }
                try (PreparedStatement prepStmt = connection.prepareStatement(sqlQuery)) {
                    prepStmt.setString(1, uuid);
                    if (parentCommentID != null) {
                        prepStmt.setString(2, parentCommentID);
                        prepStmt.setInt(3, offset);
                        prepStmt.setInt(4, limit);
                    } else {
                        prepStmt.setInt(2, offset);
                        prepStmt.setInt(3, limit);
                    }
                    try (ResultSet resultSet = prepStmt.executeQuery()) {
                        while (resultSet.next()) {
                            Comment comment = new Comment();
                            comment.setId(resultSet.getString("COMMENT_ID"));
                            comment.setText(resultSet.getString("COMMENT_TEXT"));
                            comment.setUser(resultSet.getString("CREATED_BY"));
                            comment.setCreatedTime(resultSet.getTimestamp("CREATED_TIME"));
                            comment.setUpdatedTime(resultSet.getTimestamp("UPDATED_TIME"));
                            comment.setApiId(resultSet.getString("API_ID"));
                            comment.setParentCommentID(resultSet.getString("PARENT_COMMENT_ID"));
                            comment.setEntryPoint(resultSet.getString("ENTRY_POINT"));
                            comment.setCategory(resultSet.getString("CATEGORY"));
                            if (parentCommentID == null) {
                                comment.setReplies(getComments(uuid, resultSet.getString("COMMENT_ID"), APIConstants.REPLYLIMIT, APIConstants.REPLYOFFSET, connection));
                            } else {
                                CommentList emptyCommentList = new CommentList();
                                Pagination emptyPagination = new Pagination();
                                emptyCommentList.setPagination(emptyPagination);
                                emptyCommentList.getPagination().setTotal(0);
                                emptyCommentList.setCount(0);
                                comment.setReplies(emptyCommentList);
                            }
                            list.add(comment);
                        }
                    }
                }
            } else {
                commentList.getPagination().setTotal(total);
                commentList.setCount(total);
                return commentList;
            }
        }
    } catch (SQLException e) {
        handleException("Failed to retrieve comments for API with UUID " + uuid, e);
    }
    pagination.setLimit(limit);
    pagination.setOffset(offset);
    commentList.getPagination().setTotal(total);
    commentList.setList(list);
    commentList.setCount(list.size());
    return commentList;
}
Also used : Comment(org.wso2.carbon.apimgt.api.model.Comment) Pagination(org.wso2.carbon.apimgt.api.model.Pagination) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) ResultSet(java.sql.ResultSet) CommentList(org.wso2.carbon.apimgt.api.model.CommentList) PreparedStatement(java.sql.PreparedStatement)

Example 88 with Comment

use of org.wso2.carbon.apimgt.core.models.Comment in project carbon-apimgt by wso2.

the class ApisApiServiceImpl method editCommentOfAPI.

@Override
public Response editCommentOfAPI(String commentId, String apiId, PatchRequestBodyDTO patchRequestBodyDTO, MessageContext messageContext) throws APIManagementException {
    String username = RestApiCommonUtil.getLoggedInUsername();
    String organization = RestApiUtil.getValidatedOrganization(messageContext);
    try {
        APIConsumer apiConsumer = RestApiCommonUtil.getLoggedInUserConsumer();
        ApiTypeWrapper apiTypeWrapper = apiConsumer.getAPIorAPIProductByUUID(apiId, organization);
        Comment comment = apiConsumer.getComment(apiTypeWrapper, commentId, 0, 0);
        if (comment != null) {
            if (comment.getUser().equals(username)) {
                boolean commentEdited = false;
                if (patchRequestBodyDTO.getCategory() != null && !(patchRequestBodyDTO.getCategory().equals(comment.getCategory()))) {
                    comment.setCategory(patchRequestBodyDTO.getCategory());
                    commentEdited = true;
                }
                if (patchRequestBodyDTO.getContent() != null && !(patchRequestBodyDTO.getContent().equals(comment.getText()))) {
                    comment.setText(patchRequestBodyDTO.getContent());
                    commentEdited = true;
                }
                if (commentEdited) {
                    if (apiConsumer.editComment(apiTypeWrapper, commentId, comment)) {
                        Comment editedComment = apiConsumer.getComment(apiTypeWrapper, commentId, 0, 0);
                        CommentDTO commentDTO = CommentMappingUtil.fromCommentToDTO(editedComment);
                        String uriString = RestApiConstants.RESOURCE_PATH_APIS + "/" + apiId + RestApiConstants.RESOURCE_PATH_COMMENTS + "/" + commentId;
                        URI uri = new URI(uriString);
                        return Response.ok(uri).entity(commentDTO).build();
                    }
                } else {
                    return Response.ok().build();
                }
            } else {
                RestApiUtil.handleAuthorizationFailure(RestApiConstants.RESOURCE_COMMENTS, String.valueOf(commentId), log);
            }
        } else {
            RestApiUtil.handleResourceNotFoundError(RestApiConstants.RESOURCE_COMMENTS, String.valueOf(commentId), log);
        }
    } catch (URISyntaxException e) {
        String errorMessage = "Error while retrieving comment content location for API " + apiId;
        RestApiUtil.handleInternalServerError(errorMessage, e, log);
    }
    return null;
}
Also used : Comment(org.wso2.carbon.apimgt.api.model.Comment) ApiTypeWrapper(org.wso2.carbon.apimgt.api.model.ApiTypeWrapper) APIConsumer(org.wso2.carbon.apimgt.api.APIConsumer) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI)

Aggregations

Comment (org.wso2.carbon.apimgt.core.models.Comment)36 APIStore (org.wso2.carbon.apimgt.core.api.APIStore)28 ApiDAO (org.wso2.carbon.apimgt.core.dao.ApiDAO)23 Test (org.testng.annotations.Test)22 SQLException (java.sql.SQLException)18 BeforeTest (org.testng.annotations.BeforeTest)18 API (org.wso2.carbon.apimgt.core.models.API)17 CompositeAPI (org.wso2.carbon.apimgt.core.models.CompositeAPI)17 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)16 Comment (org.wso2.carbon.apimgt.api.model.Comment)16 APIMgtDAOException (org.wso2.carbon.apimgt.core.exception.APIMgtDAOException)15 PreparedStatement (java.sql.PreparedStatement)13 Connection (java.sql.Connection)12 HashMap (java.util.HashMap)12 Map (java.util.Map)12 ArrayList (java.util.ArrayList)10 CommentDTO (org.wso2.carbon.apimgt.rest.api.store.dto.CommentDTO)9 URI (java.net.URI)8 URISyntaxException (java.net.URISyntaxException)8 Test (org.junit.Test)8