parent
ff96884268
commit
6066d93edd
@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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<Integer, Integer> 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<Integer, Integer> getRatingVariety() {
|
||||
return ratingVariety;
|
||||
}
|
||||
|
||||
public void setRatingVariety(TreeMap<Integer, Integer> ratingVariety) {
|
||||
this.ratingVariety = ratingVariety;
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
@ -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;
|
||||
}
|
68
components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/CommentDAO.java → components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/ReviewDAO.java
68
components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/CommentDAO.java → components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/ReviewDAO.java
416
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 → 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
416
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 → 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
@ -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<Comment> 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();
|
||||
}
|
||||
}
|
||||
}
|
@ -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<Review> 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<Integer> ratingValues = this.reviewDAO.getAllRatingValues(appReleaseUuuid);
|
||||
TreeMap<Integer, Integer> 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);
|
||||
}
|
||||
}
|
175
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 → 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
175
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 → 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
Loading…
Reference in new issue