Search in sources :

Example 81 with Comment

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

the class ApisApiServiceImpl method getCommentOfAPI.

@Override
public Response getCommentOfAPI(String commentId, String apiId, String xWSO2Tenant, String ifNoneMatch, Boolean includeCommenterInfo, Integer replyLimit, Integer replyOffset, MessageContext messageContext) throws APIManagementException {
    String requestedTenantDomain = RestApiUtil.getRequestedTenantDomain(xWSO2Tenant);
    try {
        APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
        ApiTypeWrapper apiTypeWrapper = apiProvider.getAPIorAPIProductByUUID(apiId, requestedTenantDomain);
        Comment comment = apiProvider.getComment(apiTypeWrapper, commentId, replyLimit, replyOffset);
        if (comment != null) {
            CommentDTO commentDTO;
            if (includeCommenterInfo) {
                Map<String, Map<String, String>> userClaimsMap = CommentMappingUtil.retrieveUserClaims(comment.getUser(), new HashMap<>());
                commentDTO = CommentMappingUtil.fromCommentToDTOWithUserInfo(comment, userClaimsMap);
            } else {
                commentDTO = CommentMappingUtil.fromCommentToDTO(comment);
            }
            String uriString = RestApiConstants.RESOURCE_PATH_APIS + "/" + apiId + RestApiConstants.RESOURCE_PATH_COMMENTS + "/" + commentId;
            URI uri = new URI(uriString);
            return Response.ok(uri).entity(commentDTO).build();
        } else {
            RestApiUtil.handleResourceNotFoundError(RestApiConstants.RESOURCE_COMMENTS, String.valueOf(commentId), log);
        }
    } catch (APIManagementException e) {
        if (RestApiUtil.isDueToAuthorizationFailure(e)) {
            RestApiUtil.handleAuthorizationFailure(RestApiConstants.RESOURCE_API, apiId, e, log);
        } else if (RestApiUtil.isDueToResourceNotFound(e)) {
            RestApiUtil.handleResourceNotFoundError(RestApiConstants.RESOURCE_API, apiId, e, log);
        } else {
            String errorMessage = "Error while retrieving comment for API : " + apiId + "with comment ID " + commentId;
            RestApiUtil.handleInternalServerError(errorMessage, e, log);
        }
    } catch (URISyntaxException e) {
        String errorMessage = "Error while retrieving comment content location : " + apiId;
        RestApiUtil.handleInternalServerError(errorMessage, e, log);
    }
    return null;
}
Also used : Comment(org.wso2.carbon.apimgt.api.model.Comment) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) ApiTypeWrapper(org.wso2.carbon.apimgt.api.model.ApiTypeWrapper) CommentDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.CommentDTO) URISyntaxException(java.net.URISyntaxException) APIProvider(org.wso2.carbon.apimgt.api.APIProvider) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) URI(java.net.URI)

Example 82 with Comment

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

the class CommentMappingUtil method fromDTOToComment.

/**
 * Converts a CommentDTO to a Comment object.
 *
 * @param body     commentDTO body
 * @param username username of the consumer
 * @param apiId    API ID
 * @return Comment object
 */
public static Comment fromDTOToComment(CommentDTO body, String username, String apiId) {
    Comment comment = new Comment();
    comment.setText(body.getContent());
    comment.setUser(username);
    comment.setApiId(apiId);
    return comment;
}
Also used : Comment(org.wso2.carbon.apimgt.api.model.Comment)

Example 83 with Comment

use of org.wso2.carbon.apimgt.api.model.Comment 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.publisher.v1.dto.CommentDTO)

Example 84 with Comment

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

the class ApiMgtDAO method addComment.

/**
 ****************************
 * Adds a comment for an API
 *
 * @param uuid API uuid
 * @param comment Commented Text
 * @param user User who did the comment
 * @return Comment ID
 */
public String addComment(String uuid, Comment comment, String user) throws APIManagementException {
    String commentId = null;
    try (Connection connection = APIMgtDBUtil.getConnection()) {
        int id = -1;
        connection.setAutoCommit(false);
        id = getAPIID(uuid, connection);
        if (id == -1) {
            String msg = "Could not load API record for API with UUID: " + uuid;
            throw new APIManagementException(msg);
        }
        String addCommentQuery = SQLConstants.ADD_COMMENT_SQL;
        commentId = UUID.randomUUID().toString();
        try (PreparedStatement insertPrepStmt = connection.prepareStatement(addCommentQuery)) {
            insertPrepStmt.setString(1, commentId);
            insertPrepStmt.setString(2, comment.getText());
            insertPrepStmt.setString(3, user);
            insertPrepStmt.setTimestamp(4, new Timestamp(System.currentTimeMillis()), Calendar.getInstance());
            insertPrepStmt.setInt(5, id);
            insertPrepStmt.setString(6, comment.getParentCommentID());
            insertPrepStmt.setString(7, comment.getEntryPoint());
            insertPrepStmt.setString(8, comment.getCategory());
            insertPrepStmt.executeUpdate();
            connection.commit();
        }
    } catch (SQLException e) {
        handleException("Failed to add comment data, for API with UUID " + uuid, e);
    }
    return commentId;
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) SQLException(java.sql.SQLException) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) Timestamp(java.sql.Timestamp)

Example 85 with Comment

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

the class ApiMgtDAO method deleteComment.

private boolean deleteComment(String uuid, String commentId, Connection connection) throws APIManagementException {
    int id = -1;
    String deleteCommentQuery = SQLConstants.DELETE_COMMENT_SQL;
    String getCommentIDsOfReplies = SQLConstants.GET_IDS_OF_REPLIES_SQL;
    ResultSet resultSet = null;
    try {
        id = getAPIID(uuid, connection);
        if (id == -1) {
            String msg = "Could not load API record for API with UUID: " + uuid;
            throw new APIManagementException(msg, ExceptionCodes.from(ExceptionCodes.API_NOT_FOUND, uuid));
        }
        connection.setAutoCommit(false);
        try (PreparedStatement prepStmtGetReplies = connection.prepareStatement(getCommentIDsOfReplies)) {
            prepStmtGetReplies.setString(1, uuid);
            prepStmtGetReplies.setString(2, commentId);
            resultSet = prepStmtGetReplies.executeQuery();
            while (resultSet.next()) {
                deleteComment(uuid, resultSet.getString("COMMENT_ID"), connection);
            }
            try (PreparedStatement prepStmt = connection.prepareStatement(deleteCommentQuery)) {
                prepStmt.setInt(1, id);
                prepStmt.setString(2, commentId);
                prepStmt.execute();
                connection.commit();
                return true;
            }
        }
    } catch (SQLException e) {
        handleException("Error while deleting comment " + commentId + " from the database", e);
    }
    return false;
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

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