Search in sources :

Example 36 with Comment

use of org.wso2.carbon.apimgt.core.models.Comment in project carbon-apimgt by wso2.

the class ApiMgtDAO method getComments.

/**
 **************************************
 * Returns all the Comments on an API
 *
 * @param apiTypeWrapper API type Wrapper
 * @param parentCommentID Parent Comment ID
 * @return Comment Array
 * @throws APIManagementException
 */
public CommentList getComments(ApiTypeWrapper apiTypeWrapper, String parentCommentID, Integer limit, Integer offset) throws APIManagementException {
    CommentList commentList = null;
    try (Connection connection = APIMgtDBUtil.getConnection()) {
        int id = -1;
        String uuid;
        Identifier identifier;
        String currentApiUuid;
        if (apiTypeWrapper.isAPIProduct()) {
            identifier = apiTypeWrapper.getApiProduct().getId();
            uuid = apiTypeWrapper.getApiProduct().getUuid();
            APIRevision apiRevision = checkAPIUUIDIsARevisionUUID(uuid);
            if (apiRevision != null && apiRevision.getApiUUID() != null) {
                currentApiUuid = apiRevision.getApiUUID();
            } else {
                currentApiUuid = uuid;
            }
        } else {
            identifier = apiTypeWrapper.getApi().getId();
            uuid = apiTypeWrapper.getApi().getUuid();
            APIRevision apiRevision = checkAPIUUIDIsARevisionUUID(uuid);
            if (apiRevision != null && apiRevision.getApiUUID() != null) {
                currentApiUuid = apiRevision.getApiUUID();
            } else {
                currentApiUuid = uuid;
            }
        }
        id = getAPIID(currentApiUuid, connection);
        if (id == -1) {
            String msg = "Could not load API record for: " + identifier.getName();
            throw new APIManagementException(msg);
        }
        commentList = getComments(currentApiUuid, parentCommentID, limit, offset, connection);
    } catch (SQLException e) {
        handleException("Failed to retrieve comments for  " + apiTypeWrapper.getName(), e);
    }
    return commentList;
}
Also used : DeployedAPIRevision(org.wso2.carbon.apimgt.api.model.DeployedAPIRevision) APIRevision(org.wso2.carbon.apimgt.api.model.APIRevision) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) APIProductIdentifier(org.wso2.carbon.apimgt.api.model.APIProductIdentifier) Identifier(org.wso2.carbon.apimgt.api.model.Identifier) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) SQLException(java.sql.SQLException) Connection(java.sql.Connection) CommentList(org.wso2.carbon.apimgt.api.model.CommentList)

Example 37 with Comment

use of org.wso2.carbon.apimgt.core.models.Comment in project carbon-apimgt by wso2.

the class ApiMgtDAO method getComment.

/**
 ************************
 * Returns a specific comment of an API
 *
 * @param commentId  Comment ID
 * @param apiTypeWrapper Api Type Wrapper
 * @return Comment Array
 * @throws APIManagementException
 */
public Comment getComment(ApiTypeWrapper apiTypeWrapper, String commentId, Integer replyLimit, Integer replyOffset) throws APIManagementException {
    String uuid;
    Identifier identifier;
    if (apiTypeWrapper.isAPIProduct()) {
        identifier = apiTypeWrapper.getApiProduct().getId();
        uuid = apiTypeWrapper.getApiProduct().getUuid();
    } else {
        identifier = apiTypeWrapper.getApi().getId();
        uuid = apiTypeWrapper.getApi().getUuid();
    }
    try (Connection connection = APIMgtDBUtil.getConnection()) {
        Comment comment = new Comment();
        int id = -1;
        String getCommentQuery = SQLConstants.GET_COMMENT_SQL;
        id = getAPIID(uuid, connection);
        if (id == -1) {
            String msg = "Could not load API record for: " + identifier.getName();
            throw new APIManagementException(msg);
        }
        try (PreparedStatement prepStmt = connection.prepareStatement(getCommentQuery)) {
            prepStmt.setString(1, uuid);
            prepStmt.setString(2, commentId);
            try (ResultSet resultSet = prepStmt.executeQuery()) {
                if (resultSet.next()) {
                    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"));
                    comment.setReplies(getComments(uuid, commentId, replyLimit, replyOffset, connection));
                    return comment;
                }
            }
        }
    } catch (SQLException e) {
        handleException("Failed to retrieve comment for API " + identifier.getName() + "with comment ID " + commentId, e);
    }
    return null;
}
Also used : Comment(org.wso2.carbon.apimgt.api.model.Comment) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) APIProductIdentifier(org.wso2.carbon.apimgt.api.model.APIProductIdentifier) Identifier(org.wso2.carbon.apimgt.api.model.Identifier) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 38 with Comment

use of org.wso2.carbon.apimgt.core.models.Comment in project carbon-apimgt by wso2.

the class CommentMappingUtil method fromCommentToDTOWithUserInfo.

/**
 * Converts a Comment object into corresponding REST API CommentDTO object with User Info.
 *
 * @param comment comment object
 * @return CommentDTO
 */
public static CommentDTO fromCommentToDTOWithUserInfo(Comment comment, Map<String, Map<String, String>> userClaimsMap) throws APIManagementException {
    CommentDTO commentDTO = fromCommentToDTO(comment);
    if (userClaimsMap.get(comment.getUser()) != null) {
        Map userClaims = userClaimsMap.get(comment.getUser());
        CommenterInfoDTO commenterInfoDTO = new CommenterInfoDTO();
        commenterInfoDTO.setFullName((String) userClaims.get(APIConstants.FULL_NAME));
        commenterInfoDTO.setFirstName((String) userClaims.get(APIConstants.FIRST_NAME));
        commenterInfoDTO.setLastName((String) userClaims.get(APIConstants.LAST_NAME));
        commentDTO.setCommenterInfo(commenterInfoDTO);
    }
    return commentDTO;
}
Also used : CommenterInfoDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.CommenterInfoDTO) CommentDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.CommentDTO) HashMap(java.util.HashMap) Map(java.util.Map)

Example 39 with Comment

use of org.wso2.carbon.apimgt.core.models.Comment 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 40 with Comment

use of org.wso2.carbon.apimgt.core.models.Comment in project carbon-apimgt by wso2.

the class CommentMappingUtil method fromCommentToDTOWithUserInfo.

/**
 * Converts a Comment object into corresponding REST API CommentDTO object with User Info
 *
 * @param comment comment object
 * @return CommentDTO
 */
public static CommentDTO fromCommentToDTOWithUserInfo(Comment comment, Map<String, Map<String, String>> userClaimsMap) throws APIManagementException {
    CommentDTO commentDTO = fromCommentToDTO(comment);
    if (userClaimsMap.get(comment.getUser()) != null) {
        Map userClaims = userClaimsMap.get(comment.getUser());
        CommenterInfoDTO commenterInfoDTO = new CommenterInfoDTO();
        commenterInfoDTO.setFullName((String) userClaims.get(APIConstants.FULL_NAME));
        commenterInfoDTO.setFirstName((String) userClaims.get(APIConstants.FIRST_NAME));
        commenterInfoDTO.setLastName((String) userClaims.get(APIConstants.LAST_NAME));
        commentDTO.setCommenterInfo(commenterInfoDTO);
    }
    return commentDTO;
}
Also used : CommenterInfoDTO(org.wso2.carbon.apimgt.rest.api.store.v1.dto.CommenterInfoDTO) CommentDTO(org.wso2.carbon.apimgt.rest.api.store.v1.dto.CommentDTO) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

Comment (org.wso2.carbon.apimgt.core.models.Comment)36 APIStore (org.wso2.carbon.apimgt.core.api.APIStore)28 ApiDAO (org.wso2.carbon.apimgt.core.dao.ApiDAO)23 Test (org.testng.annotations.Test)22 SQLException (java.sql.SQLException)18 BeforeTest (org.testng.annotations.BeforeTest)18 API (org.wso2.carbon.apimgt.core.models.API)17 CompositeAPI (org.wso2.carbon.apimgt.core.models.CompositeAPI)17 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)16 Comment (org.wso2.carbon.apimgt.api.model.Comment)16 APIMgtDAOException (org.wso2.carbon.apimgt.core.exception.APIMgtDAOException)15 PreparedStatement (java.sql.PreparedStatement)13 Connection (java.sql.Connection)12 HashMap (java.util.HashMap)12 Map (java.util.Map)12 ArrayList (java.util.ArrayList)10 CommentDTO (org.wso2.carbon.apimgt.rest.api.store.dto.CommentDTO)9 URI (java.net.URI)8 URISyntaxException (java.net.URISyntaxException)8 Test (org.junit.Test)8