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);
}
}
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;
}
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;
}
Aggregations