Search in sources :

Example 6 with CommentList

use of org.wso2.carbon.apimgt.api.model.CommentList 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.publisher.v1.dto.PaginationDTO) CommentDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.CommentDTO) CommentListDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.CommentListDTO) HashMap(java.util.HashMap) Map(java.util.Map)

Example 7 with CommentList

use of org.wso2.carbon.apimgt.api.model.CommentList in project carbon-apimgt by wso2.

the class ApisApiServiceImpl method getAllCommentsOfAPI.

@Override
public Response getAllCommentsOfAPI(String apiId, String xWSO2Tenant, Integer limit, Integer offset, Boolean includeCommenterInfo, MessageContext messageContext) throws APIManagementException {
    String requestedTenantDomain = RestApiUtil.getRequestedTenantDomain(xWSO2Tenant);
    try {
        APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
        ApiTypeWrapper apiTypeWrapper = apiProvider.getAPIorAPIProductByUUID(apiId, requestedTenantDomain);
        String parentCommentID = null;
        CommentList comments = apiProvider.getComments(apiTypeWrapper, parentCommentID, limit, offset);
        CommentListDTO commentDTO = CommentMappingUtil.fromCommentListToDTO(comments, includeCommenterInfo);
        String uriString = RestApiConstants.RESOURCE_PATH_APIS + "/" + apiId + RestApiConstants.RESOURCE_PATH_COMMENTS;
        URI uri = new URI(uriString);
        return Response.ok(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 get comments of API " + apiId, e, log);
        }
    } catch (URISyntaxException e) {
        String errorMessage = "Error while retrieving comments content location for API " + apiId;
        RestApiUtil.handleInternalServerError(errorMessage, e, log);
    }
    return null;
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) ApiTypeWrapper(org.wso2.carbon.apimgt.api.model.ApiTypeWrapper) CommentList(org.wso2.carbon.apimgt.api.model.CommentList) CommentListDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.CommentListDTO) URISyntaxException(java.net.URISyntaxException) APIProvider(org.wso2.carbon.apimgt.api.APIProvider) URI(java.net.URI)

Example 8 with CommentList

use of org.wso2.carbon.apimgt.api.model.CommentList in project carbon-apimgt by wso2.

the class ApisApiServiceImpl method getRepliesOfComment.

@Override
public Response getRepliesOfComment(String commentId, String apiId, String xWSO2Tenant, Integer limit, Integer offset, String ifNoneMatch, Boolean includeCommenterInfo, MessageContext messageContext) throws APIManagementException {
    String requestedTenantDomain = RestApiUtil.getRequestedTenantDomain(xWSO2Tenant);
    try {
        APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
        ApiTypeWrapper apiTypeWrapper = apiProvider.getAPIorAPIProductByUUID(apiId, requestedTenantDomain);
        CommentList comments = apiProvider.getComments(apiTypeWrapper, commentId, limit, offset);
        CommentListDTO commentDTO = CommentMappingUtil.fromCommentListToDTO(comments, includeCommenterInfo);
        String uriString = RestApiConstants.RESOURCE_PATH_APIS + "/" + apiId + RestApiConstants.RESOURCE_PATH_COMMENTS;
        URI uri = new URI(uriString);
        return Response.ok(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 get comments of API " + apiId, e, log);
        }
    } catch (URISyntaxException e) {
        String errorMessage = "Error while retrieving comments content location for API " + apiId;
        RestApiUtil.handleInternalServerError(errorMessage, e, log);
    }
    return null;
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) ApiTypeWrapper(org.wso2.carbon.apimgt.api.model.ApiTypeWrapper) CommentList(org.wso2.carbon.apimgt.api.model.CommentList) CommentListDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.CommentListDTO) URISyntaxException(java.net.URISyntaxException) APIProvider(org.wso2.carbon.apimgt.api.APIProvider) URI(java.net.URI)

Example 9 with CommentList

use of org.wso2.carbon.apimgt.api.model.CommentList in project carbon-apimgt by wso2.

the class ApisApiServiceImpl method getRepliesOfComment.

@Override
public Response getRepliesOfComment(String commentId, String apiId, String xWSO2Tenant, Integer limit, Integer offset, String ifNoneMatch, Boolean includeCommenterInfo, MessageContext messageContext) throws APIManagementException {
    String organization = RestApiUtil.getValidatedOrganization(messageContext);
    try {
        APIConsumer apiConsumer = RestApiCommonUtil.getLoggedInUserConsumer();
        ApiTypeWrapper apiTypeWrapper = apiConsumer.getAPIorAPIProductByUUID(apiId, organization);
        CommentList comments = apiConsumer.getComments(apiTypeWrapper, commentId, limit, offset);
        CommentListDTO commentDTO = CommentMappingUtil.fromCommentListToDTO(comments, includeCommenterInfo);
        String uriString = RestApiConstants.RESOURCE_PATH_APIS + "/" + apiId + RestApiConstants.RESOURCE_PATH_COMMENTS;
        URI uri = new URI(uriString);
        return Response.ok(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 get comments of API " + apiId, e, log);
        }
    } catch (URISyntaxException e) {
        String errorMessage = "Error while retrieving comments content location for API " + apiId;
        RestApiUtil.handleInternalServerError(errorMessage, e, log);
    }
    return null;
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) ApiTypeWrapper(org.wso2.carbon.apimgt.api.model.ApiTypeWrapper) CommentList(org.wso2.carbon.apimgt.api.model.CommentList) APIConsumer(org.wso2.carbon.apimgt.api.APIConsumer) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI)

Example 10 with CommentList

use of org.wso2.carbon.apimgt.api.model.CommentList in project carbon-apimgt by wso2.

the class ApiDAOImpl method getCommentsForApi.

@Override
public List<Comment> getCommentsForApi(String apiId) throws APIMgtDAOException {
    List<Comment> commentList = new ArrayList<>();
    final String getCommentsQuery = "SELECT UUID, COMMENT_TEXT, USER_IDENTIFIER, API_ID, " + "CREATED_BY, CREATED_TIME, UPDATED_BY, LAST_UPDATED_TIME " + "FROM AM_API_COMMENTS WHERE API_ID = ?";
    try (Connection connection = DAOUtil.getConnection();
        PreparedStatement statement = connection.prepareStatement(getCommentsQuery)) {
        try {
            statement.setString(1, apiId);
            statement.execute();
            try (ResultSet rs = statement.getResultSet()) {
                while (rs.next()) {
                    commentList.add(constructCommentFromResultSet(rs));
                }
            }
        } catch (SQLException e) {
            connection.rollback();
            String errorMessage = "getting all comments for API " + apiId;
            throw new APIMgtDAOException(DAOUtil.DAO_ERROR_PREFIX + errorMessage, e);
        }
    } catch (SQLException e) {
        String errorMessage = "getting all comments for API " + apiId;
        throw new APIMgtDAOException(DAOUtil.DAO_ERROR_PREFIX + errorMessage, e);
    }
    return commentList;
}
Also used : Comment(org.wso2.carbon.apimgt.core.models.Comment) APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Aggregations

ArrayList (java.util.ArrayList)9 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)8 CommentList (org.wso2.carbon.apimgt.api.model.CommentList)6 Comment (org.wso2.carbon.apimgt.core.models.Comment)6 URI (java.net.URI)4 URISyntaxException (java.net.URISyntaxException)4 SQLException (java.sql.SQLException)4 ApiTypeWrapper (org.wso2.carbon.apimgt.api.model.ApiTypeWrapper)4 Comment (org.wso2.carbon.apimgt.api.model.Comment)4 Connection (java.sql.Connection)3 PreparedStatement (java.sql.PreparedStatement)3 ResultSet (java.sql.ResultSet)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 APIStore (org.wso2.carbon.apimgt.core.api.APIStore)3 CommentListDTO (org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.CommentListDTO)3 CommentListDTO (org.wso2.carbon.apimgt.rest.api.store.dto.CommentListDTO)3 Test (org.junit.Test)2 APIConsumer (org.wso2.carbon.apimgt.api.APIConsumer)2 APIProvider (org.wso2.carbon.apimgt.api.APIProvider)2