From 48f0636fa15907442352bb618461f96370f4f10b Mon Sep 17 00:00:00 2001 From: lasanthaDLPDS Date: Thu, 20 Sep 2018 18:21:44 +0530 Subject: [PATCH] Fix DAO layer issues and improve review management --- .../mgt/common/services/ReviewManager.java | 14 ++-- .../mgt/core/dao/ApplicationReleaseDAO.java | 4 +- .../application/mgt/core/dao/ReviewDAO.java | 57 ++++++++----- .../core/dao/impl/Comment/ReviewDAOImpl.java | 76 +++++++++++------ .../GenericApplicationDAOImpl.java | 16 ++-- .../GenericApplicationReleaseDAOImpl.java | 8 +- .../visibility/GenericVisibilityDAOImpl.java | 6 +- .../mgt/core/impl/ReviewManagerImpl.java | 81 ++++++++++--------- .../api/services/ReviewManagementAPI.java | 21 +++-- .../impl/ReviewManagementAPIImpl.java | 24 +++--- .../api/services/ReviewManagementAPITest.java | 8 +- 11 files changed, 183 insertions(+), 132 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 c2ddf4784e..b8c010fa5a 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 @@ -33,12 +33,11 @@ public interface ReviewManager { * To add a review to a application * * @param review review of the application. - * @param appId id of the application. - * @param appReleaseId id of the application release + * @param uuid uuid of the application release. * @return {@link Review} Review added * @throws ReviewManagementException Exceptions of the review management. */ - boolean addReview(Review review,int appId, int appReleaseId) throws ReviewManagementException; + boolean addReview(Review review, String uuid) throws ReviewManagementException; /** * Get all comments to pagination @@ -62,20 +61,25 @@ public interface ReviewManager { /** * To delete review using review id. * + * @param uuid UUID of the application release * @param commentId id of the comment + * @return If review is successfully deleted return true, otherwise returns false * @throws ReviewManagementException Exceptions of the comment management */ - void deleteReview(String loggedInUser, int commentId) throws ReviewManagementException; + boolean deleteReview(String uuid, int commentId) throws ReviewManagementException; /** * To update a review. * * @param review review of the application. * @param reviewId id of the review + * @param uuid UUID of the application release + * @param checkExistence Pass true if it is required to check the existence of the review, otherwise pass false * @return {@link Review}updated review * @throws ReviewManagementException Exceptions of the review management */ - boolean updateReview(Review review, int reviewId, boolean checkExistence) throws ReviewManagementException; + boolean updateReview(Review review, int reviewId, String uuid, boolean checkExistence) + throws ReviewManagementException; /** * To get the overall rating for a application release diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/ApplicationReleaseDAO.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/ApplicationReleaseDAO.java index ec573b8d23..a542dddecd 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/ApplicationReleaseDAO.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/ApplicationReleaseDAO.java @@ -90,10 +90,10 @@ public interface ApplicationReleaseDAO { /** * To retrieve rating of an application release. - * @param id Id of the application Release. + * @param uuid UUID of the application Release. * @throws ApplicationManagementDAOException Application Management DAO Exception. */ - Rating getRating(int id) throws ApplicationManagementDAOException; + Rating getRating(String uuid) throws ApplicationManagementDAOException; /** 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 6bab98ef4d..69ec7a2abe 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 @@ -28,24 +28,32 @@ import java.sql.SQLException; import java.util.List; /** - * This interface specifies the database access operations performed for comments. + * This interface specifies the database access operations performed for reviews. */ public interface ReviewDAO { /** - * To add a review to a application. + * To add a review to an application release. * - * @param tenantId tenantId of the commented application. + * @param tenantId tenantId. * @param review review of the application. + * @param uuid UUID of the application release * @return If review is added successfully, it return true otherwise false * @throws ReviewManagementDAOException Exceptions of the review management DAO. */ - 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; + boolean addReview(Review review, String uuid, int tenantId) throws ReviewManagementDAOException; + + /** + * To verify whether review is exists or not. + * + * @param uuid UUID of the application release. + * @param username username of the logged in user. + * @param tenantId tenantId of the commented application. + * @return If review exists, review returns + * @throws ReviewManagementDAOException Exceptions of the review management DAO. + */ + Review haveUerCommented(String uuid, String username, int tenantId) throws ReviewManagementDAOException; /** * To update already added comment. @@ -68,7 +76,7 @@ import java.util.List; * @throws DBConnectionException db connection exception * @throws SQLException sql exception */ - Review getComment(int commentId) throws ReviewManagementException, SQLException, DBConnectionException; + Review getReview(int commentId) throws ReviewManagementException, SQLException, DBConnectionException; /** * To get all the comments @@ -101,18 +109,27 @@ import java.util.List; * @throws DBConnectionException db connection exception. * @throws SQLException sql exception */ - int getCommentCountByApp(String appType, String appName, String version) + int getReviewCountByApp(String appType, String appName, String version) throws ReviewManagementException, DBConnectionException, SQLException; /** - * To delete comment using comment id. + * To delete review using review id and uuid of the application release. * - * @param commentId id of the comment - * @throws ReviewManagementException Exceptions of the comment management. - * @throws DBConnectionException db connection exception. - * @throws SQLException sql exception + * @param username username of the review owner + * @param reviewId id of the review + * @return If review is successfully deleted return 1, otherwise returns 0. + * @throws ReviewManagementDAOException Review management DAO exception. */ - void deleteComment(int commentId) throws ReviewManagementException, DBConnectionException, SQLException; + int deleteReview(String username, int reviewId) throws ReviewManagementDAOException; + + /** + * To delete review using review id, in this case, it doesn't check whether user is review owner or not. + * + * @param reviewId id of the review + * @return If review is successfully deleted return 1, otherwise returns 0. + * @throws ReviewManagementDAOException Review management DAO exception. + */ + int deleteReviewByAdmin(int reviewId) throws ReviewManagementDAOException; /** * To delete comments using application details. @@ -122,15 +139,15 @@ import java.util.List; * @param version version of the commented application. * @throws ReviewManagementException Exceptions of the comment management. */ - void deleteComments(String appType, String appName, String version) throws ReviewManagementException; + void deleteReviews(String appType, String appName, String version) throws ReviewManagementException; /** * To get comment count for pagination * - * @param request - * @param uuid + * @param request pagination request + * @param uuid uuid of the application release * @return Review count * @throws ReviewManagementException */ - int getCommentCount(PaginationRequest request, String uuid) throws ReviewManagementException; + int getReviewCount(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/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 6afa5246cc..534ff7a70a 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 @@ -48,12 +48,10 @@ public class ReviewDAOImpl extends AbstractDAOImpl implements ReviewDAO { private String sql; @Override - public boolean addReview(Review review, int appId, int appReleaseId, int tenantId) + public boolean addReview(Review review, String uuid, int tenantId) 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); + log.debug("Request received in DAO Layer to add review for application release. Application UUID: " + uuid); } PreparedStatement statement = null; ResultSet rs = null; @@ -67,8 +65,8 @@ public class ReviewDAOImpl extends AbstractDAOImpl implements ReviewDAO { statement.setString(2, review.getComment()); statement.setInt(3, review.getParentId()); statement.setString(4, review.getUsername()); - statement.setString(5,""); - statement.setString(6,""); + statement.setString(5,uuid); + statement.setString(6,uuid); statement.executeUpdate(); rs = statement.getGeneratedKeys(); return rs.next(); @@ -85,26 +83,25 @@ public class ReviewDAOImpl extends AbstractDAOImpl implements ReviewDAO { } @Override - public Review isExistReview(int appId, int appReleaseId, String username, int tenantId) - throws DBConnectionException, SQLException { + public Review haveUerCommented(String uuid, String username, int tenantId) throws ReviewManagementDAOException { if (log.isDebugEnabled()) { log.debug( - "Request received in DAO Layer to check whether review exist or not rein the IoTS APPM. Application id: " - + appId + " Application release id: " + appReleaseId + " comment owner: " + username); + "Request received in DAO Layer to check whether user have already commented or not for the " + + "application release. Application UUID: " + uuid + " comment owner: " + username + + " tenant-id " + tenantId); } Connection conn; PreparedStatement statement = null; ResultSet rs = null; Review review = null; 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 = ?;"; + + "AP_APP_RELEASE_ID = (SELECT ID FROM AP_APP_RELEASE WHERE UUID=?) AND USERNAME = ? AND TENANT_ID = ?;"; try { conn = this.getDBConnection(); statement = conn.prepareStatement(sql); - statement.setInt(1, appId); - statement.setInt(2, appReleaseId); - statement.setString(3, username); - statement.setInt(4, tenantId); + statement.setString(1, uuid); + statement.setString(2, username); + statement.setInt(3, tenantId); rs = statement.executeQuery(); if (rs.next()){ @@ -118,6 +115,13 @@ public class ReviewDAOImpl extends AbstractDAOImpl implements ReviewDAO { review.setRating(rs.getInt("RATING")); } return review; + } catch (SQLException e) { + throw new ReviewManagementDAOException("Error occured while accessing the Database when checking whether " + + "user has already commented for the application ro not", e); + } catch (DBConnectionException e) { + throw new ReviewManagementDAOException("Error occured while getting the database connection when checking " + + "whether user has already commented for the application ro not", e); + } finally { Util.cleanupResources(statement, rs); } @@ -149,7 +153,7 @@ public class ReviewDAOImpl extends AbstractDAOImpl implements ReviewDAO { } @Override - public Review getComment(int commentId) throws ReviewManagementException { + public Review getReview(int commentId) throws ReviewManagementException { if (log.isDebugEnabled()) { log.debug("Getting review with the review id(" + commentId + ") from the database"); @@ -259,7 +263,7 @@ public class ReviewDAOImpl extends AbstractDAOImpl implements ReviewDAO { } @Override - public int getCommentCount(PaginationRequest request, String uuid) throws ReviewManagementException { + public int getReviewCount(PaginationRequest request, String uuid) throws ReviewManagementException { int commentCount = 0; Connection conn; @@ -293,7 +297,7 @@ public class ReviewDAOImpl extends AbstractDAOImpl implements ReviewDAO { } @Override - public int getCommentCountByApp(String appType, String appName, String version) + public int getReviewCountByApp(String appType, String appName, String version) throws ReviewManagementException, DBConnectionException, SQLException { Connection conn; @@ -320,24 +324,48 @@ public class ReviewDAOImpl extends AbstractDAOImpl implements ReviewDAO { } @Override - public void deleteComment(int commentId) - throws ReviewManagementException, DBConnectionException, SQLException { + public int deleteReview(String username, int reviewId) throws ReviewManagementDAOException { + Connection conn; + PreparedStatement statement = null; + try { + conn = this.getDBConnection(); + sql = "DELETE FROM AP_APP_REVIEW WHERE ID=? AND USERNAME = ?);"; + statement = conn.prepareStatement(sql); + statement.setInt(1, reviewId); + statement.setString(2, username); + return statement.executeUpdate(); + } catch (SQLException e) { + throw new ReviewManagementDAOException("Error occured while accessing the Database", e); + } catch (DBConnectionException e) { + throw new ReviewManagementDAOException("Error occured while getting the database connection", e); + } finally { + Util.cleanupResources(statement, null); + } + } + + @Override + public int deleteReviewByAdmin(int reviewId) throws ReviewManagementDAOException { Connection conn; PreparedStatement statement = null; try { conn = this.getDBConnection(); - sql = "DELETE FROM AP_APP_COMMENT WHERE ID=?;"; + sql = "DELETE FROM AP_APP_REVIEW WHERE ID=?;"; statement = conn.prepareStatement(sql); - statement.setInt(1, commentId); - statement.executeUpdate(); + statement.setInt(1, reviewId); + return statement.executeUpdate(); + } catch (SQLException e) { + throw new ReviewManagementDAOException("Error occured while accessing the Database", e); + } catch (DBConnectionException e) { + throw new ReviewManagementDAOException("Error occured while getting the database connection", e); + } finally { Util.cleanupResources(statement, null); } } @Override - public void deleteComments(String appType, String appName, String version) + public void deleteReviews(String appType, String appName, String version) throws ReviewManagementException { Connection conn; 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/application/GenericApplicationDAOImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/application/GenericApplicationDAOImpl.java index 586d6d92f0..b10a692a93 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/application/GenericApplicationDAOImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/application/GenericApplicationDAOImpl.java @@ -136,9 +136,9 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic Pagination pagination = new Pagination(); String sql = "SELECT AP_APP.ID AS APP_ID, AP_APP.NAME AS APP_NAME, AP_APP.TYPE AS APP_TYPE, AP_APP.APP_CATEGORY" + " AS APP_CATEGORY, AP_APP.SUB_TYPE AS SUB_TYPE, AP_APP.CURRENCY AS CURRENCY, " - + "AP_APP.RESTRICTED AS RESTRICTED, AP_APP_TAG.TAG AS APP_TAG, AP_UNRESTRICTED_ROLES.ROLE " + + "AP_APP.RESTRICTED AS RESTRICTED, AP_APP_TAG.TAG AS APP_TAG, AP_UNRESTRICTED_ROLE.ROLE " + "AS ROLE FROM ((AP_APP LEFT JOIN AP_APP_TAG ON AP_APP.ID = AP_APP_TAG.AP_APP_ID) " - + "LEFT JOIN AP_UNRESTRICTED_ROLES ON AP_APP.ID = AP_UNRESTRICTED_ROLES.AP_APP_ID) " + + "LEFT JOIN AP_UNRESTRICTED_ROLE ON AP_APP.ID = AP_UNRESTRICTED_ROLE.AP_APP_ID) " + "WHERE AP_APP.TENANT_ID = ? AND AP_APP.STATUS != ?"; @@ -299,8 +299,8 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic String sql = "SELECT AP_APP.ID AS APP_ID, AP_APP.NAME AS APP_NAME, AP_APP.TYPE AS APP_TYPE, AP_APP.APP_CATEGORY " + "AS APP_CATEGORY, AP_APP.SUB_TYPE AS SUB_TYPE ,AP_APP.CURRENCY AS CURRENCY," - + " AP_APP.RESTRICTED AS RESTRICTED, AP_APP_TAG.TAG AS APP_TAG, AP_UNRESTRICTED_ROLES.ROLE " - + "AS ROLE FROM AP_APP, AP_APP_TAG, AP_UNRESTRICTED_ROLES WHERE AP_APP.NAME=? AND " + + " AP_APP.RESTRICTED AS RESTRICTED, AP_APP_TAG.TAG AS APP_TAG, AP_UNRESTRICTED_ROLE.ROLE " + + "AS ROLE FROM AP_APP, AP_APP_TAG, AP_UNRESTRICTED_ROLE WHERE AP_APP.NAME=? AND " + "AP_APP.TYPE= ? AND AP_APP.TENANT_ID=?;"; stmt = conn.prepareStatement(sql); @@ -343,8 +343,8 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic String sql = "SELECT AP_APP.ID AS APP_ID, AP_APP.NAME AS APP_NAME, AP_APP.TYPE AS APP_TYPE, AP_APP.APP_CATEGORY " + "AS APP_CATEGORY, AP_APP.SUB_TYPE AS SUB_TYPE ,AP_APP.CURRENCY AS CURRENCY, " - + "AP_APP.RESTRICTED AS RESTRICTED, AP_APP_TAG.TAG AS APP_TAG, AP_UNRESTRICTED_ROLES.ROLE " - + "AS ROLE FROM AP_APP, AP_APP_TAG, AP_UNRESTRICTED_ROLES WHERE AP_APP.ID=? AND " + + "AP_APP.RESTRICTED AS RESTRICTED, AP_APP_TAG.TAG AS APP_TAG, AP_UNRESTRICTED_ROLE.ROLE " + + "AS ROLE FROM AP_APP, AP_APP_TAG, AP_UNRESTRICTED_ROLE WHERE AP_APP.ID=? AND " + "AP_APP.TENANT_ID=?;"; stmt = conn.prepareStatement(sql); @@ -385,7 +385,7 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic String sql = "SELECT AP_APP.ID AS APP_ID, AP_APP.NAME AS APP_NAME, AP_APP.TYPE AS APP_TYPE, AP_APP.APP_CATEGORY " + "AS APP_CATEGORY, AP_APP.SUB_TYPE AS SUB_TYPE, AP_APP_TAG.TAG AS TAG, " - + "AP_UNRESTRICTED_ROLES.ROLE AS ROLE FROM AP_APP, AP_APP_TAG, AP_UNRESTRICTED_ROLES " + + "AP_UNRESTRICTED_ROLE.ROLE AS ROLE FROM AP_APP, AP_APP_TAG, AP_UNRESTRICTED_ROLE " + "WHERE AP_APP.ID = ?;"; stmt = conn.prepareStatement(sql); @@ -571,7 +571,7 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic + "AP_APP_RELEASE.STARS," + "AP_APP.ID AS APP_ID, AP_APP.NAME AS APP_NAME, AP_APP.TYPE AS APP_TYPE, " + "AP_APP.APP_CATEGORY AS APP_CATEGORY, AP_APP.SUB_TYPE AS SUB_TYPE, AP_APP.CURRENCY AS CURRENCY, " - + "AP_UNRESTRICTED_ROLES.ROLE AS ROLE FROM AP_APP, AP_UNRESTRICTED_ROLES, AP_APP_RELEASE " + + "AP_UNRESTRICTED_ROLE.ROLE AS ROLE FROM AP_APP, AP_UNRESTRICTED_ROLE, AP_APP_RELEASE " + "WHERE AP_APP_RELEASE.UUID=? AND AP_APP.TENANT_ID=?;"; stmt = conn.prepareStatement(sql); 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/application/release/GenericApplicationReleaseDAOImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/application/release/GenericApplicationReleaseDAOImpl.java index 105d72fc25..9000654804 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/application/release/GenericApplicationReleaseDAOImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/application/release/GenericApplicationReleaseDAOImpl.java @@ -306,20 +306,20 @@ public class GenericApplicationReleaseDAOImpl extends AbstractDAOImpl implements /** * To retrieve rating of an application release. * - * @param id Id of the application Release. + * @param uuid UUID of the application Release. * @throws ApplicationManagementDAOException Application Management DAO Exception. */ @Override - public Rating getRating(int id) throws ApplicationManagementDAOException { + public Rating getRating(String uuid) throws ApplicationManagementDAOException { Connection connection; PreparedStatement statement = null; ResultSet resultSet = null; Rating rating = null; - String sql = "SELECT RATING, RATED_USERS FROM AP_APP_RELEASE WHERE ID = ?;"; + String sql = "SELECT RATING, RATED_USERS FROM AP_APP_RELEASE WHERE UUID = ?;"; try { connection = this.getDBConnection(); statement = connection.prepareStatement(sql); - statement.setInt(1, id); + statement.setString(1, uuid); resultSet = statement.executeQuery(); if (resultSet.next()){ 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/visibility/GenericVisibilityDAOImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/visibility/GenericVisibilityDAOImpl.java index 9ae0719cd3..05f9ea7345 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/visibility/GenericVisibilityDAOImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/visibility/GenericVisibilityDAOImpl.java @@ -49,7 +49,7 @@ public class GenericVisibilityDAOImpl extends AbstractDAOImpl implements Visibil Connection conn; PreparedStatement stmt = null; ResultSet rs = null; - String sql = "INSERT INTO AP_UNRESTRICTED_ROLES (ROLE, TENANT_ID, AP_APP_ID) VALUES (?, ?, ?)"; + String sql = "INSERT INTO AP_UNRESTRICTED_ROLE (ROLE, TENANT_ID, AP_APP_ID) VALUES (?, ?, ?)"; try{ conn = this.getDBConnection(); conn.setAutoCommit(false); @@ -81,7 +81,7 @@ public class GenericVisibilityDAOImpl extends AbstractDAOImpl implements Visibil ResultSet rs = null; List unrestrictedRoles = new ArrayList<>(); UnrestrictedRole unrestrictedRole; - String sql = "SELECT ID, ROLE FROM AP_UNRESTRICTED_ROLES WHERE AP_APP_ID = ? AND TENANT_ID = ?;"; + String sql = "SELECT ID, ROLE FROM AP_UNRESTRICTED_ROLE WHERE AP_APP_ID = ? AND TENANT_ID = ?;"; try{ conn = this.getDBConnection(); conn.setAutoCommit(false); @@ -115,7 +115,7 @@ public class GenericVisibilityDAOImpl extends AbstractDAOImpl implements Visibil Connection conn; PreparedStatement stmt = null; ResultSet rs = null; - String sql = "DELETE FROM AP_UNRESTRICTED_ROLES WHERE AP_APP_ID = ? AND ROLE = ? AND TENANT_ID = ?;"; + String sql = "DELETE FROM AP_UNRESTRICTED_ROLE WHERE AP_APP_ID = ? AND ROLE = ? AND TENANT_ID = ?;"; try{ conn = this.getDBConnection(); conn.setAutoCommit(false); 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 85e4ec184b..f86834ddd5 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 @@ -47,7 +47,7 @@ import java.util.List; import java.util.TreeMap; /** - * This class is the default implementation for the Managing the comments. + * This class is the default implementation for the Managing the reviews. */ public class ReviewManagerImpl implements ReviewManager { @@ -64,17 +64,17 @@ public class ReviewManagerImpl implements ReviewManager { this.applicationReleaseDAO = ApplicationManagementDAOFactory.getApplicationReleaseDAO(); } - @Override public boolean addReview(Review review, int appId, int appReleaseId) throws ReviewManagementException { + @Override public boolean addReview(Review review, String uuid) throws ReviewManagementException { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); String username = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); boolean isSuccess; try { ConnectionManagerUtil.beginDBTransaction(); - Review existingReview = reviewDAO.isExistReview(appId, appReleaseId, username, tenantId); + Review existingReview = reviewDAO.haveUerCommented(uuid, username, tenantId); if (existingReview != null && review.getRating() > 0 && review.getRating() != existingReview.getRating()) { - Runnable task = () -> calculateRating(review.getRating(), existingReview.getRating()); + Runnable task = () -> calculateRating(review.getRating(), existingReview.getRating(), uuid); new Thread(task).start(); - isSuccess = updateReview(review, existingReview.getId(), false); + isSuccess = updateReview(review, existingReview.getId(),uuid, false); if (isSuccess) { ConnectionManagerUtil.commitDBTransaction(); } else { @@ -82,11 +82,11 @@ public class ReviewManagerImpl implements ReviewManager { } } else { if (review.getRating() > 0) { - Runnable task = () -> calculateRating(review.getRating(), -12345); + Runnable task = () -> calculateRating(review.getRating(), -12345, uuid); new Thread(task).start(); } review.setUsername(username); - isSuccess = this.reviewDAO.addReview(review, appId, appReleaseId, tenantId); + isSuccess = this.reviewDAO.addReview(review, uuid, tenantId); if (isSuccess) { ConnectionManagerUtil.commitDBTransaction(); } else { @@ -97,24 +97,19 @@ public class ReviewManagerImpl implements ReviewManager { } catch (DBConnectionException e) { ConnectionManagerUtil.rollbackDBTransaction(); 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 ReviewManagementException( - "SQL Exception occurs,Review for application with app id:" + appId + " and app release id:" - + appReleaseId + " is failed", e); + "DB Connection error occurs ,Review for application release with UUID: " + uuid + " is failed", e); } catch (TransactionManagementException e) { ConnectionManagerUtil.rollbackDBTransaction(); throw new ReviewManagementException( - "Transaction Management Exception occurs,Review for application with app id:" + appId - + " and app release id:" + appReleaseId + " is failed ", e); + "Transaction Management Exception occurs,Review for application release with UUID:" + uuid + + " is failed ", e); } finally { ConnectionManagerUtil.closeDBConnection(); } } - @Override public boolean updateReview(Review review, int reviewId, boolean checkExistence) + @Override + public boolean updateReview(Review review, int reviewId, String uuid, boolean checkExistence) throws ReviewManagementException { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); String username = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); @@ -131,10 +126,10 @@ public class ReviewManagerImpl implements ReviewManager { "User " + review.getUsername() + "doesn't match with the logged in user: " + username); } if (checkExistence) { - existingReview = this.reviewDAO.getComment(reviewId); + existingReview = this.reviewDAO.getReview(reviewId); if (existingReview != null) { if (review.getRating() > 0 && review.getRating() != existingReview.getRating()) { - Runnable task = () -> calculateRating(review.getRating(), existingReview.getRating()); + Runnable task = () -> calculateRating(review.getRating(), existingReview.getRating(), uuid); new Thread(task).start(); } } else { @@ -216,7 +211,7 @@ public class ReviewManagerImpl implements ReviewManager { } try { ConnectionManagerUtil.openDBConnection(); - review = this.reviewDAO.getComment(commentId); + review = this.reviewDAO.getReview(commentId); } catch (DBConnectionException e) { throw new ReviewManagementException( "DB Connection error occurs ,Review with review id " + commentId + "cannot get.", e); @@ -229,36 +224,44 @@ public class ReviewManagerImpl implements ReviewManager { return review; } - @Override public void deleteReview(String loggedInUser, int commentId) throws ReviewManagementException { + @Override + public boolean deleteReview(String uuid, int reviewId) throws ReviewManagementException { Review existingReview; int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); String username = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); + int isReviewDeleted; try { - 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); + existingReview = getReview(reviewId); if (existingReview == null) { throw new ReviewManagementException( - "Cannot delete a non-existing review for the application with review id" + commentId); + "Cannot delete a non-existing review for the application with review id" + reviewId); } - Runnable task = () -> calculateRating(0, existingReview.getRating()); + Runnable task = () -> calculateRating(0, existingReview.getRating(), uuid); new Thread(task).start(); ConnectionManagerUtil.beginDBTransaction(); - this.reviewDAO.deleteComment(commentId); - ConnectionManagerUtil.commitDBTransaction(); + if (isAdminUser(username, tenantId, CarbonConstants.UI_ADMIN_PERMISSION_COLLECTION)) { + isReviewDeleted = this.reviewDAO.deleteReviewByAdmin(reviewId); + if (isReviewDeleted == 1) { + ConnectionManagerUtil.commitDBTransaction(); + return true; + } + } else { + isReviewDeleted = this.reviewDAO.deleteReview(username, reviewId); + if (isReviewDeleted == 1) { + ConnectionManagerUtil.commitDBTransaction(); + return true; + } + } + return false; } catch (DBConnectionException e) { throw new ReviewManagementException( - "DB Connection error occurs deleting review with review id " + commentId + ".", e); - } catch (SQLException e) { - throw new ReviewManagementException("SQL error occurs deleting review with review id " + commentId + ".", + "DB Connection error occurs deleting review with review id " + reviewId + ".", e); + } catch (ReviewManagementDAOException e) { + throw new ReviewManagementException("Error occured while deleting review with review id " + reviewId + ".", e); } catch (TransactionManagementException e) { throw new ReviewManagementException( - "Transaction Management Exception occurs deleting review with review id " + commentId + ".", e); + "Transaction Management Exception occurs deleting review with review id " + reviewId + ".", e); } catch (UserStoreException e) { throw new ReviewManagementException( "User-store exception while checking whether the user " + username + " of tenant " + tenantId @@ -273,7 +276,7 @@ public class ReviewManagerImpl implements ReviewManager { int appReleaseId = 0; try { ConnectionManagerUtil.openDBConnection(); - Rating rating = this.applicationReleaseDAO.getRating(appReleaseId); + Rating rating = this.applicationReleaseDAO.getRating(appReleaseUuuid); if (rating == null) { throw new ReviewManagementException("Couldn't find rating for application release id: " + appReleaseId + ". Please check the existence of the application relese"); @@ -309,12 +312,12 @@ public class ReviewManagerImpl implements ReviewManager { } } - private void calculateRating(int newRatingVal, int oldRatingVal) { + private void calculateRating(int newRatingVal, int oldRatingVal, String uuid) { // todo need to pass app release id int appReleaseId = 0; try { ConnectionManagerUtil.beginDBTransaction(); - Rating rating = this.applicationReleaseDAO.getRating(appReleaseId); + Rating rating = this.applicationReleaseDAO.getRating(uuid); if (rating == null) { log.error("Couldn't find rating for application release id: " + appReleaseId); } else { 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/ReviewManagementAPI.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/ReviewManagementAPI.java index c8cef67b32..92d2298e50 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/ReviewManagementAPI.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/ReviewManagementAPI.java @@ -255,7 +255,7 @@ public interface ReviewManagementAPI { @PathParam("reviewId") int reviewId); @DELETE - @Path("/{commentId}") + @Path("/{uuid}/{reviewId}") @Produces(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON) @ApiOperation( @@ -287,18 +287,17 @@ public interface ReviewManagementAPI { response = ErrorResponse.class) }) - Response deleteComment( - @ApiParam( - name="commentId", - value="Id of the comment.", - required = true) - @PathParam("commentId") - int commentId, + Response deleteReview( @ApiParam( - name="username", - value="logged in username", + name="uuid", + value="UUID of the application release.", required = true) - @QueryParam("username") String username); + @PathParam("uuid") String uuid, + @ApiParam( + name="reviewId", + value="Id of the review.", + required = true) + @PathParam("reviewId") int reviewId); @GET @Path("/{uuid}/{stars}") 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 e188d4861e..ebcefc0fe2 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 @@ -90,8 +90,7 @@ public class ReviewManagementAPIImpl implements ReviewManagementAPI { String msg = "Found more than one application release for the UUID: " + uuid; return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); } - boolean isReviewCreated = reviewManager - .addReview(review, application.getId(), application.getApplicationReleases().get(0).getId()); + boolean isReviewCreated = reviewManager.addReview(review, uuid); if (isReviewCreated) { return Response.status(Response.Status.CREATED).entity(review).build(); } else { @@ -120,7 +119,7 @@ public class ReviewManagementAPIImpl implements ReviewManagementAPI { @PathParam("reviewId") int reviewId) { ReviewManager reviewManager = APIUtil.getReviewManager(); try { - if (reviewManager.updateReview(review, reviewId, true)) { + if (reviewManager.updateReview(review, reviewId, uuid, true)) { return Response.status(Response.Status.OK).entity(review).build(); } else { String msg = "Review updating failed. Please contact the administrator"; @@ -136,25 +135,26 @@ public class ReviewManagementAPIImpl implements ReviewManagementAPI { @Override @DELETE - @Path("/{commentId}") - public Response deleteComment( - @PathParam("commentId") int commentId, - @QueryParam("username") String username) { + @Path("/{uuid}/{reviewId}") + public Response deleteReview( + @PathParam("uuid") String uuid, + @PathParam("reviewId") int reviewId) { ReviewManager reviewManager = APIUtil.getReviewManager(); try { - if (commentId == 0) { + if (reviewId == 0) { return Response.status(Response.Status.NOT_FOUND).entity("Review not found").build(); + } else if (reviewManager.deleteReview(uuid, reviewId)) { + return Response.status(Response.Status.OK).entity("Review is deleted successfully.").build(); } else { - reviewManager.deleteReview(username, commentId); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity("Review deleting is failed.") + .build(); } } 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) - .build(); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); } - return Response.status(Response.Status.OK).entity("Review is deleted successfully.").build(); } @Override 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 54f3d504a2..c1c361d166 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 @@ -180,7 +180,7 @@ import static org.mockito.MockitoAnnotations.initMocks; // @Test // public void testDeleteComment() throws Exception { // PowerMockito.stub(PowerMockito.method(APIUtil.class, "getReviewManager")).toReturn(this.reviewManager); -// Response response = this.commentManagementAPI.deleteComment(1,""); +// Response response = this.commentManagementAPI.deleteReview(1,""); // Assert.assertNotNull(response, "The response object is null."); // Assert.assertEquals(response.getStatus(), Response.Status.OK.getStatusCode(), // "The response status should be 200."); @@ -189,8 +189,8 @@ 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 ReviewManagementException()); -// Response response = this.commentManagementAPI.deleteComment(1,""); +// Mockito.when(this.commentManagementAPI.deleteReview(1,"")).thenThrow(new ReviewManagementException()); +// Response response = this.commentManagementAPI.deleteReview(1,""); // Assert.assertNotNull(response, "The response object is null."); // Assert.assertEquals(response.getStatus(), Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), // "The response status should be 500."); @@ -199,7 +199,7 @@ import static org.mockito.MockitoAnnotations.initMocks; // @Test // public void testDeleteCommentNotFoundError() throws Exception { // PowerMockito.stub(PowerMockito.method(APIUtil.class, "getReviewManager")).toReturn(this.reviewManager); -// Response response = this.commentManagementAPI.deleteComment(0,""); +// Response response = this.commentManagementAPI.deleteReview(0,""); // Assert.assertNotNull(response, "The response object is null."); // Assert.assertEquals(response.getStatus(), Response.Status.NOT_FOUND.getStatusCode(), // "The response status should be 404.");