Search in sources :

Example 21 with CommentDTO

use of org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.CommentDTO 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 22 with CommentDTO

use of org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.CommentDTO 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 23 with CommentDTO

use of org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.CommentDTO 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)

Example 24 with CommentDTO

use of org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.CommentDTO 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;
}
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) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) URI(java.net.URI)

Example 25 with CommentDTO

use of org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.CommentDTO 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)

Aggregations

URI (java.net.URI)11 URISyntaxException (java.net.URISyntaxException)11 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)10 ApiTypeWrapper (org.wso2.carbon.apimgt.api.model.ApiTypeWrapper)10 Comment (org.wso2.carbon.apimgt.api.model.Comment)10 HashMap (java.util.HashMap)9 Map (java.util.Map)9 CommentDTO (org.wso2.carbon.apimgt.rest.api.store.dto.CommentDTO)9 Comment (org.wso2.carbon.apimgt.core.models.Comment)8 CommentDTO (org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.CommentDTO)6 APIConsumer (org.wso2.carbon.apimgt.api.APIConsumer)5 APIProvider (org.wso2.carbon.apimgt.api.APIProvider)5 APIStore (org.wso2.carbon.apimgt.core.api.APIStore)5 Test (org.junit.Test)4 CommentList (org.wso2.carbon.apimgt.api.model.CommentList)4 CommentDTO (com.odysseusinc.arachne.portal.api.v1.dto.CommentDTO)3 ArrayList (java.util.ArrayList)3 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)3 ErrorDTO (org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO)3 CommentListDTO (org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.CommentListDTO)3