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