From 6da8dada710bbab78fcbf42c44076681510ae436 Mon Sep 17 00:00:00 2001 From: lasanthaDLPDS Date: Wed, 19 Sep 2018 18:45:58 +0530 Subject: [PATCH] Improve comment management --- .../device/application/mgt/common/Review.java | 26 +- ...on.java => ReviewManagementException.java} | 8 +- .../mgt/common/services/ReviewManager.java | 29 +- .../application/mgt/core/dao/ReviewDAO.java | 319 +------ .../application/mgt/core/dao/common/Util.java | 6 +- .../core/dao/impl/Comment/ReviewDAOImpl.java | 829 ++---------------- .../ReviewManagementDAOException.java | 35 + .../mgt/core/impl/ReviewManagerImpl.java | 238 +++-- .../impl/ReviewManagementAPIImpl.java | 19 +- .../api/services/ReviewManagementAPITest.java | 14 +- 10 files changed, 290 insertions(+), 1233 deletions(-) rename components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/exception/{CommentManagementException.java => ReviewManagementException.java} (82%) create mode 100644 components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/exception/ReviewManagementDAOException.java diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/Review.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/Review.java index 2a1b33976a2..e2a488ce3e0 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/Review.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/Review.java @@ -34,9 +34,9 @@ public class Review { value = "Comment of the review") private String comment; - @ApiModelProperty(name = "replyComment", - value = "Reply comment of the review") - private String replyComment; + @ApiModelProperty(name = "parentId", + value = "Parent id of the review") + private int parentId; @ApiModelProperty(name = "username", value = "Username odf the Review creator", @@ -55,6 +55,10 @@ public class Review { value = "Rating value of the application release") private int rating; + @ApiModelProperty(name = "replyReview", + value = "Replying review") + private Review replyReview; + public int getId() { return id; } @@ -103,12 +107,20 @@ public class Review { this.rating = rating; } - public String getReplyComment() { - return replyComment; + public int getParentId() { + return parentId; + } + + public void setParentId(int parentId) { + this.parentId = parentId; + } + + public Review getReplyReview() { + return replyReview; } - public void setReplyComment(String replyComment) { - this.replyComment = replyComment; + public void setReplyReview(Review replyReview) { + this.replyReview = replyReview; } } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/exception/CommentManagementException.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/exception/ReviewManagementException.java similarity index 82% rename from components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/exception/CommentManagementException.java rename to components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/exception/ReviewManagementException.java index 2f81f5144a1..46255f6a853 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/exception/CommentManagementException.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/exception/ReviewManagementException.java @@ -18,20 +18,20 @@ */ package org.wso2.carbon.device.application.mgt.common.exception; -public class CommentManagementException extends Exception { +public class ReviewManagementException extends Exception { private String message; - public CommentManagementException(String message, Throwable throwable) { + public ReviewManagementException(String message, Throwable throwable) { super(message, throwable); setMessage(message); } - public CommentManagementException(String message) { + public ReviewManagementException(String message) { super(message); setMessage(message); } - public CommentManagementException() { + public ReviewManagementException() { } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/ReviewManager.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/ReviewManager.java index d9e62337efc..c2ddf4784e9 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/ReviewManager.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/ReviewManager.java @@ -22,8 +22,7 @@ import org.wso2.carbon.device.application.mgt.common.Rating; import org.wso2.carbon.device.application.mgt.common.Review; import org.wso2.carbon.device.application.mgt.common.PaginationRequest; import org.wso2.carbon.device.application.mgt.common.PaginationResult; -import org.wso2.carbon.device.application.mgt.common.exception.ApplicationManagementException; -import org.wso2.carbon.device.application.mgt.common.exception.CommentManagementException; +import org.wso2.carbon.device.application.mgt.common.exception.ReviewManagementException; /** * ReviewManager is responsible for handling all the add/update/delete/get operations related with @@ -37,36 +36,36 @@ public interface ReviewManager { * @param appId id of the application. * @param appReleaseId id of the application release * @return {@link Review} Review added - * @throws CommentManagementException Exceptions of the review management. + * @throws ReviewManagementException Exceptions of the review management. */ - boolean addReview(Review review,int appId, int appReleaseId) throws CommentManagementException; + boolean addReview(Review review,int appId, int appReleaseId) throws ReviewManagementException; /** * Get all comments to pagination * - * @param request Pagination request + * @param request Pagination request {@link PaginationRequest} * @param uuid uuid of the application release * @return {@link PaginationResult} pagination result with starting offSet and limit - * @throws CommentManagementException Exceptions of the comment management. + * @throws ReviewManagementException Exceptions of the comment management. */ - PaginationResult getAllReviews(PaginationRequest request, String uuid) throws CommentManagementException; + PaginationResult getAllReviews(PaginationRequest request, String uuid) throws ReviewManagementException; /** * To get the comment with id. * * @param commentId id of the comment * @return {@link Review}Review of the comment id - * @throws CommentManagementException Exceptions of the comment management. + * @throws ReviewManagementException Exceptions of the comment management. */ - Review getReview(int commentId) throws CommentManagementException; + Review getReview(int commentId) throws ReviewManagementException; /** * To delete review using review id. * * @param commentId id of the comment - * @throws CommentManagementException Exceptions of the comment management + * @throws ReviewManagementException Exceptions of the comment management */ - void deleteReview(String loggedInUser, int commentId) throws CommentManagementException; + void deleteReview(String loggedInUser, int commentId) throws ReviewManagementException; /** * To update a review. @@ -74,16 +73,16 @@ public interface ReviewManager { * @param review review of the application. * @param reviewId id of the review * @return {@link Review}updated review - * @throws CommentManagementException Exceptions of the review management + * @throws ReviewManagementException Exceptions of the review management */ - boolean updateReview(Review review, int reviewId, boolean checkExistence) throws CommentManagementException; + boolean updateReview(Review review, int reviewId, boolean checkExistence) throws ReviewManagementException; /** * To get the overall rating for a application release * * @param appReleaseUuuid UUID of the application release. * @return {@link Review}updated review - * @throws CommentManagementException Exceptions of the review management + * @throws ReviewManagementException Exceptions of the review management */ - Rating getRating(String appReleaseUuuid) throws CommentManagementException; + Rating getRating(String appReleaseUuuid) throws ReviewManagementException; } \ No newline at end of file diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/ReviewDAO.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/ReviewDAO.java index 009823af99c..6bab98ef4d7 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/ReviewDAO.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/ReviewDAO.java @@ -20,12 +20,11 @@ package org.wso2.carbon.device.application.mgt.core.dao; import org.wso2.carbon.device.application.mgt.common.Review; import org.wso2.carbon.device.application.mgt.common.PaginationRequest; -import org.wso2.carbon.device.application.mgt.common.exception.CommentManagementException; +import org.wso2.carbon.device.application.mgt.common.exception.ReviewManagementException; import org.wso2.carbon.device.application.mgt.common.exception.DBConnectionException; -import org.wso2.carbon.device.application.mgt.core.exception.ApplicationManagementDAOException; +import org.wso2.carbon.device.application.mgt.core.exception.ReviewManagementDAOException; import java.sql.SQLException; -import java.sql.Timestamp; import java.util.List; /** @@ -39,263 +38,58 @@ import java.util.List; * * @param tenantId tenantId of the commented application. * @param review review of the application. - * @param createdBy Username of the created person. - * @param parentId parent id of the parent review. - * @param uuid uuid of the application * @return If review is added successfully, it return true otherwise false - * @throws CommentManagementException Exceptions of the review management. - * @throws DBConnectionException db connection exception. + * @throws ReviewManagementDAOException Exceptions of the review management DAO. */ - boolean addReview(Review review, int appId, int appReleaseId, int tenantId) - throws CommentManagementException, DBConnectionException, SQLException; + boolean addReview(Review review, int appId, int appReleaseId, int tenantId) throws ReviewManagementDAOException; Review isExistReview(int appId, int appReleaseId, String username, int tenantId) throws DBConnectionException, SQLException; - - /** - * To add a review to a application. - * - * @param review review of the application. - * @param createdBy Username of the created person. - * @param appType type of the commented application. - * @param appName name of the commented application. - * @param version version of the commented application. - * @return review id - * @throws CommentManagementException Exceptions of the review management. - */ - int addReview(int tenantId, Review review, String createdBy, String appType, String appName, String version) - throws CommentManagementException, DBConnectionException, SQLException; - /** * To update already added comment. * - * @param commentId id of the comment - * @param updatedComment comment after updated - * @param modifiedBy Username of the modified person. - * @param modifiedAt time of the modification. * @return {@link Review}Updated comment - * @throws CommentManagementException Exceptions of the comment management. + * @throws ReviewManagementException Exceptions of the comment management. * @throws DBConnectionException db connection exception * @throws SQLException sql exception */ boolean updateReview(Review review, int reviewId, int tenantId) - throws CommentManagementException, DBConnectionException, SQLException; + throws ReviewManagementException, DBConnectionException, SQLException; - /** - * To update already added comment. - * - * @param uuid uuid of the comment - * @param commentId id of the comment - * @param updatedComment comment after updated - * @param modifiedBy Username of the modified person. - * @param modifiedAt time of the modification. - * @return {@link Review}Updated comment - * @throws CommentManagementException Exceptions of the comment management. - * @throws DBConnectionException db connection exception - * @throws SQLException sql exception - */ - Review updateReview(String uuid, int commentId, String updatedComment, String modifiedBy, Timestamp modifiedAt) - throws CommentManagementException, DBConnectionException, SQLException; /** * To get the comment with id. * * @param commentId id of the comment * @return {@link Review}Review - * @throws CommentManagementException Exceptions of the comment management. + * @throws ReviewManagementException Exceptions of the comment management. * @throws DBConnectionException db connection exception * @throws SQLException sql exception */ - Review getComment(int commentId) throws CommentManagementException, SQLException, DBConnectionException; - - /** - * To get the comment with id. - * - * @param uuid uuid of the comment - * @return {@link List} List of comments in the application - * @throws CommentManagementException Exceptions of the comment management. - * @throws DBConnectionException db connection exception - * @throws SQLException sql exception - */ - List getComment(String uuid) throws CommentManagementException, SQLException, DBConnectionException; + Review getComment(int commentId) throws ReviewManagementException, SQLException, DBConnectionException; /** * To get all the comments * * @param uuid uuid of the application * @param request {@link PaginationRequest}pagination request with offSet and limit + * @param tenantId Tenant id * @return {@link List}List of all the comments in an application - * @throws DBConnectionException db connection exception - * @throws SQLException sql exception + * @throws ReviewManagementDAOException Review management DAO exception **/ - List getAllComments(String uuid, PaginationRequest request) throws SQLException, DBConnectionException; + List getAllReviews(String uuid, PaginationRequest request, int tenantId) + throws ReviewManagementDAOException; /** * To get list of comments using release id and application id. * - * @param appReleasedId Id of the released version of the application. - * @param appId id of the commented application. * @return {@link List}List of comments - * @throws CommentManagementException Exceptions of the comment management. + * @throws ReviewManagementException Exceptions of the comment management. */ - List getAllRatingValues(String uuid)throws SQLException, DBConnectionException; - List getComments(int appReleasedId, int appId) throws CommentManagementException; - - /** - * To get list of comments using application type, application name and version of the application. - * - * @param appType type of the commented application. - * @param appName name of the commented application. - * @param version version of the commented application. - * @return {@link List}List of comments - * @throws CommentManagementException Exceptions of the comment management. - * @throws DBConnectionException db connection exception. - * @throws SQLException sql exception - */ - List getComments(String appType, String appName, String version) - throws CommentManagementException, DBConnectionException, SQLException; - - /** - * To get list of comments using tenant id. - * - * @param tenantId tenant id of the commented application - * @return {@link List}List of comments - * @throws CommentManagementException Exceptions of the comment management. - * @throws DBConnectionException db connection exception. - * @throws SQLException sql exception - */ - List getComments(int tenantId) throws CommentManagementException, DBConnectionException, SQLException; - - /** - * To get list of comments by created user. - * - * @param createdBy Username of the created person. - * @return {@link List}List of comments - * @throws CommentManagementException Exceptions of the comment management. - * @throws DBConnectionException db connection exception. - * @throws SQLException sql exception - */ - List getCommentsByUser(String createdBy) - throws CommentManagementException, DBConnectionException, SQLException; - - /** - * To get list of comments by created use and created time. - * - * @param createdBy Username of the created person. - * @param createdAt time of the comment created. - * @return {@link List}List of comments - * @throws CommentManagementException Exceptions of the comment management. - * @throws DBConnectionException db connection exception. - * @throws SQLException sql exception - */ - List getCommentsByUser(String createdBy, Timestamp createdAt) - throws CommentManagementException, DBConnectionException, SQLException; - - /** - * To get list of comments by modified users. - * - * @param modifiedBy Username of the modified person. - * @return {@link List}List of comments - * @throws CommentManagementException Exceptions of the comment management. - * @throws DBConnectionException db connection exception. - * @throws SQLException sql exception - */ - List getCommentsByModifiedUser(String modifiedBy) - throws CommentManagementException, DBConnectionException, SQLException; - - /** - * To get list of comments using modified user's name and modified time. - * - * @param modifiedBy Username of the modified person. - * @param modifiedAt time of the modification - * @return {@link List}List of comments - * @throws CommentManagementException Exceptions of the comment management. - * @throws DBConnectionException db connection exception. - * @throws SQLException sql exception - */ - List getCommentsByModifiedUser(String modifiedBy, Timestamp modifiedAt) - throws CommentManagementException, DBConnectionException, SQLException; - - /** - * To get list of comments using application type, application name , application version and parent id of the comment. - * - * @param appType type of the commented application. - * @param appName name of the commented application. - * @param version version of the commented application. - * @param parentId parent id of the parent comment. - * @return {@link List}List of comments - * @throws CommentManagementException Exceptions of the comment management. - * @throws DBConnectionException db connection exception. - * @throws SQLException sql exception - */ - List getComments(String appType, String appName, String version, int parentId) - throws CommentManagementException, DBConnectionException, SQLException; - - /** - * To get a count of the comments by usernames. - * - * @param uuid uuid of the application - * @return Count of the comments - * @throws CommentManagementException Exceptions of the comment management. - * @throws DBConnectionException db connection exception. - * @throws SQLException sql exception - */ - int getCommentCount(String uuid) throws CommentManagementException, DBConnectionException, SQLException; - - /** - * To get a count of the comments by usernames. - * - * @param createdBy Username of the created person. - * @return Count of the comments - * @throws CommentManagementException Exceptions of the comment management. - * @throws DBConnectionException db connection exception. - * @throws SQLException sql exception - */ - int getCommentCountByUser(String createdBy) throws CommentManagementException, DBConnectionException, SQLException; - - /** - * To get the comment count by parent comment id. - * - * @param uuid uuid of the comment - * @param parentId id of the parent comment - * @return Count of the comments - * @throws CommentManagementException Exceptions of the comment management. - * @throws DBConnectionException db connection exception. - * @throws SQLException sql exception - */ - int getCommentCountByParent(String uuid, int parentId) - throws CommentManagementException, DBConnectionException, SQLException; - - /** - * To get a count of comments by modification details. - * - * @param modifiedBy Username of the modified person. - * @param modifedAt time of the modification - * @return Count of the comments - * @throws CommentManagementException Exceptions of the comment management. - * @throws DBConnectionException db connection exception. - * @throws SQLException sql exception - */ - int getCommentCountByUser(String modifiedBy, Timestamp modifedAt) - throws CommentManagementException, DBConnectionException, SQLException; - - /** - * To get count of comments by application versions. - * - * @param appId id of the commented application. - * @param appReleaseId Id of the released version of the application. - * @return Count of the comments - * @throws CommentManagementException Exceptions of the comment management. - * @throws DBConnectionException db connection exception. - * @throws SQLException sql exception - */ - int getCommentCountByApp(int appId, int appReleaseId) - throws CommentManagementException, DBConnectionException, SQLException; - /** * To get count of comments by application details. * @@ -303,44 +97,22 @@ import java.util.List; * @param appName name of the commented application. * @param version version of the commented application. * @return Count of the comments - * @throws CommentManagementException Exceptions of the comment management. + * @throws ReviewManagementException Exceptions of the comment management. * @throws DBConnectionException db connection exception. * @throws SQLException sql exception */ int getCommentCountByApp(String appType, String appName, String version) - throws CommentManagementException, DBConnectionException, SQLException; + throws ReviewManagementException, DBConnectionException, SQLException; /** * To delete comment using comment id. * * @param commentId id of the comment - * @throws CommentManagementException Exceptions of the comment management. - * @throws DBConnectionException db connection exception. - * @throws SQLException sql exception - */ - void deleteComment(int commentId) throws CommentManagementException, DBConnectionException, SQLException; - - /** - * To delete comment using comment id. - * - * @param uuid uuid of the comment - * @throws CommentManagementException Exceptions of the comment management. + * @throws ReviewManagementException Exceptions of the comment management. * @throws DBConnectionException db connection exception. * @throws SQLException sql exception */ - void deleteComment(String uuid) throws CommentManagementException, DBConnectionException, SQLException; - - /** - * To delete comments using application details. - * - * @param appId id of the commented application. - * @param appReleaseID Id of the released version of the application. - * @throws CommentManagementException Exceptions of the comment management. - * @throws DBConnectionException db connection exception. - * @throws SQLException sql exception - */ - void deleteComments(int appId, int appReleaseID) - throws CommentManagementException, DBConnectionException, SQLException; + void deleteComment(int commentId) throws ReviewManagementException, DBConnectionException, SQLException; /** * To delete comments using application details. @@ -348,58 +120,9 @@ import java.util.List; * @param appType type of the commented application. * @param appName name of the commented application. * @param version version of the commented application. - * @throws CommentManagementException Exceptions of the comment management. - */ - void deleteComments(String appType, String appName, String version) throws CommentManagementException; - - /** - * To delete comments using users created and application details. - * - * @param appType type of the commented application. - * @param appName name of the commented application. - * @param version version of the commented application. - * @param createdBy Username of the created person. - * @throws CommentManagementException Exceptions of the comment management. - */ - void deleteComments(String appType, String appName, String version, String createdBy) - throws CommentManagementException; - - /** - * To delete comments by parent id of the comment. - * - * @param uuid uuid of the application - * @param parentId parent id of the parent comment. - * @throws CommentManagementException Exceptions of the comment management. - */ - void deleteComments(String uuid, int parentId) throws CommentManagementException; - - /** - * To add the star rating to the application. - * - * @param stars Star value - * @param uuid UUID of the application - * @return Star value - * @throws ApplicationManagementDAOException Application Management DAO Exception. - */ - int updateStars(int stars, String uuid) throws ApplicationManagementDAOException; - - /** - * To get the average star value of the application - * - * @param uuid uuid of the application - * @return Average of star values - * @throws ApplicationManagementDAOException Application Management DAO Exception. - */ - int getStars(String uuid) throws ApplicationManagementDAOException; - - /** - * To get number of rated users - * - * @param uuid uuid of the application - * @return Number of rated users - * @throws ApplicationManagementDAOException Application Management DAO Exception. + * @throws ReviewManagementException Exceptions of the comment management. */ - int getRatedUser(String uuid) throws ApplicationManagementDAOException; + void deleteComments(String appType, String appName, String version) throws ReviewManagementException; /** * To get comment count for pagination @@ -407,7 +130,7 @@ import java.util.List; * @param request * @param uuid * @return Review count - * @throws CommentManagementException + * @throws ReviewManagementException */ - int getCommentCount(PaginationRequest request, String uuid) throws CommentManagementException; + int getCommentCount(PaginationRequest request, String uuid) throws ReviewManagementException; } \ No newline at end of file diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/common/Util.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/common/Util.java index 697b30a9af8..a850452445f 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/common/Util.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/common/Util.java @@ -25,7 +25,7 @@ import org.wso2.carbon.device.application.mgt.common.*; import org.wso2.carbon.device.application.mgt.common.Application; import org.wso2.carbon.device.application.mgt.common.PaginationRequest; -import org.wso2.carbon.device.application.mgt.common.exception.CommentManagementException; +import org.wso2.carbon.device.application.mgt.common.exception.ReviewManagementException; import org.wso2.carbon.device.application.mgt.core.config.Configuration; import org.wso2.carbon.device.application.mgt.core.config.ConfigurationManager; @@ -202,14 +202,14 @@ public class Util { } public static PaginationRequest validateCommentListPageSize(PaginationRequest paginationRequest) throws - CommentManagementException { + ReviewManagementException { if (paginationRequest.getLimit() == 0) { Configuration commentManagementConfig = ConfigurationManager.getInstance().getConfiguration(); if (commentManagementConfig != null) { paginationRequest.setLimit( commentManagementConfig.getPaginationConfiguration().getCommentListPageSize()); } else { - throw new CommentManagementException( + throw new ReviewManagementException( "Application Management configuration has not initialized. Please check the application-mgt.xml file."); } } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/Comment/ReviewDAOImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/Comment/ReviewDAOImpl.java index 80b8c03b152..6afa5246cce 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/Comment/ReviewDAOImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/Comment/ReviewDAOImpl.java @@ -21,18 +21,17 @@ package org.wso2.carbon.device.application.mgt.core.dao.impl.Comment; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.wso2.carbon.device.application.mgt.common.ApplicationRelease; import org.wso2.carbon.device.application.mgt.common.Review; import org.wso2.carbon.device.application.mgt.common.PaginationRequest; -import org.wso2.carbon.device.application.mgt.common.exception.CommentManagementException; +import org.wso2.carbon.device.application.mgt.common.exception.ReviewManagementException; import org.wso2.carbon.device.application.mgt.common.exception.DBConnectionException; import org.wso2.carbon.device.application.mgt.core.dao.ReviewDAO; import org.wso2.carbon.device.application.mgt.core.dao.common.Util; import org.wso2.carbon.device.application.mgt.core.dao.impl.AbstractDAOImpl; import org.wso2.carbon.device.application.mgt.core.exception.ApplicationManagementDAOException; +import org.wso2.carbon.device.application.mgt.core.exception.ReviewManagementDAOException; import java.sql.SQLException; -import java.sql.Timestamp; import java.sql.ResultSet; import java.sql.Connection; import java.sql.PreparedStatement; @@ -50,30 +49,37 @@ public class ReviewDAOImpl extends AbstractDAOImpl implements ReviewDAO { @Override public boolean addReview(Review review, int appId, int appReleaseId, int tenantId) - throws CommentManagementException, DBConnectionException, SQLException { - + throws ReviewManagementDAOException { + //todo if (log.isDebugEnabled()) { log.debug("Request received in DAO Layer to add review for application release. Application id: " + appId + "Application Release id: " + appReleaseId); } - Connection conn = this.getDBConnection(); PreparedStatement statement = null; ResultSet rs = null; - sql = "INSERT INTO AP_APP_COMMENT (TENANT_ID, COMMENT_TEXT, CREATED_BY, PARENT_ID,AP_APP_RELEASE_ID," - + "AP_APP_ID) VALUES (?,?,?,?,(SELECT ID FROM AP_APP_RELEASE WHERE UUID= ?)," + sql = "INSERT INTO AP_APP_Review (TENANT_ID, COMMENT, PARENT_ID, USERNAME, AP_APP_RELEASE_ID, AP_APP_ID) " + + "VALUES (?,?,?,?,(SELECT ID FROM AP_APP_RELEASE WHERE UUID= ?)," + "(SELECT AP_APP_ID FROM AP_APP_RELEASE WHERE UUID=?));"; try { + Connection conn = this.getDBConnection(); statement = conn.prepareStatement(sql, new String[] { "id" }); statement.setInt(1, tenantId); statement.setString(2, review.getComment()); -// statement.setString(3, createdBy); -// statement.setInt(4, parentId); -// statement.setString(5, uuid); -// statement.setString(6, uuid); + statement.setInt(3, review.getParentId()); + statement.setString(4, review.getUsername()); + statement.setString(5,""); + statement.setString(6,""); statement.executeUpdate(); rs = statement.getGeneratedKeys(); return rs.next(); - } finally { + } + catch (DBConnectionException e) { + throw new ReviewManagementDAOException("Error occurred while obtaining the DB connection while " + + "adding review for application UUID: "+ "Tenant Id: " + tenantId, e); + }catch (SQLException e) { + throw new ReviewManagementDAOException("Error occurred while getting application list for the tenant" + + " " + tenantId + ". While executing " + sql, e); + } finally { Util.cleanupResources(statement, rs); } } @@ -90,8 +96,8 @@ public class ReviewDAOImpl extends AbstractDAOImpl implements ReviewDAO { PreparedStatement statement = null; ResultSet rs = null; Review review = null; - sql = "SELECT ID, COMMENT, REPLY_COMMENT, CREATED_AT, MODEFIED_AT, USERNAME, PARENT_ID, RATING FROM AP_APP_REVIEW " - + "WHERE AP_APP_ID = ? AND AP_APP_RELEASE_ID = ? AND USERNAME = ? AND TENANT_ID = ?;"; + sql = "SELECT ID, COMMENT, CREATED_AT, MODEFIED_AT, USERNAME, PARENT_ID, RATING FROM AP_APP_REVIEW WHERE " + + "AP_APP_ID = ? AND AP_APP_RELEASE_ID = ? AND USERNAME = ? AND TENANT_ID = ?;"; try { conn = this.getDBConnection(); statement = conn.prepareStatement(sql); @@ -105,7 +111,7 @@ public class ReviewDAOImpl extends AbstractDAOImpl implements ReviewDAO { review = new Review(); review.setId(rs.getInt("ID")); review.setComment(rs.getString("COMMENT")); - review.setReplyComment(rs.getString("REPLY_COMMENT")); + review.setParentId(rs.getInt("PARENT_ID")); review.setCreatedAt(rs.getTimestamp("CREATED_AT")); review.setModifiedAt(rs.getTimestamp("MODIFIED_AT")); review.setUsername(rs.getString("USERNAME")); @@ -117,45 +123,8 @@ public class ReviewDAOImpl extends AbstractDAOImpl implements ReviewDAO { } } - - @Override - public int addReview(int tenantId, Review review, String createdBy, String appType, String appName, - String version) throws CommentManagementException, DBConnectionException, SQLException { - - if (log.isDebugEnabled()) { - log.debug("Request received in DAO Layer to add to application (" + appName + ") and version (" + version - + ")"); - } - Connection conn = this.getDBConnection(); - PreparedStatement statement = null; - ResultSet rs; - int commentId = -1; - sql = "INSERT INTO AP_APP_COMMENT ( TENANT_ID,COMMENT_TEXT, CREATED_BY,AP_APP_RELEASE_ID,AP_APP_ID) " - + "VALUES (?,?,?,(SELECT ID FROM AP_APP_RELEASE WHERE VERSION =? AND (SELECT ID FROM AP_APP WHERE " - + "TYPE=? AND NAME=?)),(SELECT ID FROM AP_APP WHERE TYPE=? AND NAME=?));"; - try { - statement = conn.prepareStatement(sql, new String[] { "id" }); - statement.setInt(1, tenantId); - statement.setString(2, review.getComment()); - statement.setString(3, createdBy); - statement.setString(4, version); - statement.setString(5, appType); - statement.setString(6, appName); - statement.setString(7, appType); - statement.setString(8, appName); - statement.executeUpdate(); - rs = statement.getGeneratedKeys(); - if (rs.next()) { - commentId = rs.getInt(1); - } - } finally { - Util.cleanupResources(statement, null); - } - return commentId; - } - @Override - public boolean updateReview(Review review, int reviewId, int tenantId) throws CommentManagementException, DBConnectionException, SQLException { + public boolean updateReview(Review review, int reviewId, int tenantId) throws ReviewManagementException, DBConnectionException, SQLException { if (log.isDebugEnabled()) { log.debug("Request received in DAO Layer to update the comment with ID (" + reviewId + ")"); @@ -180,33 +149,7 @@ public class ReviewDAOImpl extends AbstractDAOImpl implements ReviewDAO { } @Override - public Review updateReview(String uuid, int commentId, String updatedComment, String modifiedBy, - Timestamp modifiedAt) throws CommentManagementException, DBConnectionException, SQLException { - - if (log.isDebugEnabled()) { - log.debug("Request received in DAO Layer to update the comment with application (" + uuid + ") and " - + "comment id ( " + commentId + ")"); - } - Connection connection; - PreparedStatement statement = null; - ResultSet rs = null; - sql = "UPDATE AP_APP_COMMENT SET COMMENT_TEXT=?,MODEFIED_BY=? WHERE ID=?; "; - try { - connection = this.getDBConnection(); - statement = connection.prepareStatement(sql); - statement.setString(1, updatedComment); - statement.setString(2, modifiedBy); - statement.setInt(3, commentId); - statement.executeUpdate(); - rs = statement.getResultSet(); - } finally { - Util.cleanupResources(statement, rs); - } - return getComment(commentId); - } - - @Override - public Review getComment(int commentId) throws CommentManagementException { + public Review getComment(int commentId) throws ReviewManagementException { if (log.isDebugEnabled()) { log.debug("Getting review with the review id(" + commentId + ") from the database"); @@ -232,7 +175,7 @@ public class ReviewDAOImpl extends AbstractDAOImpl implements ReviewDAO { return review; } } catch (SQLException e) { - throw new CommentManagementException( + throw new ReviewManagementException( "SQL Error occurred while retrieving information of the review " + commentId, e); } catch (DBConnectionException e) { log.error("DB Connection Exception occurred while retrieving information of the review " + commentId, e); @@ -243,46 +186,8 @@ public class ReviewDAOImpl extends AbstractDAOImpl implements ReviewDAO { } @Override - public List getComment(String uuid) throws CommentManagementException { - - if (log.isDebugEnabled()) { - log.debug("Getting comment with the application release(" + uuid + ") from the database"); - } - Connection conn; - PreparedStatement statement = null; - ResultSet rs; - List reviews = new ArrayList<>(); - try { - conn = this.getDBConnection(); - sql = "SELECT COMMENT_TEXT FROM AP_APP_COMMENT WHERE (SELECT ID FROM AP_APP_RELEASE where UUID=?)AND " - + "(SELECT AP_APP_ID FROM AP_APP_RELEASE where UUID=?);"; - statement = conn.prepareStatement(sql); - statement.setString(1, uuid); - statement.setString(2, uuid); - rs = statement.executeQuery(); - while (rs.next()) { - Review review = new Review(); - review.setId(rs.getInt("ID")); -// review.setTenantId(rs.getInt("TENANT_ID")); - review.setComment(rs.getString("COMMENT_TEXT")); - review.setCreatedAt(rs.getTimestamp("CREATED_AT")); - review.setUsername(rs.getString("CREATED_BY")); - review.setModifiedAt(rs.getTimestamp("MODEFIED_AT")); - reviews.add(review); - } - } catch (DBConnectionException e) { - log.error("DB Connection Exception occurred while retrieving reviews", e); - } catch (SQLException e) { - throw new CommentManagementException("SQL Error occurred while retrieving reviews", e); - } finally { - Util.cleanupResources(statement, null); - } - return reviews; - } - - @Override - public List getAllComments(String uuid, PaginationRequest request) - throws SQLException, DBConnectionException { + public List getAllReviews(String uuid, PaginationRequest request, int tenantId) + throws ReviewManagementDAOException { if (log.isDebugEnabled()) { log.debug("Getting comment of the application release (" + uuid + ") from the database"); @@ -293,16 +198,17 @@ public class ReviewDAOImpl extends AbstractDAOImpl implements ReviewDAO { List reviews = new ArrayList<>(); try { conn = this.getDBConnection(); - sql = "SELECT AP_APP_COMMENT.ID AS ID, AP_APP_COMMENT.COMMENT_TEXT AS " - + "COMMENT_TEXT, AP_APP_COMMENT.CREATED_BY AS CREATED_BY, AP_APP_COMMENT.MODIFIED_BY AS " - + "MODIFIED_BY, AP_APP_COMMENT.PARENT_ID AS PARENT_ID FROM AP_APP_COMMENT, AP_APP_RELEASE WHERE " + sql = "SELECT AP_APP_COMMENT.ID AS ID, AP_APP_COMMENT.COMMENT_TEXT AS COMMENT_TEXT, " + + "AP_APP_COMMENT.CREATED_BY AS CREATED_BY, AP_APP_COMMENT.MODIFIED_BY AS MODIFIED_BY, " + + "AP_APP_COMMENT.PARENT_ID AS PARENT_ID FROM AP_APP_COMMENT, AP_APP_RELEASE WHERE " + "AP_APP_COMMENT.AP_APP_RELEASE_ID=AP_APP_RELEASE.ID AND AP_APP_RELEASE.UUID =? AND " + "AP_APP_COMMENT.TENANT_ID = ? AND AP_APP_COMMENT.TENANT_ID = AP_APP_RELEASE.TENANT_ID " + "LIMIT ? OFFSET ?;"; statement = conn.prepareStatement(sql); statement.setString(1, uuid); - statement.setInt(2, request.getLimit()); - statement.setInt(3, request.getOffSet()); + statement.setInt(2, tenantId); + statement.setInt(3, request.getLimit()); + statement.setInt(4, request.getOffSet()); rs = statement.executeQuery(); while (rs.next()) { Review review = new Review(); @@ -311,7 +217,12 @@ public class ReviewDAOImpl extends AbstractDAOImpl implements ReviewDAO { review.setUsername(rs.getString("CREATED_BY")); reviews.add(review); } - } finally { + } catch (DBConnectionException e) { + throw new ReviewManagementDAOException( + "Error occurred while obtaining the DB connection when verifying application existence", e); + } catch (SQLException e) { + throw new ReviewManagementDAOException("Error occurred while adding unrestricted roles", e); + }finally { Util.cleanupResources(statement, rs); } return reviews; @@ -348,7 +259,7 @@ public class ReviewDAOImpl extends AbstractDAOImpl implements ReviewDAO { } @Override - public int getCommentCount(PaginationRequest request, String uuid) throws CommentManagementException { + public int getCommentCount(PaginationRequest request, String uuid) throws ReviewManagementException { int commentCount = 0; Connection conn; @@ -372,7 +283,7 @@ public class ReviewDAOImpl extends AbstractDAOImpl implements ReviewDAO { } } } catch (SQLException e) { - throw new CommentManagementException("SQL Error occurred while retrieving count of comments", e); + throw new ReviewManagementException("SQL Error occurred while retrieving count of comments", e); } catch (DBConnectionException e) { log.error("DB Connection Exception occurred while retrieving count of comments", e); } finally { @@ -382,668 +293,74 @@ public class ReviewDAOImpl extends AbstractDAOImpl implements ReviewDAO { } @Override - public List getComments(int appReleasedId, int appId) throws CommentManagementException { - - if (log.isDebugEnabled()) { - log.debug("Getting reviews with the application release id(" + appReleasedId + ") and " + "application id(" - + appId + ") from the database"); - } - Connection conn; - PreparedStatement statement = null; - ResultSet rs; - List reviews = new ArrayList<>(); - try { - conn = this.getDBConnection(); - sql = "SELECT COMMENT_TEXT FROM AP_APP_COMMENT WHERE AP_APP_RELEASE_ID=? AND AP_APP_ID=?;"; - statement = conn.prepareStatement(sql); - statement.setInt(1, appReleasedId); - statement.setInt(2, appId); - rs = statement.executeQuery(); - while (rs.next()) { - Review review = new Review(); - review.setId(rs.getInt("ID")); -// review.setTenantId(rs.getInt("TENANT_ID")); - review.setComment(rs.getString("COMMENT_TEXT")); - review.setCreatedAt(rs.getTimestamp("CREATED_AT")); - review.setUsername(rs.getString("CREATED_BY")); - review.setModifiedAt(rs.getTimestamp("MODEFIED_AT")); - reviews.add(review); - } - } catch (DBConnectionException e) { - log.error("DB Connection Exception occurred while retrieving information of reviews", e); - } catch (SQLException e) { - throw new CommentManagementException("SQL Error occurred while retrieving information of reviews", e); - } finally { - Util.cleanupResources(statement, null); - } - return reviews; - } - - @Override - public List getComments(String appType, String appName, String version) - throws CommentManagementException, DBConnectionException, SQLException { + public int getCommentCountByApp(String appType, String appName, String version) + throws ReviewManagementException, DBConnectionException, SQLException { - if (log.isDebugEnabled()) { - log.debug("Getting reviews with the application name(" + appName + "),application type(" + appType + ") " - + "and application version (" + version + ") from the database"); - } Connection conn; PreparedStatement statement = null; - ResultSet rs; - List reviews = new ArrayList<>(); + int commentCount = 0; try { conn = this.getDBConnection(); - sql = "SELECT COMMENT_TEXT,PARENT_ID,TENANT_ID FROM AP_APP_COMMENT C ," - + "(SELECT ID AS RELEASE_ID, AP_APP_ID AS RELEASE_AP_APP_ID FROM AP_APP_RELEASE R WHERE VERSION=?) R," - + "(SELECT ID AS APP_ID FROM AP_APP P WHERE NAME=? AND TYPE=?)P" - + " WHERE AP_APP_RELEASE_ID=RELEASE_ID AND RELEASE_AP_APP_ID=APP_ID AND AP_APP_ID=RELEASE_AP_APP_ID" - + "ORDER BY CREATED_AT DESC;"; + sql = "SELECT COUNT(ID) AS COMMENT_COUNT FROM AP_APP_COMMENT C, " + + "(SELECT ID AS RELEASE_ID, AP_APP_ID AS RELEASE_AP_APP_ID FROM AP_APP_RELEASE R WHERE VERSION=? )R," + + "(SELECT ID AS APP_ID FROM AP_APP P WHERE NAME=? and TYPE=?)P " + + "WHERE AP_APP_RELEASE_ID=RELEASE_ID AND RELEASE_AP_APP_ID=APP_ID AND AP_APP_ID=RELEASE_AP_APP_ID;"; statement = conn.prepareStatement(sql); statement.setString(1, version); statement.setString(2, appName); statement.setString(3, appType); - rs = statement.executeQuery(); - while (rs.next()) { - Review review = new Review(); - review.setId(rs.getInt("ID")); -// review.setTenantId(rs.getInt("TENANT_ID")); - review.setComment(rs.getString("COMMENT_TEXT")); - review.setCreatedAt(rs.getTimestamp("CREATED_AT")); - review.setUsername(rs.getString("CREATED_BY")); - review.setModifiedAt(rs.getTimestamp("MODEFIED_AT")); - reviews.add(review); - } - } finally { - Util.cleanupResources(statement, null); - } - return reviews; - } - - @Override - public List getComments(int tenantId) - throws CommentManagementException, DBConnectionException, SQLException { - - if (log.isDebugEnabled()) { - log.debug("Getting reviews with the tenant_id(" + tenantId + ") from the database"); - } - Connection conn; - PreparedStatement statement = null; - ResultSet rs; - List reviews = new ArrayList<>(); - try { - conn = this.getDBConnection(); - sql = "SELECT COMMENT_TEXT FROM AP_APP_COMMENT WHERE TENANT_ID='?';"; - statement = conn.prepareStatement(sql); - statement.setInt(1, tenantId); - rs = statement.executeQuery(); - while (rs.next()) { - Review review = new Review(); - review.setId(rs.getInt("ID")); -// review.setTenantId(rs.getInt("TENANT_ID")); - review.setComment(rs.getString("COMMENT_TEXT")); - review.setCreatedAt(rs.getTimestamp("CREATED_AT")); - review.setUsername(rs.getString("CREATED_BY")); - review.setModifiedAt(rs.getTimestamp("MODEFIED_AT")); - reviews.add(review); - } - } finally { - Util.cleanupResources(statement, null); - } - return reviews; - } - - @Override - public List getCommentsByUser(String createdBy) - throws CommentManagementException, DBConnectionException, SQLException { - - if (log.isDebugEnabled()) { - log.debug("Getting reviews with the created by(" + createdBy + ") from the database"); - } - Connection conn; - PreparedStatement statement = null; - ResultSet rs; - List reviews = new ArrayList<>(); - try { - conn = this.getDBConnection(); - sql = "SELECT COMMENT_TEXT ,PARENT_ID,TENANT_ID,CREATED_AT FROM AP_APP_COMMENT WHERE CREATED_BY= ?" - + " ORDER BY CREATED_AT DESC;"; - statement = conn.prepareStatement(sql); - statement.setString(1, createdBy); - rs = statement.executeQuery(); - while (rs.next()) { - Review review = new Review(); - review.setId(rs.getInt("ID")); -// review.setTenantId(rs.getInt("TENANT_ID")); - review.setComment(rs.getString("COMMENT_TEXT")); - review.setCreatedAt(rs.getTimestamp("CREATED_AT")); - review.setUsername(rs.getString("CREATED_BY")); - review.setModifiedAt(rs.getTimestamp("MODEFIED_AT")); - reviews.add(review); + ResultSet rs = statement.executeQuery(); + if (rs.next()) { + commentCount = rs.getInt("COMMENT_COUNT"); } } finally { Util.cleanupResources(statement, null); } - return reviews; + return commentCount; } @Override - public List getCommentsByUser(String createdBy, Timestamp createdAt) - throws CommentManagementException, DBConnectionException, SQLException { + public void deleteComment(int commentId) + throws ReviewManagementException, DBConnectionException, SQLException { - if (log.isDebugEnabled()) { - log.debug( - "Getting reviews with the created by(" + createdBy + ") at (" + createdAt + ") from the database"); - } Connection conn; PreparedStatement statement = null; - ResultSet rs; - List reviews = new ArrayList<>(); try { conn = this.getDBConnection(); - sql = "SELECT COMMENT_TEXT,PARENT_ID,TENANT_ID FROM AP_APP_COMMENT WHERE CREATED_BY=?" - + "AND CREATED_AT= ? ORDER BY CREATED_AT DESC;"; + sql = "DELETE FROM AP_APP_COMMENT WHERE ID=?;"; statement = conn.prepareStatement(sql); - statement.setString(1, createdBy); - statement.setTimestamp(2, createdAt); - rs = statement.executeQuery(); - while (rs.next()) { - Review review = new Review(); - review.setId(rs.getInt("ID")); -// review.setTenantId(rs.getInt("TENANT_ID")); - review.setComment(rs.getString("COMMENT_TEXT")); - review.setCreatedAt(rs.getTimestamp("CREATED_AT")); - review.setUsername(rs.getString("CREATED_BY")); - review.setModifiedAt(rs.getTimestamp("MODEFIED_AT")); - reviews.add(review); - } + statement.setInt(1, commentId); + statement.executeUpdate(); } finally { Util.cleanupResources(statement, null); } - return reviews; } @Override - public List getCommentsByModifiedUser(String modifiedBy) - throws CommentManagementException, DBConnectionException, SQLException { + public void deleteComments(String appType, String appName, String version) + throws ReviewManagementException { - if (log.isDebugEnabled()) { - log.debug("Getting reviews with the modified by(" + modifiedBy + ") from the database"); - } Connection conn; PreparedStatement statement = null; - ResultSet rs; - List reviews = new ArrayList<>(); try { conn = this.getDBConnection(); - sql = "SELECT COMMENT_TEXT,PARENT_ID,TENANT_ID,CREATED_AT,MODEFIED_AT FROM AP_APP_COMMENT " - + "WHERE MODEFIED_BY= ? ORDER BY CREATED_AT DESC;"; + sql = "DELETE FROM AP_APP_COMMENT WHERE " + + "(SELECT AP_APP_RELEASE_ID FROM AP_APP_RELEASE WHERE VERSION=? AND " + + "(SELECT AP_APP_ID FROM AP_APP WHERE NAME=? AND TYPE=?)) AND " + + "(SELECT AP_APP_ID FROM AP_APP AND NAME=? AND TYPE=?);"; statement = conn.prepareStatement(sql); - statement.setString(1, modifiedBy); - rs = statement.executeQuery(); - while (rs.next()) { - Review review = new Review(); - review.setId(rs.getInt("ID")); -// review.setTenantId(rs.getInt("TENANT_ID")); - review.setComment(rs.getString("COMMENT_TEXT")); - review.setCreatedAt(rs.getTimestamp("CREATED_AT")); - review.setUsername(rs.getString("CREATED_BY")); - review.setModifiedAt(rs.getTimestamp("MODEFIED_AT")); - reviews.add(review); - } + statement.setString(1, version); + statement.setString(2, appName); + statement.setString(3, appType); + statement.setString(4, appName); + statement.setString(5, appType); + statement.executeUpdate(); + } catch (DBConnectionException e) { + log.error("DB Connection Exception occurred while deleting comments", e); + } catch (SQLException e) { + throw new ReviewManagementException("SQL Error occurred while deleting comments", e); } finally { Util.cleanupResources(statement, null); } - return reviews; - } - - @Override - public List getCommentsByModifiedUser(String modifiedBy, Timestamp modifiedAt) - throws CommentManagementException, DBConnectionException, SQLException { - - if (log.isDebugEnabled()) { - log.debug("Getting reviews with the modified by(" + modifiedBy + ") at (" + modifiedAt + ") from the " - + "database"); - } - Connection conn; - PreparedStatement statement = null; - ResultSet rs; - List reviews = new ArrayList<>(); - try { - conn = this.getDBConnection(); - sql = "SELECT COMMENT_TEXT,PARENT_ID,TENANT_ID,CREATED_AT FROM AP_APP_COMMENT WHERE MODEFIED_BY= ?," - + "MODEFIED_AT=? ORDER BY CREATED_AT DESC;"; - statement = conn.prepareStatement(sql); - statement.setString(1, modifiedBy); - statement.setTimestamp(2, modifiedAt); - rs = statement.executeQuery(); - while (rs.next()) { - Review review = new Review(); - review.setId(rs.getInt("ID")); -// review.setTenantId(rs.getInt("TENANT_ID")); - review.setComment(rs.getString("COMMENT_TEXT")); - review.setCreatedAt(rs.getTimestamp("CREATED_AT")); - review.setUsername(rs.getString("CREATED_BY")); - review.setModifiedAt(rs.getTimestamp("MODEFIED_AT")); - reviews.add(review); - } - } finally { - Util.cleanupResources(statement, null); - } - return reviews; - } - - @Override - public List getComments(String appType, String appName, String version, int parentId) - throws CommentManagementException, DBConnectionException, SQLException { - - if (log.isDebugEnabled()) { - log.debug( - "Getting reviews with the application name(" + appName + "),application type(" + appType + ") and" - + "application version (" + version + ") from the database"); - } - Connection conn; - PreparedStatement statement = null; - ResultSet rs; - List reviews = new ArrayList<>(); - try { - conn = this.getDBConnection(); - sql = "SELECT COMMENT_TEXT,TENANT_ID FROM AP_APP_COMMENT C ," - + "(SELECT ID AS RELEASE_ID, AP_APP_ID AS RELEASE_AP_APP_ID FROM AP_APP_RELEASE R WHERE VERSION=? ) " - + "R,(SELECT ID AS APP_ID FROM AP_APP P WHERE NAME=? AND TYPE=?)P " - + "WHERE PARENT_ID=? AND AP_APP_RELEASE_ID=RELEASE_ID AND RELEASE_AP_APP_ID=APP_ID AND " - + "AP_APP_ID=RELEASE_AP_APP_ID ORDER BY CREATED_AT DESC;"; - statement = conn.prepareStatement(sql); - statement.setString(1, version); - statement.setString(2, appName); - statement.setString(3, appType); - statement.setInt(4, parentId); - rs = statement.executeQuery(); - while (rs.next()) { - Review review = new Review(); - review.setId(rs.getInt("ID")); -// review.setTenantId(rs.getInt("TENANT_ID")); - review.setComment(rs.getString("COMMENT_TEXT")); - review.setCreatedAt(rs.getTimestamp("CREATED_AT")); - review.setUsername(rs.getString("CREATED_BY")); - review.setModifiedAt(rs.getTimestamp("MODEFIED_AT")); - reviews.add(review); - } - } finally { - Util.cleanupResources(statement, null); - } - return reviews; - } - - @Override - public int getCommentCount(String uuid) - throws CommentManagementException, DBConnectionException, SQLException { - - Connection conn; - PreparedStatement statement = null; - int commentCount = 0; - try { - conn = this.getDBConnection(); - sql = "SELECT COUNT(AP_APP_COMMENT.ID) FROM AP_APP_COMMENT,AP_APP_RELEASE WHERE " - + "AP_APP_COMMENT.AP_APP_RELEASE_ID=AP_APP_RELEASE.ID AND AP_APP_COMMENT.AP_APP_ID=" - + "AP_APP_RELEASE.AP_APP_ID AND AP_APP_RELEASE.UUID=?;"; - statement = conn.prepareStatement(sql); - statement.setString(1, uuid); - ResultSet rs = statement.executeQuery(); - if (rs.next()) { - commentCount = rs.getInt("ID"); - } - } finally { - Util.cleanupResources(statement, null); - return commentCount; - } - } - - @Override - public int getCommentCountByUser(String createdBy) - throws CommentManagementException, DBConnectionException, SQLException { - - Connection conn; - PreparedStatement statement = null; - int commentCount = 0; - try { - conn = this.getDBConnection(); - sql = "SELECT COUNT(ID) FROM AP_APP_COMMENT WHERE CREATED_BY= ?;"; - statement = conn.prepareStatement(sql); - statement.setString(1, createdBy); - ResultSet rs = statement.executeQuery(); - if (rs.next()) { - commentCount = rs.getInt("COMMENT_COUNT"); - } - } finally { - Util.cleanupResources(statement, null); - } - return commentCount; - } - - @Override - public int getCommentCountByUser(String modifiedBy, Timestamp modifedAt) - throws CommentManagementException, DBConnectionException, SQLException { - - Connection conn; - PreparedStatement statement = null; - int commentCount = 0; - try { - conn = this.getDBConnection(); - sql = "SELECT COUNT(ID) FROM AP_APP_COMMENT WHERE MODEFIED_BY= ? AND MODEFIED_AT=?;"; - statement = conn.prepareStatement(sql); - statement.setString(1, modifiedBy); - statement.setTimestamp(2, modifedAt); - ResultSet rs = statement.executeQuery(); - if (rs.next()) { - commentCount = rs.getInt("COMMENT_COUNT"); - } - } finally { - Util.cleanupResources(statement, null); - } - return commentCount; - } - - @Override - public int getCommentCountByApp(int appId, int appReleaseId) - throws CommentManagementException, DBConnectionException, SQLException { - - Connection conn; - PreparedStatement statement = null; - int commentCount = 0; - try { - conn = this.getDBConnection(); - sql = "SELECT COUNT(ID) FROM AP_APP_COMMENT WHERE AP_APP_RELEASE_ID=? AND AP_APP_ID=?;"; - statement = conn.prepareStatement(sql); - statement.setInt(1, appReleaseId); - statement.setInt(2, appId); - ResultSet rs = statement.executeQuery(); - if (rs.next()) { - commentCount = rs.getInt("COMMENT_COUNT"); - } - } finally { - Util.cleanupResources(statement, null); - } - return commentCount; - } - - @Override - public int getCommentCountByApp(String appType, String appName, String version) - throws CommentManagementException, DBConnectionException, SQLException { - - Connection conn; - PreparedStatement statement = null; - int commentCount = 0; - try { - conn = this.getDBConnection(); - sql = "SELECT COUNT(ID) AS COMMENT_COUNT FROM AP_APP_COMMENT C, " - + "(SELECT ID AS RELEASE_ID, AP_APP_ID AS RELEASE_AP_APP_ID FROM AP_APP_RELEASE R WHERE VERSION=? )R," - + "(SELECT ID AS APP_ID FROM AP_APP P WHERE NAME=? and TYPE=?)P " - + "WHERE AP_APP_RELEASE_ID=RELEASE_ID AND RELEASE_AP_APP_ID=APP_ID AND AP_APP_ID=RELEASE_AP_APP_ID;"; - statement = conn.prepareStatement(sql); - statement.setString(1, version); - statement.setString(2, appName); - statement.setString(3, appType); - ResultSet rs = statement.executeQuery(); - if (rs.next()) { - commentCount = rs.getInt("COMMENT_COUNT"); - } - } finally { - Util.cleanupResources(statement, null); - } - return commentCount; - } - - public int getCommentCountByParent(String uuid, int parentId) - throws CommentManagementException, DBConnectionException, SQLException { - - Connection conn; - PreparedStatement statement = null; - int commentCount = 0; - try { - conn = this.getDBConnection(); - sql = "SELECT COUNT(AP_APP_COMMENT.ID) FROM AP_APP_COMMENT,AP_APP_RELEASE WHERE " - + "AP_APP_COMMENT.AP_APP_RELEASE_ID=AP_APP_RELEASE.ID AND " - + "AP_APP_COMMENT.AP_APP_ID=AP_APP_RELEASE.AP_APP_ID and AP_APP_RELEASE.UUID=? and PARENT_ID=?;"; - - statement = conn.prepareStatement(sql); - statement.setString(1, uuid); - statement.setInt(2, parentId); - ResultSet rs = statement.executeQuery(); - if (rs.next()) { - commentCount = rs.getInt("COMMENT_COUNT"); - } - } finally { - Util.cleanupResources(statement, null); - } - return commentCount; - } - - @Override - public void deleteComment(int commentId) - throws CommentManagementException, DBConnectionException, SQLException { - - Connection conn; - PreparedStatement statement = null; - try { - conn = this.getDBConnection(); - sql = "DELETE FROM AP_APP_COMMENT WHERE ID=?;"; - statement = conn.prepareStatement(sql); - statement.setInt(1, commentId); - statement.executeUpdate(); - } finally { - Util.cleanupResources(statement, null); - } - } - - public void deleteComment(String uuid) throws CommentManagementException, DBConnectionException, SQLException { - - Connection conn; - PreparedStatement statement = null; - try { - conn = this.getDBConnection(); - sql = "DELETE FROM AP_APP_COMMENT WHERE " - + "(SELECT ID FROM AP_APP_RELEASE WHERE UUID=?)AND (SELECT AP_APP_ID FROM AP_APP_RELEASE " - + "WHERE UUID=?);"; - statement = conn.prepareStatement(sql); - statement.setString(1, uuid); - statement.setString(2, uuid); - statement.executeUpdate(); - } finally { - Util.cleanupResources(statement, null); - } - } - - @Override - public void deleteComments(int appId, int appReleaseID) - throws CommentManagementException, DBConnectionException, SQLException { - - Connection conn; - PreparedStatement statement = null; - try { - conn = this.getDBConnection(); - sql = "DELETE FROM AP_APP_COMMENT WHERE AP_APP_RELEASE_ID=? and AP_APP_ID=?;"; - statement = conn.prepareStatement(sql); - statement.setInt(1, appReleaseID); - statement.setInt(2, appId); - statement.executeUpdate(); - } finally { - Util.cleanupResources(statement, null); - } - } - - @Override - public void deleteComments(String appType, String appName, String version) - throws CommentManagementException { - - Connection conn; - PreparedStatement statement = null; - try { - conn = this.getDBConnection(); - sql = "DELETE FROM AP_APP_COMMENT WHERE " - + "(SELECT AP_APP_RELEASE_ID FROM AP_APP_RELEASE WHERE VERSION=? AND " - + "(SELECT AP_APP_ID FROM AP_APP WHERE NAME=? AND TYPE=?)) AND " - + "(SELECT AP_APP_ID FROM AP_APP AND NAME=? AND TYPE=?);"; - statement = conn.prepareStatement(sql); - statement.setString(1, version); - statement.setString(2, appName); - statement.setString(3, appType); - statement.setString(4, appName); - statement.setString(5, appType); - statement.executeUpdate(); - } catch (DBConnectionException e) { - log.error("DB Connection Exception occurred while deleting comments", e); - } catch (SQLException e) { - throw new CommentManagementException("SQL Error occurred while deleting comments", e); - } finally { - Util.cleanupResources(statement, null); - } - } - - @Override - public void deleteComments(String appType, String appName, String version, String createdBy) - throws CommentManagementException { - - Connection conn; - PreparedStatement statement = null; - try { - conn = this.getDBConnection(); - sql = "DELETE FROM AP_APP_COMMENT WHERE " - + "(SELECT AP_APP_RELEASE_ID FROM AP_APP_RELEASE WHERE VERSION=? AND " - + "(SELECT AP_APP_ID FROM AP_APP WHERE NAME=? AND TYPE=?)) AND " - + "(SELECT AP_APP_ID FROM AP_APP WHERE NAME=? and TYPE=?) AND CREATED_BY=?;"; - statement = conn.prepareStatement(sql); - statement.setString(1, version); - statement.setString(2, appName); - statement.setString(3, appType); - statement.setString(4, appName); - statement.setString(5, appType); - statement.setString(6, createdBy); - statement.executeUpdate(); - } catch (DBConnectionException e) { - log.error("DB Connection Exception occurred while deleting comments ", e); - } catch (SQLException e) { - throw new CommentManagementException("Error occurred while deleting comments", e); - } finally { - Util.cleanupResources(statement, null); - } - } - - @Override - public void deleteComments(String uuid, int parentId) throws CommentManagementException { - - Connection conn; - PreparedStatement statement = null; - try { - conn = this.getDBConnection(); - sql = "DELETE FROM AP_APP_COMMENT WHERE AP_APP_RELEASE_ID=(SELECT ID FROM AP_APP_RELEASE WHERE UUID=?) " - + "AND AP_APP_ID=(SELECT AP_APP_ID FROM AP_APP_RELEASE where UUID=?)AND PARENT_ID=?;"; - statement = conn.prepareStatement(sql); - statement.setString(1, uuid); - statement.setString(2, uuid); - statement.setInt(3, parentId); - statement.executeUpdate(); - } catch (DBConnectionException e) { - log.error("DB Connection Exception occurred while deleting comments.", e); - } catch (SQLException e) { - throw new CommentManagementException("Error occurred while deleting comments", e); - } finally { - Util.cleanupResources(statement, null); - } - } - - @Override - public int updateStars(int stars, String uuid) throws ApplicationManagementDAOException { - - Connection connection; - PreparedStatement statement = null; - ResultSet resultSet = null; - try { - connection = this.getDBConnection(); - sql = "UPDATE AP_APP_RELEASE SET STARS=?, NO_OF_RATED_USERS=(NO_OF_RATED_USERS+1) WHERE UUID=?;"; - statement = connection.prepareStatement(sql); - statement.setInt(1, stars); - statement.setString(2, uuid); - resultSet = statement.executeQuery(sql); - if (resultSet != null) { - resultSet.getInt("STARS"); - } - int numORows = resultSet.getRow(); - if (resultSet.next()) { - ApplicationRelease applicationRelease = new ApplicationRelease(); - applicationRelease.setRating(resultSet.getDouble("RATING")); - Util.cleanupResources(statement, resultSet); - } - } catch (SQLException e) { - throw new ApplicationManagementDAOException( - "SQL Exception while trying to add stars to an application (UUID : " + uuid + "), by executing " - + "the query " + e); - } catch (DBConnectionException e) { - log.error("DB Connection Exception while trying to add stars to an application (UUID : " + uuid + "), ", - e); - } finally { - Util.cleanupResources(statement, null); - return getStars(uuid); - } - } - - @Override - public int getStars(String uuid) throws ApplicationManagementDAOException { - - Connection connection; - PreparedStatement statement = null; - ResultSet resultSet = null; - int Stars = 0; - try { - connection = this.getDBConnection(); - sql = "SELECT STARS FROM AP_APP_RELEASE WHERE UUID=?;"; - statement = connection.prepareStatement(sql); - statement.setString(1, uuid); - resultSet = statement.executeQuery(); - if (resultSet.next()) { - ApplicationRelease applicationRelease = new ApplicationRelease(); - Stars = resultSet.getInt("STARS"); - return Stars; - } - } catch (SQLException e) { - throw new ApplicationManagementDAOException( - "SQL Exception while trying to get stars from an application (UUID : " + uuid + "), by executing " - + "the query " + e); - } catch (DBConnectionException e) { - log.error( - "DB Connection Exception while trying to get stars from an application (UUID : " + uuid + ")," + e); - } finally { - Util.cleanupResources(statement, resultSet); - } - return Stars; - } - - @Override - public int getRatedUser(String uuid) throws ApplicationManagementDAOException { - - Connection connection; - PreparedStatement statement = null; - ResultSet resultSet = null; - int ratedUsers = 0; - try { - connection = this.getDBConnection(); - sql = "SELECT NO_OF_RATED_USERS FROM AP_APP_RELEASE WHERE UUID=?;"; - statement = connection.prepareStatement(sql); - statement.setString(1, uuid); - resultSet = statement.executeQuery(); - if (resultSet.next()) { - ApplicationRelease applicationRelease = new ApplicationRelease(); - ratedUsers = resultSet.getInt("NO_OF_RATED_USERS"); - applicationRelease.setRatedUsers(ratedUsers); - Util.cleanupResources(statement, resultSet); - return ratedUsers; - } - } catch (SQLException e) { - log.error("SQL Exception occurs.", e); - } catch (DBConnectionException e) { - log.error("DB Connection Exception occurs.", e); - } finally { - Util.cleanupResources(statement, resultSet); - } - return ratedUsers; } } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/exception/ReviewManagementDAOException.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/exception/ReviewManagementDAOException.java new file mode 100644 index 00000000000..f32f08b3244 --- /dev/null +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/exception/ReviewManagementDAOException.java @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +package org.wso2.carbon.device.application.mgt.core.exception; + +import org.wso2.carbon.device.application.mgt.common.exception.ReviewManagementException; + +/** + * Exception thrown during the Review Management DAO operations. + */ +public class ReviewManagementDAOException extends ReviewManagementException { + + public ReviewManagementDAOException(String message, Throwable throwable) { + super(message, throwable); + } + + public ReviewManagementDAOException(String message) { + super(message, new Exception()); + } +} diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ReviewManagerImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ReviewManagerImpl.java index f742fd5c57b..85e4ec184b8 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ReviewManagerImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ReviewManagerImpl.java @@ -1,20 +1,20 @@ /* -* Copyright (c) 2018, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. -* WSO2 Inc. licenses this file to you under the Apache License, -* Version 2.0 (the "License"); you may not use this file except -* in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, -* software distributed under the License is distributed on an -* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -* KIND, either express or implied. See the License for the -* specific language governing permissions and limitations -* under the License. -* -*/ + * Copyright (c) 2018, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ package org.wso2.carbon.device.application.mgt.core.impl; import org.apache.commons.logging.Log; @@ -25,7 +25,7 @@ import org.wso2.carbon.device.application.mgt.common.Rating; import org.wso2.carbon.device.application.mgt.common.Review; import org.wso2.carbon.device.application.mgt.common.PaginationRequest; import org.wso2.carbon.device.application.mgt.common.PaginationResult; -import org.wso2.carbon.device.application.mgt.common.exception.CommentManagementException; +import org.wso2.carbon.device.application.mgt.common.exception.ReviewManagementException; import org.wso2.carbon.device.application.mgt.common.exception.DBConnectionException; import org.wso2.carbon.device.application.mgt.common.exception.TransactionManagementException; import org.wso2.carbon.device.application.mgt.common.services.*; @@ -34,6 +34,7 @@ import org.wso2.carbon.device.application.mgt.core.dao.ReviewDAO; import org.wso2.carbon.device.application.mgt.core.dao.common.ApplicationManagementDAOFactory; import org.wso2.carbon.device.application.mgt.core.dao.common.Util; import org.wso2.carbon.device.application.mgt.core.exception.ApplicationManagementDAOException; +import org.wso2.carbon.device.application.mgt.core.exception.ReviewManagementDAOException; import org.wso2.carbon.device.application.mgt.core.internal.DataHolder; import org.wso2.carbon.device.application.mgt.core.util.ConnectionManagerUtil; import org.wso2.carbon.user.api.UserRealm; @@ -41,6 +42,7 @@ import org.wso2.carbon.user.api.UserStoreException; import org.wso2.carbon.utils.multitenancy.MultitenantUtils; import java.sql.SQLException; +import java.util.ArrayList; import java.util.List; import java.util.TreeMap; @@ -62,8 +64,7 @@ public class ReviewManagerImpl implements ReviewManager { this.applicationReleaseDAO = ApplicationManagementDAOFactory.getApplicationReleaseDAO(); } - @Override - public boolean addReview(Review review, int appId, int appReleaseId) throws CommentManagementException { + @Override public boolean addReview(Review review, int appId, int appReleaseId) throws ReviewManagementException { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); String username = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); boolean isSuccess; @@ -71,16 +72,7 @@ public class ReviewManagerImpl implements ReviewManager { ConnectionManagerUtil.beginDBTransaction(); Review existingReview = reviewDAO.isExistReview(appId, appReleaseId, username, tenantId); if (existingReview != null && review.getRating() > 0 && review.getRating() != existingReview.getRating()) { - Runnable task = () -> { - try { - if (calculateRating(review.getRating(), existingReview.getRating()) <= 0.0) { - log.error("Application release rating updating task is failed"); - } - } catch (CommentManagementException e) { - // todo - log.error("comment management error occured"); - } - }; + Runnable task = () -> calculateRating(review.getRating(), existingReview.getRating()); new Thread(task).start(); isSuccess = updateReview(review, existingReview.getId(), false); if (isSuccess) { @@ -89,17 +81,8 @@ public class ReviewManagerImpl implements ReviewManager { ConnectionManagerUtil.rollbackDBTransaction(); } } else { - if (review.getRating()>0){ - Runnable task = () -> { - try { - if (calculateRating(review.getRating(), -12345) <= 0.0) { - log.error("Application release rating inserting task is failed"); - } - } catch (CommentManagementException e) { - // todo - log.error("comment management error occured"); - } - }; + if (review.getRating() > 0) { + Runnable task = () -> calculateRating(review.getRating(), -12345); new Thread(task).start(); } review.setUsername(username); @@ -113,17 +96,17 @@ public class ReviewManagerImpl implements ReviewManager { return isSuccess; } catch (DBConnectionException e) { ConnectionManagerUtil.rollbackDBTransaction(); - throw new CommentManagementException( + throw new ReviewManagementException( "DB Connection error occurs ,Review for application with app id: " + appId + " and app release id: " + appReleaseId + " is failed", e); } catch (SQLException e) { ConnectionManagerUtil.rollbackDBTransaction(); - throw new CommentManagementException( + throw new ReviewManagementException( "SQL Exception occurs,Review for application with app id:" + appId + " and app release id:" + appReleaseId + " is failed", e); } catch (TransactionManagementException e) { ConnectionManagerUtil.rollbackDBTransaction(); - throw new CommentManagementException( + throw new ReviewManagementException( "Transaction Management Exception occurs,Review for application with app id:" + appId + " and app release id:" + appReleaseId + " is failed ", e); } finally { @@ -131,9 +114,8 @@ public class ReviewManagerImpl implements ReviewManager { } } - @Override - public boolean updateReview(Review review, int reviewId, boolean checkExistence) - throws CommentManagementException { + @Override public boolean updateReview(Review review, int reviewId, boolean checkExistence) + throws ReviewManagementException { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); String username = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); Review existingReview; @@ -143,28 +125,20 @@ public class ReviewManagerImpl implements ReviewManager { } try { ConnectionManagerUtil.openDBConnection(); + // todo if (!username.equals(review.getUsername())) { - throw new CommentManagementException( + throw new ReviewManagementException( "User " + review.getUsername() + "doesn't match with the logged in user: " + username); } if (checkExistence) { existingReview = this.reviewDAO.getComment(reviewId); if (existingReview != null) { if (review.getRating() > 0 && review.getRating() != existingReview.getRating()) { - Runnable task = () -> { - try { - if (calculateRating(review.getRating(), existingReview.getRating()) <= 0.0) { - log.error("Application release review updating task is failed"); - } - } catch (CommentManagementException e) { - // todo - log.error("error"); - } - }; + Runnable task = () -> calculateRating(review.getRating(), existingReview.getRating()); new Thread(task).start(); } } else { - throw new CommentManagementException("Couldn't find a review for review id: " + reviewId); + throw new ReviewManagementException("Couldn't find a review for review id: " + reviewId); } } ConnectionManagerUtil.beginDBTransaction(); @@ -176,13 +150,13 @@ public class ReviewManagerImpl implements ReviewManager { } return isSuccess; } catch (SQLException e) { - throw new CommentManagementException("SQL Error occurs updating review with review id " + reviewId + ".", + throw new ReviewManagementException("SQL Error occurs updating review with review id " + reviewId + ".", e); } catch (DBConnectionException e) { - throw new CommentManagementException( + throw new ReviewManagementException( "DB Connection error occurs updating review with review id " + reviewId + ".", e); } catch (TransactionManagementException e) { - throw new CommentManagementException( + throw new ReviewManagementException( "Transaction management error occurs when updating review with review id " + reviewId + ".", e); } finally { ConnectionManagerUtil.closeDBConnection(); @@ -190,39 +164,51 @@ public class ReviewManagerImpl implements ReviewManager { } @Override - public PaginationResult getAllReviews(PaginationRequest request, String uuid) throws CommentManagementException { + public PaginationResult getAllReviews(PaginationRequest request, String uuid) + throws ReviewManagementException { + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); PaginationResult paginationResult = new PaginationResult(); int numOfComments; List reviews; + TreeMap hierarchicalReviewSet = new TreeMap<>(); if (log.isDebugEnabled()) { - log.debug("get all reviews of the application release" + uuid); + log.debug("Get all reviews of the application release uuid: " + uuid); } try { ConnectionManagerUtil.openDBConnection(); - reviews = this.reviewDAO.getAllComments(uuid, Util.validateCommentListPageSize(request)); - numOfComments = reviews.size(); - paginationResult.setData(reviews); + reviews = this.reviewDAO.getAllReviews(uuid, Util.validateCommentListPageSize(request), tenantId); + + for (Review review : reviews) { + if (hierarchicalReviewSet.containsKey(review.getParentId())) { + Review parentReview = hierarchicalReviewSet.get(review.getParentId()); + parentReview.setReplyReview(review); + hierarchicalReviewSet.replace(review.getParentId(), parentReview); + } else { + hierarchicalReviewSet.put(review.getId(), review); + } + } + numOfComments = hierarchicalReviewSet.size(); if (numOfComments > 0) { + paginationResult.setData(new ArrayList<>(hierarchicalReviewSet.values())); paginationResult.setRecordsFiltered(numOfComments); paginationResult.setRecordsTotal(numOfComments); } else { + paginationResult.setData(new ArrayList()); paginationResult.setRecordsFiltered(0); paginationResult.setRecordsTotal(0); } return paginationResult; + } catch (ReviewManagementDAOException e) { + throw new ReviewManagementException("Error occured while getting all reviews for application uuid: " + uuid, + e); } catch (DBConnectionException e) { - throw new CommentManagementException( - "DB Connection error occurs , while getting reviews of application release UUID: " + uuid, e); - } catch (SQLException e) { - throw new CommentManagementException( - "Error occured in the data layer, while getting reviews of application release UUID: " + uuid, e); + throw new ReviewManagementException("Error occured while getting the DB connection.", e); } finally { ConnectionManagerUtil.closeDBConnection(); } } - @Override - public Review getReview(int commentId) throws CommentManagementException { + @Override public Review getReview(int commentId) throws ReviewManagementException { PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); Review review; if (log.isDebugEnabled()) { @@ -232,10 +218,10 @@ public class ReviewManagerImpl implements ReviewManager { ConnectionManagerUtil.openDBConnection(); review = this.reviewDAO.getComment(commentId); } catch (DBConnectionException e) { - throw new CommentManagementException( + throw new ReviewManagementException( "DB Connection error occurs ,Review with review id " + commentId + "cannot get.", e); } catch (SQLException e) { - throw new CommentManagementException( + throw new ReviewManagementException( "SQL Exception occurs,Review with review id " + commentId + "cannot get.", e); } finally { ConnectionManagerUtil.closeDBConnection(); @@ -243,47 +229,38 @@ public class ReviewManagerImpl implements ReviewManager { return review; } - @Override - public void deleteReview(String loggedInUser, int commentId) throws CommentManagementException { + @Override public void deleteReview(String loggedInUser, int commentId) throws ReviewManagementException { Review existingReview; int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); String username = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); try { - if (!loggedInUser.equals(username) && !isAdminUser(username, tenantId, CarbonConstants.UI_ADMIN_PERMISSION_COLLECTION)){ - throw new CommentManagementException( + if (!loggedInUser.equals(username) && !isAdminUser(username, tenantId, + CarbonConstants.UI_ADMIN_PERMISSION_COLLECTION)) { + throw new ReviewManagementException( "You don't have permission to delete the review. Please contact the administrator. Review Id: " + commentId); } existingReview = getReview(commentId); if (existingReview == null) { - throw new CommentManagementException( + throw new ReviewManagementException( "Cannot delete a non-existing review for the application with review id" + commentId); } - Runnable task = () -> { - try { - if (calculateRating(0, existingReview.getRating()) <= 0.0) { - log.error("Application release review updating task is failed"); - } - } catch (CommentManagementException e) { - // todo - log.error("error occured"); - } - }; + Runnable task = () -> calculateRating(0, existingReview.getRating()); new Thread(task).start(); ConnectionManagerUtil.beginDBTransaction(); this.reviewDAO.deleteComment(commentId); ConnectionManagerUtil.commitDBTransaction(); } catch (DBConnectionException e) { - throw new CommentManagementException( + throw new ReviewManagementException( "DB Connection error occurs deleting review with review id " + commentId + ".", e); } catch (SQLException e) { - throw new CommentManagementException("SQL error occurs deleting review with review id " + commentId + ".", + throw new ReviewManagementException("SQL error occurs deleting review with review id " + commentId + ".", e); } catch (TransactionManagementException e) { - throw new CommentManagementException( + throw new ReviewManagementException( "Transaction Management Exception occurs deleting review with review id " + commentId + ".", e); } catch (UserStoreException e) { - throw new CommentManagementException( + throw new ReviewManagementException( "User-store exception while checking whether the user " + username + " of tenant " + tenantId + " has the publisher permission"); } finally { @@ -291,15 +268,14 @@ public class ReviewManagerImpl implements ReviewManager { } } - @Override - public Rating getRating(String appReleaseUuuid) throws CommentManagementException{ + @Override public Rating getRating(String appReleaseUuuid) throws ReviewManagementException { //todo int appReleaseId = 0; try { ConnectionManagerUtil.openDBConnection(); Rating rating = this.applicationReleaseDAO.getRating(appReleaseId); if (rating == null) { - throw new CommentManagementException("Couldn't find rating for application release id: " + appReleaseId + throw new ReviewManagementException("Couldn't find rating for application release id: " + appReleaseId + ". Please check the existence of the application relese"); } @@ -316,65 +292,63 @@ public class ReviewManagerImpl implements ReviewManager { return rating; } catch (ApplicationManagementDAOException e) { ConnectionManagerUtil.rollbackDBTransaction(); - throw new CommentManagementException( + throw new ReviewManagementException( "Error occured while updated the rating value of the application release id: " + appReleaseId + " can not get.", e); } catch (DBConnectionException e) { ConnectionManagerUtil.rollbackDBTransaction(); - throw new CommentManagementException( - "DB Connection error occured while updated the rating value of the application release id: " + appReleaseId - + " can not get.", e); + throw new ReviewManagementException( + "DB Connection error occured while updated the rating value of the application release id: " + + appReleaseId + " can not get.", e); } catch (SQLException e) { - throw new CommentManagementException( - "DB Connection error occured while updated the rating value of the application release id: " + appReleaseId - + " can not get.", e); + throw new ReviewManagementException( + "DB Connection error occured while updated the rating value of the application release id: " + + appReleaseId + " can not get.", e); } finally { ConnectionManagerUtil.closeDBConnection(); } } - private double calculateRating(int newRatingVal, int oldRatingVal) throws CommentManagementException { + private void calculateRating(int newRatingVal, int oldRatingVal) { // todo need to pass app release id int appReleaseId = 0; try { ConnectionManagerUtil.beginDBTransaction(); Rating rating = this.applicationReleaseDAO.getRating(appReleaseId); if (rating == null) { - throw new CommentManagementException( - "Couldn't find rating for application release id: " + appReleaseId); - } - double updatedRating; - int numOfUsers = rating.getNoOfUsers(); - double currentRating = rating.getRatingValue() * numOfUsers; - if (oldRatingVal == -12345) { - updatedRating = (currentRating + newRatingVal) / (numOfUsers + 1); - this.applicationReleaseDAO.updateRatingValue(appReleaseId, updatedRating, numOfUsers + 1); - } else if ( newRatingVal == 0){ - updatedRating = (currentRating - newRatingVal) / (numOfUsers - 1); - this.applicationReleaseDAO.updateRatingValue(appReleaseId, updatedRating, numOfUsers - 1); - } else{ - double tmpVal; - tmpVal = currentRating - oldRatingVal; - updatedRating = (tmpVal + newRatingVal) / numOfUsers; - this.applicationReleaseDAO.updateRatingValue(appReleaseId, updatedRating, numOfUsers ); + log.error("Couldn't find rating for application release id: " + appReleaseId); + } else { + double updatedRating; + int numOfUsers = rating.getNoOfUsers(); + double currentRating = rating.getRatingValue() * numOfUsers; + if (oldRatingVal == -12345) { + updatedRating = (currentRating + newRatingVal) / (numOfUsers + 1); + this.applicationReleaseDAO.updateRatingValue(appReleaseId, updatedRating, numOfUsers + 1); + } else if (newRatingVal == 0) { + updatedRating = (currentRating - newRatingVal) / (numOfUsers - 1); + this.applicationReleaseDAO.updateRatingValue(appReleaseId, updatedRating, numOfUsers - 1); + } else { + double tmpVal; + tmpVal = currentRating - oldRatingVal; + updatedRating = (tmpVal + newRatingVal) / numOfUsers; + this.applicationReleaseDAO.updateRatingValue(appReleaseId, updatedRating, numOfUsers); + } } ConnectionManagerUtil.commitDBTransaction(); - return updatedRating; } catch (ApplicationManagementDAOException e) { ConnectionManagerUtil.rollbackDBTransaction(); - throw new CommentManagementException( - "Error occured while updated the rating value of the application release id: " + appReleaseId - + " can not get.", e); + log.error("Error occured while updated the rating value of the application release id: " + appReleaseId + + " can not get."); } catch (TransactionManagementException e) { ConnectionManagerUtil.rollbackDBTransaction(); - throw new CommentManagementException( - "Transaction Management Exception occured while updated the rating value of the application release id: " + appReleaseId - + " can not get.", e); + log.error( + "Transaction Management Exception occured while updated the rating value of the application release id: " + + appReleaseId + " can not get."); + } catch (DBConnectionException e) { ConnectionManagerUtil.rollbackDBTransaction(); - throw new CommentManagementException( - "DB Connection error occured while updated the rating value of the application release id: " + appReleaseId - + " can not get.", e); + log.error("DB Connection error occured while updated the rating value of the application release id: " + + appReleaseId + " can not get."); } finally { ConnectionManagerUtil.closeDBConnection(); } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/main/java/org/wso2/carbon/device/application/mgt/store/api/services/impl/ReviewManagementAPIImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/main/java/org/wso2/carbon/device/application/mgt/store/api/services/impl/ReviewManagementAPIImpl.java index 6ee1fc018d8..6252ddada73 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/main/java/org/wso2/carbon/device/application/mgt/store/api/services/impl/ReviewManagementAPIImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/main/java/org/wso2/carbon/device/application/mgt/store/api/services/impl/ReviewManagementAPIImpl.java @@ -32,7 +32,7 @@ import org.wso2.carbon.device.application.mgt.store.api.APIUtil; import org.wso2.carbon.device.application.mgt.store.api.services.ReviewManagementAPI; import org.wso2.carbon.device.application.mgt.common.PaginationRequest; import org.wso2.carbon.device.application.mgt.common.exception.ApplicationManagementException; -import org.wso2.carbon.device.application.mgt.common.exception.CommentManagementException; +import org.wso2.carbon.device.application.mgt.common.exception.ReviewManagementException; import javax.ws.rs.Path; import javax.ws.rs.Consumes; @@ -59,17 +59,15 @@ public class ReviewManagementAPIImpl implements ReviewManagementAPI { @PathParam("uuid") String uuid, @QueryParam("offset") int offSet, @QueryParam("limit") int limit) { - ReviewManager reviewManager = APIUtil.getCommentsManager(); PaginationRequest request = new PaginationRequest(offSet, limit); try { PaginationResult paginationResult = reviewManager.getAllReviews(request, uuid); return Response.status(Response.Status.OK).entity(paginationResult).build(); - } catch (CommentManagementException e) { - String msg = "Error occurred while retrieving comments."; + } catch (ReviewManagementException e) { + String msg = "Error occurred while retrieving reviews for application UUID: " + uuid; log.error(msg, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg) - .build(); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); } } @@ -80,7 +78,6 @@ public class ReviewManagementAPIImpl implements ReviewManagementAPI { public Response addReview( @ApiParam Review review, @PathParam("uuid") String uuid) { - ReviewManager reviewManager = APIUtil.getCommentsManager(); ApplicationManager applicationManager = APIUtil.getApplicationManager(); Application application; @@ -96,7 +93,7 @@ public class ReviewManagementAPIImpl implements ReviewManagementAPI { log.error(msg); return Response.status(Response.Status.BAD_REQUEST).build(); } - } catch (CommentManagementException e) { + } catch (ReviewManagementException e) { String msg = "Error occurred while creating the review"; log.error(msg, e); return Response.status(Response.Status.INTERNAL_SERVER_ERROR) @@ -131,7 +128,7 @@ public class ReviewManagementAPIImpl implements ReviewManagementAPI { } else { return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity("").build(); } - } catch (CommentManagementException e) { + } catch (ReviewManagementException e) { String msg = "Error occurred while retrieving comments."; log.error(msg, e); return Response.status(Response.Status.INTERNAL_SERVER_ERROR) @@ -153,7 +150,7 @@ public class ReviewManagementAPIImpl implements ReviewManagementAPI { } else { reviewManager.deleteReview(username, commentId); } - } catch (CommentManagementException e) { + } catch (ReviewManagementException e) { String msg = "Error occurred while deleting the comment."; log.error(msg, e); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg) @@ -172,7 +169,7 @@ public class ReviewManagementAPIImpl implements ReviewManagementAPI { Rating rating; try { rating = reviewManager.getRating(uuid); - } catch (CommentManagementException e) { + } catch (ReviewManagementException e) { log.error("Review Management Exception occurs", e); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build(); } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/test/java/org/wso2/carbon/device/application/mgt/store/api/services/ReviewManagementAPITest.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/test/java/org/wso2/carbon/device/application/mgt/store/api/services/ReviewManagementAPITest.java index c59db2a67db..f3412cab3c2 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/test/java/org/wso2/carbon/device/application/mgt/store/api/services/ReviewManagementAPITest.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/test/java/org/wso2/carbon/device/application/mgt/store/api/services/ReviewManagementAPITest.java @@ -34,7 +34,7 @@ import org.testng.annotations.ObjectFactory; import org.testng.annotations.Test; import org.wso2.carbon.device.application.mgt.common.Review; import org.wso2.carbon.device.application.mgt.common.exception.ApplicationManagementException; -import org.wso2.carbon.device.application.mgt.common.exception.CommentManagementException; +import org.wso2.carbon.device.application.mgt.common.exception.ReviewManagementException; import org.wso2.carbon.device.application.mgt.common.services.ReviewManager; import org.wso2.carbon.device.application.mgt.store.api.APIUtil; import org.wso2.carbon.device.application.mgt.store.api.services.impl.ReviewManagementAPIImpl; @@ -63,7 +63,7 @@ import static org.mockito.MockitoAnnotations.initMocks; } @BeforeClass - void init() throws CommentManagementException { + void init() throws ReviewManagementException { log.info("Initializing ReviewManagementAPI tests"); initMocks(this); @@ -84,7 +84,7 @@ import static org.mockito.MockitoAnnotations.initMocks; @Test public void testGetAllCommentsInternalError() throws Exception { PowerMockito.stub(PowerMockito.method(APIUtil.class, "getReviewManager")).toReturn(this.reviewManager); - Mockito.doThrow(new CommentManagementException()).when(this.reviewManager) + Mockito.doThrow(new ReviewManagementException()).when(this.reviewManager) .getAllReviews(Mockito.any(), Mockito.anyString()); Response response = this.commentManagementAPI.getAllReviews("a", 1, 4); Assert.assertNotNull(response, "The response object is null."); @@ -129,7 +129,7 @@ import static org.mockito.MockitoAnnotations.initMocks; Review review = CommentMgtTestHelper.getDummyComment("a", "a"); PowerMockito.stub(PowerMockito.method(APIUtil.class, "getReviewManager")).toReturn(this.reviewManager); Mockito.when(this.commentManagementAPI.addReview(Mockito.any(), Mockito.anyString())) - .thenThrow(new CommentManagementException()); + .thenThrow(new ReviewManagementException()); Response response = this.commentManagementAPI.addReview(review, null); Assert.assertNotNull(response, "The response object is null."); Assert.assertEquals(response.getStatus(), Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), @@ -170,7 +170,7 @@ import static org.mockito.MockitoAnnotations.initMocks; public void testUpdateCommentInternalServerError() throws Exception { Review review = CommentMgtTestHelper.getDummyComment("a", "a"); PowerMockito.stub(PowerMockito.method(APIUtil.class, "getReviewManager")).toReturn(this.reviewManager); - Mockito.doThrow(new CommentManagementException()).when(this.reviewManager).updateReview(review, 9, true); + Mockito.doThrow(new ReviewManagementException()).when(this.reviewManager).updateReview(review, 9, true); Response response = this.commentManagementAPI.updateComment(review, 9); Assert.assertNotNull(response, "The response object is null."); Assert.assertEquals(response.getStatus(), Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), @@ -189,7 +189,7 @@ import static org.mockito.MockitoAnnotations.initMocks; @Test public void testDeleteCommentInternalError() throws Exception { PowerMockito.stub(PowerMockito.method(APIUtil.class, "getReviewManager")).toReturn(this.reviewManager); - Mockito.when(this.commentManagementAPI.deleteComment(1,"")).thenThrow(new CommentManagementException()); + Mockito.when(this.commentManagementAPI.deleteComment(1,"")).thenThrow(new ReviewManagementException()); Response response = this.commentManagementAPI.deleteComment(1,""); Assert.assertNotNull(response, "The response object is null."); Assert.assertEquals(response.getStatus(), Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), @@ -219,7 +219,7 @@ import static org.mockito.MockitoAnnotations.initMocks; public void testGetStarsCommentError() throws Exception { PowerMockito.stub(PowerMockito.method(APIUtil.class, "getReviewManager")).toReturn(this.reviewManager); Mockito.when(this.commentManagementAPI.getRating(Mockito.anyString())) - .thenThrow(new CommentManagementException()); + .thenThrow(new ReviewManagementException()); Response response = this.commentManagementAPI.getRating("a"); Assert.assertNotNull(response, "The response object is null."); Assert.assertEquals(response.getStatus(), Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(),