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;
import java.sql.Timestamp;
import java.util.TreeMap;
import java.util.List;
public class Review {
@ -30,7 +30,7 @@ public class Review {
private Timestamp createdAt;
private Timestamp modifiedAt;
private int rating;
private TreeMap<Integer, Review> replyComments;
private List<Review> replies;
public int getId() {
return id;
@ -96,11 +96,7 @@ public class Review {
this.rating = rating;
}
public TreeMap<Integer, Review> getReplyComments() {
return replyComments;
}
public List<Review> getReplies() { return replies; }
public void setReplyComments(TreeMap<Integer, Review> replyComments) {
this.replyComments = replyComments;
}
public void setReplies(List<Review> replies) { this.replies = replies; }
}

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

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

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

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

@ -85,7 +85,7 @@ public class ReviewManagementAPIImpl implements ReviewManagementAPI {
if (isReviewCreated) {
return Response.status(Response.Status.CREATED).entity(reviewWrapper).build();
} 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);
return Response.status(Response.Status.BAD_REQUEST).entity(msg).build();
}

Loading…
Cancel
Save