From a5d39e76b59d83c19bf8d9a9db273ff7c52daf01 Mon Sep 17 00:00:00 2001 From: lasanthaDLPDS Date: Thu, 4 Jul 2019 21:17:15 +0530 Subject: [PATCH] Improve review update logic --- .../mgt/common/services/ReviewManager.java | 2 +- .../application/mgt/core/dao/ReviewDAO.java | 2 +- .../core/dao/impl/review/ReviewDAOImpl.java | 8 +++- .../mgt/core/impl/ReviewManagerImpl.java | 35 +++++++++----- .../admin/ReviewManagementAdminAPI.java | 48 ------------------- .../admin/ReviewManagementAdminAPIImpl.java | 33 ------------- .../impl/ReviewManagementAPIImpl.java | 6 +-- 7 files changed, 33 insertions(+), 101 deletions(-) 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 779f718466..7a063b36bd 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 @@ -78,7 +78,7 @@ public interface ReviewManager { * @return {@link Review}updated review * @throws ReviewManagementException Exceptions of the reviewTmp management */ - boolean updateReview(ReviewWrapper updatingReview, int reviewId, String uuid, boolean isPrivilegedUser) + Review updateReview(ReviewWrapper updatingReview, int reviewId, String uuid, boolean isPrivilegedUser) throws ReviewManagementException, ApplicationManagementException; /** 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 904c5dd791..19d8e727ef 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 @@ -62,7 +62,7 @@ import java.util.List; * @return row count if updating is succeed otherwise 0 * @throws ReviewManagementDAOException Exceptions of the reviewTmp management. */ - int updateReview(ReviewDTO reviewDTO, int reviewId, boolean isActiveReview, int tenantId) + ReviewDTO updateReview(ReviewDTO reviewDTO, int reviewId, boolean isActiveReview, int tenantId) throws ReviewManagementDAOException; 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/review/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/review/ReviewDAOImpl.java index 14dec383b8..5c30e9ce55 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/review/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/review/ReviewDAOImpl.java @@ -142,7 +142,7 @@ public class ReviewDAOImpl extends AbstractDAOImpl implements ReviewDAO { } @Override - public int updateReview(ReviewDTO reviewDTO, int reviewId, boolean isActiveReview, int tenantId) + public ReviewDTO updateReview(ReviewDTO reviewDTO, int reviewId, boolean isActiveReview, int tenantId) throws ReviewManagementDAOException { if (log.isDebugEnabled()) { log.debug("Request received to DAO Layer to update the Review which has ID " + reviewId); @@ -167,7 +167,11 @@ public class ReviewDAOImpl extends AbstractDAOImpl implements ReviewDAO { statement.setBoolean(4, isActiveReview); statement.setInt(5, reviewId); statement.setInt(6, tenantId); - return statement.executeUpdate(); + if (statement.executeUpdate() == 1) { + reviewDTO.setModifiedAt(timestamp); + return reviewDTO; + } + return null; } } catch (DBConnectionException e) { String msg = "Error occured while getting the db connection to update review for review ID: " + reviewId; 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 8336f34bec..79664eb409 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 @@ -272,7 +272,7 @@ public class ReviewManagerImpl implements ReviewManager { } @Override - public boolean updateReview(ReviewWrapper updatingReview, int reviewId, String uuid, + public Review updateReview(ReviewWrapper updatingReview, int reviewId, String uuid, boolean isPrivilegedUser) throws ReviewManagementException, ApplicationManagementException { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); String username = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); @@ -299,6 +299,8 @@ public class ReviewManagerImpl implements ReviewManager { .calculateRating(updatingReview.getRating(), reviewDTO.getRating(), uuid, tenantId); new Thread(task).start(); reviewDTO.setRating(updatingReview.getRating()); + } + if (!reviewDTO.getContent().equals(updatingReview.getContent())) { reviewDTO.setContent(updatingReview.getContent()); } } else { @@ -310,22 +312,28 @@ public class ReviewManagerImpl implements ReviewManager { reviewDTO.setContent(updatingReview.getContent()); } - if (updateReviewInDB(reviewDTO, reviewId, isActiveReview, tenantId)) { + ReviewDTO updatedReviewDTO = updateReviewInDB(reviewDTO, reviewId, isActiveReview, tenantId); + if (updatedReviewDTO != null) { if (!isActiveReview) { - if (addReview(updatingReview, uuid, true) != null) { - return true; + Review newReview = addReview(updatingReview, uuid, true); + if (newReview != null) { + return newReview; } else { - if (updateReviewInDB(reviewDTO, reviewId, true, tenantId)) { - return false; + ReviewDTO restoringReviewDTO = updateReviewInDB(reviewDTO, reviewId, true, tenantId); + if (restoringReviewDTO != null) { + String msg = "Review Updating Status: Adding new Review for application release which has" + + " UUID: " + uuid + " is failed and the old review is restored."; + log.error(msg); + throw new ApplicationManagementException(msg); } else { - String msg = "Review Updating Status: Adding new Review for application release which has UUID: " - + "" + uuid + " is failed and the old review restoring is also failed."; + String msg = "Review Updating Status: Adding new Review for application release which has " + + "UUID: " + uuid + " is failed and the old review restoring is also failed."; log.error(msg); throw new ApplicationManagementException(msg); } } } - return true; + return reviewDTOToReview(updatedReviewDTO); } else { String msg = "Review Updating is failed. Hence please contact the administrator."; log.error(msg); @@ -333,16 +341,17 @@ public class ReviewManagerImpl implements ReviewManager { } } - private boolean updateReviewInDB(ReviewDTO reviewDTO, int reviewId, boolean isActiveReview, + private ReviewDTO updateReviewInDB(ReviewDTO reviewDTO, int reviewId, boolean isActiveReview, int tenantId) throws ReviewManagementException, ApplicationManagementException { try { ConnectionManagerUtil.beginDBTransaction(); - if (this.reviewDAO.updateReview(reviewDTO, reviewId, isActiveReview, tenantId) == 1) { + ReviewDTO updatedReviewDTO = this.reviewDAO.updateReview(reviewDTO, reviewId, isActiveReview, tenantId); + if (updatedReviewDTO != null) { ConnectionManagerUtil.commitDBTransaction(); - return true; + return updatedReviewDTO; } ConnectionManagerUtil.rollbackDBTransaction(); - return false; + return null; } catch (ReviewManagementDAOException e) { ConnectionManagerUtil.rollbackDBTransaction(); String msg = "Error occured while getting reviewTmp with reviewTmp id " + reviewId + "."; diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.api/src/main/java/org/wso2/carbon/device/application/mgt/publisher/api/services/admin/ReviewManagementAdminAPI.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.api/src/main/java/org/wso2/carbon/device/application/mgt/publisher/api/services/admin/ReviewManagementAdminAPI.java index 2ed8047ef7..e8f8a4c783 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.api/src/main/java/org/wso2/carbon/device/application/mgt/publisher/api/services/admin/ReviewManagementAdminAPI.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.api/src/main/java/org/wso2/carbon/device/application/mgt/publisher/api/services/admin/ReviewManagementAdminAPI.java @@ -88,54 +88,6 @@ scopes = { public interface ReviewManagementAdminAPI { String SCOPE = "scope"; - @PUT - @Path("/{uuid}/{reviewId}") - @Produces(MediaType.APPLICATION_JSON) - @Consumes(MediaType.APPLICATION_JSON) - @ApiOperation( - consumes = MediaType.APPLICATION_JSON, - produces = MediaType.APPLICATION_JSON, - httpMethod = "PUT", - value = "Edit a review", - notes = "This will edit the review", - tags = "Review Management", - extensions = { - @Extension(properties = { - @ExtensionProperty(name = SCOPE, value = "perm:admin:app:review:update") - }) - } - ) - @ApiResponses( - value = { - @ApiResponse( - code = 200, - message = "OK. \n Successfully updated reviewTmp.", - response = Review.class), - @ApiResponse( - code = 400, - message = "Bad Request. \n Invalid request or validation error."), - @ApiResponse( - code = 500, - message = "Internal Server Error. \n Error occurred while updating the new reviewTmp.", - response = ErrorResponse.class) - }) - Response updateReview( - @ApiParam( - name = "reviewTmp", - value = "The review that need to be updated.", - required = true) - @Valid ReviewWrapper updatingReview, - @ApiParam( - name = "uuid", - value = "uuid of the application release", - required = true) - @PathParam("uuid") String uuid, - @ApiParam( - name = "reviewId", - value = "review id of the updating reviewTmp.", - required = true) - @PathParam("reviewId") int reviewId); - @DELETE @Path("/{uuid}/{reviewId}") @Produces(MediaType.APPLICATION_JSON) diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.api/src/main/java/org/wso2/carbon/device/application/mgt/publisher/api/services/impl/admin/ReviewManagementAdminAPIImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.api/src/main/java/org/wso2/carbon/device/application/mgt/publisher/api/services/impl/admin/ReviewManagementAdminAPIImpl.java index b0718a0f7c..fbda07e392 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.api/src/main/java/org/wso2/carbon/device/application/mgt/publisher/api/services/impl/admin/ReviewManagementAdminAPIImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.api/src/main/java/org/wso2/carbon/device/application/mgt/publisher/api/services/impl/admin/ReviewManagementAdminAPIImpl.java @@ -48,38 +48,6 @@ public class ReviewManagementAdminAPIImpl implements ReviewManagementAdminAPI { private static Log log = LogFactory.getLog(ReviewManagementAdminAPIImpl.class); - //todo remove this API - @Override - @PUT - @Consumes("application/json") - @Path("/{uuid}/{reviewId}") - public Response updateReview( - @ApiParam ReviewWrapper updatingReview, - @PathParam("uuid") String uuid, - @PathParam("reviewId") int reviewId) { - ReviewManager reviewManager = APIUtil.getReviewManager(); - try { - if (reviewManager.updateReview(updatingReview, reviewId, uuid, true)) { - return Response.status(Response.Status.OK).entity(updatingReview).build(); - } else { - String msg = "Review updating failed. Please contact the administrator"; - log.error(msg); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); - } - } catch (ReviewManagementException e) { - String msg = "Error occurred while retrieving comments."; - log.error(msg, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); - } catch (NotFoundException e) { - String msg = "Couldn't found application release data for UUID " + uuid + " or Review for review ID: " + reviewId; - log.error(msg, e); - return Response.status(Response.Status.NOT_FOUND).entity(msg).build(); - } catch (ApplicationManagementException e) { - String msg = "Error occurred when getting application release data for application release UUID:." + uuid; - log.error(msg, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); } - } - @Override @DELETE @Path("/{uuid}/{reviewId}") @@ -91,7 +59,6 @@ public class ReviewManagementAdminAPIImpl implements ReviewManagementAdminAPI { try { reviewManager.deleteReview(uuid, reviewId, true); return Response.status(Response.Status.OK).entity("Review is deleted successfully.").build(); - } catch (NotFoundException e) { String msg = "Couldn't found an application review to delete which match with the request."; log.error(msg, e); 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 fece4a992d..c8859622d8 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 @@ -199,8 +199,9 @@ public class ReviewManagementAPIImpl implements ReviewManagementAPI { @PathParam("reviewId") int reviewId) { ReviewManager reviewManager = APIUtil.getReviewManager(); try { - if (reviewManager.updateReview(updatingReview, reviewId, uuid, false)) { - return Response.status(Response.Status.OK).entity(updatingReview).build(); + Review updatedReview = reviewManager.updateReview(updatingReview, reviewId, uuid, false); + if (updatedReview != null) { + return Response.status(Response.Status.OK).entity(updatedReview).build(); } else { String msg = "Review updating failed. Please contact the administrator"; log.error(msg); @@ -230,7 +231,6 @@ public class ReviewManagementAPIImpl implements ReviewManagementAPI { public Response deleteReview( @PathParam("uuid") String uuid, @PathParam("reviewId") int reviewId) { - ReviewManager reviewManager = APIUtil.getReviewManager(); try { reviewManager.deleteReview(uuid, reviewId, false);