Search in sources :

Example 11 with CommentList

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

the class APIStoreImpl method getCommentsForApi.

@Override
public List<Comment> getCommentsForApi(String apiId) throws APICommentException, APIMgtResourceNotFoundException {
    try {
        failIfApiNotExists(apiId);
        List<Comment> commentList = getApiDAO().getCommentsForApi(apiId);
        return commentList;
    } catch (APIMgtDAOException e) {
        String errorMsg = "Error occurred while retrieving comments for api " + apiId;
        log.error(errorMsg, e);
        throw new APICommentException(errorMsg, e, e.getErrorHandler());
    }
}
Also used : Comment(org.wso2.carbon.apimgt.core.models.Comment) APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) APICommentException(org.wso2.carbon.apimgt.core.exception.APICommentException)

Example 12 with CommentList

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

the class APIStoreImplTestCase method testGetAllCommentsForApi.

@Test(description = "Get all comments for an api")
public void testGetAllCommentsForApi() throws APIManagementException {
    ApiDAO apiDAO = Mockito.mock(ApiDAO.class);
    APIStore apiStore = getApiStoreImpl(apiDAO);
    API api = SampleTestObjectCreator.createDefaultAPI().build();
    Mockito.when(apiDAO.isAPIExists(api.getId())).thenReturn(true);
    List<Comment> commentList = new ArrayList<>();
    Comment comment1 = SampleTestObjectCreator.createDefaultComment(api.getId());
    Comment comment2 = SampleTestObjectCreator.createDefaultComment(api.getId());
    commentList.add(comment1);
    commentList.add(comment2);
    Mockito.when(apiDAO.getCommentsForApi(api.getId())).thenReturn(commentList);
    List<Comment> commentListFromDB = apiStore.getCommentsForApi(api.getId());
    Assert.assertNotNull(commentListFromDB);
    Assert.assertEquals(commentList.size(), commentListFromDB.size());
    Mockito.verify(apiDAO, Mockito.times(1)).isAPIExists(api.getId());
    Mockito.verify(apiDAO, Mockito.times(1)).getCommentsForApi(api.getId());
}
Also used : Comment(org.wso2.carbon.apimgt.core.models.Comment) ArrayList(java.util.ArrayList) CompositeAPI(org.wso2.carbon.apimgt.core.models.CompositeAPI) API(org.wso2.carbon.apimgt.core.models.API) ApiDAO(org.wso2.carbon.apimgt.core.dao.ApiDAO) APIStore(org.wso2.carbon.apimgt.core.api.APIStore) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest)

Example 13 with CommentList

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

the class ApisApiServiceImpl method apisApiIdCommentsGet.

/**
 * Retrives A list of comments for a given API ID
 *
 * @param apiId   API ID
 * @param limit   Max number of comments to return
 * @param offset  Starting point of pagination
 * @param request msf4j request object
 * @return CommentListDTO object
 * @throws NotFoundException if this method is not defined in ApisApiServiceImpl
 */
@Override
public Response apisApiIdCommentsGet(String apiId, Integer limit, Integer offset, Request request) throws NotFoundException {
    String username = RestApiUtil.getLoggedInUsername(request);
    try {
        APIStore apiStore = RestApiUtil.getConsumer(username);
        List<Comment> commentList = apiStore.getCommentsForApi(apiId);
        CommentListDTO commentListDTO = CommentMappingUtil.fromCommentListToDTO(commentList, limit, offset);
        return Response.ok().entity(commentListDTO).build();
    } catch (APIManagementException e) {
        String errorMessage = "Error while retrieving comments for api : " + apiId;
        Map<String, String> paramList = new HashMap<String, String>();
        paramList.put(APIMgtConstants.ExceptionsConstants.API_ID, apiId);
        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) CommentListDTO(org.wso2.carbon.apimgt.rest.api.store.dto.CommentListDTO) HashMap(java.util.HashMap) Map(java.util.Map) APIStore(org.wso2.carbon.apimgt.core.api.APIStore)

Example 14 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.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 15 with CommentList

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

the class ApiMgtDAO method getComments.

/**
 **************************************
 * Returns all the Comments on an API
 *
 * @param uuid API UUID
 * @param parentCommentID Parent Comment ID
 * @param limit           The limit
 * @param offset          The offset
 * @param connection Database connection
 * @return Comment Array
 * @throws APIManagementException
 */
private CommentList getComments(String uuid, String parentCommentID, Integer limit, Integer offset, Connection connection) throws APIManagementException {
    List<Comment> list = new ArrayList<Comment>();
    CommentList commentList = new CommentList();
    Pagination pagination = new Pagination();
    commentList.setPagination(pagination);
    int total = 0;
    String sqlQuery;
    String sqlQueryForCount;
    if (parentCommentID == null) {
        sqlQueryForCount = SQLConstants.GET_ROOT_COMMENTS_COUNT_SQL;
    } else {
        sqlQueryForCount = SQLConstants.GET_REPLIES_COUNT_SQL;
    }
    try (PreparedStatement prepStmtForCount = connection.prepareStatement(sqlQueryForCount)) {
        prepStmtForCount.setString(1, uuid);
        if (parentCommentID != null) {
            prepStmtForCount.setString(2, parentCommentID);
        }
        try (ResultSet resultSetForCount = prepStmtForCount.executeQuery()) {
            while (resultSetForCount.next()) {
                total = resultSetForCount.getInt("COMMENT_COUNT");
            }
            if (total > 0 && limit > 0) {
                if (parentCommentID == null) {
                    sqlQuery = SQLConstantManagerFactory.getSQlString("GET_ROOT_COMMENTS_SQL");
                } else {
                    sqlQuery = SQLConstantManagerFactory.getSQlString("GET_REPLIES_SQL");
                }
                try (PreparedStatement prepStmt = connection.prepareStatement(sqlQuery)) {
                    prepStmt.setString(1, uuid);
                    if (parentCommentID != null) {
                        prepStmt.setString(2, parentCommentID);
                        prepStmt.setInt(3, offset);
                        prepStmt.setInt(4, limit);
                    } else {
                        prepStmt.setInt(2, offset);
                        prepStmt.setInt(3, limit);
                    }
                    try (ResultSet resultSet = prepStmt.executeQuery()) {
                        while (resultSet.next()) {
                            Comment comment = new Comment();
                            comment.setId(resultSet.getString("COMMENT_ID"));
                            comment.setText(resultSet.getString("COMMENT_TEXT"));
                            comment.setUser(resultSet.getString("CREATED_BY"));
                            comment.setCreatedTime(resultSet.getTimestamp("CREATED_TIME"));
                            comment.setUpdatedTime(resultSet.getTimestamp("UPDATED_TIME"));
                            comment.setApiId(resultSet.getString("API_ID"));
                            comment.setParentCommentID(resultSet.getString("PARENT_COMMENT_ID"));
                            comment.setEntryPoint(resultSet.getString("ENTRY_POINT"));
                            comment.setCategory(resultSet.getString("CATEGORY"));
                            if (parentCommentID == null) {
                                comment.setReplies(getComments(uuid, resultSet.getString("COMMENT_ID"), APIConstants.REPLYLIMIT, APIConstants.REPLYOFFSET, connection));
                            } else {
                                CommentList emptyCommentList = new CommentList();
                                Pagination emptyPagination = new Pagination();
                                emptyCommentList.setPagination(emptyPagination);
                                emptyCommentList.getPagination().setTotal(0);
                                emptyCommentList.setCount(0);
                                comment.setReplies(emptyCommentList);
                            }
                            list.add(comment);
                        }
                    }
                }
            } else {
                commentList.getPagination().setTotal(total);
                commentList.setCount(total);
                return commentList;
            }
        }
    } catch (SQLException e) {
        handleException("Failed to retrieve comments for API with UUID " + uuid, e);
    }
    pagination.setLimit(limit);
    pagination.setOffset(offset);
    commentList.getPagination().setTotal(total);
    commentList.setList(list);
    commentList.setCount(list.size());
    return commentList;
}
Also used : Comment(org.wso2.carbon.apimgt.api.model.Comment) Pagination(org.wso2.carbon.apimgt.api.model.Pagination) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) ResultSet(java.sql.ResultSet) CommentList(org.wso2.carbon.apimgt.api.model.CommentList) 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