Merge branch 'application-mgt-new' into 'application-mgt-new'

Fix DAO layer issues and improve review management

See merge request entgra/carbon-device-mgt!6
feature/appm-store/pbac
Dharmakeerthi Lasantha 6 years ago
commit d1095826b8

@ -33,12 +33,11 @@ public interface ReviewManager {
* To add a review to a application * To add a review to a application
* *
* @param review review of the application. * @param review review of the application.
* @param appId id of the application. * @param uuid uuid of the application release.
* @param appReleaseId id of the application release
* @return {@link Review} Review added * @return {@link Review} Review added
* @throws ReviewManagementException Exceptions of the review management. * @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 * Get all comments to pagination
@ -62,20 +61,25 @@ public interface ReviewManager {
/** /**
* To delete review using review id. * To delete review using review id.
* *
* @param uuid UUID of the application release
* @param commentId id of the comment * @param commentId id of the comment
* @return If review is successfully deleted return true, otherwise returns false
* @throws ReviewManagementException Exceptions of the comment management * @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. * To update a review.
* *
* @param review review of the application. * @param review review of the application.
* @param reviewId id of the review * @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 * @return {@link Review}updated review
* @throws ReviewManagementException Exceptions of the review management * @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 * To get the overall rating for a application release

@ -90,10 +90,10 @@ public interface ApplicationReleaseDAO {
/** /**
* To retrieve rating of an application release. * 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. * @throws ApplicationManagementDAOException Application Management DAO Exception.
*/ */
Rating getRating(int id) throws ApplicationManagementDAOException; Rating getRating(String uuid) throws ApplicationManagementDAOException;
/** /**

@ -28,24 +28,32 @@ import java.sql.SQLException;
import java.util.List; 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 { 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 review review of the application.
* @param uuid UUID of the application release
* @return If review is added successfully, it return true otherwise false * @return If review is added successfully, it return true otherwise false
* @throws ReviewManagementDAOException Exceptions of the review management DAO. * @throws ReviewManagementDAOException Exceptions of the review management DAO.
*/ */
boolean addReview(Review review, int appId, int appReleaseId, int tenantId) throws ReviewManagementDAOException; boolean addReview(Review review, String uuid, int tenantId) throws ReviewManagementDAOException;
/**
Review isExistReview(int appId, int appReleaseId, String username, int tenantId) * To verify whether review is exists or not.
throws DBConnectionException, SQLException; *
* @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. * To update already added comment.
@ -68,7 +76,7 @@ import java.util.List;
* @throws DBConnectionException db connection exception * @throws DBConnectionException db connection exception
* @throws SQLException sql 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 * To get all the comments
@ -101,18 +109,27 @@ import java.util.List;
* @throws DBConnectionException db connection exception. * @throws DBConnectionException db connection exception.
* @throws SQLException sql 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; 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 * @param username username of the review owner
* @throws ReviewManagementException Exceptions of the comment management. * @param reviewId id of the review
* @throws DBConnectionException db connection exception. * @return If review is successfully deleted return 1, otherwise returns 0.
* @throws SQLException sql exception * @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. * To delete comments using application details.
@ -122,15 +139,15 @@ import java.util.List;
* @param version version of the commented application. * @param version version of the commented application.
* @throws ReviewManagementException Exceptions of the comment management. * @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 * To get comment count for pagination
* *
* @param request * @param request pagination request
* @param uuid * @param uuid uuid of the application release
* @return Review count * @return Review count
* @throws ReviewManagementException * @throws ReviewManagementException
*/ */
int getCommentCount(PaginationRequest request, String uuid) throws ReviewManagementException; int getReviewCount(PaginationRequest request, String uuid) throws ReviewManagementException;
} }

@ -48,12 +48,10 @@ public class ReviewDAOImpl extends AbstractDAOImpl implements ReviewDAO {
private String sql; private String sql;
@Override @Override
public boolean addReview(Review review, int appId, int appReleaseId, int tenantId) public boolean addReview(Review review, String uuid, int tenantId)
throws ReviewManagementDAOException { throws ReviewManagementDAOException {
//todo
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("Request received in DAO Layer to add review for application release. Application id: " + appId log.debug("Request received in DAO Layer to add review for application release. Application UUID: " + uuid);
+ "Application Release id: " + appReleaseId);
} }
PreparedStatement statement = null; PreparedStatement statement = null;
ResultSet rs = null; ResultSet rs = null;
@ -67,8 +65,8 @@ public class ReviewDAOImpl extends AbstractDAOImpl implements ReviewDAO {
statement.setString(2, review.getComment()); statement.setString(2, review.getComment());
statement.setInt(3, review.getParentId()); statement.setInt(3, review.getParentId());
statement.setString(4, review.getUsername()); statement.setString(4, review.getUsername());
statement.setString(5,""); statement.setString(5,uuid);
statement.setString(6,""); statement.setString(6,uuid);
statement.executeUpdate(); statement.executeUpdate();
rs = statement.getGeneratedKeys(); rs = statement.getGeneratedKeys();
return rs.next(); return rs.next();
@ -85,26 +83,25 @@ public class ReviewDAOImpl extends AbstractDAOImpl implements ReviewDAO {
} }
@Override @Override
public Review isExistReview(int appId, int appReleaseId, String username, int tenantId) public Review haveUerCommented(String uuid, String username, int tenantId) throws ReviewManagementDAOException {
throws DBConnectionException, SQLException {
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug( log.debug(
"Request received in DAO Layer to check whether review exist or not rein the IoTS APPM. Application id: " "Request received in DAO Layer to check whether user have already commented or not for the "
+ appId + " Application release id: " + appReleaseId + " comment owner: " + username); + "application release. Application UUID: " + uuid + " comment owner: " + username +
" tenant-id " + tenantId);
} }
Connection conn; Connection conn;
PreparedStatement statement = null; PreparedStatement statement = null;
ResultSet rs = null; ResultSet rs = null;
Review review = null; Review review = null;
sql = "SELECT ID, COMMENT, CREATED_AT, MODEFIED_AT, USERNAME, PARENT_ID, RATING FROM AP_APP_REVIEW WHERE " 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 { try {
conn = this.getDBConnection(); conn = this.getDBConnection();
statement = conn.prepareStatement(sql); statement = conn.prepareStatement(sql);
statement.setInt(1, appId); statement.setString(1, uuid);
statement.setInt(2, appReleaseId); statement.setString(2, username);
statement.setString(3, username); statement.setInt(3, tenantId);
statement.setInt(4, tenantId);
rs = statement.executeQuery(); rs = statement.executeQuery();
if (rs.next()){ if (rs.next()){
@ -118,6 +115,13 @@ public class ReviewDAOImpl extends AbstractDAOImpl implements ReviewDAO {
review.setRating(rs.getInt("RATING")); review.setRating(rs.getInt("RATING"));
} }
return review; 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 { } finally {
Util.cleanupResources(statement, rs); Util.cleanupResources(statement, rs);
} }
@ -149,7 +153,7 @@ public class ReviewDAOImpl extends AbstractDAOImpl implements ReviewDAO {
} }
@Override @Override
public Review getComment(int commentId) throws ReviewManagementException { public Review getReview(int commentId) throws ReviewManagementException {
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("Getting review with the review id(" + commentId + ") from the database"); log.debug("Getting review with the review id(" + commentId + ") from the database");
@ -259,7 +263,7 @@ public class ReviewDAOImpl extends AbstractDAOImpl implements ReviewDAO {
} }
@Override @Override
public int getCommentCount(PaginationRequest request, String uuid) throws ReviewManagementException { public int getReviewCount(PaginationRequest request, String uuid) throws ReviewManagementException {
int commentCount = 0; int commentCount = 0;
Connection conn; Connection conn;
@ -293,7 +297,7 @@ public class ReviewDAOImpl extends AbstractDAOImpl implements ReviewDAO {
} }
@Override @Override
public int getCommentCountByApp(String appType, String appName, String version) public int getReviewCountByApp(String appType, String appName, String version)
throws ReviewManagementException, DBConnectionException, SQLException { throws ReviewManagementException, DBConnectionException, SQLException {
Connection conn; Connection conn;
@ -320,24 +324,48 @@ public class ReviewDAOImpl extends AbstractDAOImpl implements ReviewDAO {
} }
@Override @Override
public void deleteComment(int commentId) public int deleteReview(String username, int reviewId) throws ReviewManagementDAOException {
throws ReviewManagementException, DBConnectionException, SQLException { 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; Connection conn;
PreparedStatement statement = null; PreparedStatement statement = null;
try { try {
conn = this.getDBConnection(); conn = this.getDBConnection();
sql = "DELETE FROM AP_APP_COMMENT WHERE ID=?;"; sql = "DELETE FROM AP_APP_REVIEW WHERE ID=?;";
statement = conn.prepareStatement(sql); statement = conn.prepareStatement(sql);
statement.setInt(1, commentId); statement.setInt(1, reviewId);
statement.executeUpdate(); 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 { } finally {
Util.cleanupResources(statement, null); Util.cleanupResources(statement, null);
} }
} }
@Override @Override
public void deleteComments(String appType, String appName, String version) public void deleteReviews(String appType, String appName, String version)
throws ReviewManagementException { throws ReviewManagementException {
Connection conn; Connection conn;

@ -136,9 +136,9 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic
Pagination pagination = new Pagination(); 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" 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, " + " 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) " + "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 != ?"; + "WHERE AP_APP.TENANT_ID = ? AND AP_APP.STATUS != ?";
@ -299,8 +299,8 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic
String sql = 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 " "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," + "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, AP_APP_TAG, AP_UNRESTRICTED_ROLES WHERE AP_APP.NAME=? AND " + "AS ROLE FROM AP_APP, AP_APP_TAG, AP_UNRESTRICTED_ROLE WHERE AP_APP.NAME=? AND "
+ "AP_APP.TYPE= ? AND AP_APP.TENANT_ID=?;"; + "AP_APP.TYPE= ? AND AP_APP.TENANT_ID=?;";
stmt = conn.prepareStatement(sql); stmt = conn.prepareStatement(sql);
@ -343,8 +343,8 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic
String sql = 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 " "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, " + "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, AP_APP_TAG, AP_UNRESTRICTED_ROLES WHERE AP_APP.ID=? AND " + "AS ROLE FROM AP_APP, AP_APP_TAG, AP_UNRESTRICTED_ROLE WHERE AP_APP.ID=? AND "
+ "AP_APP.TENANT_ID=?;"; + "AP_APP.TENANT_ID=?;";
stmt = conn.prepareStatement(sql); stmt = conn.prepareStatement(sql);
@ -385,7 +385,7 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic
String sql = 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 " "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, " + "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 = ?;"; + "WHERE AP_APP.ID = ?;";
stmt = conn.prepareStatement(sql); stmt = conn.prepareStatement(sql);
@ -571,7 +571,7 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic
+ "AP_APP_RELEASE.STARS," + "AP_APP_RELEASE.STARS,"
+ "AP_APP.ID AS APP_ID, AP_APP.NAME AS APP_NAME, AP_APP.TYPE AS APP_TYPE, " + "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.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=?;"; + "WHERE AP_APP_RELEASE.UUID=? AND AP_APP.TENANT_ID=?;";
stmt = conn.prepareStatement(sql); stmt = conn.prepareStatement(sql);

@ -308,20 +308,20 @@ public class GenericApplicationReleaseDAOImpl extends AbstractDAOImpl implements
/** /**
* To retrieve rating of an application release. * 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. * @throws ApplicationManagementDAOException Application Management DAO Exception.
*/ */
@Override @Override
public Rating getRating(int id) throws ApplicationManagementDAOException { public Rating getRating(String uuid) throws ApplicationManagementDAOException {
Connection connection; Connection connection;
PreparedStatement statement = null; PreparedStatement statement = null;
ResultSet resultSet = null; ResultSet resultSet = null;
Rating rating = 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 { try {
connection = this.getDBConnection(); connection = this.getDBConnection();
statement = connection.prepareStatement(sql); statement = connection.prepareStatement(sql);
statement.setInt(1, id); statement.setString(1, uuid);
resultSet = statement.executeQuery(); resultSet = statement.executeQuery();
if (resultSet.next()){ if (resultSet.next()){

@ -49,7 +49,7 @@ public class GenericVisibilityDAOImpl extends AbstractDAOImpl implements Visibil
Connection conn; Connection conn;
PreparedStatement stmt = null; PreparedStatement stmt = null;
ResultSet rs = 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{ try{
conn = this.getDBConnection(); conn = this.getDBConnection();
conn.setAutoCommit(false); conn.setAutoCommit(false);
@ -81,7 +81,7 @@ public class GenericVisibilityDAOImpl extends AbstractDAOImpl implements Visibil
ResultSet rs = null; ResultSet rs = null;
List<UnrestrictedRole> unrestrictedRoles = new ArrayList<>(); List<UnrestrictedRole> unrestrictedRoles = new ArrayList<>();
UnrestrictedRole unrestrictedRole; 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{ try{
conn = this.getDBConnection(); conn = this.getDBConnection();
conn.setAutoCommit(false); conn.setAutoCommit(false);
@ -115,7 +115,7 @@ public class GenericVisibilityDAOImpl extends AbstractDAOImpl implements Visibil
Connection conn; Connection conn;
PreparedStatement stmt = null; PreparedStatement stmt = null;
ResultSet rs = 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{ try{
conn = this.getDBConnection(); conn = this.getDBConnection();
conn.setAutoCommit(false); conn.setAutoCommit(false);

@ -47,7 +47,7 @@ import java.util.List;
import java.util.TreeMap; 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 { public class ReviewManagerImpl implements ReviewManager {
@ -64,17 +64,17 @@ public class ReviewManagerImpl implements ReviewManager {
this.applicationReleaseDAO = ApplicationManagementDAOFactory.getApplicationReleaseDAO(); 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); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true);
String username = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); String username = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername();
boolean isSuccess; boolean isSuccess;
try { try {
ConnectionManagerUtil.beginDBTransaction(); 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()) { 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(); new Thread(task).start();
isSuccess = updateReview(review, existingReview.getId(), false); isSuccess = updateReview(review, existingReview.getId(),uuid, false);
if (isSuccess) { if (isSuccess) {
ConnectionManagerUtil.commitDBTransaction(); ConnectionManagerUtil.commitDBTransaction();
} else { } else {
@ -82,11 +82,11 @@ public class ReviewManagerImpl implements ReviewManager {
} }
} else { } else {
if (review.getRating() > 0) { if (review.getRating() > 0) {
Runnable task = () -> calculateRating(review.getRating(), -12345); Runnable task = () -> calculateRating(review.getRating(), -12345, uuid);
new Thread(task).start(); new Thread(task).start();
} }
review.setUsername(username); review.setUsername(username);
isSuccess = this.reviewDAO.addReview(review, appId, appReleaseId, tenantId); isSuccess = this.reviewDAO.addReview(review, uuid, tenantId);
if (isSuccess) { if (isSuccess) {
ConnectionManagerUtil.commitDBTransaction(); ConnectionManagerUtil.commitDBTransaction();
} else { } else {
@ -97,24 +97,19 @@ public class ReviewManagerImpl implements ReviewManager {
} catch (DBConnectionException e) { } catch (DBConnectionException e) {
ConnectionManagerUtil.rollbackDBTransaction(); ConnectionManagerUtil.rollbackDBTransaction();
throw new ReviewManagementException( throw new ReviewManagementException(
"DB Connection error occurs ,Review for application with app id: " + appId + " and app release id: " "DB Connection error occurs ,Review for application release with UUID: " + uuid + " is failed", e);
+ 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);
} catch (TransactionManagementException e) { } catch (TransactionManagementException e) {
ConnectionManagerUtil.rollbackDBTransaction(); ConnectionManagerUtil.rollbackDBTransaction();
throw new ReviewManagementException( throw new ReviewManagementException(
"Transaction Management Exception occurs,Review for application with app id:" + appId "Transaction Management Exception occurs,Review for application release with UUID:" + uuid +
+ " and app release id:" + appReleaseId + " is failed ", e); " is failed ", e);
} finally { } finally {
ConnectionManagerUtil.closeDBConnection(); 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 { throws ReviewManagementException {
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true);
String username = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); 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); "User " + review.getUsername() + "doesn't match with the logged in user: " + username);
} }
if (checkExistence) { if (checkExistence) {
existingReview = this.reviewDAO.getComment(reviewId); existingReview = this.reviewDAO.getReview(reviewId);
if (existingReview != null) { if (existingReview != null) {
if (review.getRating() > 0 && review.getRating() != existingReview.getRating()) { 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(); new Thread(task).start();
} }
} else { } else {
@ -216,7 +211,7 @@ public class ReviewManagerImpl implements ReviewManager {
} }
try { try {
ConnectionManagerUtil.openDBConnection(); ConnectionManagerUtil.openDBConnection();
review = this.reviewDAO.getComment(commentId); review = this.reviewDAO.getReview(commentId);
} catch (DBConnectionException e) { } catch (DBConnectionException e) {
throw new ReviewManagementException( throw new ReviewManagementException(
"DB Connection error occurs ,Review with review id " + commentId + "cannot get.", e); "DB Connection error occurs ,Review with review id " + commentId + "cannot get.", e);
@ -229,36 +224,44 @@ public class ReviewManagerImpl implements ReviewManager {
return review; return review;
} }
@Override public void deleteReview(String loggedInUser, int commentId) throws ReviewManagementException { @Override
public boolean deleteReview(String uuid, int reviewId) throws ReviewManagementException {
Review existingReview; Review existingReview;
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true);
String username = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); String username = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername();
int isReviewDeleted;
try { try {
if (!loggedInUser.equals(username) && !isAdminUser(username, tenantId, existingReview = getReview(reviewId);
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) { if (existingReview == null) {
throw new ReviewManagementException( 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(); new Thread(task).start();
ConnectionManagerUtil.beginDBTransaction(); ConnectionManagerUtil.beginDBTransaction();
this.reviewDAO.deleteComment(commentId); if (isAdminUser(username, tenantId, CarbonConstants.UI_ADMIN_PERMISSION_COLLECTION)) {
ConnectionManagerUtil.commitDBTransaction(); 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) { } catch (DBConnectionException e) {
throw new ReviewManagementException( throw new ReviewManagementException(
"DB Connection error occurs deleting review with review id " + commentId + ".", e); "DB Connection error occurs deleting review with review id " + reviewId + ".", e);
} catch (SQLException e) { } catch (ReviewManagementDAOException e) {
throw new ReviewManagementException("SQL error occurs deleting review with review id " + commentId + ".", throw new ReviewManagementException("Error occured while deleting review with review id " + reviewId + ".",
e); e);
} catch (TransactionManagementException e) { } catch (TransactionManagementException e) {
throw new ReviewManagementException( 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) { } catch (UserStoreException e) {
throw new ReviewManagementException( throw new ReviewManagementException(
"User-store exception while checking whether the user " + username + " of tenant " + tenantId "User-store exception while checking whether the user " + username + " of tenant " + tenantId
@ -273,7 +276,7 @@ public class ReviewManagerImpl implements ReviewManager {
int appReleaseId = 0; int appReleaseId = 0;
try { try {
ConnectionManagerUtil.openDBConnection(); ConnectionManagerUtil.openDBConnection();
Rating rating = this.applicationReleaseDAO.getRating(appReleaseId); Rating rating = this.applicationReleaseDAO.getRating(appReleaseUuuid);
if (rating == null) { if (rating == null) {
throw new ReviewManagementException("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"); + ". 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 // todo need to pass app release id
int appReleaseId = 0; int appReleaseId = 0;
try { try {
ConnectionManagerUtil.beginDBTransaction(); ConnectionManagerUtil.beginDBTransaction();
Rating rating = this.applicationReleaseDAO.getRating(appReleaseId); Rating rating = this.applicationReleaseDAO.getRating(uuid);
if (rating == null) { if (rating == null) {
log.error("Couldn't find rating for application release id: " + appReleaseId); log.error("Couldn't find rating for application release id: " + appReleaseId);
} else { } else {

@ -255,7 +255,7 @@ public interface ReviewManagementAPI {
@PathParam("reviewId") int reviewId); @PathParam("reviewId") int reviewId);
@DELETE @DELETE
@Path("/{commentId}") @Path("/{uuid}/{reviewId}")
@Produces(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON)
@ApiOperation( @ApiOperation(
@ -287,18 +287,17 @@ public interface ReviewManagementAPI {
response = ErrorResponse.class) response = ErrorResponse.class)
}) })
Response deleteComment( Response deleteReview(
@ApiParam(
name="commentId",
value="Id of the comment.",
required = true)
@PathParam("commentId")
int commentId,
@ApiParam( @ApiParam(
name="username", name="uuid",
value="logged in username", value="UUID of the application release.",
required = true) 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 @GET
@Path("/{uuid}/{stars}") @Path("/{uuid}/{stars}")

@ -90,8 +90,7 @@ public class ReviewManagementAPIImpl implements ReviewManagementAPI {
String msg = "Found more than one application release for the UUID: " + uuid; String msg = "Found more than one application release for the UUID: " + uuid;
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
} }
boolean isReviewCreated = reviewManager boolean isReviewCreated = reviewManager.addReview(review, uuid);
.addReview(review, application.getId(), application.getApplicationReleases().get(0).getId());
if (isReviewCreated) { if (isReviewCreated) {
return Response.status(Response.Status.CREATED).entity(review).build(); return Response.status(Response.Status.CREATED).entity(review).build();
} else { } else {
@ -120,7 +119,7 @@ public class ReviewManagementAPIImpl implements ReviewManagementAPI {
@PathParam("reviewId") int reviewId) { @PathParam("reviewId") int reviewId) {
ReviewManager reviewManager = APIUtil.getReviewManager(); ReviewManager reviewManager = APIUtil.getReviewManager();
try { try {
if (reviewManager.updateReview(review, reviewId, true)) { if (reviewManager.updateReview(review, reviewId, uuid, true)) {
return Response.status(Response.Status.OK).entity(review).build(); return Response.status(Response.Status.OK).entity(review).build();
} else { } else {
String msg = "Review updating failed. Please contact the administrator"; String msg = "Review updating failed. Please contact the administrator";
@ -136,25 +135,26 @@ public class ReviewManagementAPIImpl implements ReviewManagementAPI {
@Override @Override
@DELETE @DELETE
@Path("/{commentId}") @Path("/{uuid}/{reviewId}")
public Response deleteComment( public Response deleteReview(
@PathParam("commentId") int commentId, @PathParam("uuid") String uuid,
@QueryParam("username") String username) { @PathParam("reviewId") int reviewId) {
ReviewManager reviewManager = APIUtil.getReviewManager(); ReviewManager reviewManager = APIUtil.getReviewManager();
try { try {
if (commentId == 0) { if (reviewId == 0) {
return Response.status(Response.Status.NOT_FOUND).entity("Review not found").build(); 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 { } else {
reviewManager.deleteReview(username, commentId); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity("Review deleting is failed.")
.build();
} }
} catch (ReviewManagementException e) { } catch (ReviewManagementException e) {
String msg = "Error occurred while deleting the comment."; String msg = "Error occurred while deleting the comment.";
log.error(msg, e); log.error(msg, e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg) return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
.build();
} }
return Response.status(Response.Status.OK).entity("Review is deleted successfully.").build();
} }
@Override @Override

@ -180,7 +180,7 @@ import static org.mockito.MockitoAnnotations.initMocks;
// @Test // @Test
// public void testDeleteComment() throws Exception { // public void testDeleteComment() throws Exception {
// PowerMockito.stub(PowerMockito.method(APIUtil.class, "getReviewManager")).toReturn(this.reviewManager); // 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.assertNotNull(response, "The response object is null.");
// Assert.assertEquals(response.getStatus(), Response.Status.OK.getStatusCode(), // Assert.assertEquals(response.getStatus(), Response.Status.OK.getStatusCode(),
// "The response status should be 200."); // "The response status should be 200.");
@ -189,8 +189,8 @@ import static org.mockito.MockitoAnnotations.initMocks;
// @Test // @Test
// public void testDeleteCommentInternalError() throws Exception { // public void testDeleteCommentInternalError() throws Exception {
// PowerMockito.stub(PowerMockito.method(APIUtil.class, "getReviewManager")).toReturn(this.reviewManager); // PowerMockito.stub(PowerMockito.method(APIUtil.class, "getReviewManager")).toReturn(this.reviewManager);
// Mockito.when(this.commentManagementAPI.deleteComment(1,"")).thenThrow(new ReviewManagementException()); // Mockito.when(this.commentManagementAPI.deleteReview(1,"")).thenThrow(new ReviewManagementException());
// Response response = this.commentManagementAPI.deleteComment(1,""); // Response response = this.commentManagementAPI.deleteReview(1,"");
// Assert.assertNotNull(response, "The response object is null."); // Assert.assertNotNull(response, "The response object is null.");
// Assert.assertEquals(response.getStatus(), Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), // Assert.assertEquals(response.getStatus(), Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(),
// "The response status should be 500."); // "The response status should be 500.");
@ -199,7 +199,7 @@ import static org.mockito.MockitoAnnotations.initMocks;
// @Test // @Test
// public void testDeleteCommentNotFoundError() throws Exception { // public void testDeleteCommentNotFoundError() throws Exception {
// PowerMockito.stub(PowerMockito.method(APIUtil.class, "getReviewManager")).toReturn(this.reviewManager); // 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.assertNotNull(response, "The response object is null.");
// Assert.assertEquals(response.getStatus(), Response.Status.NOT_FOUND.getStatusCode(), // Assert.assertEquals(response.getStatus(), Response.Status.NOT_FOUND.getStatusCode(),
// "The response status should be 404."); // "The response status should be 404.");

Loading…
Cancel
Save