Improve review management functionalities

feature/appm-store/pbac
lasanthaDLPDS 6 years ago
parent ba0dec38fa
commit 4692eb5f92

@ -18,16 +18,17 @@
*/
package org.wso2.carbon.device.application.mgt.common.services;
import javassist.NotFoundException;
import org.wso2.carbon.device.application.mgt.common.Rating;
import org.wso2.carbon.device.application.mgt.common.Review;
import org.wso2.carbon.device.application.mgt.common.PaginationRequest;
import org.wso2.carbon.device.application.mgt.common.PaginationResult;
import org.wso2.carbon.device.application.mgt.common.exception.ApplicationManagementException;
import org.wso2.carbon.device.application.mgt.common.exception.RequestValidatingException;
import org.wso2.carbon.device.application.mgt.common.exception.ReviewDoesNotExistException;
import org.wso2.carbon.device.application.mgt.common.exception.ReviewManagementException;
/**
* ReviewManager is responsible for handling all the add/update/delete/get operations related with
*/
@ -41,7 +42,8 @@ public interface ReviewManager {
* @return {@link Review} Review added
* @throws ReviewManagementException Exceptions of the review management.
*/
boolean addReview(Review review, String uuid) throws ReviewManagementException, RequestValidatingException;
boolean addReview(Review review, String uuid)
throws ReviewManagementException, RequestValidatingException, ApplicationManagementException;
/**
* Get all review with pagination

@ -139,4 +139,13 @@ public interface ApplicationReleaseDAO {
*/
boolean verifyReleaseExistence(int appId, String uuid, int tenantId) throws ApplicationManagementDAOException;
/**
* To verify whether application release exist or not for given application release uuid.
*
* @param uuid UUID of the application release.
* @param tenantId Tenant Id
* @throws ApplicationManagementDAOException Application Management DAO Exception.
*/
boolean verifyReleaseExistenceByUuid(String uuid, int tenantId) throws ApplicationManagementDAOException;
}

@ -55,7 +55,7 @@ public class ReviewDAOImpl extends AbstractDAOImpl implements ReviewDAO {
}
PreparedStatement statement = null;
ResultSet rs = null;
sql = "INSERT INTO AP_APP_REVIEW (TENANT_ID, COMMENT, PARENT_ID, USERNAME,CREATED_AT, MODIFIES_AT,"
sql = "INSERT INTO AP_APP_REVIEW (TENANT_ID, COMMENT, PARENT_ID, USERNAME,CREATED_AT, MODIFIED_AT,"
+ " AP_APP_RELEASE_ID, AP_APP_ID) VALUES (?,?,?,?,?,?,(SELECT ID FROM AP_APP_RELEASE WHERE UUID= ?),"
+ "(SELECT AP_APP_ID FROM AP_APP_RELEASE WHERE UUID=?));";
try {
@ -99,7 +99,7 @@ public class ReviewDAOImpl extends AbstractDAOImpl implements ReviewDAO {
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 "
sql = "SELECT ID, COMMENT, CREATED_AT, MODIFIED_AT, USERNAME, PARENT_ID, RATING FROM AP_APP_REVIEW WHERE "
+ "AP_APP_RELEASE_ID = (SELECT ID FROM AP_APP_RELEASE WHERE UUID=?) AND USERNAME = ? AND TENANT_ID = ?;";
try {
conn = this.getDBConnection();
@ -217,9 +217,10 @@ public class ReviewDAOImpl extends AbstractDAOImpl implements ReviewDAO {
conn = this.getDBConnection();
sql = "SELECT AP_APP_REVIEW.ID AS ID, AP_APP_REVIEW.COMMENT AS COMMENT, "
+ "AP_APP_REVIEW.CREATED_AT AS CREATED_AT, AP_APP_REVIEW.MODIFIED_AT AS MODIFIED_AT, "
+ "AP_APP_REVIEW.USERNAME AS USERNAME, AP_APP_REVIEW.PARENT_ID AS PARENT_ID "
+ "FROM AP_APP_REVIEW, AP_APP_RELEASE WHERE AP_APP_REVIEW.AP_APP_RELEASE_ID=AP_APP_RELEASE.ID AND "
+ "AP_APP_RELEASE.UUID =? AND AP_APP_REVIEW.TENANT_ID = ? AND "
+ "AP_APP_REVIEW.USERNAME AS USERNAME, AP_APP_REVIEW.PARENT_ID AS PARENT_ID, "
+ "AP_APP_REVIEW.RATING AS RATING FROM AP_APP_REVIEW, AP_APP_RELEASE WHERE "
+ "AP_APP_REVIEW.AP_APP_RELEASE_ID=AP_APP_RELEASE.ID AND AP_APP_RELEASE.UUID =? "
+ "AND AP_APP_REVIEW.TENANT_ID = ? AND "
+ "AP_APP_REVIEW.TENANT_ID = AP_APP_RELEASE.TENANT_ID LIMIT ? OFFSET ?;";
statement = conn.prepareStatement(sql);
statement.setString(1, uuid);
@ -235,6 +236,7 @@ public class ReviewDAOImpl extends AbstractDAOImpl implements ReviewDAO {
review.setModifiedAt(rs.getTimestamp("MODIFIED_AT"));
review.setParentId(rs.getInt("PARENT_ID"));
review.setUsername(rs.getString("USERNAME"));
review.setRating(rs.getInt("RATING"));
reviews.add(review);
}
} catch (DBConnectionException e) {

@ -297,7 +297,7 @@ public class GenericApplicationReleaseDAOImpl extends AbstractDAOImpl implements
throws ApplicationManagementDAOException {
Connection connection;
PreparedStatement statement = null;
String sql = "UPDATE AP_APP_RELEASE SET RATING = ?,RATED_USERS = ? WHERE UUID = ?;";
String sql = "UPDATE AP_APP_RELEASE SET RATING = ?, RATED_USERS = ? WHERE UUID = ?;";
try {
connection = this.getDBConnection();
statement = connection.prepareStatement(sql);
@ -327,7 +327,7 @@ public class GenericApplicationReleaseDAOImpl extends AbstractDAOImpl implements
PreparedStatement statement = null;
ResultSet resultSet = null;
Rating rating = null;
String sql = "SELECT RATING, RATED_USERS FROM AP_APP_RELEASE WHERE UUID = ? AND TENANT_D=?;";
String sql = "SELECT RATING, RATED_USERS FROM AP_APP_RELEASE WHERE UUID = ? AND TENANT_ID=?;";
try {
connection = this.getDBConnection();
statement = connection.prepareStatement(sql);
@ -466,6 +466,40 @@ public class GenericApplicationReleaseDAOImpl extends AbstractDAOImpl implements
}
}
@Override
public boolean verifyReleaseExistenceByUuid(String uuid, int tenantId) throws ApplicationManagementDAOException {
if (log.isDebugEnabled()) {
log.debug("Verifying application release existence by application release uuid: " + uuid);
}
Connection conn;
PreparedStatement stmt = null;
ResultSet rs = null;
try {
conn = this.getDBConnection();
String sql =
"SELECT AR.ID AS RELEASE_ID FROM AP_APP_RELEASE AS AR WHERE AR.UUID = ? AND AR.TENANT_ID = ?;";
stmt = conn.prepareStatement(sql);
stmt.setString(1, uuid);
stmt.setInt(2, tenantId);
rs = stmt.executeQuery();
if (log.isDebugEnabled()) {
log.debug("Successfully retrieved basic details of the application release with the application UUID: "
+ uuid);
}
return rs.next();
} catch (SQLException e) {
throw new ApplicationManagementDAOException(
"Error occurred when executing query to get application release details for App release uuid: "
+ uuid, e);
} catch (DBConnectionException e) {
throw new ApplicationManagementDAOException("Error occurred while obtaining the DB connection.", e);
} finally {
Util.cleanupResources(stmt, rs);
}
}
/**
* This method is capable to construct {@link ApplicationRelease} and return the object
*

@ -25,7 +25,6 @@ import org.wso2.carbon.device.application.mgt.common.Rating;
import org.wso2.carbon.device.application.mgt.common.Review;
import org.wso2.carbon.device.application.mgt.common.PaginationRequest;
import org.wso2.carbon.device.application.mgt.common.PaginationResult;
import org.wso2.carbon.device.application.mgt.common.exception.RequestValidatingException;
import org.wso2.carbon.device.application.mgt.common.exception.ReviewDoesNotExistException;
import org.wso2.carbon.device.application.mgt.common.exception.ReviewManagementException;
import org.wso2.carbon.device.application.mgt.common.exception.DBConnectionException;
@ -35,6 +34,7 @@ import org.wso2.carbon.device.application.mgt.core.dao.ApplicationReleaseDAO;
import org.wso2.carbon.device.application.mgt.core.dao.ReviewDAO;
import org.wso2.carbon.device.application.mgt.core.dao.common.ApplicationManagementDAOFactory;
import org.wso2.carbon.device.application.mgt.core.exception.ApplicationManagementDAOException;
import org.wso2.carbon.device.application.mgt.core.exception.NotFoundException;
import org.wso2.carbon.device.application.mgt.core.exception.ReviewManagementDAOException;
import org.wso2.carbon.device.application.mgt.core.internal.DataHolder;
import org.wso2.carbon.device.application.mgt.core.util.ConnectionManagerUtil;
@ -64,23 +64,27 @@ public class ReviewManagerImpl implements ReviewManager {
this.applicationReleaseDAO = ApplicationManagementDAOFactory.getApplicationReleaseDAO();
}
@Override public boolean addReview(Review review, String uuid)
throws ReviewManagementException, RequestValidatingException {
@Override
public boolean addReview(Review review, String uuid) throws ReviewManagementException, NotFoundException {
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true);
String username = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername();
boolean isSuccess = false;
try {
ConnectionManagerUtil.openDBConnection();
Review existingReview = reviewDAO.haveUerCommented(uuid, username, tenantId);
if (!this.applicationReleaseDAO.verifyReleaseExistenceByUuid(uuid, tenantId)){
throw new NotFoundException("Couldn't find application release for the application UUID: " + uuid);
}
Review existingReview = this.reviewDAO.haveUerCommented(uuid, username, tenantId);
if (existingReview != null && isAuthorizedUser(username, existingReview.getUsername(), tenantId)
&& review.getRating() > 0 && review.getRating() != existingReview.getRating()) {
Runnable task = () -> calculateRating(review.getRating(), existingReview.getRating(), uuid);
Runnable task = () -> calculateRating(review.getRating(), existingReview.getRating(), uuid, tenantId);
new Thread(task).start();
isSuccess = updateReview(review, existingReview.getId(), uuid, existingReview);
} else if (review.getRating() > 0) {
Runnable task = () -> calculateRating(review.getRating(), -12345, uuid);
Runnable task = () -> calculateRating(review.getRating(), -12345, uuid, tenantId);
new Thread(task).start();
review.setUsername(username);
try {
ConnectionManagerUtil.beginDBTransaction();
isSuccess = this.reviewDAO.addReview(review, uuid, tenantId);
if (isSuccess) {
@ -88,20 +92,23 @@ public class ReviewManagerImpl implements ReviewManager {
} else {
ConnectionManagerUtil.rollbackDBTransaction();
}
} catch (TransactionManagementException e) {
ConnectionManagerUtil.rollbackDBTransaction();
throw new ReviewManagementException(
"Transaction Management Exception occurs,Review for application release with UUID:" + uuid
+ " is failed ", e);
}
}
return isSuccess;
} catch (DBConnectionException e) {
ConnectionManagerUtil.rollbackDBTransaction();
throw new ReviewManagementException(
"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 release with UUID:" + uuid +
" is failed ", e);
} catch (UserStoreException e) {
throw new ReviewManagementException("Error occured while verifying user's permission to update the review.",
e);
} catch (ApplicationManagementDAOException e) {
throw new ReviewManagementException(
"Error occured while verifying whether application release is exists or not.", e);
} finally {
ConnectionManagerUtil.closeDBConnection();
}
@ -112,18 +119,20 @@ public class ReviewManagerImpl implements ReviewManager {
throws ReviewManagementException {
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true);
String username = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername();
boolean isConnectionOpen = false;
if (log.isDebugEnabled()) {
log.debug("Review updating request is received for the review id " + reviewId);
}
try {
if (existingReview == null) {
ConnectionManagerUtil.openDBConnection();
isConnectionOpen = true;
existingReview = this.reviewDAO.getReview(reviewId);
if (existingReview != null && isAuthorizedUser(username, existingReview.getUsername(), tenantId)) {
if (review.getRating() > 0 && review.getRating() != existingReview.getRating()) {
Review finalExistingReview = existingReview;
Runnable task = () -> calculateRating(review.getRating(), finalExistingReview.getRating(),
uuid);
uuid, tenantId);
new Thread(task).start();
}
} else {
@ -138,6 +147,7 @@ public class ReviewManagerImpl implements ReviewManager {
if (review.getRating() == 0) {
review.setRating(existingReview.getRating());
}
try {
ConnectionManagerUtil.beginDBTransaction();
if (this.reviewDAO.updateReview(review, reviewId, username, tenantId) == 1) {
ConnectionManagerUtil.commitDBTransaction();
@ -145,23 +155,32 @@ public class ReviewManagerImpl implements ReviewManager {
}
ConnectionManagerUtil.rollbackDBTransaction();
return false;
} catch (TransactionManagementException e) {
ConnectionManagerUtil.rollbackDBTransaction();
throw new ReviewManagementException(
"Transaction management error occurs when updating review with review id " + reviewId + ".", e);
} catch (ReviewManagementDAOException e) {
ConnectionManagerUtil.rollbackDBTransaction();
throw new ReviewManagementException(
"Error occured while updating review with review id " + reviewId + ".", e);
}
} catch (ReviewManagementDAOException e) {
throw new ReviewManagementException("Error occured while updating review with review id " + reviewId + ".",
throw new ReviewManagementException("Error occured while getting review with review id " + reviewId + ".",
e);
} catch (DBConnectionException e) {
throw new ReviewManagementException(
"DB Connection error occurs updating review with review id " + reviewId + ".", e);
} catch (TransactionManagementException e) {
throw new ReviewManagementException(
"Transaction management error occurs when updating review with review id " + reviewId + ".", e);
} catch (UserStoreException e) {
throw new ReviewManagementException(
"Error occured while verifying user's permission to update the review. review id: " + reviewId
+ ".", e);
} finally {
if (isConnectionOpen) {
ConnectionManagerUtil.closeDBConnection();
}
}
}
@Override
public PaginationResult getAllReviews(PaginationRequest request, String uuid)
@ -221,7 +240,7 @@ public class ReviewManagerImpl implements ReviewManager {
throw new ReviewDoesNotExistException(
"Cannot delete a non-existing review for the application with review id" + reviewId);
}
Runnable task = () -> calculateRating(0, existingReview.getRating(), uuid);
Runnable task = () -> calculateRating(0, existingReview.getRating(), uuid, tenantId);
new Thread(task).start();
ConnectionManagerUtil.beginDBTransaction();
if (isAuthorizedUser(username, existingReview.getUsername(), tenantId)
@ -290,10 +309,9 @@ public class ReviewManagerImpl implements ReviewManager {
}
}
private void calculateRating(int newRatingVal, int oldRatingVal, String uuid) {
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true);
private void calculateRating(int newRatingVal, int oldRatingVal, String uuid, int tenantId) {
try {
ConnectionManagerUtil.beginDBTransaction();
ConnectionManagerUtil.openDBConnection();
Rating rating = this.applicationReleaseDAO.getRating(uuid, tenantId);
if (rating == null) {
log.error("Couldn't find rating for application release uuid: " + uuid);
@ -302,31 +320,36 @@ public class ReviewManagerImpl implements ReviewManager {
int numOfUsers = rating.getNoOfUsers();
double currentRating = rating.getRatingValue() * numOfUsers;
if (oldRatingVal == -12345) {
updatedRating = (currentRating + newRatingVal) / (numOfUsers + 1);
this.applicationReleaseDAO.updateRatingValue(uuid, updatedRating, numOfUsers + 1);
numOfUsers++;
updatedRating = (currentRating + newRatingVal) / numOfUsers;
} else if (newRatingVal == 0) {
updatedRating = (currentRating - newRatingVal) / (numOfUsers - 1);
this.applicationReleaseDAO.updateRatingValue(uuid, updatedRating, numOfUsers - 1);
numOfUsers--;
updatedRating = (currentRating - newRatingVal) / numOfUsers;
} else {
double tmpVal;
tmpVal = currentRating - oldRatingVal;
updatedRating = (tmpVal + newRatingVal) / numOfUsers;
this.applicationReleaseDAO.updateRatingValue(uuid, updatedRating, numOfUsers);
}
}
try {
ConnectionManagerUtil.beginDBTransaction();
this.applicationReleaseDAO.updateRatingValue(uuid, updatedRating, numOfUsers);
ConnectionManagerUtil.commitDBTransaction();
} catch (ApplicationManagementDAOException e) {
ConnectionManagerUtil.rollbackDBTransaction();
log.error("Error occured while updated the rating value of the application release UUID: " + uuid);
} catch (TransactionManagementException e) {
ConnectionManagerUtil.rollbackDBTransaction();
log.error(
"Transaction Management Exception occured while updated the rating value of the application release UUID: "
+ uuid);
} catch (DBConnectionException e) {
+ uuid, e);
} catch (ApplicationManagementDAOException e) {
ConnectionManagerUtil.rollbackDBTransaction();
log.error("Error occured while updated the rating value of the application release UUID: " + uuid,
e);
}
}
} catch (ApplicationManagementDAOException e) {
log.error("Error occured while getting the rating value of the application release UUID: " + uuid, e);
} catch (DBConnectionException e) {
log.error("DB Connection error occured while updated the rating value of the application release UUID: "
+ uuid + " can not get.");
+ uuid + " can not get.", e);
} finally {
ConnectionManagerUtil.closeDBConnection();
}

@ -29,6 +29,7 @@ import org.wso2.carbon.device.application.mgt.common.exception.RequestValidating
import org.wso2.carbon.device.application.mgt.common.exception.ReviewDoesNotExistException;
import org.wso2.carbon.device.application.mgt.common.services.ApplicationManager;
import org.wso2.carbon.device.application.mgt.common.services.ReviewManager;
import org.wso2.carbon.device.application.mgt.core.exception.NotFoundException;
import org.wso2.carbon.device.application.mgt.store.api.APIUtil;
import org.wso2.carbon.device.application.mgt.store.api.services.ReviewManagementAPI;
import org.wso2.carbon.device.application.mgt.common.PaginationRequest;
@ -81,18 +82,7 @@ public class ReviewManagementAPIImpl implements ReviewManagementAPI {
@ApiParam Review review,
@PathParam("uuid") String uuid) {
ReviewManager reviewManager = APIUtil.getReviewManager();
ApplicationManager applicationManager = APIUtil.getApplicationManager();
Application application;
try {
application = applicationManager.getApplicationByRelease(uuid);
if (application.getApplicationReleases().isEmpty()){
String msg = "Couldn't Found an one application release for the UUID: " + uuid;
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
}
if (application.getApplicationReleases().size()>1){
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, uuid);
if (isReviewCreated) {
return Response.status(Response.Status.CREATED).entity(review).build();
@ -101,18 +91,23 @@ public class ReviewManagementAPIImpl implements ReviewManagementAPI {
log.error(msg);
return Response.status(Response.Status.BAD_REQUEST).entity(msg).build();
}
} catch (NotFoundException e) {
String msg = "Couldn't find an application release for UUID: " + uuid;
log.error(msg, e);
return Response.status(Response.Status.NOT_FOUND).entity(msg).build();
} catch (ReviewManagementException e) {
String msg = "Error occurred while creating the review";
log.error(msg, e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
} catch (ApplicationManagementException e) {
log.error("Error occured while getting the application for application UUID: " + uuid);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR)
.entity("").build();
} catch (RequestValidatingException e) {
String msg = "Error occurred while adding for application release. UUID of the application release: " + uuid;
String msg =
"Error occurred while adding for application release. UUID of the application release: " + uuid;
log.error(msg, e);
return Response.status(Response.Status.BAD_REQUEST).entity(msg).build();
} catch (ApplicationManagementException e) {
String msg = "Error occured while accessing application release for UUID: " + uuid;
log.error(msg, e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
}
}

Loading…
Cancel
Save