Improve review management and lifecycle management in APPM

feature/appm-store/pbac
lasanthaDLPDS 6 years ago
parent 03ae304234
commit 5ea379dfd0

@ -18,7 +18,7 @@
package org.wso2.carbon.device.application.mgt.common.response; package org.wso2.carbon.device.application.mgt.common.response;
import java.sql.Timestamp; import java.sql.Timestamp;
import java.util.TreeMap; import java.util.List;
public class Review { public class Review {
@ -30,7 +30,7 @@ public class Review {
private Timestamp createdAt; private Timestamp createdAt;
private Timestamp modifiedAt; private Timestamp modifiedAt;
private int rating; private int rating;
private TreeMap<Integer, Review> replyComments; private List<Review> replies;
public int getId() { public int getId() {
return id; return id;
@ -96,11 +96,7 @@ public class Review {
this.rating = rating; this.rating = rating;
} }
public TreeMap<Integer, Review> getReplyComments() { public List<Review> getReplies() { return replies; }
return replyComments;
}
public void setReplyComments(TreeMap<Integer, Review> replyComments) { public void setReplies(List<Review> replies) { this.replies = replies; }
this.replyComments = replyComments;
}
} }

@ -150,8 +150,9 @@ public interface ApplicationManager {
* @param releaseUuid UUID of the ApplicationDTO Release. * @param releaseUuid UUID of the ApplicationDTO Release.
* @param lifecycleChanger Lifecycle changer that contains the action and the reson for the change. * @param lifecycleChanger Lifecycle changer that contains the action and the reson for the change.
* @throws ApplicationManagementException ApplicationDTO Management Exception. * @throws ApplicationManagementException ApplicationDTO Management Exception.
* @return
*/ */
void changeLifecycleState(String releaseUuid, LifecycleChanger lifecycleChanger) ApplicationRelease changeLifecycleState(String releaseUuid, LifecycleChanger lifecycleChanger)
throws ApplicationManagementException; throws ApplicationManagementException;
/** /**

@ -1618,7 +1618,7 @@ public class ApplicationManagerImpl implements ApplicationManager {
} }
@Override @Override
public void changeLifecycleState(String releaseUuid, LifecycleChanger lifecycleChanger) public ApplicationRelease changeLifecycleState(String releaseUuid, LifecycleChanger lifecycleChanger)
throws ApplicationManagementException { throws ApplicationManagementException {
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true);
@ -1664,6 +1664,7 @@ public class ApplicationManagerImpl implements ApplicationManager {
} }
this.lifecycleStateDAO.addLifecycleState(lifecycleState, applicationReleaseDTO.getId(), tenantId); this.lifecycleStateDAO.addLifecycleState(lifecycleState, applicationReleaseDTO.getId(), tenantId);
ConnectionManagerUtil.commitDBTransaction(); ConnectionManagerUtil.commitDBTransaction();
return releaseDtoToRelease(applicationReleaseDTO);
} else { } else {
String msg = "Invalid lifecycle state transition from '" + applicationReleaseDTO.getCurrentState() + "'" String msg = "Invalid lifecycle state transition from '" + applicationReleaseDTO.getCurrentState() + "'"
+ " to '" + lifecycleChanger.getAction() + "'"; + " to '" + lifecycleChanger.getAction() + "'";

@ -172,8 +172,12 @@ public class ReviewManagerImpl implements ReviewManager {
} }
ReviewDTO replyComment = reviewWrapperToDO(reviewWrapper); ReviewDTO replyComment = reviewWrapperToDO(reviewWrapper);
replyComment.setUsername(username); replyComment.setUsername(username);
replyComment.setRootParentId(parentReview.getRootParentId());
replyComment.setImmediateParentId(parentReview.getId()); replyComment.setImmediateParentId(parentReview.getId());
if (parentReview.getRootParentId() == -1) {
replyComment.setRootParentId(parentReview.getId());
} else {
replyComment.setRootParentId(parentReview.getRootParentId());
}
if (this.reviewDAO.addReview(replyComment, applicationReleaseDTO.getId(), tenantId)) { if (this.reviewDAO.addReview(replyComment, applicationReleaseDTO.getId(), tenantId)) {
ConnectionManagerUtil.commitDBTransaction(); ConnectionManagerUtil.commitDBTransaction();
return true; return true;
@ -214,12 +218,24 @@ public class ReviewManagerImpl implements ReviewManager {
review.setImmediateParentId(reviewDTO.getImmediateParentId()); review.setImmediateParentId(reviewDTO.getImmediateParentId());
review.setCreatedAt(reviewDTO.getCreatedAt()); review.setCreatedAt(reviewDTO.getCreatedAt());
review.setModifiedAt(reviewDTO.getModifiedAt()); review.setModifiedAt(reviewDTO.getModifiedAt());
review.setReplyComments(new TreeMap<>()); review.setReplies(new ArrayList<>());
reviews.add(review); reviews.add(review);
} }
return reviews; return reviews;
} }
private Review reviewDTOToReview(ReviewDTO reviewDTO){
Review review = new Review();
review.setId(reviewDTO.getId());
review.setContent(reviewDTO.getContent());
review.setRootParentId(reviewDTO.getRootParentId());
review.setImmediateParentId(reviewDTO.getImmediateParentId());
review.setCreatedAt(reviewDTO.getCreatedAt());
review.setModifiedAt(reviewDTO.getModifiedAt());
review.setReplies(new ArrayList<>());
return review;
}
@Override @Override
public boolean updateReview(ReviewWrapper updatingReview, int reviewId, String uuid) public boolean updateReview(ReviewWrapper updatingReview, int reviewId, String uuid)
throws ReviewManagementException, ApplicationManagementException { throws ReviewManagementException, ApplicationManagementException {
@ -283,38 +299,35 @@ public class ReviewManagerImpl implements ReviewManager {
throws ReviewManagementException { throws ReviewManagementException {
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true);
PaginationResult paginationResult = new PaginationResult(); PaginationResult paginationResult = new PaginationResult();
int numOfComments; TreeMap<Integer, ReviewNode<ReviewDTO>> reviewTree = new TreeMap<>();
TreeMap<Integer, ReviewNode<Review>> reviewTree = new TreeMap<>();
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("Get all reviewTmps of the application release uuid: " + uuid); log.debug("Get all reviewTmps of the application release uuid: " + uuid);
} }
try { try {
ConnectionManagerUtil.openDBConnection(); ConnectionManagerUtil.openDBConnection();
List<ReviewDTO> reviewDTOs= this.reviewDAO.getAllReviews(uuid, request, tenantId); List<ReviewDTO> reviewDTOs= this.reviewDAO.getAllReviews(uuid, request, tenantId);
List<Review> reviews = reviewDTOToReview(reviewDTOs); reviewDTOs.sort(Comparator.comparing(ReviewDTO::getId));
reviews.sort(Comparator.comparing(Review::getId));
for (ReviewDTO reviewDTO : reviewDTOs) {
for (Review review : reviews) { if (reviewDTO.getRootParentId() == -1 && reviewDTO.getImmediateParentId() == -1) {
if (review.getRootParentId() == -1 && review.getImmediateParentId() == -1) { ReviewNode<ReviewDTO> rootNode = new ReviewNode<>(reviewDTO);
ReviewNode<Review> rootNode = new ReviewNode<>(review); reviewTree.put(reviewDTO.getId(), rootNode);
reviewTree.put(review.getId(), rootNode); } else if (reviewTree.containsKey(reviewDTO.getImmediateParentId())) {
} else if (reviewTree.containsKey(review.getImmediateParentId())) { ReviewNode<ReviewDTO> childNode = new ReviewNode<>(reviewDTO);
ReviewNode<Review> childNode = new ReviewNode<>(review); reviewTree.get(reviewDTO.getImmediateParentId()).addChild(childNode);
reviewTree.get(review.getImmediateParentId()).addChild(childNode);
} else { } else {
reviewTree.put(review.getId(), findAndSetChild(reviewTree.get(review.getRootParentId()), review)); reviewTree.put(reviewDTO.getId(), findAndSetChild(reviewTree.get(reviewDTO.getRootParentId()), reviewDTO));
} }
} }
numOfComments = reviewTree.size(); int numOfReviews = reviewTree.size();
if (numOfComments > 0) { List<Review> results = new ArrayList<>();
paginationResult.setData(new ArrayList<>(reviewTree.values()));
paginationResult.setRecordsFiltered(numOfComments); for (ReviewNode<ReviewDTO> reviewNode : reviewTree.values()){
paginationResult.setRecordsTotal(numOfComments); results.add(printTree(null, reviewNode));
} else {
paginationResult.setData(new ArrayList<ReviewTmp>());
paginationResult.setRecordsFiltered(0);
paginationResult.setRecordsTotal(0);
} }
paginationResult.setData(new ArrayList<>(results));
paginationResult.setRecordsFiltered(numOfReviews);
paginationResult.setRecordsTotal(numOfReviews);
return paginationResult; return paginationResult;
} catch (ReviewManagementDAOException e) { } catch (ReviewManagementDAOException e) {
throw new ReviewManagementException("Error occured while getting all reviewTmps for application uuid: " + uuid, throw new ReviewManagementException("Error occured while getting all reviewTmps for application uuid: " + uuid,
@ -326,16 +339,30 @@ public class ReviewManagerImpl implements ReviewManager {
} }
} }
private ReviewNode<Review> findAndSetChild(ReviewNode<Review> node, Review review) { private ReviewNode<ReviewDTO> findAndSetChild(ReviewNode<ReviewDTO> node, ReviewDTO reviewDTO) {
for (ReviewNode<Review> each : node.getChildren()) { for (ReviewNode<ReviewDTO> each : node.getChildren()) {
if ((each.getData()).getId() == review.getImmediateParentId()) { if ((each.getData()).getId() == reviewDTO.getImmediateParentId()) {
ReviewNode<Review> childNode = new ReviewNode<>(review); ReviewNode<ReviewDTO> childNode = new ReviewNode<>(reviewDTO);
each.addChild(childNode); each.addChild(childNode);
} }
} }
return node; return node;
} }
private Review printTree(Review parentReview, ReviewNode<ReviewDTO> node) {
Review review = reviewDTOToReview(node.getData());
if (parentReview != null){
parentReview.getReplies().add(review);
}
if (node.getChildren().isEmpty()){
return review;
}
for (ReviewNode<ReviewDTO> reviewDTOReviewNode : node.getChildren()) {
printTree(review, reviewDTOReviewNode);
}
return review;
}
@Override @Override
public boolean deleteReview(String uuid, int reviewId) public boolean deleteReview(String uuid, int reviewId)
throws ReviewManagementException, ReviewDoesNotExistException { throws ReviewManagementException, ReviewDoesNotExistException {

@ -453,7 +453,9 @@ public class ApplicationManagementPublisherAPIImpl implements ApplicationManagem
@Valid LifecycleChanger lifecycleChanger) { @Valid LifecycleChanger lifecycleChanger) {
ApplicationManager applicationManager = APIUtil.getApplicationManager(); ApplicationManager applicationManager = APIUtil.getApplicationManager();
try { try {
applicationManager.changeLifecycleState(applicationUuid, lifecycleChanger); ApplicationRelease applicationRelease = applicationManager
.changeLifecycleState(applicationUuid, lifecycleChanger);
return Response.status(Response.Status.CREATED).entity(applicationRelease).build();
} catch (BadRequestException e) { } catch (BadRequestException e) {
String msg = "Request payload contains invalid data, hence veryfy the request payload."; String msg = "Request payload contains invalid data, hence veryfy the request payload.";
log.error(msg, e); log.error(msg, e);
@ -472,7 +474,6 @@ public class ApplicationManagementPublisherAPIImpl implements ApplicationManagem
log.error(msg, e); log.error(msg, e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build(); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
} }
return Response.status(Response.Status.CREATED).entity("Lifecycle state added successfully.").build();
} }
@GET @GET

@ -85,7 +85,7 @@ public class ReviewManagementAPIImpl implements ReviewManagementAPI {
if (isReviewCreated) { if (isReviewCreated) {
return Response.status(Response.Status.CREATED).entity(reviewWrapper).build(); return Response.status(Response.Status.CREATED).entity(reviewWrapper).build();
} else { } else {
String msg = "Given reviewTmp is not valid. Please check the reviewTmp payload."; String msg = "Review is not valid. Please check the reviewTmp payload.";
log.error(msg); log.error(msg);
return Response.status(Response.Status.BAD_REQUEST).entity(msg).build(); return Response.status(Response.Status.BAD_REQUEST).entity(msg).build();
} }

Loading…
Cancel
Save