use of org.wso2.carbon.apimgt.core.models.Comment in project carbon-apimgt by wso2.
the class ApisApiServiceImpl method getCommentOfAPI.
@Override
public Response getCommentOfAPI(String commentId, String apiId, String xWSO2Tenant, String ifNoneMatch, Boolean includeCommenterInfo, Integer replyLimit, Integer replyOffset, MessageContext messageContext) throws APIManagementException {
String requestedTenantDomain = RestApiUtil.getRequestedTenantDomain(xWSO2Tenant);
try {
APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
ApiTypeWrapper apiTypeWrapper = apiProvider.getAPIorAPIProductByUUID(apiId, requestedTenantDomain);
Comment comment = apiProvider.getComment(apiTypeWrapper, commentId, replyLimit, replyOffset);
if (comment != null) {
CommentDTO commentDTO;
if (includeCommenterInfo) {
Map<String, Map<String, String>> userClaimsMap = CommentMappingUtil.retrieveUserClaims(comment.getUser(), new HashMap<>());
commentDTO = CommentMappingUtil.fromCommentToDTOWithUserInfo(comment, userClaimsMap);
} else {
commentDTO = CommentMappingUtil.fromCommentToDTO(comment);
}
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 {
RestApiUtil.handleResourceNotFoundError(RestApiConstants.RESOURCE_COMMENTS, String.valueOf(commentId), log);
}
} catch (APIManagementException e) {
if (RestApiUtil.isDueToAuthorizationFailure(e)) {
RestApiUtil.handleAuthorizationFailure(RestApiConstants.RESOURCE_API, apiId, e, log);
} else if (RestApiUtil.isDueToResourceNotFound(e)) {
RestApiUtil.handleResourceNotFoundError(RestApiConstants.RESOURCE_API, apiId, e, log);
} else {
String errorMessage = "Error while retrieving comment for API : " + apiId + "with comment ID " + commentId;
RestApiUtil.handleInternalServerError(errorMessage, e, log);
}
} catch (URISyntaxException e) {
String errorMessage = "Error while retrieving comment content location : " + apiId;
RestApiUtil.handleInternalServerError(errorMessage, e, log);
}
return null;
}
use of org.wso2.carbon.apimgt.core.models.Comment in project carbon-apimgt by wso2.
the class CommentMappingUtil method fromDTOToComment.
/**
* Converts a CommentDTO to a Comment object.
*
* @param body commentDTO body
* @param username username of the consumer
* @param apiId API ID
* @return Comment object
*/
public static Comment fromDTOToComment(CommentDTO body, String username, String apiId) {
Comment comment = new Comment();
comment.setText(body.getContent());
comment.setUser(username);
comment.setApiId(apiId);
return comment;
}
use of org.wso2.carbon.apimgt.core.models.Comment in project carbon-apimgt by wso2.
the class CommentMappingUtil method fromCommentToDTO.
/**
* Converts a Comment object into corresponding REST API CommentDTO object.
*
* @param comment comment object
* @return CommentDTO
*/
public static CommentDTO fromCommentToDTO(Comment comment) throws APIManagementException {
CommentDTO commentDTO = new CommentDTO();
commentDTO.setId(comment.getId());
commentDTO.setContent(comment.getText());
commentDTO.setCreatedBy(comment.getUser());
commentDTO.setCreatedTime(comment.getCreatedTime().toString());
if (comment.getUpdatedTime() != null) {
commentDTO.setUpdatedTime(comment.getUpdatedTime().toString());
}
commentDTO.setCategory(comment.getCategory());
commentDTO.setParentCommentId(comment.getParentCommentID());
if (APIConstants.CommentEntryPoint.DEVPORTAL.toString().equals(comment.getEntryPoint())) {
commentDTO.setEntryPoint(CommentDTO.EntryPointEnum.DEVPORTAL);
} else if (APIConstants.CommentEntryPoint.PUBLISHER.toString().equals(comment.getEntryPoint())) {
commentDTO.setEntryPoint(CommentDTO.EntryPointEnum.PUBLISHER);
}
commentDTO.setReplies(fromCommentListToDTO(comment.getReplies(), false));
return commentDTO;
}
use of org.wso2.carbon.apimgt.core.models.Comment in project carbon-apimgt by wso2.
the class ApiMgtDAO method addComment.
/**
****************************
* Adds a comment for an API
*
* @param uuid API uuid
* @param comment Commented Text
* @param user User who did the comment
* @return Comment ID
*/
public String addComment(String uuid, Comment comment, String user) throws APIManagementException {
String commentId = null;
try (Connection connection = APIMgtDBUtil.getConnection()) {
int id = -1;
connection.setAutoCommit(false);
id = getAPIID(uuid, connection);
if (id == -1) {
String msg = "Could not load API record for API with UUID: " + uuid;
throw new APIManagementException(msg);
}
String addCommentQuery = SQLConstants.ADD_COMMENT_SQL;
commentId = UUID.randomUUID().toString();
try (PreparedStatement insertPrepStmt = connection.prepareStatement(addCommentQuery)) {
insertPrepStmt.setString(1, commentId);
insertPrepStmt.setString(2, comment.getText());
insertPrepStmt.setString(3, user);
insertPrepStmt.setTimestamp(4, new Timestamp(System.currentTimeMillis()), Calendar.getInstance());
insertPrepStmt.setInt(5, id);
insertPrepStmt.setString(6, comment.getParentCommentID());
insertPrepStmt.setString(7, comment.getEntryPoint());
insertPrepStmt.setString(8, comment.getCategory());
insertPrepStmt.executeUpdate();
connection.commit();
}
} catch (SQLException e) {
handleException("Failed to add comment data, for API with UUID " + uuid, e);
}
return commentId;
}
use of org.wso2.carbon.apimgt.core.models.Comment in project carbon-apimgt by wso2.
the class ApiMgtDAO method deleteComment.
private boolean deleteComment(String uuid, String commentId, Connection connection) throws APIManagementException {
int id = -1;
String deleteCommentQuery = SQLConstants.DELETE_COMMENT_SQL;
String getCommentIDsOfReplies = SQLConstants.GET_IDS_OF_REPLIES_SQL;
ResultSet resultSet = null;
try {
id = getAPIID(uuid, connection);
if (id == -1) {
String msg = "Could not load API record for API with UUID: " + uuid;
throw new APIManagementException(msg, ExceptionCodes.from(ExceptionCodes.API_NOT_FOUND, uuid));
}
connection.setAutoCommit(false);
try (PreparedStatement prepStmtGetReplies = connection.prepareStatement(getCommentIDsOfReplies)) {
prepStmtGetReplies.setString(1, uuid);
prepStmtGetReplies.setString(2, commentId);
resultSet = prepStmtGetReplies.executeQuery();
while (resultSet.next()) {
deleteComment(uuid, resultSet.getString("COMMENT_ID"), connection);
}
try (PreparedStatement prepStmt = connection.prepareStatement(deleteCommentQuery)) {
prepStmt.setInt(1, id);
prepStmt.setString(2, commentId);
prepStmt.execute();
connection.commit();
return true;
}
}
} catch (SQLException e) {
handleException("Error while deleting comment " + commentId + " from the database", e);
}
return false;
}
Aggregations