Search in sources :

Example 16 with CommentDTO

use of org.wso2.carbon.apimgt.rest.api.store.dto.CommentDTO in project carbon-apimgt by wso2.

the class CommentMappingUtilTestCase method testFromCommentToDTO.

@Test
public void testFromCommentToDTO() {
    String commentUUID = UUID.randomUUID().toString();
    Comment comment = new Comment();
    comment.setUuid(commentUUID);
    comment.setCommentedUser("commentedUser");
    comment.setCommentText("this is a comment");
    comment.setCreatedUser("createdUser");
    comment.setUpdatedUser("updatedUser");
    comment.setCreatedTime(LocalDateTime.now().minusHours(1));
    comment.setUpdatedTime(LocalDateTime.now());
    CommentDTO commentDTO = commentMappingUtil.fromCommentToDTO(comment);
    Assert.assertEquals(commentDTO.getCommentId().toString(), commentUUID);
}
Also used : Comment(org.wso2.carbon.apimgt.core.models.Comment) CommentDTO(org.wso2.carbon.apimgt.rest.api.store.dto.CommentDTO) Test(org.junit.Test)

Example 17 with CommentDTO

use of org.wso2.carbon.apimgt.rest.api.store.dto.CommentDTO in project carbon-apimgt by wso2.

the class CommentMappingUtilTestCase method testFromDTOToComment.

@Test
public void testFromDTOToComment() {
    String apiID = UUID.randomUUID().toString();
    String username = "user";
    CommentDTO commentDTO = new CommentDTO();
    commentDTO.setApiId(apiID);
    commentDTO.setCommentText("comment text");
    commentDTO.setCreatedBy("creater");
    commentDTO.setLastUpdatedBy("updater");
    Comment comment = commentMappingUtil.fromDTOToComment(commentDTO, username);
    Assert.assertEquals(comment.getApiId(), apiID);
    Assert.assertEquals(comment.getCommentText(), "comment text");
}
Also used : Comment(org.wso2.carbon.apimgt.core.models.Comment) CommentDTO(org.wso2.carbon.apimgt.rest.api.store.dto.CommentDTO) Test(org.junit.Test)

Example 18 with CommentDTO

use of org.wso2.carbon.apimgt.rest.api.store.dto.CommentDTO in project carbon-apimgt by wso2.

the class ApisApiServiceImpl method apisApiIdCommentsPost.

/**
 * Update a comment
 *
 * @param apiId   API ID
 * @param body    comment body
 * @param request msf4j request object
 * @return comment update response
 * @throws NotFoundException if this method is not defined in ApisApiServiceImpl
 */
@Override
public Response apisApiIdCommentsPost(String apiId, CommentDTO body, Request request) throws NotFoundException {
    String username = RestApiUtil.getLoggedInUsername(request);
    try {
        APIStore apiStore = RestApiUtil.getConsumer(username);
        Comment comment = CommentMappingUtil.fromDTOToComment(body, username);
        String createdCommentId = apiStore.addComment(comment, apiId);
        Comment createdComment = apiStore.getCommentByUUID(createdCommentId, apiId);
        CommentDTO createdCommentDTO = CommentMappingUtil.fromCommentToDTO(createdComment);
        URI location = new URI(RestApiConstants.RESOURCE_PATH_APIS + "/" + apiId + RestApiConstants.SUBRESOURCE_PATH_COMMENTS + "/" + createdCommentId);
        String fingerprint = getEtag(comment.getUuid(), request.getProperty("LOGGED_IN_USER").toString());
        return Response.status(Response.Status.CREATED).header(RestApiConstants.LOCATION_HEADER, location).header(HttpHeaders.ETAG, "\"" + fingerprint + "\"").entity(createdCommentDTO).build();
    } catch (APIManagementException e) {
        String errorMessage = "Error while adding comment to api : " + apiId;
        Map<String, String> paramList = new HashMap<String, String>();
        paramList.put(APIMgtConstants.ExceptionsConstants.API_ID, body.getApiId());
        ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
        log.error(errorMessage, e);
        return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
    } catch (URISyntaxException e) {
        String errorMessage = "Error while adding location header in response for comment";
        ErrorHandler errorHandler = ExceptionCodes.LOCATION_HEADER_INCORRECT;
        ErrorDTO errorDTO = RestApiUtil.getErrorDTO(errorHandler);
        log.error(errorMessage, e);
        return Response.status(errorHandler.getHttpStatusCode()).entity(errorDTO).build();
    }
}
Also used : Comment(org.wso2.carbon.apimgt.core.models.Comment) ErrorHandler(org.wso2.carbon.apimgt.core.exception.ErrorHandler) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) ErrorDTO(org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO) CommentDTO(org.wso2.carbon.apimgt.rest.api.store.dto.CommentDTO) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) HashMap(java.util.HashMap) Map(java.util.Map) APIStore(org.wso2.carbon.apimgt.core.api.APIStore)

Example 19 with CommentDTO

use of org.wso2.carbon.apimgt.rest.api.store.dto.CommentDTO in project carbon-apimgt by wso2.

the class ApisApiServiceImpl method apisApiIdCommentsCommentIdGet.

/**
 * Retrives comments for a given API ID and comment ID
 *
 * @param commentId       Comment ID
 * @param apiId           API ID
 * @param ifNoneMatch     If-None-Match header value
 * @param ifModifiedSince If-Modified-Since header value
 * @param request         msf4j request object
 * @return CommentDTO object
 * @throws NotFoundException if this method is not defined in ApisApiServiceImpl
 */
@Override
public Response apisApiIdCommentsCommentIdGet(String commentId, String apiId, String ifNoneMatch, String ifModifiedSince, Request request) throws NotFoundException {
    String username = RestApiUtil.getLoggedInUsername(request);
    try {
        APIStore apiStore = RestApiUtil.getConsumer(username);
        String existingFingerprint = apisApiIdCommentsCommentIdGetFingerprint(commentId, apiId, ifNoneMatch, ifModifiedSince, request);
        if (!StringUtils.isEmpty(ifNoneMatch) && !StringUtils.isEmpty(existingFingerprint) && ifNoneMatch.contains(existingFingerprint)) {
            return Response.notModified().build();
        }
        Comment comment = apiStore.getCommentByUUID(commentId, apiId);
        CommentDTO commentDTO = CommentMappingUtil.fromCommentToDTO(comment);
        return Response.ok().header(HttpHeaders.ETAG, "\"" + existingFingerprint + "\"").entity(commentDTO).build();
    } catch (APIManagementException e) {
        String errorMessage = "Error while retrieving comment with commentId: " + commentId + " of apiID :" + apiId;
        Map<String, String> paramList = new HashMap<String, String>();
        paramList.put(APIMgtConstants.ExceptionsConstants.API_ID, apiId);
        paramList.put(APIMgtConstants.ExceptionsConstants.COMMENT_ID, commentId);
        ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
        log.error(errorMessage, e);
        return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
    }
}
Also used : Comment(org.wso2.carbon.apimgt.core.models.Comment) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) ErrorDTO(org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO) CommentDTO(org.wso2.carbon.apimgt.rest.api.store.dto.CommentDTO) HashMap(java.util.HashMap) Map(java.util.Map) APIStore(org.wso2.carbon.apimgt.core.api.APIStore)

Example 20 with CommentDTO

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

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 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 CommentDTO (org.wso2.carbon.apimgt.rest.api.store.v1.dto.CommentDTO)3