diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/response/Review.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/response/Review.java index 55140969cbd..4acfa1e6a6e 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/response/Review.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/response/Review.java @@ -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 replyComments; + private List replies; public int getId() { return id; @@ -96,11 +96,7 @@ public class Review { this.rating = rating; } - public TreeMap getReplyComments() { - return replyComments; - } + public List getReplies() { return replies; } - public void setReplyComments(TreeMap replyComments) { - this.replyComments = replyComments; - } + public void setReplies(List replies) { this.replies = replies; } } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/ApplicationManager.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/ApplicationManager.java index 9b9874062ed..cf49bf2a326 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/ApplicationManager.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/ApplicationManager.java @@ -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; /** diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ApplicationManagerImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ApplicationManagerImpl.java index fc8f1ce530a..04a63b485f5 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ApplicationManagerImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ApplicationManagerImpl.java @@ -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() + "'"; diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ReviewManagerImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ReviewManagerImpl.java index 73fca86a403..b1f3ac8dcb5 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ReviewManagerImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ReviewManagerImpl.java @@ -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> reviewTree = new TreeMap<>(); + TreeMap> reviewTree = new TreeMap<>(); if (log.isDebugEnabled()) { log.debug("Get all reviewTmps of the application release uuid: " + uuid); } try { ConnectionManagerUtil.openDBConnection(); List reviewDTOs= this.reviewDAO.getAllReviews(uuid, request, tenantId); - List reviews = reviewDTOToReview(reviewDTOs); - reviews.sort(Comparator.comparing(Review::getId)); - - for (Review review : reviews) { - if (review.getRootParentId() == -1 && review.getImmediateParentId() == -1) { - ReviewNode rootNode = new ReviewNode<>(review); - reviewTree.put(review.getId(), rootNode); - } else if (reviewTree.containsKey(review.getImmediateParentId())) { - ReviewNode 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 rootNode = new ReviewNode<>(reviewDTO); + reviewTree.put(reviewDTO.getId(), rootNode); + } else if (reviewTree.containsKey(reviewDTO.getImmediateParentId())) { + ReviewNode 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()); - paginationResult.setRecordsFiltered(0); - paginationResult.setRecordsTotal(0); + int numOfReviews = reviewTree.size(); + List results = new ArrayList<>(); + + for (ReviewNode 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 findAndSetChild(ReviewNode node, Review review) { - for (ReviewNode each : node.getChildren()) { - if ((each.getData()).getId() == review.getImmediateParentId()) { - ReviewNode childNode = new ReviewNode<>(review); + private ReviewNode findAndSetChild(ReviewNode node, ReviewDTO reviewDTO) { + for (ReviewNode each : node.getChildren()) { + if ((each.getData()).getId() == reviewDTO.getImmediateParentId()) { + ReviewNode childNode = new ReviewNode<>(reviewDTO); each.addChild(childNode); } } return node; } + private Review printTree(Review parentReview, ReviewNode node) { + Review review = reviewDTOToReview(node.getData()); + if (parentReview != null){ + parentReview.getReplies().add(review); + } + if (node.getChildren().isEmpty()){ + return review; + } + for (ReviewNode reviewDTOReviewNode : node.getChildren()) { + printTree(review, reviewDTOReviewNode); + } + return review; + } + @Override public boolean deleteReview(String uuid, int reviewId) throws ReviewManagementException, ReviewDoesNotExistException { diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.api/src/main/java/org/wso2/carbon/device/application/mgt/publisher/api/services/impl/ApplicationManagementPublisherAPIImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.api/src/main/java/org/wso2/carbon/device/application/mgt/publisher/api/services/impl/ApplicationManagementPublisherAPIImpl.java index 57f91e07207..9d53177a031 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.api/src/main/java/org/wso2/carbon/device/application/mgt/publisher/api/services/impl/ApplicationManagementPublisherAPIImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.api/src/main/java/org/wso2/carbon/device/application/mgt/publisher/api/services/impl/ApplicationManagementPublisherAPIImpl.java @@ -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 diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/main/java/org/wso2/carbon/device/application/mgt/store/api/services/impl/ReviewManagementAPIImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/main/java/org/wso2/carbon/device/application/mgt/store/api/services/impl/ReviewManagementAPIImpl.java index 5fb81372298..2b1309639e6 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/main/java/org/wso2/carbon/device/application/mgt/store/api/services/impl/ReviewManagementAPIImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/main/java/org/wso2/carbon/device/application/mgt/store/api/services/impl/ReviewManagementAPIImpl.java @@ -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(); }