diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/Comment.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/Comment.java deleted file mode 100644 index a01176a8cca..00000000000 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/Comment.java +++ /dev/null @@ -1,110 +0,0 @@ -/* - * Copyright (c) 2018, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. - * - * WSO2 Inc. licenses this file to you under the Apache License, - * Version 2.0 (the "License"); you may not use this file except - * in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * - */ -package org.wso2.carbon.device.application.mgt.common; - -import java.sql.Timestamp; - -/** - * Represents a commentText for an {@link Application}. - */ -public class Comment { - - private int id; - - private String commentText; - - //TODO: Pagination, commentText ID for child - private int parent; - - private int tenantId; - - private String createdBy; - - private Timestamp createdAt; - - private String modifiedBy; - - private Timestamp modifiedAt; - - public int getTenantId() { - return tenantId; - } - - public void setTenantId(int tenantId) { - this.tenantId = tenantId; - } - - public int getId() { - return id; - } - - public void setId(int id) { - this.id = id; - } - - public String getCommentText() { - return commentText; - } - - public void setCommentText(String commentText) { - this.commentText = commentText; - } - - public int getParent() { - return parent; - } - - public void setParent(int parent) { - this.parent = parent; - } - - public String getCreatedBy() { - return createdBy; - } - - public void setCreatedBy(String createdBy) { - this.createdBy = createdBy; - } - - public Timestamp getCreatedAt() { - return createdAt; - } - - public void setCreatedAt(Timestamp createdAt) { - this.createdAt = createdAt; - } - - public String getModifiedBy() { - return modifiedBy; - } - - public void setModifiedBy(String modifiedBy) { - this.modifiedBy = modifiedBy; - } - - public Timestamp getModifiedAt() { - return modifiedAt; - } - - public void setModifiedAt(Timestamp modifiedAt) { - this.modifiedAt = modifiedAt; - } - -} - diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/LifecycleState.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/LifecycleState.java index 2c0dc8349bb..0fcec128374 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/LifecycleState.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/LifecycleState.java @@ -24,7 +24,7 @@ import io.swagger.annotations.ApiModelProperty; import java.util.List; -@ApiModel(value = "LifecycleState", description = "LifecycleState represents the an Lifecycle state for an application release") +@ApiModel(value = "LifecycleState", description = "LifecycleState represents the Lifecycle state for an application release") public class LifecycleState { @ApiModelProperty(name = "id", diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/Rating.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/Rating.java new file mode 100644 index 00000000000..51caf0c52f5 --- /dev/null +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/Rating.java @@ -0,0 +1,66 @@ +/* + * Copyright (c) 2018, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +package org.wso2.carbon.device.application.mgt.common; + +import java.util.TreeMap; + +/** + * Rating represents the an overall rating value and number of users who has rated for an application release. + */ +public class Rating { + + /** + * Rating value of the application release. + */ + private double ratingValue; + + /** + * Number of users who has rated for the application release. + */ + private int noOfUsers; + + /** + * Represent the rating variety for the application release + */ + private TreeMap ratingVariety; + + public double getRatingValue() { + return ratingValue; + } + + public void setRatingValue(double ratingValue) { + this.ratingValue = ratingValue; + } + + public int getNoOfUsers() { + return noOfUsers; + } + + public void setNoOfUsers(int noOfUsers) { + this.noOfUsers = noOfUsers; + } + + public TreeMap getRatingVariety() { + return ratingVariety; + } + + public void setRatingVariety(TreeMap ratingVariety) { + this.ratingVariety = ratingVariety; + } +} diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/Review.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/Review.java new file mode 100644 index 00000000000..2a1b33976a2 --- /dev/null +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/Review.java @@ -0,0 +1,114 @@ +/* + * Copyright (c) 2018, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +package org.wso2.carbon.device.application.mgt.common; + +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; + +import java.sql.Timestamp; + +@ApiModel(value = "Review", description = "Review represents the user's review for an application release") +public class Review { + + @ApiModelProperty(name = "id", + value = "The Id given to the comment when it store to the App manager") + private int id; + + @ApiModelProperty(name = "comment", + value = "Comment of the review") + private String comment; + + @ApiModelProperty(name = "replyComment", + value = "Reply comment of the review") + private String replyComment; + + @ApiModelProperty(name = "username", + value = "Username odf the Review creator", + required = true) + private String username; + + @ApiModelProperty(name = "createdAt", + value = "Timestamp fo the review is created") + private Timestamp createdAt; + + @ApiModelProperty(name = "modifiedAt", + value = "Timestamp of the review is modified") + private Timestamp modifiedAt; + + @ApiModelProperty(name = "rating", + value = "Rating value of the application release") + private int rating; + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getComment() { + return comment; + } + + public void setComment(String comment) { + this.comment = comment; + } + + public String getUsername() { + return username; + } + + public void setUsername(String username) { + this.username = username; + } + + public Timestamp getCreatedAt() { + return createdAt; + } + + public void setCreatedAt(Timestamp createdAt) { + this.createdAt = createdAt; + } + + public Timestamp getModifiedAt() { + return modifiedAt; + } + + public void setModifiedAt(Timestamp modifiedAt) { + this.modifiedAt = modifiedAt; + } + + public int getRating() { + return rating; + } + + public void setRating(int rating) { + this.rating = rating; + } + + public String getReplyComment() { + return replyComment; + } + + public void setReplyComment(String replyComment) { + this.replyComment = replyComment; + } +} + diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/CommentsManager.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/CommentsManager.java deleted file mode 100644 index 9a5269e2369..00000000000 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/CommentsManager.java +++ /dev/null @@ -1,120 +0,0 @@ -/* - * Copyright (c) 2018, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. - * - * WSO2 Inc. licenses this file to you under the Apache License, - * Version 2.0 (the "License"); you may not use this file except - * in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * - */ -package org.wso2.carbon.device.application.mgt.common.services; - -import org.wso2.carbon.device.application.mgt.common.Comment; -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.exception.ApplicationManagementException; -import org.wso2.carbon.device.application.mgt.common.exception.CommentManagementException; - -import java.util.List; - -/** - * CommentsManager is responsible for handling all the add/update/delete/get operations related with - */ -public interface CommentsManager { - - /** - * To add a comment to a application - * - * @param comment comment of the application. - * @param uuid uuid of the application release\ - * @return {@link Comment} Comment added - * @throws CommentManagementException Exceptions of the comment management. - */ - Comment addComment(Comment comment, String uuid) throws CommentManagementException; - - /** - * To validate the pre-request of the comment - * - * @param commentId ID of the comment. - * @param comment comment needed to be validate. - * @return validated the comment. - * @throws CommentManagementException Exceptions of the comment management. - */ - Boolean validateComment(int commentId, String comment) throws CommentManagementException; - - /** - * Get all comments to pagination - * - * @param request Pagination request - * @param uuid uuid of the application release - * @return {@link PaginationResult} pagination result with starting offSet and limit - * @throws CommentManagementException Exceptions of the comment management. - */ - PaginationResult getAllComments(PaginationRequest request, String uuid) throws CommentManagementException; - - /** - * To get the comment with id. - * - * @param commentId id of the comment - * @return {@link Comment}Comment of the comment id - * @throws CommentManagementException Exceptions of the comment management. - */ - Comment getComment(int commentId) throws CommentManagementException; - - /** - * To delete comment using comment id. - * - * @param commentId id of the comment - * @throws CommentManagementException Exceptions of the comment management - */ - void deleteComment(int commentId) throws CommentManagementException; - - /** - * To update a comment. - * - * @param comment comment of the application. - * @param commentId id of the comment - * @return {@link Comment}updated comment - * @throws CommentManagementException Exceptions of the comment management - */ - Comment updateComment(Comment comment, int commentId) throws CommentManagementException; - - /** - * To get number of rated users - * - * @param uuid uuid of the application - * @return number of rated users - * @throws CommentManagementException Exceptions of the comment management - * @throws ApplicationManagementException Application Management Exception. - */ - int getRatedUser(String uuid) throws CommentManagementException, ApplicationManagementException; - - /** - * To get the average of stars - * - * @param uuid uuid of the comment - * @return value of the stars of an application - * @throws CommentManagementException Exceptions of the comment management - * @throws ApplicationManagementException Application Management Exception. - */ - int getStars(String uuid) throws CommentManagementException, ApplicationManagementException; - - /** - * To update rating stars - * - * @param stars amount of stars - * @param uuid uuid of the application - * @return value of the added stars - * @throws ApplicationManagementException Application Management Exception. - */ - int updateStars(int stars, String uuid) throws ApplicationManagementException; -} \ No newline at end of file diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/ReviewManager.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/ReviewManager.java new file mode 100644 index 00000000000..d9e62337efc --- /dev/null +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/ReviewManager.java @@ -0,0 +1,89 @@ +/* + * Copyright (c) 2018, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +package org.wso2.carbon.device.application.mgt.common.services; + +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.PaginationRequest; +import org.wso2.carbon.device.application.mgt.common.PaginationResult; +import org.wso2.carbon.device.application.mgt.common.exception.ApplicationManagementException; +import org.wso2.carbon.device.application.mgt.common.exception.CommentManagementException; + +/** + * ReviewManager is responsible for handling all the add/update/delete/get operations related with + */ +public interface ReviewManager { + + /** + * To add a review to a application + * + * @param review review of the application. + * @param appId id of the application. + * @param appReleaseId id of the application release + * @return {@link Review} Review added + * @throws CommentManagementException Exceptions of the review management. + */ + boolean addReview(Review review,int appId, int appReleaseId) throws CommentManagementException; + + /** + * Get all comments to pagination + * + * @param request Pagination request + * @param uuid uuid of the application release + * @return {@link PaginationResult} pagination result with starting offSet and limit + * @throws CommentManagementException Exceptions of the comment management. + */ + PaginationResult getAllReviews(PaginationRequest request, String uuid) throws CommentManagementException; + + /** + * To get the comment with id. + * + * @param commentId id of the comment + * @return {@link Review}Review of the comment id + * @throws CommentManagementException Exceptions of the comment management. + */ + Review getReview(int commentId) throws CommentManagementException; + + /** + * To delete review using review id. + * + * @param commentId id of the comment + * @throws CommentManagementException Exceptions of the comment management + */ + void deleteReview(String loggedInUser, int commentId) throws CommentManagementException; + + /** + * To update a review. + * + * @param review review of the application. + * @param reviewId id of the review + * @return {@link Review}updated review + * @throws CommentManagementException Exceptions of the review management + */ + boolean updateReview(Review review, int reviewId, boolean checkExistence) throws CommentManagementException; + + /** + * To get the overall rating for a application release + * + * @param appReleaseUuuid UUID of the application release. + * @return {@link Review}updated review + * @throws CommentManagementException Exceptions of the review management + */ + Rating getRating(String appReleaseUuuid) throws CommentManagementException; +} \ No newline at end of file diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/ApplicationReleaseDAO.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/ApplicationReleaseDAO.java index 4d40ca52021..e3bcfecad2e 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/ApplicationReleaseDAO.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/ApplicationReleaseDAO.java @@ -19,6 +19,7 @@ package org.wso2.carbon.device.application.mgt.core.dao; import org.wso2.carbon.device.application.mgt.common.ApplicationRelease; +import org.wso2.carbon.device.application.mgt.common.Rating; import org.wso2.carbon.device.application.mgt.core.exception.ApplicationManagementDAOException; import java.util.List; @@ -80,10 +81,18 @@ public interface ApplicationReleaseDAO { /** * To update an Application release. * @param id id of the ApplicationRelease that need to be updated. - * @param stars given stars for the application. + * @param rating given stars for the application. + * @param ratedUsers number of users who has rated for the application release. * @throws ApplicationManagementDAOException Application Management DAO Exception */ - void updateStars(int id, int stars) throws ApplicationManagementDAOException; + int updateRatingValue(int id, double rating, int ratedUsers) throws ApplicationManagementDAOException; + + /** + * To retrieve rating of an application release. + * @param id Id of the application Release. + * @throws ApplicationManagementDAOException Application Management DAO Exception. + */ + Rating getRating(int id) throws ApplicationManagementDAOException; /** diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/CommentDAO.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/ReviewDAO.java similarity index 86% rename from components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/CommentDAO.java rename to components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/ReviewDAO.java index 429635bf10c..009823af99c 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/CommentDAO.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/ReviewDAO.java @@ -18,7 +18,7 @@ */ package org.wso2.carbon.device.application.mgt.core.dao; -import org.wso2.carbon.device.application.mgt.common.Comment; +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.exception.CommentManagementException; import org.wso2.carbon.device.application.mgt.common.exception.DBConnectionException; @@ -32,35 +32,40 @@ import java.util.List; * This interface specifies the database access operations performed for comments. */ - public interface CommentDAO { + public interface ReviewDAO { /** - * To add a comment to a application. + * To add a review to a application. * * @param tenantId tenantId of the commented application. - * @param comment comment of the application. + * @param review review of the application. * @param createdBy Username of the created person. - * @param parentId parent id of the parent comment. + * @param parentId parent id of the parent review. * @param uuid uuid of the application - * @return If comment is added successfully, it return true otherwise false - * @throws CommentManagementException Exceptions of the comment management. + * @return If review is added successfully, it return true otherwise false + * @throws CommentManagementException Exceptions of the review management. * @throws DBConnectionException db connection exception. */ - boolean addComment(int tenantId, Comment comment, String createdBy, int parentId, String uuid) + boolean addReview(Review review, int appId, int appReleaseId, int tenantId) throws CommentManagementException, DBConnectionException, SQLException; + + Review isExistReview(int appId, int appReleaseId, String username, int tenantId) + throws DBConnectionException, SQLException; + + /** - * To add a comment to a application. + * To add a review to a application. * - * @param comment comment of the application. + * @param review review of the application. * @param createdBy Username of the created person. * @param appType type of the commented application. * @param appName name of the commented application. * @param version version of the commented application. - * @return comment id - * @throws CommentManagementException Exceptions of the comment management. + * @return review id + * @throws CommentManagementException Exceptions of the review management. */ - int addComment(int tenantId, Comment comment, String createdBy, String appType, String appName, String version) + int addReview(int tenantId, Review review, String createdBy, String appType, String appName, String version) throws CommentManagementException, DBConnectionException, SQLException; /** @@ -70,12 +75,12 @@ import java.util.List; * @param updatedComment comment after updated * @param modifiedBy Username of the modified person. * @param modifiedAt time of the modification. - * @return {@link Comment}Updated comment + * @return {@link Review}Updated comment * @throws CommentManagementException Exceptions of the comment management. * @throws DBConnectionException db connection exception * @throws SQLException sql exception */ - Comment updateComment(int commentId, String updatedComment, String modifiedBy, Timestamp modifiedAt) + boolean updateReview(Review review, int reviewId, int tenantId) throws CommentManagementException, DBConnectionException, SQLException; /** @@ -86,24 +91,24 @@ import java.util.List; * @param updatedComment comment after updated * @param modifiedBy Username of the modified person. * @param modifiedAt time of the modification. - * @return {@link Comment}Updated comment + * @return {@link Review}Updated comment * @throws CommentManagementException Exceptions of the comment management. * @throws DBConnectionException db connection exception * @throws SQLException sql exception */ - Comment updateComment(String uuid, int commentId, String updatedComment, String modifiedBy, Timestamp modifiedAt) + Review updateReview(String uuid, int commentId, String updatedComment, String modifiedBy, Timestamp modifiedAt) throws CommentManagementException, DBConnectionException, SQLException; /** * To get the comment with id. * * @param commentId id of the comment - * @return {@link Comment}Comment + * @return {@link Review}Review * @throws CommentManagementException Exceptions of the comment management. * @throws DBConnectionException db connection exception * @throws SQLException sql exception */ - Comment getComment(int commentId) throws CommentManagementException, SQLException, DBConnectionException; + Review getComment(int commentId) throws CommentManagementException, SQLException, DBConnectionException; /** * To get the comment with id. @@ -114,7 +119,7 @@ import java.util.List; * @throws DBConnectionException db connection exception * @throws SQLException sql exception */ - List getComment(String uuid) throws CommentManagementException, SQLException, DBConnectionException; + List getComment(String uuid) throws CommentManagementException, SQLException, DBConnectionException; /** * To get all the comments @@ -125,7 +130,7 @@ import java.util.List; * @throws DBConnectionException db connection exception * @throws SQLException sql exception **/ - List getAllComments(String uuid, PaginationRequest request) throws SQLException, DBConnectionException; + List getAllComments(String uuid, PaginationRequest request) throws SQLException, DBConnectionException; /** * To get list of comments using release id and application id. @@ -135,7 +140,10 @@ import java.util.List; * @return {@link List}List of comments * @throws CommentManagementException Exceptions of the comment management. */ - List getComments(int appReleasedId, int appId) throws CommentManagementException; + + List getAllRatingValues(String uuid)throws SQLException, DBConnectionException; + + List getComments(int appReleasedId, int appId) throws CommentManagementException; /** * To get list of comments using application type, application name and version of the application. @@ -148,7 +156,7 @@ import java.util.List; * @throws DBConnectionException db connection exception. * @throws SQLException sql exception */ - List getComments(String appType, String appName, String version) + List getComments(String appType, String appName, String version) throws CommentManagementException, DBConnectionException, SQLException; /** @@ -160,7 +168,7 @@ import java.util.List; * @throws DBConnectionException db connection exception. * @throws SQLException sql exception */ - List getComments(int tenantId) throws CommentManagementException, DBConnectionException, SQLException; + List getComments(int tenantId) throws CommentManagementException, DBConnectionException, SQLException; /** * To get list of comments by created user. @@ -171,7 +179,7 @@ import java.util.List; * @throws DBConnectionException db connection exception. * @throws SQLException sql exception */ - List getCommentsByUser(String createdBy) + List getCommentsByUser(String createdBy) throws CommentManagementException, DBConnectionException, SQLException; /** @@ -184,7 +192,7 @@ import java.util.List; * @throws DBConnectionException db connection exception. * @throws SQLException sql exception */ - List getCommentsByUser(String createdBy, Timestamp createdAt) + List getCommentsByUser(String createdBy, Timestamp createdAt) throws CommentManagementException, DBConnectionException, SQLException; /** @@ -196,7 +204,7 @@ import java.util.List; * @throws DBConnectionException db connection exception. * @throws SQLException sql exception */ - List getCommentsByModifiedUser(String modifiedBy) + List getCommentsByModifiedUser(String modifiedBy) throws CommentManagementException, DBConnectionException, SQLException; /** @@ -209,7 +217,7 @@ import java.util.List; * @throws DBConnectionException db connection exception. * @throws SQLException sql exception */ - List getCommentsByModifiedUser(String modifiedBy, Timestamp modifiedAt) + List getCommentsByModifiedUser(String modifiedBy, Timestamp modifiedAt) throws CommentManagementException, DBConnectionException, SQLException; /** @@ -224,7 +232,7 @@ import java.util.List; * @throws DBConnectionException db connection exception. * @throws SQLException sql exception */ - List getComments(String appType, String appName, String version, int parentId) + List getComments(String appType, String appName, String version, int parentId) throws CommentManagementException, DBConnectionException, SQLException; /** @@ -398,7 +406,7 @@ import java.util.List; * * @param request * @param uuid - * @return Comment count + * @return Review count * @throws CommentManagementException */ int getCommentCount(PaginationRequest request, String uuid) throws CommentManagementException; diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/common/ApplicationManagementDAOFactory.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/common/ApplicationManagementDAOFactory.java index 0ab4126a1e7..368111b9e08 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/common/ApplicationManagementDAOFactory.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/common/ApplicationManagementDAOFactory.java @@ -23,7 +23,7 @@ import org.apache.commons.logging.LogFactory; import org.wso2.carbon.device.application.mgt.common.exception.UnsupportedDatabaseEngineException; import org.wso2.carbon.device.application.mgt.core.config.ConfigurationManager; import org.wso2.carbon.device.application.mgt.core.dao.*; -import org.wso2.carbon.device.application.mgt.core.dao.impl.Comment.CommentDAOImpl; +import org.wso2.carbon.device.application.mgt.core.dao.impl.Comment.ReviewDAOImpl; import org.wso2.carbon.device.application.mgt.core.dao.impl.application.GenericApplicationDAOImpl; import org.wso2.carbon.device.application.mgt.core.dao.impl.application.release.GenericApplicationReleaseDAOImpl; import org.wso2.carbon.device.application.mgt.core.dao.impl.application.OracleApplicationDAOImpl; @@ -162,13 +162,13 @@ public class ApplicationManagementDAOFactory { throw new IllegalStateException("Database engine has not initialized properly."); } - public static CommentDAO getCommentDAO() { + public static ReviewDAO getCommentDAO() { if (databaseEngine != null) { switch (databaseEngine) { case Constants.DataBaseTypes.DB_TYPE_H2: case Constants.DataBaseTypes.DB_TYPE_MYSQL: case Constants.DataBaseTypes.DB_TYPE_POSTGRESQL: - return new CommentDAOImpl(); + return new ReviewDAOImpl(); default: throw new UnsupportedDatabaseEngineException("Unsupported database engine : " + databaseEngine); } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/Comment/CommentDAOImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/Comment/ReviewDAOImpl.java similarity index 71% rename from components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/Comment/CommentDAOImpl.java rename to components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/Comment/ReviewDAOImpl.java index 497e10c395d..80b8c03b152 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/Comment/CommentDAOImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/Comment/ReviewDAOImpl.java @@ -22,11 +22,11 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.wso2.carbon.device.application.mgt.common.ApplicationRelease; -import org.wso2.carbon.device.application.mgt.common.Comment; +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.exception.CommentManagementException; import org.wso2.carbon.device.application.mgt.common.exception.DBConnectionException; -import org.wso2.carbon.device.application.mgt.core.dao.CommentDAO; +import org.wso2.carbon.device.application.mgt.core.dao.ReviewDAO; import org.wso2.carbon.device.application.mgt.core.dao.common.Util; import org.wso2.carbon.device.application.mgt.core.dao.impl.AbstractDAOImpl; import org.wso2.carbon.device.application.mgt.core.exception.ApplicationManagementDAOException; @@ -40,20 +40,21 @@ import java.util.ArrayList; import java.util.List; /** - * This handles CommentDAO related operations. + * This handles ReviewDAO related operations. */ -public class CommentDAOImpl extends AbstractDAOImpl implements CommentDAO { +public class ReviewDAOImpl extends AbstractDAOImpl implements ReviewDAO { - private static final Log log = LogFactory.getLog(CommentDAOImpl.class); + private static final Log log = LogFactory.getLog(ReviewDAOImpl.class); private String sql; @Override - public boolean addComment(int tenantId, Comment comment, String createdBy, int parentId, String uuid) + public boolean addReview(Review review, int appId, int appReleaseId, int tenantId) throws CommentManagementException, DBConnectionException, SQLException { if (log.isDebugEnabled()) { - log.debug("Request received in DAO Layer to add comment for application release (" + uuid + ")"); + log.debug("Request received in DAO Layer to add review for application release. Application id: " + appId + + "Application Release id: " + appReleaseId); } Connection conn = this.getDBConnection(); PreparedStatement statement = null; @@ -64,11 +65,11 @@ public class CommentDAOImpl extends AbstractDAOImpl implements CommentDAO { try { statement = conn.prepareStatement(sql, new String[] { "id" }); statement.setInt(1, tenantId); - statement.setString(2, comment.getCommentText()); - statement.setString(3, createdBy); - statement.setInt(4, parentId); - statement.setString(5, uuid); - statement.setString(6, uuid); + statement.setString(2, review.getComment()); +// statement.setString(3, createdBy); +// statement.setInt(4, parentId); +// statement.setString(5, uuid); +// statement.setString(6, uuid); statement.executeUpdate(); rs = statement.getGeneratedKeys(); return rs.next(); @@ -78,7 +79,47 @@ public class CommentDAOImpl extends AbstractDAOImpl implements CommentDAO { } @Override - public int addComment(int tenantId, Comment comment, String createdBy, String appType, String appName, + public Review isExistReview(int appId, int appReleaseId, String username, int tenantId) + throws DBConnectionException, SQLException { + if (log.isDebugEnabled()) { + log.debug( + "Request received in DAO Layer to check whether review exist or not rein the IoTS APPM. Application id: " + + appId + " Application release id: " + appReleaseId + " comment owner: " + username); + } + Connection conn; + PreparedStatement statement = null; + ResultSet rs = null; + Review review = null; + sql = "SELECT ID, COMMENT, REPLY_COMMENT, CREATED_AT, MODEFIED_AT, USERNAME, PARENT_ID, RATING FROM AP_APP_REVIEW " + + "WHERE AP_APP_ID = ? AND AP_APP_RELEASE_ID = ? AND USERNAME = ? AND TENANT_ID = ?;"; + try { + conn = this.getDBConnection(); + statement = conn.prepareStatement(sql); + statement.setInt(1, appId); + statement.setInt(2, appReleaseId); + statement.setString(3, username); + statement.setInt(4, tenantId); + + rs = statement.executeQuery(); + if (rs.next()){ + review = new Review(); + review.setId(rs.getInt("ID")); + review.setComment(rs.getString("COMMENT")); + review.setReplyComment(rs.getString("REPLY_COMMENT")); + review.setCreatedAt(rs.getTimestamp("CREATED_AT")); + review.setModifiedAt(rs.getTimestamp("MODIFIED_AT")); + review.setUsername(rs.getString("USERNAME")); + review.setRating(rs.getInt("RATING")); + } + return review; + } finally { + Util.cleanupResources(statement, rs); + } + } + + + @Override + public int addReview(int tenantId, Review review, String createdBy, String appType, String appName, String version) throws CommentManagementException, DBConnectionException, SQLException { if (log.isDebugEnabled()) { @@ -95,7 +136,7 @@ public class CommentDAOImpl extends AbstractDAOImpl implements CommentDAO { try { statement = conn.prepareStatement(sql, new String[] { "id" }); statement.setInt(1, tenantId); - statement.setString(2, comment.getCommentText()); + statement.setString(2, review.getComment()); statement.setString(3, createdBy); statement.setString(4, version); statement.setString(5, appType); @@ -114,11 +155,10 @@ public class CommentDAOImpl extends AbstractDAOImpl implements CommentDAO { } @Override - public Comment updateComment(int commentId, String updatedComment, String modifiedBy, - Timestamp modifiedAt) throws CommentManagementException, DBConnectionException, SQLException { + public boolean updateReview(Review review, int reviewId, int tenantId) throws CommentManagementException, DBConnectionException, SQLException { if (log.isDebugEnabled()) { - log.debug("Request received in DAO Layer to update the comment with ID (" + commentId + ")"); + log.debug("Request received in DAO Layer to update the comment with ID (" + reviewId + ")"); } Connection connection; PreparedStatement statement = null; @@ -127,19 +167,20 @@ public class CommentDAOImpl extends AbstractDAOImpl implements CommentDAO { try { connection = this.getDBConnection(); statement = connection.prepareStatement(sql); - statement.setString(1, updatedComment); - statement.setString(2, modifiedBy); - statement.setInt(3, commentId); +// statement.setString(1, updatedComment); +// statement.setString(2, modifiedBy); + statement.setInt(3, reviewId); statement.executeUpdate(); rs = statement.executeQuery(); } finally { Util.cleanupResources(statement, rs); } - return getComment(commentId); + // todo + return false; } @Override - public Comment updateComment(String uuid, int commentId, String updatedComment, String modifiedBy, + public Review updateReview(String uuid, int commentId, String updatedComment, String modifiedBy, Timestamp modifiedAt) throws CommentManagementException, DBConnectionException, SQLException { if (log.isDebugEnabled()) { @@ -165,15 +206,15 @@ public class CommentDAOImpl extends AbstractDAOImpl implements CommentDAO { } @Override - public Comment getComment(int commentId) throws CommentManagementException { + public Review getComment(int commentId) throws CommentManagementException { if (log.isDebugEnabled()) { - log.debug("Getting comment with the comment id(" + commentId + ") from the database"); + log.debug("Getting review with the review id(" + commentId + ") from the database"); } Connection conn; PreparedStatement statement = null; ResultSet rs; - Comment comment = new Comment(); + Review review = new Review(); try { conn = this.getDBConnection(); sql = "SELECT COMMENT_TEXT FROM AP_APP_COMMENT WHERE ID=?;"; @@ -181,30 +222,28 @@ public class CommentDAOImpl extends AbstractDAOImpl implements CommentDAO { statement.setInt(1, commentId); rs = statement.executeQuery(); if (rs.next()) { - comment.setId(rs.getInt("ID")); - comment.setTenantId(rs.getInt("TENANT_ID")); - comment.setCommentText(rs.getString("COMMENT_TEXT")); - comment.setCreatedAt(rs.getTimestamp("CREATED_AT")); - comment.setCreatedBy(rs.getString("CREATED_BY")); - comment.setModifiedAt(rs.getTimestamp("MODEFIED_AT")); - comment.setModifiedBy(rs.getString("MODEFIED_AT")); - comment.setParent(rs.getInt("PARENT_ID")); + review.setId(rs.getInt("ID")); +// review.setTenantId(rs.getInt("TENANT_ID")); + review.setComment(rs.getString("COMMENT_TEXT")); + review.setCreatedAt(rs.getTimestamp("CREATED_AT")); + review.setUsername(rs.getString("CREATED_BY")); + review.setModifiedAt(rs.getTimestamp("MODEFIED_AT")); Util.cleanupResources(statement, rs); - return comment; + return review; } } catch (SQLException e) { throw new CommentManagementException( - "SQL Error occurred while retrieving information of the comment " + commentId, e); + "SQL Error occurred while retrieving information of the review " + commentId, e); } catch (DBConnectionException e) { - log.error("DB Connection Exception occurred while retrieving information of the comment " + commentId, e); + log.error("DB Connection Exception occurred while retrieving information of the review " + commentId, e); } finally { Util.cleanupResources(statement, null); } - return comment; + return review; } @Override - public List getComment(String uuid) throws CommentManagementException { + public List getComment(String uuid) throws CommentManagementException { if (log.isDebugEnabled()) { log.debug("Getting comment with the application release(" + uuid + ") from the database"); @@ -212,7 +251,7 @@ public class CommentDAOImpl extends AbstractDAOImpl implements CommentDAO { Connection conn; PreparedStatement statement = null; ResultSet rs; - List comments = new ArrayList<>(); + List reviews = new ArrayList<>(); try { conn = this.getDBConnection(); sql = "SELECT COMMENT_TEXT FROM AP_APP_COMMENT WHERE (SELECT ID FROM AP_APP_RELEASE where UUID=?)AND " @@ -222,29 +261,27 @@ public class CommentDAOImpl extends AbstractDAOImpl implements CommentDAO { statement.setString(2, uuid); rs = statement.executeQuery(); while (rs.next()) { - Comment comment = new Comment(); - comment.setId(rs.getInt("ID")); - comment.setTenantId(rs.getInt("TENANT_ID")); - comment.setCommentText(rs.getString("COMMENT_TEXT")); - comment.setCreatedAt(rs.getTimestamp("CREATED_AT")); - comment.setCreatedBy(rs.getString("CREATED_BY")); - comment.setModifiedAt(rs.getTimestamp("MODEFIED_AT")); - comment.setModifiedBy(rs.getString("MODEFIED_AT")); - comment.setParent(rs.getInt("PARENT_ID")); - comments.add(comment); + Review review = new Review(); + review.setId(rs.getInt("ID")); +// review.setTenantId(rs.getInt("TENANT_ID")); + review.setComment(rs.getString("COMMENT_TEXT")); + review.setCreatedAt(rs.getTimestamp("CREATED_AT")); + review.setUsername(rs.getString("CREATED_BY")); + review.setModifiedAt(rs.getTimestamp("MODEFIED_AT")); + reviews.add(review); } } catch (DBConnectionException e) { - log.error("DB Connection Exception occurred while retrieving comments", e); + log.error("DB Connection Exception occurred while retrieving reviews", e); } catch (SQLException e) { - throw new CommentManagementException("SQL Error occurred while retrieving comments", e); + throw new CommentManagementException("SQL Error occurred while retrieving reviews", e); } finally { Util.cleanupResources(statement, null); } - return comments; + return reviews; } @Override - public List getAllComments(String uuid, PaginationRequest request) + public List getAllComments(String uuid, PaginationRequest request) throws SQLException, DBConnectionException { if (log.isDebugEnabled()) { @@ -253,36 +290,61 @@ public class CommentDAOImpl extends AbstractDAOImpl implements CommentDAO { Connection conn; PreparedStatement statement = null; ResultSet rs = null; - List comments = new ArrayList<>(); + List reviews = new ArrayList<>(); try { conn = this.getDBConnection(); - sql = "SELECT AP_APP_COMMENT.ID AS ID, AP_APP_COMMENT.COMMENT_TEXT AS COMMENT_TEXT, " - + "AP_APP_COMMENT.CREATED_AT AS CREATED_AT, AP_APP_COMMENT.CREATED_BY AS CREATED_BY, " - + "AP_APP_COMMENT.MODIFIED_AT AS MODIFIED_AT, AP_APP_COMMENT.MODIFIED_BY AS MODIFIED_BY, " - + "AP_APP_COMMENT.PARENT_ID AS PARENT_ID, AP_APP_COMMENT.TENANT_ID AS TENANT_ID FROM" - + " AP_APP_COMMENT, AP_APP_RELEASE WHERE AP_APP_COMMENT.AP_APP_RELEASE_ID=AP_APP_RELEASE.ID " - + "AND AP_APP_RELEASE.UUID =? LIMIT ? OFFSET ?;"; + sql = "SELECT AP_APP_COMMENT.ID AS ID, AP_APP_COMMENT.COMMENT_TEXT AS " + + "COMMENT_TEXT, AP_APP_COMMENT.CREATED_BY AS CREATED_BY, AP_APP_COMMENT.MODIFIED_BY AS " + + "MODIFIED_BY, AP_APP_COMMENT.PARENT_ID AS PARENT_ID FROM AP_APP_COMMENT, AP_APP_RELEASE WHERE " + + "AP_APP_COMMENT.AP_APP_RELEASE_ID=AP_APP_RELEASE.ID AND AP_APP_RELEASE.UUID =? AND " + + "AP_APP_COMMENT.TENANT_ID = ? AND AP_APP_COMMENT.TENANT_ID = AP_APP_RELEASE.TENANT_ID " + + "LIMIT ? OFFSET ?;"; statement = conn.prepareStatement(sql); statement.setString(1, uuid); statement.setInt(2, request.getLimit()); statement.setInt(3, request.getOffSet()); rs = statement.executeQuery(); while (rs.next()) { - Comment comment = new Comment(); - comment.setId(rs.getInt("ID")); - comment.setCommentText(rs.getString("COMMENT_TEXT")); - comment.setCreatedAt(rs.getTimestamp("CREATED_AT")); - comment.setCreatedBy(rs.getString("CREATED_BY")); - comment.setModifiedAt(rs.getTimestamp("MODIFIED_AT")); - comment.setModifiedBy(rs.getString("MODIFIED_BY")); - comment.setParent(rs.getInt("PARENT_ID")); - comment.setTenantId(rs.getInt("TENANT_ID")); - comments.add(comment); + Review review = new Review(); + review.setId(rs.getInt("ID")); + review.setComment(rs.getString("COMMENT_TEXT")); + review.setUsername(rs.getString("CREATED_BY")); + reviews.add(review); + } + } finally { + Util.cleanupResources(statement, rs); + } + return reviews; + } + + @Override + public List getAllRatingValues(String uuid)throws SQLException, DBConnectionException { + + if (log.isDebugEnabled()) { + log.debug("Getting comment of the application release (" + uuid + ") from the database"); + } + Connection conn; + PreparedStatement statement = null; + ResultSet rs = null; + List reviews = new ArrayList<>(); + try { + conn = this.getDBConnection(); +// todo + sql = "SELECT AP_APP_COMMENT.ID AS ID, AP_APP_COMMENT.COMMENT_TEXT AS " + + "COMMENT_TEXT, AP_APP_COMMENT.CREATED_BY AS CREATED_BY, AP_APP_COMMENT.MODIFIED_BY AS " + + "MODIFIED_BY, AP_APP_COMMENT.PARENT_ID AS PARENT_ID FROM AP_APP_COMMENT, AP_APP_RELEASE WHERE " + + "AP_APP_COMMENT.AP_APP_RELEASE_ID=AP_APP_RELEASE.ID AND AP_APP_RELEASE.UUID =? AND " + + "AP_APP_COMMENT.TENANT_ID = ? AND AP_APP_COMMENT.TENANT_ID = AP_APP_RELEASE.TENANT_ID;"; + statement = conn.prepareStatement(sql); + statement.setString(1, uuid); + rs = statement.executeQuery(); + while (rs.next()) { + reviews.add(rs.getInt("RATING")); } } finally { Util.cleanupResources(statement, rs); } - return comments; + return reviews; } @Override @@ -320,16 +382,16 @@ public class CommentDAOImpl extends AbstractDAOImpl implements CommentDAO { } @Override - public List getComments(int appReleasedId, int appId) throws CommentManagementException { + public List getComments(int appReleasedId, int appId) throws CommentManagementException { if (log.isDebugEnabled()) { - log.debug("Getting comments with the application release id(" + appReleasedId + ") and " + "application id(" + log.debug("Getting reviews with the application release id(" + appReleasedId + ") and " + "application id(" + appId + ") from the database"); } Connection conn; PreparedStatement statement = null; ResultSet rs; - List comments = new ArrayList<>(); + List reviews = new ArrayList<>(); try { conn = this.getDBConnection(); sql = "SELECT COMMENT_TEXT FROM AP_APP_COMMENT WHERE AP_APP_RELEASE_ID=? AND AP_APP_ID=?;"; @@ -338,39 +400,37 @@ public class CommentDAOImpl extends AbstractDAOImpl implements CommentDAO { statement.setInt(2, appId); rs = statement.executeQuery(); while (rs.next()) { - Comment comment = new Comment(); - comment.setId(rs.getInt("ID")); - comment.setTenantId(rs.getInt("TENANT_ID")); - comment.setCommentText(rs.getString("COMMENT_TEXT")); - comment.setCreatedAt(rs.getTimestamp("CREATED_AT")); - comment.setCreatedBy(rs.getString("CREATED_BY")); - comment.setModifiedAt(rs.getTimestamp("MODEFIED_AT")); - comment.setModifiedBy(rs.getString("modifiedBy")); - comment.setParent(rs.getInt("PARENT_ID")); - comments.add(comment); + Review review = new Review(); + review.setId(rs.getInt("ID")); +// review.setTenantId(rs.getInt("TENANT_ID")); + review.setComment(rs.getString("COMMENT_TEXT")); + review.setCreatedAt(rs.getTimestamp("CREATED_AT")); + review.setUsername(rs.getString("CREATED_BY")); + review.setModifiedAt(rs.getTimestamp("MODEFIED_AT")); + reviews.add(review); } } catch (DBConnectionException e) { - log.error("DB Connection Exception occurred while retrieving information of comments", e); + log.error("DB Connection Exception occurred while retrieving information of reviews", e); } catch (SQLException e) { - throw new CommentManagementException("SQL Error occurred while retrieving information of comments", e); + throw new CommentManagementException("SQL Error occurred while retrieving information of reviews", e); } finally { Util.cleanupResources(statement, null); } - return comments; + return reviews; } @Override - public List getComments(String appType, String appName, String version) + public List getComments(String appType, String appName, String version) throws CommentManagementException, DBConnectionException, SQLException { if (log.isDebugEnabled()) { - log.debug("Getting comments with the application name(" + appName + "),application type(" + appType + ") " + log.debug("Getting reviews with the application name(" + appName + "),application type(" + appType + ") " + "and application version (" + version + ") from the database"); } Connection conn; PreparedStatement statement = null; ResultSet rs; - List comments = new ArrayList<>(); + List reviews = new ArrayList<>(); try { conn = this.getDBConnection(); sql = "SELECT COMMENT_TEXT,PARENT_ID,TENANT_ID FROM AP_APP_COMMENT C ," @@ -384,34 +444,32 @@ public class CommentDAOImpl extends AbstractDAOImpl implements CommentDAO { statement.setString(3, appType); rs = statement.executeQuery(); while (rs.next()) { - Comment comment = new Comment(); - comment.setId(rs.getInt("ID")); - comment.setTenantId(rs.getInt("TENANT_ID")); - comment.setCommentText(rs.getString("COMMENT_TEXT")); - comment.setCreatedAt(rs.getTimestamp("CREATED_AT")); - comment.setCreatedBy(rs.getString("CREATED_BY")); - comment.setModifiedAt(rs.getTimestamp("MODEFIED_AT")); - comment.setModifiedBy(rs.getString("modifiedBy")); - comment.setParent(rs.getInt("PARENT_ID")); - comments.add(comment); + Review review = new Review(); + review.setId(rs.getInt("ID")); +// review.setTenantId(rs.getInt("TENANT_ID")); + review.setComment(rs.getString("COMMENT_TEXT")); + review.setCreatedAt(rs.getTimestamp("CREATED_AT")); + review.setUsername(rs.getString("CREATED_BY")); + review.setModifiedAt(rs.getTimestamp("MODEFIED_AT")); + reviews.add(review); } } finally { Util.cleanupResources(statement, null); } - return comments; + return reviews; } @Override - public List getComments(int tenantId) + public List getComments(int tenantId) throws CommentManagementException, DBConnectionException, SQLException { if (log.isDebugEnabled()) { - log.debug("Getting comments with the tenant_id(" + tenantId + ") from the database"); + log.debug("Getting reviews with the tenant_id(" + tenantId + ") from the database"); } Connection conn; PreparedStatement statement = null; ResultSet rs; - List comments = new ArrayList<>(); + List reviews = new ArrayList<>(); try { conn = this.getDBConnection(); sql = "SELECT COMMENT_TEXT FROM AP_APP_COMMENT WHERE TENANT_ID='?';"; @@ -419,34 +477,32 @@ public class CommentDAOImpl extends AbstractDAOImpl implements CommentDAO { statement.setInt(1, tenantId); rs = statement.executeQuery(); while (rs.next()) { - Comment comment = new Comment(); - comment.setId(rs.getInt("ID")); - comment.setTenantId(rs.getInt("TENANT_ID")); - comment.setCommentText(rs.getString("COMMENT_TEXT")); - comment.setCreatedAt(rs.getTimestamp("CREATED_AT")); - comment.setCreatedBy(rs.getString("CREATED_BY")); - comment.setModifiedAt(rs.getTimestamp("MODEFIED_AT")); - comment.setModifiedBy(rs.getString("modifiedBy")); - comment.setParent(rs.getInt("PARENT_ID")); - comments.add(comment); + Review review = new Review(); + review.setId(rs.getInt("ID")); +// review.setTenantId(rs.getInt("TENANT_ID")); + review.setComment(rs.getString("COMMENT_TEXT")); + review.setCreatedAt(rs.getTimestamp("CREATED_AT")); + review.setUsername(rs.getString("CREATED_BY")); + review.setModifiedAt(rs.getTimestamp("MODEFIED_AT")); + reviews.add(review); } } finally { Util.cleanupResources(statement, null); } - return comments; + return reviews; } @Override - public List getCommentsByUser(String createdBy) + public List getCommentsByUser(String createdBy) throws CommentManagementException, DBConnectionException, SQLException { if (log.isDebugEnabled()) { - log.debug("Getting comments with the created by(" + createdBy + ") from the database"); + log.debug("Getting reviews with the created by(" + createdBy + ") from the database"); } Connection conn; PreparedStatement statement = null; ResultSet rs; - List comments = new ArrayList<>(); + List reviews = new ArrayList<>(); try { conn = this.getDBConnection(); sql = "SELECT COMMENT_TEXT ,PARENT_ID,TENANT_ID,CREATED_AT FROM AP_APP_COMMENT WHERE CREATED_BY= ?" @@ -455,35 +511,33 @@ public class CommentDAOImpl extends AbstractDAOImpl implements CommentDAO { statement.setString(1, createdBy); rs = statement.executeQuery(); while (rs.next()) { - Comment comment = new Comment(); - comment.setId(rs.getInt("ID")); - comment.setTenantId(rs.getInt("TENANT_ID")); - comment.setCommentText(rs.getString("COMMENT_TEXT")); - comment.setCreatedAt(rs.getTimestamp("CREATED_AT")); - comment.setCreatedBy(rs.getString("CREATED_BY")); - comment.setModifiedAt(rs.getTimestamp("MODEFIED_AT")); - comment.setModifiedBy(rs.getString("modifiedBy")); - comment.setParent(rs.getInt("PARENT_ID")); - comments.add(comment); + Review review = new Review(); + review.setId(rs.getInt("ID")); +// review.setTenantId(rs.getInt("TENANT_ID")); + review.setComment(rs.getString("COMMENT_TEXT")); + review.setCreatedAt(rs.getTimestamp("CREATED_AT")); + review.setUsername(rs.getString("CREATED_BY")); + review.setModifiedAt(rs.getTimestamp("MODEFIED_AT")); + reviews.add(review); } } finally { Util.cleanupResources(statement, null); } - return comments; + return reviews; } @Override - public List getCommentsByUser(String createdBy, Timestamp createdAt) + public List getCommentsByUser(String createdBy, Timestamp createdAt) throws CommentManagementException, DBConnectionException, SQLException { if (log.isDebugEnabled()) { log.debug( - "Getting comments with the created by(" + createdBy + ") at (" + createdAt + ") from the database"); + "Getting reviews with the created by(" + createdBy + ") at (" + createdAt + ") from the database"); } Connection conn; PreparedStatement statement = null; ResultSet rs; - List comments = new ArrayList<>(); + List reviews = new ArrayList<>(); try { conn = this.getDBConnection(); sql = "SELECT COMMENT_TEXT,PARENT_ID,TENANT_ID FROM AP_APP_COMMENT WHERE CREATED_BY=?" @@ -493,34 +547,32 @@ public class CommentDAOImpl extends AbstractDAOImpl implements CommentDAO { statement.setTimestamp(2, createdAt); rs = statement.executeQuery(); while (rs.next()) { - Comment comment = new Comment(); - comment.setId(rs.getInt("ID")); - comment.setTenantId(rs.getInt("TENANT_ID")); - comment.setCommentText(rs.getString("COMMENT_TEXT")); - comment.setCreatedAt(rs.getTimestamp("CREATED_AT")); - comment.setCreatedBy(rs.getString("CREATED_BY")); - comment.setModifiedAt(rs.getTimestamp("MODEFIED_AT")); - comment.setModifiedBy(rs.getString("modifiedBy")); - comment.setParent(rs.getInt("PARENT_ID")); - comments.add(comment); + Review review = new Review(); + review.setId(rs.getInt("ID")); +// review.setTenantId(rs.getInt("TENANT_ID")); + review.setComment(rs.getString("COMMENT_TEXT")); + review.setCreatedAt(rs.getTimestamp("CREATED_AT")); + review.setUsername(rs.getString("CREATED_BY")); + review.setModifiedAt(rs.getTimestamp("MODEFIED_AT")); + reviews.add(review); } } finally { Util.cleanupResources(statement, null); } - return comments; + return reviews; } @Override - public List getCommentsByModifiedUser(String modifiedBy) + public List getCommentsByModifiedUser(String modifiedBy) throws CommentManagementException, DBConnectionException, SQLException { if (log.isDebugEnabled()) { - log.debug("Getting comments with the modified by(" + modifiedBy + ") from the database"); + log.debug("Getting reviews with the modified by(" + modifiedBy + ") from the database"); } Connection conn; PreparedStatement statement = null; ResultSet rs; - List comments = new ArrayList<>(); + List reviews = new ArrayList<>(); try { conn = this.getDBConnection(); sql = "SELECT COMMENT_TEXT,PARENT_ID,TENANT_ID,CREATED_AT,MODEFIED_AT FROM AP_APP_COMMENT " @@ -529,35 +581,33 @@ public class CommentDAOImpl extends AbstractDAOImpl implements CommentDAO { statement.setString(1, modifiedBy); rs = statement.executeQuery(); while (rs.next()) { - Comment comment = new Comment(); - comment.setId(rs.getInt("ID")); - comment.setTenantId(rs.getInt("TENANT_ID")); - comment.setCommentText(rs.getString("COMMENT_TEXT")); - comment.setCreatedAt(rs.getTimestamp("CREATED_AT")); - comment.setCreatedBy(rs.getString("CREATED_BY")); - comment.setModifiedAt(rs.getTimestamp("MODEFIED_AT")); - comment.setModifiedBy(rs.getString("modifiedBy")); - comment.setParent(rs.getInt("PARENT_ID")); - comments.add(comment); + Review review = new Review(); + review.setId(rs.getInt("ID")); +// review.setTenantId(rs.getInt("TENANT_ID")); + review.setComment(rs.getString("COMMENT_TEXT")); + review.setCreatedAt(rs.getTimestamp("CREATED_AT")); + review.setUsername(rs.getString("CREATED_BY")); + review.setModifiedAt(rs.getTimestamp("MODEFIED_AT")); + reviews.add(review); } } finally { Util.cleanupResources(statement, null); } - return comments; + return reviews; } @Override - public List getCommentsByModifiedUser(String modifiedBy, Timestamp modifiedAt) + public List getCommentsByModifiedUser(String modifiedBy, Timestamp modifiedAt) throws CommentManagementException, DBConnectionException, SQLException { if (log.isDebugEnabled()) { - log.debug("Getting comments with the modified by(" + modifiedBy + ") at (" + modifiedAt + ") from the " + log.debug("Getting reviews with the modified by(" + modifiedBy + ") at (" + modifiedAt + ") from the " + "database"); } Connection conn; PreparedStatement statement = null; ResultSet rs; - List comments = new ArrayList<>(); + List reviews = new ArrayList<>(); try { conn = this.getDBConnection(); sql = "SELECT COMMENT_TEXT,PARENT_ID,TENANT_ID,CREATED_AT FROM AP_APP_COMMENT WHERE MODEFIED_BY= ?," @@ -567,36 +617,34 @@ public class CommentDAOImpl extends AbstractDAOImpl implements CommentDAO { statement.setTimestamp(2, modifiedAt); rs = statement.executeQuery(); while (rs.next()) { - Comment comment = new Comment(); - comment.setId(rs.getInt("ID")); - comment.setTenantId(rs.getInt("TENANT_ID")); - comment.setCommentText(rs.getString("COMMENT_TEXT")); - comment.setCreatedAt(rs.getTimestamp("CREATED_AT")); - comment.setCreatedBy(rs.getString("CREATED_BY")); - comment.setModifiedAt(rs.getTimestamp("MODEFIED_AT")); - comment.setModifiedBy(rs.getString("modifiedBy")); - comment.setParent(rs.getInt("PARENT_ID")); - comments.add(comment); + Review review = new Review(); + review.setId(rs.getInt("ID")); +// review.setTenantId(rs.getInt("TENANT_ID")); + review.setComment(rs.getString("COMMENT_TEXT")); + review.setCreatedAt(rs.getTimestamp("CREATED_AT")); + review.setUsername(rs.getString("CREATED_BY")); + review.setModifiedAt(rs.getTimestamp("MODEFIED_AT")); + reviews.add(review); } } finally { Util.cleanupResources(statement, null); } - return comments; + return reviews; } @Override - public List getComments(String appType, String appName, String version, int parentId) + public List getComments(String appType, String appName, String version, int parentId) throws CommentManagementException, DBConnectionException, SQLException { if (log.isDebugEnabled()) { log.debug( - "Getting comments with the application name(" + appName + "),application type(" + appType + ") and" + "Getting reviews with the application name(" + appName + "),application type(" + appType + ") and" + "application version (" + version + ") from the database"); } Connection conn; PreparedStatement statement = null; ResultSet rs; - List comments = new ArrayList<>(); + List reviews = new ArrayList<>(); try { conn = this.getDBConnection(); sql = "SELECT COMMENT_TEXT,TENANT_ID FROM AP_APP_COMMENT C ," @@ -611,21 +659,19 @@ public class CommentDAOImpl extends AbstractDAOImpl implements CommentDAO { statement.setInt(4, parentId); rs = statement.executeQuery(); while (rs.next()) { - Comment comment = new Comment(); - comment.setId(rs.getInt("ID")); - comment.setTenantId(rs.getInt("TENANT_ID")); - comment.setCommentText(rs.getString("COMMENT_TEXT")); - comment.setCreatedAt(rs.getTimestamp("CREATED_AT")); - comment.setCreatedBy(rs.getString("CREATED_BY")); - comment.setModifiedAt(rs.getTimestamp("MODEFIED_AT")); - comment.setModifiedBy(rs.getString("modifiedBy")); - comment.setParent(rs.getInt("PARENT_ID")); - comments.add(comment); + Review review = new Review(); + review.setId(rs.getInt("ID")); +// review.setTenantId(rs.getInt("TENANT_ID")); + review.setComment(rs.getString("COMMENT_TEXT")); + review.setCreatedAt(rs.getTimestamp("CREATED_AT")); + review.setUsername(rs.getString("CREATED_BY")); + review.setModifiedAt(rs.getTimestamp("MODEFIED_AT")); + reviews.add(review); } } finally { Util.cleanupResources(statement, null); } - return comments; + return reviews; } @Override diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/application/GenericApplicationDAOImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/application/GenericApplicationDAOImpl.java index e5e2153261c..12c80c4c525 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/application/GenericApplicationDAOImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/application/GenericApplicationDAOImpl.java @@ -380,7 +380,7 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic Connection conn; PreparedStatement stmt = null; ResultSet rs = null; - Boolean isAppExist = false; + boolean isAppExist = false; try { conn = this.getDBConnection(); String sql = diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/application/release/GenericApplicationReleaseDAOImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/application/release/GenericApplicationReleaseDAOImpl.java index f15c091863e..c86e7f86849 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/application/release/GenericApplicationReleaseDAOImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/application/release/GenericApplicationReleaseDAOImpl.java @@ -20,6 +20,7 @@ package org.wso2.carbon.device.application.mgt.core.dao.impl.application.release; import org.wso2.carbon.device.application.mgt.common.ApplicationRelease; +import org.wso2.carbon.device.application.mgt.common.Rating; import org.wso2.carbon.device.application.mgt.common.exception.DBConnectionException; import org.wso2.carbon.device.application.mgt.core.dao.ApplicationReleaseDAO; import org.wso2.carbon.device.application.mgt.core.dao.common.Util; @@ -268,20 +269,21 @@ public class GenericApplicationReleaseDAOImpl extends AbstractDAOImpl implements * To Update starts of an application release. * * @param id Id of the application Release. - * @param stars given stars for the application release. + * @param rating given stars for the application release. * @throws ApplicationManagementDAOException Application Management DAO Exception. */ @Override - public void updateStars(int id, int stars) throws ApplicationManagementDAOException { + public int updateRatingValue(int id, double rating, int ratedUsers) throws ApplicationManagementDAOException { Connection connection; PreparedStatement statement = null; - String sql = "UPDATE AP_APP_RELEASE SET STARS = ? WHERE ID = ?;"; + String sql = "UPDATE AP_APP_RELEASE SET RATING = ? AND RATED_USERS = ? WHERE ID = ?;"; try { connection = this.getDBConnection(); statement = connection.prepareStatement(sql); - statement.setInt(1, stars); + statement.setDouble(1, rating); + statement.setInt(2, ratedUsers); statement.setInt(2, id); - statement.executeUpdate(); + return statement.executeUpdate(); } catch (DBConnectionException e) { throw new ApplicationManagementDAOException( "Database connection exception while trying to update the application release", e); @@ -293,6 +295,42 @@ public class GenericApplicationReleaseDAOImpl extends AbstractDAOImpl implements } } + /** + * To retrieve rating of an application release. + * + * @param id Id of the application Release. + * @throws ApplicationManagementDAOException Application Management DAO Exception. + */ + @Override + public Rating getRating(int id) throws ApplicationManagementDAOException { + Connection connection; + PreparedStatement statement = null; + ResultSet resultSet = null; + Rating rating = null; + String sql = "SELECT RATING, RATED_USERS FROM AP_APP_RELEASE WHERE ID = ?;"; + try { + connection = this.getDBConnection(); + statement = connection.prepareStatement(sql); + statement.setInt(1, id); + resultSet = statement.executeQuery(); + + if (resultSet.next()){ + rating = new Rating(); + rating.setRatingValue(resultSet.getDouble("RATING")); + rating.setNoOfUsers(resultSet.getInt("RATED_USERS")); + } + return rating; + } catch (DBConnectionException e) { + throw new ApplicationManagementDAOException( + "Database connection exception while trying to update the application release", e); + } catch (SQLException e) { + throw new ApplicationManagementDAOException( + "SQL exception while updating the release ,while executing the query " + sql, e); + } finally { + Util.cleanupResources(statement, resultSet); + } + } + /** * To insert the application release properties. * @@ -369,7 +407,7 @@ public class GenericApplicationReleaseDAOImpl extends AbstractDAOImpl implements } /** - * This method is capable to construct {@ApplicationRelease} and return the object + * This method is capable to construct {@link ApplicationRelease} and return the object * @param resultSet result set obtained from the query executing. * @throws SQLException SQL exception while accessing result set data. */ diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/CommentsManagerImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/CommentsManagerImpl.java deleted file mode 100644 index e42245db50c..00000000000 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/CommentsManagerImpl.java +++ /dev/null @@ -1,289 +0,0 @@ -/* -* Copyright (c) 2018, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. -* WSO2 Inc. licenses this file to you under the Apache License, -* Version 2.0 (the "License"); you may not use this file except -* in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, -* software distributed under the License is distributed on an -* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -* KIND, either express or implied. See the License for the -* specific language governing permissions and limitations -* under the License. -* -*/ -package org.wso2.carbon.device.application.mgt.core.impl; - -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.wso2.carbon.context.PrivilegedCarbonContext; -import org.wso2.carbon.device.application.mgt.common.Comment; -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.exception.ApplicationManagementException; -import org.wso2.carbon.device.application.mgt.common.exception.CommentManagementException; -import org.wso2.carbon.device.application.mgt.common.exception.DBConnectionException; -import org.wso2.carbon.device.application.mgt.common.exception.TransactionManagementException; -import org.wso2.carbon.device.application.mgt.common.services.*; -import org.wso2.carbon.device.application.mgt.core.dao.CommentDAO; -import org.wso2.carbon.device.application.mgt.core.dao.common.ApplicationManagementDAOFactory; -import org.wso2.carbon.device.application.mgt.core.dao.common.Util; -import org.wso2.carbon.device.application.mgt.core.exception.ApplicationManagementDAOException; -import org.wso2.carbon.device.application.mgt.core.util.ConnectionManagerUtil; - -import java.sql.SQLException; -import java.sql.Timestamp; -import java.time.Instant; -import java.util.ArrayList; -import java.util.List; - -/** - * This class is the default implementation for the Managing the comments. - */ -public class CommentsManagerImpl implements CommentsManager { - - private static final Log log = LogFactory.getLog(CommentsManagerImpl.class); - private CommentDAO commentDAO; - - public CommentsManagerImpl() { - initDataAccessObjects(); - } - - private void initDataAccessObjects() { - this.commentDAO = ApplicationManagementDAOFactory.getCommentDAO(); - } - - @Override - public Comment addComment(Comment comment, String uuid) throws CommentManagementException { - - if (log.isDebugEnabled()) { - log.debug("Request for comment is received for uuid" + uuid); - } - int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); - comment.setCreatedAt(Timestamp.from(Instant.now())); - try { - ConnectionManagerUtil.beginDBTransaction(); - if (commentDAO.addComment(tenantId, comment, comment.getCreatedBy(), comment.getParent(), uuid)) { - ConnectionManagerUtil.commitDBTransaction(); - return comment; - } else { - ConnectionManagerUtil.rollbackDBTransaction(); - return null; - } - } catch (DBConnectionException e) { - ConnectionManagerUtil.rollbackDBTransaction(); - throw new CommentManagementException( - "DB Connection error occurs ,Comment for application with UUID " + uuid + "cannot add.", e); - } catch (SQLException e) { - throw new CommentManagementException( - "SQL Exception occurs,Comment for application with UUID " + uuid + "cannot add.", e); - } catch (TransactionManagementException e) { - throw new CommentManagementException( - "Transaction Management Exception occurs,Comment for application with UUID " + uuid + "cannot add.", - e); - } finally { - ConnectionManagerUtil.closeDBConnection(); - } - } - - /** - * To validate the pre-request of the comment - * - * @param commentId ID of the comment. - * @param comment comment needed to be validate. - * @return Application related with the UUID. - * @throws CommentManagementException Exceptions of the comment management. - */ - - public Boolean validateComment(int commentId, String comment) throws CommentManagementException { - - if (commentId <= 0) { - throw new CommentManagementException( - "Comment ID is null or negative. Comment id is a required parameter to get the " - + "relevant comment."); - } - if (comment == null) { - log.error("Comment can not be null, but Comment at comment id " + commentId + " is null."); - return false; - } - return true; - } - - @Override - public PaginationResult getAllComments(PaginationRequest request, String uuid) throws CommentManagementException { - - PaginationResult paginationResult = new PaginationResult(); - int numOfComments; - List comments; - if (log.isDebugEnabled()) { - log.debug("get all comments of the application release" + uuid); - } - try { - ConnectionManagerUtil.openDBConnection(); - comments = commentDAO.getAllComments(uuid, Util.validateCommentListPageSize(request)); - numOfComments = comments.size(); - paginationResult.setData(comments); - if (numOfComments > 0) { - paginationResult.setRecordsFiltered(numOfComments); - paginationResult.setRecordsTotal(numOfComments); - } else { - paginationResult.setRecordsFiltered(0); - paginationResult.setRecordsTotal(0); - } - return paginationResult; - } catch (DBConnectionException e) { - throw new CommentManagementException( - "DB Connection error occurs , while getting comments of application release UUID: " + uuid, e); - } catch (SQLException e) { - throw new CommentManagementException( - "SQL Exception occurs, while getting comments of application release UUID: " + uuid, e); - } finally { - ConnectionManagerUtil.closeDBConnection(); - } - } - - @Override - public Comment getComment(int commentId) throws CommentManagementException { - - PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); - Comment comment; - if (log.isDebugEnabled()) { - log.debug("Comment retrieval request is received for the comment id " + commentId); - } - try { - ConnectionManagerUtil.openDBConnection(); - comment = commentDAO.getComment(commentId); - } catch (DBConnectionException e) { - throw new CommentManagementException( - "DB Connection error occurs ,Comment with comment id " + commentId + "cannot get.", e); - } catch (SQLException e) { - throw new CommentManagementException( - "SQL Exception occurs,Comment with comment id " + commentId + "cannot get.", e); - } finally { - ConnectionManagerUtil.closeDBConnection(); - } - return comment; - } - - @Override - public void deleteComment(int commentId) throws CommentManagementException { - - Comment comment; - comment = getComment(commentId); - if (comment == null) { - throw new CommentManagementException( - "Cannot delete a non-existing comment for the application with comment id" + commentId); - } - try { - ConnectionManagerUtil.beginDBTransaction(); - commentDAO.deleteComment(commentId); - ConnectionManagerUtil.commitDBTransaction(); - } catch (DBConnectionException e) { - throw new CommentManagementException( - "DB Connection error occurs deleting comment with comment id " + commentId + ".", e); - } catch (SQLException e) { - throw new CommentManagementException("SQL error occurs deleting comment with comment id " + commentId + ".", - e); - } catch (TransactionManagementException e) { - throw new CommentManagementException( - "Transaction Management Exception occurs deleting comment with comment id " + commentId + ".", e); - } finally { - ConnectionManagerUtil.closeDBConnection(); - } - } - - @Override - public Comment updateComment(Comment comment, int commentId) throws CommentManagementException { - - PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); - validateComment(commentId, comment.getCommentText()); - if (log.isDebugEnabled()) { - log.debug("Comment retrieval request is received for the comment id " + commentId); - } - comment.setModifiedAt(Timestamp.from(Instant.now())); - try { - ConnectionManagerUtil.openDBConnection(); - commentDAO.getComment(commentId); - return commentDAO - .updateComment(commentId, comment.getCommentText(), comment.getModifiedBy(), comment.getModifiedAt()); - } catch (SQLException e) { - throw new CommentManagementException("SQL Error occurs updating comment with comment id " + commentId + ".", - e); - } catch (DBConnectionException e) { - throw new CommentManagementException( - "DB Connection error occurs updating comment with comment id " + commentId + ".", e); - } finally { - ConnectionManagerUtil.closeDBConnection(); - } - } - - @Override - public int getRatedUser(String uuid) throws ApplicationManagementException { - - int ratedUsers; - if (log.isDebugEnabled()) { - log.debug("Get the rated users for the application release number" + uuid); - } - try { - ConnectionManagerUtil.openDBConnection(); - ratedUsers = commentDAO.getRatedUser(uuid); - } catch (ApplicationManagementDAOException e) { - throw new ApplicationManagementException( - "Rated Users of the Application with UUID " + uuid + " can not get.", e); - } finally { - ConnectionManagerUtil.closeDBConnection(); - } - return ratedUsers; - } - - @Override - public int getStars(String uuid) throws ApplicationManagementException { - - int stars; - if (log.isDebugEnabled()) { - log.debug("Get the average of rated stars for the application " + uuid); - } - try { - ConnectionManagerUtil.openDBConnection(); - stars = commentDAO.getStars(uuid); - } catch (ApplicationManagementDAOException e) { - throw new ApplicationManagementException( - "Average stars of the Application with UUID " + uuid + " can not get.", e); - } finally { - ConnectionManagerUtil.closeDBConnection(); - } - return stars; - } - - @Override - public int updateStars(int stars, String uuid) throws ApplicationManagementException { - - if (log.isDebugEnabled()) { - log.debug("Stars are received for the application " + uuid); - } - int newStars; - try { - ConnectionManagerUtil.beginDBTransaction(); - int ratedUsers = commentDAO.getRatedUser(uuid); - int oldStars = commentDAO.getStars(uuid); - if (ratedUsers == 0) { - newStars = commentDAO.updateStars(stars, uuid); - return newStars; - } else { - int avgStars = ((oldStars * ratedUsers) + stars) / (ratedUsers + 1); - newStars = commentDAO.updateStars(avgStars, uuid); - ConnectionManagerUtil.commitDBTransaction(); - return newStars; - } - } catch (ApplicationManagementDAOException e) { - ConnectionManagerUtil.rollbackDBTransaction(); - throw new ApplicationManagementException( - "Updated average stars of the Application with UUID " + uuid + " can not get.", e); - } finally { - ConnectionManagerUtil.closeDBConnection(); - } - } -} \ No newline at end of file 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 new file mode 100644 index 00000000000..f742fd5c57b --- /dev/null +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ReviewManagerImpl.java @@ -0,0 +1,398 @@ +/* +* Copyright (c) 2018, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. +* WSO2 Inc. licenses this file to you under the Apache License, +* Version 2.0 (the "License"); you may not use this file except +* in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, +* software distributed under the License is distributed on an +* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +* KIND, either express or implied. See the License for the +* specific language governing permissions and limitations +* under the License. +* +*/ +package org.wso2.carbon.device.application.mgt.core.impl; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.CarbonConstants; +import org.wso2.carbon.context.PrivilegedCarbonContext; +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.PaginationRequest; +import org.wso2.carbon.device.application.mgt.common.PaginationResult; +import org.wso2.carbon.device.application.mgt.common.exception.CommentManagementException; +import org.wso2.carbon.device.application.mgt.common.exception.DBConnectionException; +import org.wso2.carbon.device.application.mgt.common.exception.TransactionManagementException; +import org.wso2.carbon.device.application.mgt.common.services.*; +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.common.ApplicationManagementDAOFactory; +import org.wso2.carbon.device.application.mgt.core.dao.common.Util; +import org.wso2.carbon.device.application.mgt.core.exception.ApplicationManagementDAOException; +import org.wso2.carbon.device.application.mgt.core.internal.DataHolder; +import org.wso2.carbon.device.application.mgt.core.util.ConnectionManagerUtil; +import org.wso2.carbon.user.api.UserRealm; +import org.wso2.carbon.user.api.UserStoreException; +import org.wso2.carbon.utils.multitenancy.MultitenantUtils; + +import java.sql.SQLException; +import java.util.List; +import java.util.TreeMap; + +/** + * This class is the default implementation for the Managing the comments. + */ +public class ReviewManagerImpl implements ReviewManager { + + private static final Log log = LogFactory.getLog(ReviewManagerImpl.class); + private ReviewDAO reviewDAO; + private ApplicationReleaseDAO applicationReleaseDAO; + + public ReviewManagerImpl() { + initDataAccessObjects(); + } + + private void initDataAccessObjects() { + this.reviewDAO = ApplicationManagementDAOFactory.getCommentDAO(); + this.applicationReleaseDAO = ApplicationManagementDAOFactory.getApplicationReleaseDAO(); + } + + @Override + public boolean addReview(Review review, int appId, int appReleaseId) throws CommentManagementException { + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); + String username = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); + boolean isSuccess; + try { + ConnectionManagerUtil.beginDBTransaction(); + Review existingReview = reviewDAO.isExistReview(appId, appReleaseId, username, tenantId); + if (existingReview != null && review.getRating() > 0 && review.getRating() != existingReview.getRating()) { + Runnable task = () -> { + try { + if (calculateRating(review.getRating(), existingReview.getRating()) <= 0.0) { + log.error("Application release rating updating task is failed"); + } + } catch (CommentManagementException e) { + // todo + log.error("comment management error occured"); + } + }; + new Thread(task).start(); + isSuccess = updateReview(review, existingReview.getId(), false); + if (isSuccess) { + ConnectionManagerUtil.commitDBTransaction(); + } else { + ConnectionManagerUtil.rollbackDBTransaction(); + } + } else { + if (review.getRating()>0){ + Runnable task = () -> { + try { + if (calculateRating(review.getRating(), -12345) <= 0.0) { + log.error("Application release rating inserting task is failed"); + } + } catch (CommentManagementException e) { + // todo + log.error("comment management error occured"); + } + }; + new Thread(task).start(); + } + review.setUsername(username); + isSuccess = this.reviewDAO.addReview(review, appId, appReleaseId, tenantId); + if (isSuccess) { + ConnectionManagerUtil.commitDBTransaction(); + } else { + ConnectionManagerUtil.rollbackDBTransaction(); + } + } + return isSuccess; + } catch (DBConnectionException e) { + ConnectionManagerUtil.rollbackDBTransaction(); + throw new CommentManagementException( + "DB Connection error occurs ,Review for application with app id: " + appId + " and app release id: " + + appReleaseId + " is failed", e); + } catch (SQLException e) { + ConnectionManagerUtil.rollbackDBTransaction(); + throw new CommentManagementException( + "SQL Exception occurs,Review for application with app id:" + appId + " and app release id:" + + appReleaseId + " is failed", e); + } catch (TransactionManagementException e) { + ConnectionManagerUtil.rollbackDBTransaction(); + throw new CommentManagementException( + "Transaction Management Exception occurs,Review for application with app id:" + appId + + " and app release id:" + appReleaseId + " is failed ", e); + } finally { + ConnectionManagerUtil.closeDBConnection(); + } + } + + @Override + public boolean updateReview(Review review, int reviewId, boolean checkExistence) + throws CommentManagementException { + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); + String username = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); + Review existingReview; + boolean isSuccess; + if (log.isDebugEnabled()) { + log.debug("Review updating request is received for the review id " + reviewId); + } + try { + ConnectionManagerUtil.openDBConnection(); + if (!username.equals(review.getUsername())) { + throw new CommentManagementException( + "User " + review.getUsername() + "doesn't match with the logged in user: " + username); + } + if (checkExistence) { + existingReview = this.reviewDAO.getComment(reviewId); + if (existingReview != null) { + if (review.getRating() > 0 && review.getRating() != existingReview.getRating()) { + Runnable task = () -> { + try { + if (calculateRating(review.getRating(), existingReview.getRating()) <= 0.0) { + log.error("Application release review updating task is failed"); + } + } catch (CommentManagementException e) { + // todo + log.error("error"); + } + }; + new Thread(task).start(); + } + } else { + throw new CommentManagementException("Couldn't find a review for review id: " + reviewId); + } + } + ConnectionManagerUtil.beginDBTransaction(); + isSuccess = this.reviewDAO.updateReview(review, reviewId, tenantId); + if (isSuccess) { + ConnectionManagerUtil.commitDBTransaction(); + } else { + ConnectionManagerUtil.rollbackDBTransaction(); + } + return isSuccess; + } catch (SQLException e) { + throw new CommentManagementException("SQL Error occurs updating review with review id " + reviewId + ".", + e); + } catch (DBConnectionException e) { + throw new CommentManagementException( + "DB Connection error occurs updating review with review id " + reviewId + ".", e); + } catch (TransactionManagementException e) { + throw new CommentManagementException( + "Transaction management error occurs when updating review with review id " + reviewId + ".", e); + } finally { + ConnectionManagerUtil.closeDBConnection(); + } + } + + @Override + public PaginationResult getAllReviews(PaginationRequest request, String uuid) throws CommentManagementException { + PaginationResult paginationResult = new PaginationResult(); + int numOfComments; + List reviews; + if (log.isDebugEnabled()) { + log.debug("get all reviews of the application release" + uuid); + } + try { + ConnectionManagerUtil.openDBConnection(); + reviews = this.reviewDAO.getAllComments(uuid, Util.validateCommentListPageSize(request)); + numOfComments = reviews.size(); + paginationResult.setData(reviews); + if (numOfComments > 0) { + paginationResult.setRecordsFiltered(numOfComments); + paginationResult.setRecordsTotal(numOfComments); + } else { + paginationResult.setRecordsFiltered(0); + paginationResult.setRecordsTotal(0); + } + return paginationResult; + } catch (DBConnectionException e) { + throw new CommentManagementException( + "DB Connection error occurs , while getting reviews of application release UUID: " + uuid, e); + } catch (SQLException e) { + throw new CommentManagementException( + "Error occured in the data layer, while getting reviews of application release UUID: " + uuid, e); + } finally { + ConnectionManagerUtil.closeDBConnection(); + } + } + + @Override + public Review getReview(int commentId) throws CommentManagementException { + PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); + Review review; + if (log.isDebugEnabled()) { + log.debug("Review retrieval request is received for the review id " + commentId); + } + try { + ConnectionManagerUtil.openDBConnection(); + review = this.reviewDAO.getComment(commentId); + } catch (DBConnectionException e) { + throw new CommentManagementException( + "DB Connection error occurs ,Review with review id " + commentId + "cannot get.", e); + } catch (SQLException e) { + throw new CommentManagementException( + "SQL Exception occurs,Review with review id " + commentId + "cannot get.", e); + } finally { + ConnectionManagerUtil.closeDBConnection(); + } + return review; + } + + @Override + public void deleteReview(String loggedInUser, int commentId) throws CommentManagementException { + Review existingReview; + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); + String username = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); + try { + if (!loggedInUser.equals(username) && !isAdminUser(username, tenantId, CarbonConstants.UI_ADMIN_PERMISSION_COLLECTION)){ + throw new CommentManagementException( + "You don't have permission to delete the review. Please contact the administrator. Review Id: " + + commentId); + } + existingReview = getReview(commentId); + if (existingReview == null) { + throw new CommentManagementException( + "Cannot delete a non-existing review for the application with review id" + commentId); + } + Runnable task = () -> { + try { + if (calculateRating(0, existingReview.getRating()) <= 0.0) { + log.error("Application release review updating task is failed"); + } + } catch (CommentManagementException e) { + // todo + log.error("error occured"); + } + }; + new Thread(task).start(); + ConnectionManagerUtil.beginDBTransaction(); + this.reviewDAO.deleteComment(commentId); + ConnectionManagerUtil.commitDBTransaction(); + } catch (DBConnectionException e) { + throw new CommentManagementException( + "DB Connection error occurs deleting review with review id " + commentId + ".", e); + } catch (SQLException e) { + throw new CommentManagementException("SQL error occurs deleting review with review id " + commentId + ".", + e); + } catch (TransactionManagementException e) { + throw new CommentManagementException( + "Transaction Management Exception occurs deleting review with review id " + commentId + ".", e); + } catch (UserStoreException e) { + throw new CommentManagementException( + "User-store exception while checking whether the user " + username + " of tenant " + tenantId + + " has the publisher permission"); + } finally { + ConnectionManagerUtil.closeDBConnection(); + } + } + + @Override + public Rating getRating(String appReleaseUuuid) throws CommentManagementException{ + //todo + int appReleaseId = 0; + try { + ConnectionManagerUtil.openDBConnection(); + Rating rating = this.applicationReleaseDAO.getRating(appReleaseId); + if (rating == null) { + throw new CommentManagementException("Couldn't find rating for application release id: " + appReleaseId + + ". Please check the existence of the application relese"); + } + + List ratingValues = this.reviewDAO.getAllRatingValues(appReleaseUuuid); + TreeMap ratingVariety = rating.getRatingVariety(); + for (Integer ratingVal : ratingValues) { + if (ratingVariety.containsKey(ratingVal)) { + ratingVariety.replace(ratingVal, ratingVariety.get(ratingVal) + 1); + } else { + ratingVariety.put(ratingVal, 1); + } + } + rating.setRatingVariety(ratingVariety); + return rating; + } catch (ApplicationManagementDAOException e) { + ConnectionManagerUtil.rollbackDBTransaction(); + throw new CommentManagementException( + "Error occured while updated the rating value of the application release id: " + appReleaseId + + " can not get.", e); + } catch (DBConnectionException e) { + ConnectionManagerUtil.rollbackDBTransaction(); + throw new CommentManagementException( + "DB Connection error occured while updated the rating value of the application release id: " + appReleaseId + + " can not get.", e); + } catch (SQLException e) { + throw new CommentManagementException( + "DB Connection error occured while updated the rating value of the application release id: " + appReleaseId + + " can not get.", e); + } finally { + ConnectionManagerUtil.closeDBConnection(); + } + } + + private double calculateRating(int newRatingVal, int oldRatingVal) throws CommentManagementException { + // todo need to pass app release id + int appReleaseId = 0; + try { + ConnectionManagerUtil.beginDBTransaction(); + Rating rating = this.applicationReleaseDAO.getRating(appReleaseId); + if (rating == null) { + throw new CommentManagementException( + "Couldn't find rating for application release id: " + appReleaseId); + } + double updatedRating; + int numOfUsers = rating.getNoOfUsers(); + double currentRating = rating.getRatingValue() * numOfUsers; + if (oldRatingVal == -12345) { + updatedRating = (currentRating + newRatingVal) / (numOfUsers + 1); + this.applicationReleaseDAO.updateRatingValue(appReleaseId, updatedRating, numOfUsers + 1); + } else if ( newRatingVal == 0){ + updatedRating = (currentRating - newRatingVal) / (numOfUsers - 1); + this.applicationReleaseDAO.updateRatingValue(appReleaseId, updatedRating, numOfUsers - 1); + } else{ + double tmpVal; + tmpVal = currentRating - oldRatingVal; + updatedRating = (tmpVal + newRatingVal) / numOfUsers; + this.applicationReleaseDAO.updateRatingValue(appReleaseId, updatedRating, numOfUsers ); + } + ConnectionManagerUtil.commitDBTransaction(); + return updatedRating; + } catch (ApplicationManagementDAOException e) { + ConnectionManagerUtil.rollbackDBTransaction(); + throw new CommentManagementException( + "Error occured while updated the rating value of the application release id: " + appReleaseId + + " can not get.", e); + } catch (TransactionManagementException e) { + ConnectionManagerUtil.rollbackDBTransaction(); + throw new CommentManagementException( + "Transaction Management Exception occured while updated the rating value of the application release id: " + appReleaseId + + " can not get.", e); + } catch (DBConnectionException e) { + ConnectionManagerUtil.rollbackDBTransaction(); + throw new CommentManagementException( + "DB Connection error occured while updated the rating value of the application release id: " + appReleaseId + + " can not get.", e); + } finally { + ConnectionManagerUtil.closeDBConnection(); + } + } + + /** + * To check whether current user has the permission to do some secured operation. + * + * @param username Name of the User. + * @param tenantId ID of the tenant. + * @param permission Permission that need to be checked. + * @return true if the current user has the permission, otherwise false. + * @throws UserStoreException UserStoreException + */ + private boolean isAdminUser(String username, int tenantId, String permission) throws UserStoreException { + UserRealm userRealm = DataHolder.getInstance().getRealmService().getTenantUserRealm(tenantId); + return userRealm != null && userRealm.getAuthorizationManager() != null && userRealm.getAuthorizationManager() + .isUserAuthorized(MultitenantUtils.getTenantAwareUsername(username), permission, + CarbonConstants.UI_PERMISSION_ACTION); + } +} \ No newline at end of file diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/internal/DataHolder.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/internal/DataHolder.java index 65392952c04..4f74582336b 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/internal/DataHolder.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/internal/DataHolder.java @@ -20,7 +20,7 @@ package org.wso2.carbon.device.application.mgt.core.internal; import org.wso2.carbon.device.application.mgt.common.services.ApplicationManager; import org.wso2.carbon.device.application.mgt.common.services.ApplicationStorageManager; -import org.wso2.carbon.device.application.mgt.common.services.CommentsManager; +import org.wso2.carbon.device.application.mgt.common.services.ReviewManager; import org.wso2.carbon.device.application.mgt.common.services.SubscriptionManager; import org.wso2.carbon.device.application.mgt.common.services.UnrestrictedRoleManager; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; @@ -37,7 +37,7 @@ public class DataHolder { private ApplicationManager applicationManager; - private CommentsManager commentsManager; + private ReviewManager reviewManager; private SubscriptionManager subscriptionManager; @@ -71,12 +71,12 @@ public class DataHolder { this.applicationManager = applicationManager; } - public CommentsManager getCommentsManager() { - return commentsManager; + public ReviewManager getReviewManager() { + return reviewManager; } - public void setCommentsManager(CommentsManager commentsManager) { - this.commentsManager = commentsManager; + public void setReviewManager(ReviewManager reviewManager) { + this.reviewManager = reviewManager; } public SubscriptionManager getSubscriptionManager() { diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/internal/ServiceComponent.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/internal/ServiceComponent.java index 9e5eeee881d..7a2fd489ed8 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/internal/ServiceComponent.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/internal/ServiceComponent.java @@ -25,7 +25,7 @@ import org.osgi.service.component.ComponentContext; import org.wso2.carbon.device.application.mgt.common.exception.InvalidConfigurationException; import org.wso2.carbon.device.application.mgt.common.services.ApplicationManager; import org.wso2.carbon.device.application.mgt.common.services.ApplicationStorageManager; -import org.wso2.carbon.device.application.mgt.common.services.CommentsManager; +import org.wso2.carbon.device.application.mgt.common.services.ReviewManager; import org.wso2.carbon.device.application.mgt.common.services.SubscriptionManager; import org.wso2.carbon.device.application.mgt.common.services.UnrestrictedRoleManager; import org.wso2.carbon.device.application.mgt.core.config.ConfigurationManager; @@ -74,9 +74,9 @@ public class ServiceComponent { DataHolder.getInstance().setApplicationManager(applicationManager); bundleContext.registerService(ApplicationManager.class.getName(), applicationManager, null); - CommentsManager commentsManager = ApplicationManagementUtil.getCommentsManagerInstance(); - DataHolder.getInstance().setCommentsManager(commentsManager); - bundleContext.registerService(CommentsManager.class.getName(), commentsManager, null); + ReviewManager reviewManager = ApplicationManagementUtil.getCommentsManagerInstance(); + DataHolder.getInstance().setReviewManager(reviewManager); + bundleContext.registerService(ReviewManager.class.getName(), reviewManager, null); SubscriptionManager subscriptionManager = ApplicationManagementUtil.getSubscriptionManagerInstance(); DataHolder.getInstance().setSubscriptionManager(subscriptionManager); diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/util/ApplicationManagementUtil.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/util/ApplicationManagementUtil.java index a4b905749fa..4127696e30c 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/util/ApplicationManagementUtil.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/util/ApplicationManagementUtil.java @@ -23,7 +23,7 @@ import org.apache.commons.logging.LogFactory; import org.wso2.carbon.device.application.mgt.common.exception.InvalidConfigurationException; import org.wso2.carbon.device.application.mgt.common.services.ApplicationManager; import org.wso2.carbon.device.application.mgt.common.services.ApplicationStorageManager; -import org.wso2.carbon.device.application.mgt.common.services.CommentsManager; +import org.wso2.carbon.device.application.mgt.common.services.ReviewManager; import org.wso2.carbon.device.application.mgt.common.services.SubscriptionManager; import org.wso2.carbon.device.application.mgt.common.services.UnrestrictedRoleManager; import org.wso2.carbon.device.application.mgt.core.config.ConfigurationManager; @@ -45,10 +45,10 @@ public class ApplicationManagementUtil { return getInstance(extension, ApplicationManager.class); } - public static CommentsManager getCommentsManagerInstance() throws InvalidConfigurationException { + public static ReviewManager getCommentsManagerInstance() throws InvalidConfigurationException { ConfigurationManager configurationManager = ConfigurationManager.getInstance(); Extension extension = configurationManager.getExtension(Extension.Name.CommentsManager); - return getInstance(extension, CommentsManager.class); + return getInstance(extension, ReviewManager.class); } public static UnrestrictedRoleManager getVisibilityManagerInstance() throws InvalidConfigurationException { diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/test/resources/application-mgt.xml b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/test/resources/application-mgt.xml index 974cd4df3a8..71ebffdd6bf 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/test/resources/application-mgt.xml +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/test/resources/application-mgt.xml @@ -39,7 +39,7 @@ org.wso2.carbon.device.application.mgt.core.impl.CategoryManagerImpl - org.wso2.carbon.device.application.mgt.core.impl.CommentsManagerImpl + org.wso2.carbon.device.application.mgt.core.impl.ReviewManagerImpl org.wso2.carbon.device.application.mgt.core.impl.LifecycleStateManagerImpl 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/APIUtil.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/main/java/org/wso2/carbon/device/application/mgt/store/api/APIUtil.java index ce939df4617..b82802b85d1 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/main/java/org/wso2/carbon/device/application/mgt/store/api/APIUtil.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/main/java/org/wso2/carbon/device/application/mgt/store/api/APIUtil.java @@ -21,7 +21,7 @@ package org.wso2.carbon.device.application.mgt.store.api; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.wso2.carbon.context.PrivilegedCarbonContext; -import org.wso2.carbon.device.application.mgt.common.services.CommentsManager; +import org.wso2.carbon.device.application.mgt.common.services.ReviewManager; import org.wso2.carbon.device.application.mgt.common.services.ApplicationManager; import org.wso2.carbon.device.application.mgt.common.services.ApplicationStorageManager; import org.wso2.carbon.device.application.mgt.common.services.SubscriptionManager; @@ -87,20 +87,20 @@ public class APIUtil { } /** - * To get the Comment Manager from the osgi context. + * To get the Review Manager from the osgi context. * - * @return CommentsManager instance in the current osgi context. + * @return ReviewManager instance in the current osgi context. */ - public static CommentsManager getCommentsManager() { + public static ReviewManager getCommentsManager() { PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext(); - CommentsManager commentsManager = (CommentsManager) ctx.getOSGiService(CommentsManager.class, null); - if (commentsManager == null) { + ReviewManager reviewManager = (ReviewManager) ctx.getOSGiService(ReviewManager.class, null); + if (reviewManager == null) { String msg = "Comments Manager service has not initialized."; log.error(msg); throw new IllegalStateException(msg); } - return commentsManager; + return reviewManager; } public static Response getResponse(Exception ex, Response.Status status) { 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/ReviewManagementAPI.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/ReviewManagementAPI.java index fcd5d6ea42e..a86d0d79011 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/ReviewManagementAPI.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/ReviewManagementAPI.java @@ -31,7 +31,7 @@ import io.swagger.annotations.ApiResponses; import org.wso2.carbon.apimgt.annotations.api.Scope; import org.wso2.carbon.apimgt.annotations.api.Scopes; import org.wso2.carbon.device.application.mgt.publisher.api.beans.ErrorResponse; -import org.wso2.carbon.device.application.mgt.common.Comment; +import org.wso2.carbon.device.application.mgt.common.Review; import javax.validation.Valid; import javax.ws.rs.Path; import javax.ws.rs.Consumes; @@ -62,7 +62,7 @@ import java.util.List; } ), tags = { - @Tag(name = "store_management", description = "Comment Management related " + @Tag(name = "store_management", description = "Review Management related " + "APIs") } ) @@ -75,20 +75,20 @@ import java.util.List; permissions = {"/device-mgt/comment/get"} ), @Scope( - name = "Add a Comment", + name = "Add a Review", description = "Add a comment", key = "perm:comment:add", permissions = {"/device-mgt/comment/add"} ), @Scope( - name = "Update a Comment", - description = "Update a Comment", + name = "Update a Review", + description = "Update a Review", key = "perm:comment:update", permissions = {"/device-mgt/comment/update"} ), @Scope( - name = "Delete a Comment", + name = "Delete a Review", description = "Delete a comment", key = "perm:comment:delete", permissions = {"/device-mgt/comment/delete"} @@ -96,7 +96,7 @@ import java.util.List; } ) -@Path("/reviews") +@Path("/review") @Api(value = "Comments Management", description = "This API carries all comments management related operations " + "such as get all the comments, add comment, etc.") @Produces(MediaType.APPLICATION_JSON) @@ -137,7 +137,7 @@ public interface ReviewManagementAPI { response = ErrorResponse.class) }) - Response getAllComments( + Response getAllReviews( @ApiParam( name="uuid", value="uuid of the released version of application.", @@ -165,8 +165,8 @@ public interface ReviewManagementAPI { consumes = MediaType.APPLICATION_JSON, produces = MediaType.APPLICATION_JSON, httpMethod = "POST", - value = "Add a comment", - notes = "This will add a new comment", + value = "Add a review", + notes = "This will add a new review", tags = "Store Management", extensions = { @Extension(properties = { @@ -179,24 +179,23 @@ public interface ReviewManagementAPI { value = { @ApiResponse( code = 201, - message = "OK. \n Successfully add a comment.", - response = Comment.class), + message = "OK. \n Successfully add a review.", + response = Review.class), @ApiResponse( code = 400, message = "Bad Request. \n"), @ApiResponse( code = 500, - message = "Internal Server Error. \n Error occurred adding a comment.", + message = "Internal Server Error. \n Error occurred adding a review.", response = ErrorResponse.class) }) - Response addComment( + Response addReview( @ApiParam( - name = "comment", - value = "Comment details", - required = true) - Comment comment, + name = "review", + value = "Review details", + required = true) Review review, @ApiParam( name="uuid", value="uuid of the release version of the application", @@ -212,8 +211,8 @@ public interface ReviewManagementAPI { consumes = MediaType.APPLICATION_JSON, produces = MediaType.APPLICATION_JSON, httpMethod = "PUT", - value = "Edit a comment", - notes = "This will edit the comment", + value = "Edit a review", + notes = "This will edit the review", tags = "Store Management", extensions = { @Extension(properties = { @@ -225,8 +224,8 @@ public interface ReviewManagementAPI { value = { @ApiResponse( code = 201, - message = "OK. \n Successfully updated comment.", - response = Comment.class), + message = "OK. \n Successfully updated review.", + response = Review.class), @ApiResponse( code = 400, message = "Bad Request. \n Invalid request or validation error."), @@ -236,24 +235,24 @@ public interface ReviewManagementAPI { response = ErrorResponse.class), @ApiResponse( code = 500, - message = "Internal Server Error. \n Error occurred while updating the new comment.", + message = "Internal Server Error. \n Error occurred while updating the new review.", response = ErrorResponse.class) }) Response updateComment( @ApiParam( - name = "comment", - value = "The comment that need to be updated.", + name = "review", + value = "The review that need to be updated.", required = true) - @Valid Comment comment, + @Valid Review review, @ApiParam( name="commentId", - value = "comment id of the updating comment.", + value = "review id of the updating review.", required = true) @QueryParam("commentId") int commentId); @DELETE - @Path("/{CommentId}") + @Path("/{commentId}") @Produces(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON) @ApiOperation( @@ -291,7 +290,12 @@ public interface ReviewManagementAPI { value="Id of the comment.", required = true) @PathParam("commentId") - int commentId); + int commentId, + @ApiParam( + name="username", + value="logged in username", + required = true) + @QueryParam("username") String username); @GET @Path("/{uuid}/{stars}") @@ -329,88 +333,4 @@ public interface ReviewManagementAPI { required = true) @PathParam("uuid") String uuid); - - @GET - @Path("/{uuid}/{stars}") - @Produces(MediaType.APPLICATION_JSON) - @ApiOperation( - produces = MediaType.APPLICATION_JSON, - httpMethod = "GET", - value = "get rated users", - notes = "Get all users", - tags = "Store Management", - extensions = { - @Extension(properties = { - @ExtensionProperty(name = SCOPE, value = "perm:user:get") - }) - } - ) - - @ApiResponses( - value = { - @ApiResponse( - code = 200, - message = "OK. \n Successfully retrieved user.", - response = List.class, - responseContainer = "List"), - @ApiResponse( - code = 500, - message = "Internal Server Error. \n Error occurred while getting the comment list.", - response = ErrorResponse.class) - }) - - Response getNumOfRatedUsers( - @ApiParam( - name = "uuid", - value = "uuid of the application release", - required = true) - @PathParam("uuid") - String uuid); - - @POST - @Path("/uuid/{uuid}") - @Produces(MediaType.APPLICATION_JSON) - @Consumes(MediaType.APPLICATION_JSON) - @ApiOperation( - consumes = MediaType.APPLICATION_JSON, - produces = MediaType.APPLICATION_JSON, - httpMethod = "POST", - value = "Add a star value", - notes = "This will add star value", - tags = "Store Management", - extensions = { - @Extension(properties = { - @ExtensionProperty(name = SCOPE, value = "perm:stars:add") - }) - } - ) - - @ApiResponses( - value = { - @ApiResponse( - code = 201, - message = "OK. \n Successfully rated to the application.", - response = Comment.class), - @ApiResponse( - code = 304, - message = "Not Modified. \n " + - "Empty body because the client already has the latest rating of the requested resource."), - @ApiResponse( - code = 500, - message = "Internal Server Error. \n Error occurred rating for the application.", - response = ErrorResponse.class) - }) - - Response updateRatings( - @ApiParam( - name = "stars", - value = "ratings for the application", - required = true) - int stars, - @ApiParam( - name="uuid", - value="uuid of the release version of the application", - required=true) - @PathParam("uuid") - String uuid); } 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 db2f0b803a3..6ee1fc018d8 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 @@ -21,14 +21,18 @@ package org.wso2.carbon.device.application.mgt.store.api.services.impl; import io.swagger.annotations.ApiParam; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.device.application.mgt.common.Application; +import org.wso2.carbon.device.application.mgt.common.ApplicationRelease; import org.wso2.carbon.device.application.mgt.common.PaginationResult; +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.services.ApplicationManager; +import org.wso2.carbon.device.application.mgt.common.services.ReviewManager; import org.wso2.carbon.device.application.mgt.store.api.APIUtil; import org.wso2.carbon.device.application.mgt.store.api.services.ReviewManagementAPI; -import org.wso2.carbon.device.application.mgt.common.Comment; import org.wso2.carbon.device.application.mgt.common.PaginationRequest; import org.wso2.carbon.device.application.mgt.common.exception.ApplicationManagementException; import org.wso2.carbon.device.application.mgt.common.exception.CommentManagementException; -import org.wso2.carbon.device.application.mgt.common.services.CommentsManager; import javax.ws.rs.Path; import javax.ws.rs.Consumes; @@ -39,11 +43,9 @@ import javax.ws.rs.GET; import javax.ws.rs.POST; import javax.ws.rs.DELETE; import javax.ws.rs.core.Response; -import java.util.ArrayList; -import java.util.List; /** - * Comment Management related jax-rs APIs. + * Review Management related jax-rs APIs. */ @Path("/review") public class ReviewManagementAPIImpl implements ReviewManagementAPI { @@ -52,16 +54,16 @@ public class ReviewManagementAPIImpl implements ReviewManagementAPI { @Override @GET - @Path("/application/{uuid}/comments") - public Response getAllComments( + @Path("/{uuid}") + public Response getAllReviews( @PathParam("uuid") String uuid, @QueryParam("offset") int offSet, @QueryParam("limit") int limit) { - CommentsManager commentsManager = APIUtil.getCommentsManager(); + ReviewManager reviewManager = APIUtil.getCommentsManager(); PaginationRequest request = new PaginationRequest(offSet, limit); try { - PaginationResult paginationResult = commentsManager.getAllComments(request, uuid); + PaginationResult paginationResult = reviewManager.getAllReviews(request, uuid); return Response.status(Response.Status.OK).entity(paginationResult).build(); } catch (CommentManagementException e) { String msg = "Error occurred while retrieving comments."; @@ -74,48 +76,60 @@ public class ReviewManagementAPIImpl implements ReviewManagementAPI { @Override @POST @Consumes("application/json") - @Path("/application/{uuid}/comment") - public Response addComment( - @ApiParam Comment comment, + @Path("/{uuid}") + public Response addReview( + @ApiParam Review review, @PathParam("uuid") String uuid) { - CommentsManager commentsManager = APIUtil.getCommentsManager(); + ReviewManager reviewManager = APIUtil.getCommentsManager(); + ApplicationManager applicationManager = APIUtil.getApplicationManager(); + Application application; + ApplicationRelease applicationRelease; try { - if (commentsManager.addComment(comment, uuid) != null) { - return Response.status(Response.Status.CREATED).entity(comment).build(); + application = applicationManager.getApplicationByRelease(uuid); + applicationRelease = applicationManager.getAppReleaseIfExists(application.getId(), uuid); + boolean abc = reviewManager.addReview(review, application.getId(), applicationRelease.getId()); + if (abc) { + return Response.status(Response.Status.CREATED).entity(review).build(); } else { - String msg = "Given comment is not valid "; + String msg = "Given review is not valid "; log.error(msg); return Response.status(Response.Status.BAD_REQUEST).build(); } } catch (CommentManagementException e) { - String msg = "Error occurred while creating the comment"; + String msg = "Error occurred while creating the review"; log.error(msg, e); return Response.status(Response.Status.INTERNAL_SERVER_ERROR) .entity(msg).build(); + } catch (ApplicationManagementException e) { +// todo + log.error(""); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR) + .entity("").build(); } } @Override @PUT @Consumes("application/json") - @Path("/comment/{commentId}") + @Path("/review/{commentId}") public Response updateComment( - @ApiParam Comment comment, + @ApiParam Review review, @PathParam("commentId") int commentId) { - CommentsManager commentsManager = APIUtil.getCommentsManager(); + ReviewManager reviewManager = APIUtil.getCommentsManager(); try { if (commentId == 0) { return Response.status(Response.Status.NOT_FOUND) - .entity("Comment not found").build(); - } else if (comment == null) { - String msg = "Given comment is not valid "; + .entity("Review not found").build(); + } else if (review == null) { + String msg = "Given review is not valid "; log.error(msg); return Response.status(Response.Status.BAD_REQUEST).build(); + } else if (reviewManager.updateReview(review, commentId, true)) { + return Response.status(Response.Status.OK).entity(review).build(); } else { - comment = commentsManager.updateComment(comment, commentId); - return Response.status(Response.Status.OK).entity(comment).build(); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity("").build(); } } catch (CommentManagementException e) { String msg = "Error occurred while retrieving comments."; @@ -129,14 +143,15 @@ public class ReviewManagementAPIImpl implements ReviewManagementAPI { @DELETE @Path("/comment/{commentId}") public Response deleteComment( - @PathParam("commentId") int commentId) { + @PathParam("commentId") int commentId, + @QueryParam("username") String username) { - CommentsManager commentsManager = APIUtil.getCommentsManager(); + ReviewManager reviewManager = APIUtil.getCommentsManager(); try { if (commentId == 0) { - return Response.status(Response.Status.NOT_FOUND).entity("Comment not found").build(); + return Response.status(Response.Status.NOT_FOUND).entity("Review not found").build(); } else { - commentsManager.deleteComment(commentId); + reviewManager.deleteReview(username, commentId); } } catch (CommentManagementException e) { String msg = "Error occurred while deleting the comment."; @@ -144,79 +159,24 @@ public class ReviewManagementAPIImpl implements ReviewManagementAPI { return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg) .build(); } - return Response.status(Response.Status.OK).entity("Comment is deleted successfully.").build(); + return Response.status(Response.Status.OK).entity("Review is deleted successfully.").build(); } @Override @GET - @Path("/application/{uuid}/rating") + @Path("/{uuid}/rating") public Response getRating( @PathParam("uuid") String uuid) { - CommentsManager commentsManager = APIUtil.getCommentsManager(); - int stars; + ReviewManager reviewManager = APIUtil.getCommentsManager(); + Rating rating; try { - stars = commentsManager.getStars(uuid); + rating = reviewManager.getRating(uuid); } catch (CommentManagementException e) { - log.error("Comment Management Exception occurs", e); + log.error("Review Management Exception occurs", e); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build(); - } catch (ApplicationManagementException e) { - String msg="Application Management Exception occurs"; - log.error(msg, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR) - .entity(msg).build(); } - return Response.status(Response.Status.OK).entity(stars).build(); + return Response.status(Response.Status.OK).entity(rating).build(); } - @Override - @GET - @Path("/application/{uuid}/total-rated-users") - public Response getNumOfRatedUsers( - @PathParam("uuid") String uuid) { - - CommentsManager commentsManager = APIUtil.getCommentsManager(); - int ratedUsers; - try { - ratedUsers = commentsManager.getRatedUser(uuid); - } catch (CommentManagementException e) { - String msg="Comment Management Exception occurs"; - log.error(msg, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR) - .entity(msg).build(); - } catch (ApplicationManagementException e) { - String msg="Application Management Exception occurs"; - log.error(msg, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR) - .entity("Application with UUID" + uuid + " Internal server error occurs").build(); - } - return Response.status(Response.Status.OK).entity(ratedUsers).build(); - } - - @Override - @PUT - @Consumes("application/json") - @Path("/application/{uuid}/rating") - public Response updateRatings( - @ApiParam int stars, - @PathParam("uuid") String uuid) { - - CommentsManager commentsManager = APIUtil.getCommentsManager(); - int newStars; - try { - newStars = commentsManager.updateStars(stars, uuid); - if (stars != 0) { - return Response.status(Response.Status.CREATED).entity(newStars).build(); - } else { - String msg = "Given star value is not valid "; - log.error(msg); - return Response.status(Response.Status.BAD_REQUEST).build(); - } - } catch (ApplicationManagementException e) { - String msg="Application Management Exception occurs"; - log.error(msg, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR) - .entity(msg).build(); - } - } } \ No newline at end of file diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/test/java/org/wso2/carbon/device/application/mgt/store/api/services/CommentManagementAPITest.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/test/java/org/wso2/carbon/device/application/mgt/store/api/services/ReviewManagementAPITest.java similarity index 59% rename from components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/test/java/org/wso2/carbon/device/application/mgt/store/api/services/CommentManagementAPITest.java rename to components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/test/java/org/wso2/carbon/device/application/mgt/store/api/services/ReviewManagementAPITest.java index ec802303259..c59db2a67db 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/test/java/org/wso2/carbon/device/application/mgt/store/api/services/CommentManagementAPITest.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/test/java/org/wso2/carbon/device/application/mgt/store/api/services/ReviewManagementAPITest.java @@ -32,10 +32,10 @@ import org.testng.IObjectFactory; import org.testng.annotations.BeforeClass; import org.testng.annotations.ObjectFactory; import org.testng.annotations.Test; -import org.wso2.carbon.device.application.mgt.common.Comment; +import org.wso2.carbon.device.application.mgt.common.Review; import org.wso2.carbon.device.application.mgt.common.exception.ApplicationManagementException; import org.wso2.carbon.device.application.mgt.common.exception.CommentManagementException; -import org.wso2.carbon.device.application.mgt.common.services.CommentsManager; +import org.wso2.carbon.device.application.mgt.common.services.ReviewManager; import org.wso2.carbon.device.application.mgt.store.api.APIUtil; import org.wso2.carbon.device.application.mgt.store.api.services.impl.ReviewManagementAPIImpl; import org.wso2.carbon.device.application.mgt.store.api.services.util.CommentMgtTestHelper; @@ -47,14 +47,15 @@ import static org.mockito.MockitoAnnotations.initMocks; @PowerMockIgnore("javax.ws.rs.*") @SuppressStaticInitializationFor({ "org.wso2.carbon.device.application.mgt.api.APIUtil" }) -@PrepareForTest({ APIUtil.class, CommentsManager.class, - CommentManagementAPITest.class}) -@Ignore("Since comment manager logic is invalid temporarily added Ignore annotation to skip running comment management test cases") public class CommentManagementAPITest extends +@PrepareForTest({ APIUtil.class, ReviewManager.class, + ReviewManagementAPITest.class}) +@Ignore("Since comment manager logic is invalid temporarily added Ignore annotation to skip running comment management test cases") public class ReviewManagementAPITest + extends TestCase { private static final Log log = LogFactory.getLog(ReviewManagementAPI.class); private ReviewManagementAPI commentManagementAPI; - private CommentsManager commentsManager; + private ReviewManager reviewManager; @ObjectFactory public IObjectFactory getObjectFactory() { @@ -66,81 +67,81 @@ import static org.mockito.MockitoAnnotations.initMocks; log.info("Initializing ReviewManagementAPI tests"); initMocks(this); - this.commentsManager = Mockito.mock(CommentsManager.class, Mockito.RETURNS_DEFAULTS); + this.reviewManager = Mockito.mock(ReviewManager.class, Mockito.RETURNS_DEFAULTS); this.commentManagementAPI = new ReviewManagementAPIImpl(); } @Test public void testGetAllCommentsWithValidDetails() throws Exception { - PowerMockito.stub(PowerMockito.method(APIUtil.class, "getCommentsManager")).toReturn(this.commentsManager); - Response response = this.commentManagementAPI.getAllComments("a", 1, 2); + PowerMockito.stub(PowerMockito.method(APIUtil.class, "getReviewManager")).toReturn(this.reviewManager); + Response response = this.commentManagementAPI.getAllReviews("a", 1, 2); Assert.assertNotNull(response, "The response object is null."); Assert.assertEquals(response.getStatus(), Response.Status.OK.getStatusCode(), "The response status should be 200."); - Mockito.reset(commentsManager); + Mockito.reset(reviewManager); } @Test public void testGetAllCommentsInternalError() throws Exception { - PowerMockito.stub(PowerMockito.method(APIUtil.class, "getCommentsManager")).toReturn(this.commentsManager); - Mockito.doThrow(new CommentManagementException()).when(this.commentsManager) - .getAllComments(Mockito.any(), Mockito.anyString()); - Response response = this.commentManagementAPI.getAllComments("a", 1, 4); + PowerMockito.stub(PowerMockito.method(APIUtil.class, "getReviewManager")).toReturn(this.reviewManager); + Mockito.doThrow(new CommentManagementException()).when(this.reviewManager) + .getAllReviews(Mockito.any(), Mockito.anyString()); + Response response = this.commentManagementAPI.getAllReviews("a", 1, 4); Assert.assertNotNull(response, "The response object is null."); Assert.assertEquals(response.getStatus(), Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), "The response status should be 500."); - Mockito.reset(commentsManager); + Mockito.reset(reviewManager); } @Test public void testGetAllCommentsNotFoundError() throws Exception { - PowerMockito.stub(PowerMockito.method(APIUtil.class, "getCommentsManager")).toReturn(this.commentsManager); - Response response = this.commentManagementAPI.getAllComments(null, 1, 3); + PowerMockito.stub(PowerMockito.method(APIUtil.class, "getReviewManager")).toReturn(this.reviewManager); + Response response = this.commentManagementAPI.getAllReviews(null, 1, 3); Assert.assertNotNull(response, "The response object is null."); Assert.assertEquals(response.getStatus(), Response.Status.NOT_FOUND.getStatusCode(), "The response status should be 404."); - Mockito.reset(commentsManager); + Mockito.reset(reviewManager); } @Test public void testAddComments() throws Exception { - Comment comment = CommentMgtTestHelper.getDummyComment("a", "a"); - PowerMockito.stub(PowerMockito.method(APIUtil.class, "getCommentsManager")).toReturn(this.commentsManager); - Response response = this.commentManagementAPI.addComment(comment, "a"); + Review review = CommentMgtTestHelper.getDummyComment("a", "a"); + PowerMockito.stub(PowerMockito.method(APIUtil.class, "getReviewManager")).toReturn(this.reviewManager); + Response response = this.commentManagementAPI.addReview(review, "a"); Assert.assertNotNull(response, "The response object is null."); Assert.assertEquals(response.getStatus(), Response.Status.CREATED.getStatusCode(), "The response status should be 201."); - Mockito.reset(commentsManager); + Mockito.reset(reviewManager); } @Test public void testAddNullComment() throws Exception { - PowerMockito.stub(PowerMockito.method(APIUtil.class, "getCommentsManager")).toReturn(this.commentsManager); - Response response = this.commentManagementAPI.addComment(null, "a"); + PowerMockito.stub(PowerMockito.method(APIUtil.class, "getReviewManager")).toReturn(this.reviewManager); + Response response = this.commentManagementAPI.addReview(null, "a"); Assert.assertNotNull(response, "The response object is null."); Assert.assertEquals(response.getStatus(), Response.Status.BAD_REQUEST.getStatusCode(), "The response status should be 400."); - Mockito.reset(commentsManager); + Mockito.reset(reviewManager); } @Test public void testAddCommentsInternalError() throws Exception { - Comment comment = CommentMgtTestHelper.getDummyComment("a", "a"); - PowerMockito.stub(PowerMockito.method(APIUtil.class, "getCommentsManager")).toReturn(this.commentsManager); - Mockito.when(this.commentManagementAPI.addComment(Mockito.any(), Mockito.anyString())) + Review review = CommentMgtTestHelper.getDummyComment("a", "a"); + PowerMockito.stub(PowerMockito.method(APIUtil.class, "getReviewManager")).toReturn(this.reviewManager); + Mockito.when(this.commentManagementAPI.addReview(Mockito.any(), Mockito.anyString())) .thenThrow(new CommentManagementException()); - Response response = this.commentManagementAPI.addComment(comment, null); + Response response = this.commentManagementAPI.addReview(review, null); Assert.assertNotNull(response, "The response object is null."); Assert.assertEquals(response.getStatus(), Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), "The response status should be 500."); - Mockito.reset(commentsManager); + Mockito.reset(reviewManager); } @Test public void testUpdateComment() throws Exception { - Comment comment = CommentMgtTestHelper.getDummyComment("a", "a"); - PowerMockito.stub(PowerMockito.method(APIUtil.class, "getCommentsManager")).toReturn(this.commentsManager); - Response response = this.commentManagementAPI.updateComment(comment, 1); + Review review = CommentMgtTestHelper.getDummyComment("a", "a"); + PowerMockito.stub(PowerMockito.method(APIUtil.class, "getReviewManager")).toReturn(this.reviewManager); + Response response = this.commentManagementAPI.updateComment(review, 1); Assert.assertNotNull(response, "The response object is null."); Assert.assertEquals(response.getStatus(), Response.Status.OK.getStatusCode(), "The response status should be 200."); @@ -148,7 +149,7 @@ import static org.mockito.MockitoAnnotations.initMocks; @Test public void testUpdateNullComment() throws Exception { - PowerMockito.stub(PowerMockito.method(APIUtil.class, "getCommentsManager")).toReturn(this.commentsManager); + PowerMockito.stub(PowerMockito.method(APIUtil.class, "getReviewManager")).toReturn(this.reviewManager); Response response = this.commentManagementAPI.updateComment(null, 1); Assert.assertNotNull(response, "The response object is null."); Assert.assertEquals(response.getStatus(), Response.Status.BAD_REQUEST.getStatusCode(), @@ -157,9 +158,9 @@ import static org.mockito.MockitoAnnotations.initMocks; @Test public void testUpdateCommentWhenNullCommentId() throws Exception { - Comment comment = CommentMgtTestHelper.getDummyComment("a", "a"); - PowerMockito.stub(PowerMockito.method(APIUtil.class, "getCommentsManager")).toReturn(this.commentsManager); - Response response = this.commentManagementAPI.updateComment(comment, 0); + Review review = CommentMgtTestHelper.getDummyComment("a", "a"); + PowerMockito.stub(PowerMockito.method(APIUtil.class, "getReviewManager")).toReturn(this.reviewManager); + Response response = this.commentManagementAPI.updateComment(review, 0); Assert.assertNotNull(response, "The response object is null."); Assert.assertEquals(response.getStatus(), Response.Status.NOT_FOUND.getStatusCode(), "The response status should be 404."); @@ -167,10 +168,10 @@ import static org.mockito.MockitoAnnotations.initMocks; @Test public void testUpdateCommentInternalServerError() throws Exception { - Comment comment = CommentMgtTestHelper.getDummyComment("a", "a"); - PowerMockito.stub(PowerMockito.method(APIUtil.class, "getCommentsManager")).toReturn(this.commentsManager); - Mockito.doThrow(new CommentManagementException()).when(this.commentsManager).updateComment(comment, 9); - Response response = this.commentManagementAPI.updateComment(comment, 9); + Review review = CommentMgtTestHelper.getDummyComment("a", "a"); + PowerMockito.stub(PowerMockito.method(APIUtil.class, "getReviewManager")).toReturn(this.reviewManager); + Mockito.doThrow(new CommentManagementException()).when(this.reviewManager).updateReview(review, 9, true); + Response response = this.commentManagementAPI.updateComment(review, 9); Assert.assertNotNull(response, "The response object is null."); Assert.assertEquals(response.getStatus(), Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), "The response status should be 500."); @@ -178,8 +179,8 @@ import static org.mockito.MockitoAnnotations.initMocks; @Test public void testDeleteComment() throws Exception { - PowerMockito.stub(PowerMockito.method(APIUtil.class, "getCommentsManager")).toReturn(this.commentsManager); - Response response = this.commentManagementAPI.deleteComment(1); + PowerMockito.stub(PowerMockito.method(APIUtil.class, "getReviewManager")).toReturn(this.reviewManager); + Response response = this.commentManagementAPI.deleteComment(1,""); Assert.assertNotNull(response, "The response object is null."); Assert.assertEquals(response.getStatus(), Response.Status.OK.getStatusCode(), "The response status should be 200."); @@ -187,9 +188,9 @@ import static org.mockito.MockitoAnnotations.initMocks; @Test public void testDeleteCommentInternalError() throws Exception { - PowerMockito.stub(PowerMockito.method(APIUtil.class, "getCommentsManager")).toReturn(this.commentsManager); - Mockito.when(this.commentManagementAPI.deleteComment(1)).thenThrow(new CommentManagementException()); - Response response = this.commentManagementAPI.deleteComment(1); + PowerMockito.stub(PowerMockito.method(APIUtil.class, "getReviewManager")).toReturn(this.reviewManager); + Mockito.when(this.commentManagementAPI.deleteComment(1,"")).thenThrow(new CommentManagementException()); + Response response = this.commentManagementAPI.deleteComment(1,""); Assert.assertNotNull(response, "The response object is null."); Assert.assertEquals(response.getStatus(), Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), "The response status should be 500."); @@ -197,8 +198,8 @@ import static org.mockito.MockitoAnnotations.initMocks; @Test public void testDeleteCommentNotFoundError() throws Exception { - PowerMockito.stub(PowerMockito.method(APIUtil.class, "getCommentsManager")).toReturn(this.commentsManager); - Response response = this.commentManagementAPI.deleteComment(0); + PowerMockito.stub(PowerMockito.method(APIUtil.class, "getReviewManager")).toReturn(this.reviewManager); + Response response = this.commentManagementAPI.deleteComment(0,""); Assert.assertNotNull(response, "The response object is null."); Assert.assertEquals(response.getStatus(), Response.Status.NOT_FOUND.getStatusCode(), "The response status should be 404."); @@ -206,101 +207,35 @@ import static org.mockito.MockitoAnnotations.initMocks; @Test public void testGetStars() throws Exception { - PowerMockito.stub(PowerMockito.method(APIUtil.class, "getCommentsManager")).toReturn(this.commentsManager); + PowerMockito.stub(PowerMockito.method(APIUtil.class, "getReviewManager")).toReturn(this.reviewManager); Response response = this.commentManagementAPI.getRating("a"); Assert.assertNotNull(response, "The response object is null."); Assert.assertEquals(response.getStatus(), Response.Status.OK.getStatusCode(), "The response status should be 200."); - Mockito.reset(commentsManager); + Mockito.reset(reviewManager); } @Test public void testGetStarsCommentError() throws Exception { - PowerMockito.stub(PowerMockito.method(APIUtil.class, "getCommentsManager")).toReturn(this.commentsManager); + PowerMockito.stub(PowerMockito.method(APIUtil.class, "getReviewManager")).toReturn(this.reviewManager); Mockito.when(this.commentManagementAPI.getRating(Mockito.anyString())) .thenThrow(new CommentManagementException()); Response response = this.commentManagementAPI.getRating("a"); Assert.assertNotNull(response, "The response object is null."); Assert.assertEquals(response.getStatus(), Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), "The response status should be 500."); - Mockito.reset(commentsManager); + Mockito.reset(reviewManager); } @Test public void testGetStarsApplicationError() throws Exception { - PowerMockito.stub(PowerMockito.method(APIUtil.class, "getCommentsManager")).toReturn(this.commentsManager); + PowerMockito.stub(PowerMockito.method(APIUtil.class, "getReviewManager")).toReturn(this.reviewManager); Mockito.when(this.commentManagementAPI.getRating(Mockito.anyString())) .thenThrow(new ApplicationManagementException()); Response response = this.commentManagementAPI.getRating("a"); Assert.assertNotNull(response, "The response object is null."); Assert.assertEquals(response.getStatus(), Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), "The response status should be 500."); - Mockito.reset(commentsManager); - } - - @Test - public void testGetRatedUser() throws Exception { - PowerMockito.stub(PowerMockito.method(APIUtil.class, "getCommentsManager")).toReturn(this.commentsManager); - Response response = this.commentManagementAPI.getNumOfRatedUsers("a"); - Assert.assertNotNull(response, "The response object is null."); - Assert.assertEquals(response.getStatus(), Response.Status.OK.getStatusCode(), - "The response status should be 200."); - Mockito.reset(commentsManager); - } - - @Test - public void testGetRatedUserCommentError() throws Exception { - PowerMockito.stub(PowerMockito.method(APIUtil.class, "getCommentsManager")).toReturn(this.commentsManager); - Mockito.when(this.commentManagementAPI.getNumOfRatedUsers(Mockito.anyString())) - .thenThrow(new CommentManagementException()); - Response response = this.commentManagementAPI.getNumOfRatedUsers("a"); - Assert.assertNotNull(response, "The response object is null."); - Assert.assertEquals(response.getStatus(), Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), - "The response status should be 500."); - Mockito.reset(commentsManager); - } - - @Test - public void testGetRatedUserApplicationError() throws Exception { - PowerMockito.stub(PowerMockito.method(APIUtil.class, "getCommentsManager")).toReturn(this.commentsManager); - Mockito.when(this.commentManagementAPI.getNumOfRatedUsers(Mockito.anyString())) - .thenThrow(new ApplicationManagementException()); - Response response = this.commentManagementAPI.getNumOfRatedUsers("a"); - Assert.assertNotNull(response, "The response object is null."); - Assert.assertEquals(response.getStatus(), Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), - "The response status should be 500."); - Mockito.reset(commentsManager); - } - - @Test - public void testUpdateStars() throws Exception { - PowerMockito.stub(PowerMockito.method(APIUtil.class, "getCommentsManager")).toReturn(this.commentsManager); - Response response = this.commentManagementAPI.updateRatings(3, "a"); - Assert.assertNotNull(response, "The response object is null."); - Assert.assertEquals(response.getStatus(), Response.Status.CREATED.getStatusCode(), - "The response status should be 201."); - Mockito.reset(commentsManager); - } - - @Test - public void testUpdateInvalidStars() throws Exception { - PowerMockito.stub(PowerMockito.method(APIUtil.class, "getCommentsManager")).toReturn(this.commentsManager); - Response response = this.commentManagementAPI.updateRatings(0, "a"); - Assert.assertNotNull(response, "The response object is null."); - Assert.assertEquals(response.getStatus(), Response.Status.BAD_REQUEST.getStatusCode(), - "The response status should be 400."); - Mockito.reset(commentsManager); - } - - @Test - public void testUpdateStarsApplicationError() throws Exception { - PowerMockito.stub(PowerMockito.method(APIUtil.class, "getCommentsManager")).toReturn(this.commentsManager); - Mockito.doThrow(new ApplicationManagementException()).when(this.commentsManager) - .updateStars(Mockito.anyInt(), Mockito.anyString()); - Response response = this.commentManagementAPI.updateRatings(3, "a"); - Assert.assertNotNull(response, "The response object is null."); - Assert.assertEquals(response.getStatus(), Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), - "The response status should be 500."); - Mockito.reset(commentsManager); + Mockito.reset(reviewManager); } } \ No newline at end of file diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/test/java/org/wso2/carbon/device/application/mgt/store/api/services/util/CommentMgtTestHelper.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/test/java/org/wso2/carbon/device/application/mgt/store/api/services/util/CommentMgtTestHelper.java index 636c22b40bf..5cd0418f1eb 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/test/java/org/wso2/carbon/device/application/mgt/store/api/services/util/CommentMgtTestHelper.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/test/java/org/wso2/carbon/device/application/mgt/store/api/services/util/CommentMgtTestHelper.java @@ -17,36 +17,34 @@ */ package org.wso2.carbon.device.application.mgt.store.api.services.util; -import org.wso2.carbon.device.application.mgt.common.Comment; +import org.wso2.carbon.device.application.mgt.common.Review; /** - * Helper class for Comment Management API test cases. + * Helper class for Review Management API test cases. */ public class CommentMgtTestHelper { - private static final String COMMENT_TEXT = "Dummy Comment"; + private static final String COMMENT_TEXT = "Dummy Review"; private static final String CREATED_BY = "TEST_CREATED_BY"; private static final String MODIFIED_BY = "TEST_MODIFIED_BY"; private static final int PARENT_ID = 123; private static final int COMMENT_ID = 1; /** - * Creates a Comment with given text and given uuid. - * If the text is null, the COMMENT_TEXT will be used as the Dummy Comment. + * Creates a Review with given text and given uuid. + * If the text is null, the COMMENT_TEXT will be used as the Dummy Review. * - * @param commentText : Text of the Comment - * @return Comment + * @param commentText : Text of the Review + * @return Review */ - public static Comment getDummyComment(String commentText, String uuid) { - Comment comment = new Comment(); - comment.setId(COMMENT_ID); - comment.setCreatedBy(CREATED_BY); - comment.setModifiedBy(MODIFIED_BY); - comment.setParent(PARENT_ID); - comment.setCommentText(commentText != null ? commentText : COMMENT_TEXT); - - return comment; + public static Review getDummyComment(String commentText, String uuid) { + Review review = new Review(); + review.setId(COMMENT_ID); + review.setUsername(CREATED_BY); + review.setComment(commentText != null ? commentText : COMMENT_TEXT); + + return review; } } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/test/resources/testng.xml b/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/test/resources/testng.xml index ab33c9c4105..947e329fbd4 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/test/resources/testng.xml +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/test/resources/testng.xml @@ -23,7 +23,7 @@ - +