Search in sources :

Example 76 with Comment

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;
}
Also used : CommentDTO(org.wso2.carbon.apimgt.rest.api.store.v1.dto.CommentDTO)

Example 77 with Comment

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;
}
Also used : Comment(org.wso2.carbon.apimgt.api.model.Comment)

Example 78 with 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;
}
Also used : Comment(org.wso2.carbon.apimgt.api.model.Comment) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) PaginationDTO(org.wso2.carbon.apimgt.rest.api.store.v1.dto.PaginationDTO) CommentDTO(org.wso2.carbon.apimgt.rest.api.store.v1.dto.CommentDTO) CommentListDTO(org.wso2.carbon.apimgt.rest.api.store.v1.dto.CommentListDTO) HashMap(java.util.HashMap) Map(java.util.Map)

Example 79 with Comment

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;
}
Also used : Comment(org.wso2.carbon.apimgt.api.model.Comment) JSONObject(org.json.simple.JSONObject) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) ApiTypeWrapper(org.wso2.carbon.apimgt.api.model.ApiTypeWrapper) APIProvider(org.wso2.carbon.apimgt.api.APIProvider)

Example 80 with Comment

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;
}
Also used : Comment(org.wso2.carbon.apimgt.api.model.Comment) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) ApiTypeWrapper(org.wso2.carbon.apimgt.api.model.ApiTypeWrapper) CommentDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.CommentDTO) URISyntaxException(java.net.URISyntaxException) APIProvider(org.wso2.carbon.apimgt.api.APIProvider) 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