use of org.wso2.carbon.apimgt.api.model.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.api.model.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.api.model.Comment in project carbon-apimgt by wso2.
the class CommentMappingUtil method fromCommentListToDTO.
/**
* Wraps a List of Comments to a CommentListDTO
*
* @param commentList list of comments
* @return CommentListDTO
*/
public static CommentListDTO fromCommentListToDTO(CommentList commentList, boolean includeCommenterInfo) {
CommentListDTO commentListDTO = new CommentListDTO();
List<CommentDTO> listOfCommentDTOs = new ArrayList<>();
commentListDTO.setCount(commentList.getCount());
PaginationDTO paginationDTO = new PaginationDTO();
paginationDTO.setLimit(commentList.getPagination().getLimit());
paginationDTO.setOffset(commentList.getPagination().getOffset());
paginationDTO.setTotal(commentList.getPagination().getTotal());
paginationDTO.setNext(commentList.getPagination().getNext());
paginationDTO.setPrevious(commentList.getPagination().getPrevious());
commentListDTO.setPagination(paginationDTO);
Map<String, Map<String, String>> userClaimsMap = new HashMap<>();
for (Comment comment : commentList.getList()) {
try {
if (includeCommenterInfo) {
userClaimsMap = retrieveUserClaims(comment.getUser(), userClaimsMap);
listOfCommentDTOs.add(fromCommentToDTOWithUserInfo(comment, userClaimsMap));
} else {
listOfCommentDTOs.add(fromCommentToDTO(comment));
}
} catch (APIManagementException e) {
log.error("Error while creating comments list", e);
}
}
commentListDTO.setList(listOfCommentDTOs);
return commentListDTO;
}
use of org.wso2.carbon.apimgt.api.model.Comment in project carbon-apimgt by wso2.
the class ApisApiServiceImpl method deleteComment.
@Override
public Response deleteComment(String commentId, String apiId, String ifMatch, MessageContext messageContext) throws APIManagementException {
String requestedTenantDomain = RestApiCommonUtil.getLoggedInUserTenantDomain();
String username = RestApiCommonUtil.getLoggedInUsername();
try {
APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
ApiTypeWrapper apiTypeWrapper = apiProvider.getAPIorAPIProductByUUID(apiId, requestedTenantDomain);
Comment comment = apiProvider.getComment(apiTypeWrapper, commentId, 0, 0);
if (comment != null) {
String[] tokenScopes = (String[]) PhaseInterceptorChain.getCurrentMessage().getExchange().get(RestApiConstants.USER_REST_API_SCOPES);
if (Arrays.asList(tokenScopes).contains(RestApiConstants.ADMIN_SCOPE) || comment.getUser().equals(username)) {
if (apiProvider.deleteComment(apiTypeWrapper, commentId)) {
JSONObject obj = new JSONObject();
obj.put("id", commentId);
obj.put("message", "The comment has been deleted");
return Response.ok(obj).type(MediaType.APPLICATION_JSON).build();
} else {
return Response.status(405, "Method Not Allowed").type(MediaType.APPLICATION_JSON).build();
}
} else {
return Response.status(403, "Forbidden").type(MediaType.APPLICATION_JSON).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 deleting comment " + commentId + "for API " + apiId;
RestApiUtil.handleInternalServerError(errorMessage, e, log);
}
}
return null;
}
use of org.wso2.carbon.apimgt.api.model.Comment in project carbon-apimgt by wso2.
the class ApisApiServiceImpl method addCommentToAPI.
@Override
public Response addCommentToAPI(String apiId, PostRequestBodyDTO postRequestBodyDTO, String replyTo, MessageContext messageContext) throws APIManagementException {
String username = RestApiCommonUtil.getLoggedInUsername();
String organization = RestApiUtil.getValidatedOrganization(messageContext);
try {
APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
ApiTypeWrapper apiTypeWrapper = apiProvider.getAPIorAPIProductByUUID(apiId, organization);
Comment comment = new Comment();
comment.setText(postRequestBodyDTO.getContent());
comment.setCategory(postRequestBodyDTO.getCategory());
comment.setParentCommentID(replyTo);
comment.setEntryPoint("PUBLISHER");
comment.setUser(username);
comment.setApiId(apiId);
String createdCommentId = apiProvider.addComment(apiId, comment, username);
Comment createdComment = apiProvider.getComment(apiTypeWrapper, createdCommentId, 0, 0);
CommentDTO commentDTO = CommentMappingUtil.fromCommentToDTO(createdComment);
String uriString = RestApiConstants.RESOURCE_PATH_APIS + "/" + apiId + RestApiConstants.RESOURCE_PATH_COMMENTS + "/" + createdCommentId;
URI uri = new URI(uriString);
return Response.created(uri).entity(commentDTO).build();
} catch (APIManagementException e) {
if (RestApiUtil.isDueToResourceNotFound(e) || RestApiUtil.isDueToAuthorizationFailure(e)) {
RestApiUtil.handleResourceNotFoundError(RestApiConstants.RESOURCE_API, apiId, e, log);
} else {
RestApiUtil.handleInternalServerError("Failed to add comment to the API " + apiId, e, log);
}
} catch (URISyntaxException e) {
throw new APIManagementException("Error while retrieving comment content location for API " + apiId);
}
return null;
}
Aggregations