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());
}
}
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());
}
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();
}
}
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;
}
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;
}
Aggregations