Improve review update logic

feature/appm-store/pbac
lasanthaDLPDS 5 years ago
parent 7296e38608
commit a5d39e76b5

@ -78,7 +78,7 @@ public interface ReviewManager {
* @return {@link Review}updated review * @return {@link Review}updated review
* @throws ReviewManagementException Exceptions of the reviewTmp management * @throws ReviewManagementException Exceptions of the reviewTmp management
*/ */
boolean updateReview(ReviewWrapper updatingReview, int reviewId, String uuid, boolean isPrivilegedUser) Review updateReview(ReviewWrapper updatingReview, int reviewId, String uuid, boolean isPrivilegedUser)
throws ReviewManagementException, ApplicationManagementException; throws ReviewManagementException, ApplicationManagementException;
/** /**

@ -62,7 +62,7 @@ import java.util.List;
* @return row count if updating is succeed otherwise 0 * @return row count if updating is succeed otherwise 0
* @throws ReviewManagementDAOException Exceptions of the reviewTmp management. * @throws ReviewManagementDAOException Exceptions of the reviewTmp management.
*/ */
int updateReview(ReviewDTO reviewDTO, int reviewId, boolean isActiveReview, int tenantId) ReviewDTO updateReview(ReviewDTO reviewDTO, int reviewId, boolean isActiveReview, int tenantId)
throws ReviewManagementDAOException; throws ReviewManagementDAOException;

@ -142,7 +142,7 @@ public class ReviewDAOImpl extends AbstractDAOImpl implements ReviewDAO {
} }
@Override @Override
public int updateReview(ReviewDTO reviewDTO, int reviewId, boolean isActiveReview, int tenantId) public ReviewDTO updateReview(ReviewDTO reviewDTO, int reviewId, boolean isActiveReview, int tenantId)
throws ReviewManagementDAOException { throws ReviewManagementDAOException {
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("Request received to DAO Layer to update the Review which has ID " + reviewId); log.debug("Request received to DAO Layer to update the Review which has ID " + reviewId);
@ -167,7 +167,11 @@ public class ReviewDAOImpl extends AbstractDAOImpl implements ReviewDAO {
statement.setBoolean(4, isActiveReview); statement.setBoolean(4, isActiveReview);
statement.setInt(5, reviewId); statement.setInt(5, reviewId);
statement.setInt(6, tenantId); statement.setInt(6, tenantId);
return statement.executeUpdate(); if (statement.executeUpdate() == 1) {
reviewDTO.setModifiedAt(timestamp);
return reviewDTO;
}
return null;
} }
} catch (DBConnectionException e) { } catch (DBConnectionException e) {
String msg = "Error occured while getting the db connection to update review for review ID: " + reviewId; String msg = "Error occured while getting the db connection to update review for review ID: " + reviewId;

@ -272,7 +272,7 @@ public class ReviewManagerImpl implements ReviewManager {
} }
@Override @Override
public boolean updateReview(ReviewWrapper updatingReview, int reviewId, String uuid, public Review updateReview(ReviewWrapper updatingReview, int reviewId, String uuid,
boolean isPrivilegedUser) throws ReviewManagementException, ApplicationManagementException { boolean isPrivilegedUser) throws ReviewManagementException, ApplicationManagementException {
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true);
String username = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); String username = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername();
@ -299,6 +299,8 @@ public class ReviewManagerImpl implements ReviewManager {
.calculateRating(updatingReview.getRating(), reviewDTO.getRating(), uuid, tenantId); .calculateRating(updatingReview.getRating(), reviewDTO.getRating(), uuid, tenantId);
new Thread(task).start(); new Thread(task).start();
reviewDTO.setRating(updatingReview.getRating()); reviewDTO.setRating(updatingReview.getRating());
}
if (!reviewDTO.getContent().equals(updatingReview.getContent())) {
reviewDTO.setContent(updatingReview.getContent()); reviewDTO.setContent(updatingReview.getContent());
} }
} else { } else {
@ -310,22 +312,28 @@ public class ReviewManagerImpl implements ReviewManager {
reviewDTO.setContent(updatingReview.getContent()); reviewDTO.setContent(updatingReview.getContent());
} }
if (updateReviewInDB(reviewDTO, reviewId, isActiveReview, tenantId)) { ReviewDTO updatedReviewDTO = updateReviewInDB(reviewDTO, reviewId, isActiveReview, tenantId);
if (updatedReviewDTO != null) {
if (!isActiveReview) { if (!isActiveReview) {
if (addReview(updatingReview, uuid, true) != null) { Review newReview = addReview(updatingReview, uuid, true);
return true; if (newReview != null) {
return newReview;
} else { } else {
if (updateReviewInDB(reviewDTO, reviewId, true, tenantId)) { ReviewDTO restoringReviewDTO = updateReviewInDB(reviewDTO, reviewId, true, tenantId);
return false; if (restoringReviewDTO != null) {
String msg = "Review Updating Status: Adding new Review for application release which has"
+ " UUID: " + uuid + " is failed and the old review is restored.";
log.error(msg);
throw new ApplicationManagementException(msg);
} else { } else {
String msg = "Review Updating Status: Adding new Review for application release which has UUID: " String msg = "Review Updating Status: Adding new Review for application release which has "
+ "" + uuid + " is failed and the old review restoring is also failed."; + "UUID: " + uuid + " is failed and the old review restoring is also failed.";
log.error(msg); log.error(msg);
throw new ApplicationManagementException(msg); throw new ApplicationManagementException(msg);
} }
} }
} }
return true; return reviewDTOToReview(updatedReviewDTO);
} else { } else {
String msg = "Review Updating is failed. Hence please contact the administrator."; String msg = "Review Updating is failed. Hence please contact the administrator.";
log.error(msg); log.error(msg);
@ -333,16 +341,17 @@ public class ReviewManagerImpl implements ReviewManager {
} }
} }
private boolean updateReviewInDB(ReviewDTO reviewDTO, int reviewId, boolean isActiveReview, private ReviewDTO updateReviewInDB(ReviewDTO reviewDTO, int reviewId, boolean isActiveReview,
int tenantId) throws ReviewManagementException, ApplicationManagementException { int tenantId) throws ReviewManagementException, ApplicationManagementException {
try { try {
ConnectionManagerUtil.beginDBTransaction(); ConnectionManagerUtil.beginDBTransaction();
if (this.reviewDAO.updateReview(reviewDTO, reviewId, isActiveReview, tenantId) == 1) { ReviewDTO updatedReviewDTO = this.reviewDAO.updateReview(reviewDTO, reviewId, isActiveReview, tenantId);
if (updatedReviewDTO != null) {
ConnectionManagerUtil.commitDBTransaction(); ConnectionManagerUtil.commitDBTransaction();
return true; return updatedReviewDTO;
} }
ConnectionManagerUtil.rollbackDBTransaction(); ConnectionManagerUtil.rollbackDBTransaction();
return false; return null;
} catch (ReviewManagementDAOException e) { } catch (ReviewManagementDAOException e) {
ConnectionManagerUtil.rollbackDBTransaction(); ConnectionManagerUtil.rollbackDBTransaction();
String msg = "Error occured while getting reviewTmp with reviewTmp id " + reviewId + "."; String msg = "Error occured while getting reviewTmp with reviewTmp id " + reviewId + ".";

@ -88,54 +88,6 @@ scopes = {
public interface ReviewManagementAdminAPI { public interface ReviewManagementAdminAPI {
String SCOPE = "scope"; String SCOPE = "scope";
@PUT
@Path("/{uuid}/{reviewId}")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
@ApiOperation(
consumes = MediaType.APPLICATION_JSON,
produces = MediaType.APPLICATION_JSON,
httpMethod = "PUT",
value = "Edit a review",
notes = "This will edit the review",
tags = "Review Management",
extensions = {
@Extension(properties = {
@ExtensionProperty(name = SCOPE, value = "perm:admin:app:review:update")
})
}
)
@ApiResponses(
value = {
@ApiResponse(
code = 200,
message = "OK. \n Successfully updated reviewTmp.",
response = Review.class),
@ApiResponse(
code = 400,
message = "Bad Request. \n Invalid request or validation error."),
@ApiResponse(
code = 500,
message = "Internal Server Error. \n Error occurred while updating the new reviewTmp.",
response = ErrorResponse.class)
})
Response updateReview(
@ApiParam(
name = "reviewTmp",
value = "The review that need to be updated.",
required = true)
@Valid ReviewWrapper updatingReview,
@ApiParam(
name = "uuid",
value = "uuid of the application release",
required = true)
@PathParam("uuid") String uuid,
@ApiParam(
name = "reviewId",
value = "review id of the updating reviewTmp.",
required = true)
@PathParam("reviewId") int reviewId);
@DELETE @DELETE
@Path("/{uuid}/{reviewId}") @Path("/{uuid}/{reviewId}")
@Produces(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON)

@ -48,38 +48,6 @@ public class ReviewManagementAdminAPIImpl implements ReviewManagementAdminAPI {
private static Log log = LogFactory.getLog(ReviewManagementAdminAPIImpl.class); private static Log log = LogFactory.getLog(ReviewManagementAdminAPIImpl.class);
//todo remove this API
@Override
@PUT
@Consumes("application/json")
@Path("/{uuid}/{reviewId}")
public Response updateReview(
@ApiParam ReviewWrapper updatingReview,
@PathParam("uuid") String uuid,
@PathParam("reviewId") int reviewId) {
ReviewManager reviewManager = APIUtil.getReviewManager();
try {
if (reviewManager.updateReview(updatingReview, reviewId, uuid, true)) {
return Response.status(Response.Status.OK).entity(updatingReview).build();
} else {
String msg = "Review updating failed. Please contact the administrator";
log.error(msg);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
}
} catch (ReviewManagementException e) {
String msg = "Error occurred while retrieving comments.";
log.error(msg, e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
} catch (NotFoundException e) {
String msg = "Couldn't found application release data for UUID " + uuid + " or Review for review ID: " + reviewId;
log.error(msg, e);
return Response.status(Response.Status.NOT_FOUND).entity(msg).build();
} catch (ApplicationManagementException e) {
String msg = "Error occurred when getting application release data for application release UUID:." + uuid;
log.error(msg, e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); }
}
@Override @Override
@DELETE @DELETE
@Path("/{uuid}/{reviewId}") @Path("/{uuid}/{reviewId}")
@ -91,7 +59,6 @@ public class ReviewManagementAdminAPIImpl implements ReviewManagementAdminAPI {
try { try {
reviewManager.deleteReview(uuid, reviewId, true); reviewManager.deleteReview(uuid, reviewId, true);
return Response.status(Response.Status.OK).entity("Review is deleted successfully.").build(); return Response.status(Response.Status.OK).entity("Review is deleted successfully.").build();
} catch (NotFoundException e) { } catch (NotFoundException e) {
String msg = "Couldn't found an application review to delete which match with the request."; String msg = "Couldn't found an application review to delete which match with the request.";
log.error(msg, e); log.error(msg, e);

@ -199,8 +199,9 @@ 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(updatingReview, reviewId, uuid, false)) { Review updatedReview = reviewManager.updateReview(updatingReview, reviewId, uuid, false);
return Response.status(Response.Status.OK).entity(updatingReview).build(); if (updatedReview != null) {
return Response.status(Response.Status.OK).entity(updatedReview).build();
} else { } else {
String msg = "Review updating failed. Please contact the administrator"; String msg = "Review updating failed. Please contact the administrator";
log.error(msg); log.error(msg);
@ -230,7 +231,6 @@ public class ReviewManagementAPIImpl implements ReviewManagementAPI {
public Response deleteReview( public Response deleteReview(
@PathParam("uuid") String uuid, @PathParam("uuid") String uuid,
@PathParam("reviewId") int reviewId) { @PathParam("reviewId") int reviewId) {
ReviewManager reviewManager = APIUtil.getReviewManager(); ReviewManager reviewManager = APIUtil.getReviewManager();
try { try {
reviewManager.deleteReview(uuid, reviewId, false); reviewManager.deleteReview(uuid, reviewId, false);

Loading…
Cancel
Save