diff --git a/src/main/java/org/sunbird/common/util/Constants.java b/src/main/java/org/sunbird/common/util/Constants.java index 4b0e0b32c..106146459 100644 --- a/src/main/java/org/sunbird/common/util/Constants.java +++ b/src/main/java/org/sunbird/common/util/Constants.java @@ -171,11 +171,11 @@ public class Constants { public static final String USERID = "id"; public static final String COMMENT = "comment"; public static final String COMMENT_BY = "commentBy"; - public static final String COMMENT_UPDATED_ON = "commentupdatedon"; - public static final String CREATED_ON = "createdon"; + public static final String COMMENT_UPDATED_ON = "commentUpdatedOn"; + public static final String CREATED_ON = "createdOn"; public static final String RATING = "rating"; public static final String REVIEW = "review"; - public static final String UPDATED_ON = "updatedon"; + public static final String UPDATED_ON = "updatedOn"; public static final String LATEST50REVIEWS = "latest50reviews"; public static final String SUMMARY_ACTIVITY_ID = "activityid"; public static final String SUMMARY_ACTIVITY_TYPE = "activitytype"; diff --git a/src/main/java/org/sunbird/ratings/controller/RatingsController.java b/src/main/java/org/sunbird/ratings/controller/RatingsController.java index 7d04ccf60..da32fa351 100644 --- a/src/main/java/org/sunbird/ratings/controller/RatingsController.java +++ b/src/main/java/org/sunbird/ratings/controller/RatingsController.java @@ -1,15 +1,21 @@ package org.sunbird.ratings.controller; +import java.util.Map; + +import javax.validation.Valid; + import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.ResponseEntity; -import org.springframework.web.bind.annotation.*; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RestController; import org.sunbird.common.model.SBApiResponse; import org.sunbird.ratings.model.LookupRequest; import org.sunbird.ratings.model.RequestRating; import org.sunbird.ratings.service.RatingService; -import javax.validation.Valid; - @RestController public class RatingsController { @@ -22,7 +28,6 @@ public class RatingsController { public ResponseEntity upsertRating(@Valid @RequestBody RequestRating requestRatingBody) { SBApiResponse response = ratingService.upsertRating(requestRatingBody); return new ResponseEntity<>(response, response.getResponseCode()); - } @GetMapping("/ratings/v1/read/{activityId}/{activityType}/{userId}") @@ -31,7 +36,6 @@ public ResponseEntity getRating(@PathVariable("activityId") String activityId @PathVariable("userId") String userId) { SBApiResponse response = ratingService.getRatings(activityId, activityType, userId); return new ResponseEntity<>(response, response.getResponseCode()); - } @GetMapping("/ratings/v1/summary/{activityId}/{activityType}") @@ -46,5 +50,11 @@ public ResponseEntity ratingLookUp(@RequestBody LookupRequest request) { SBApiResponse response = ratingService.ratingLookUp(request); return new ResponseEntity<>(response, response.getResponseCode()); } + + @PostMapping("/ratings/v1/read") + public ResponseEntity readRating(@RequestBody Map request) { + SBApiResponse response = ratingService.readRatings(request); + return new ResponseEntity<>(response, response.getResponseCode()); + } } diff --git a/src/main/java/org/sunbird/ratings/model/LookupDataModel.java b/src/main/java/org/sunbird/ratings/model/LookupDataModel.java index 9e4214e3c..e454d6656 100644 --- a/src/main/java/org/sunbird/ratings/model/LookupDataModel.java +++ b/src/main/java/org/sunbird/ratings/model/LookupDataModel.java @@ -3,32 +3,32 @@ import java.util.UUID; public class LookupDataModel { - private String activityid; + private String activityId; private String review; private Float rating; - private String updatedon; - private String activitytype; + private String updatedOn; + private String activityType; private String userId; public LookupDataModel() { } - public LookupDataModel(String activityid, String review, Float rating, UUID updatedon, String activitytype, String userId) { - this.activityid = activityid; + public LookupDataModel(String activityId, String review, Float rating, UUID updatedOn, String activityType, String userId) { + this.activityId = activityId; this.review = review; this.rating = rating; - this.updatedon = updatedon.toString(); - this.activitytype = activitytype; + this.updatedOn = updatedOn.toString(); + this.activityType = activityType; this.userId = userId; } - public String getActivityid() { - return activityid; + public String getActivityId() { + return activityId; } - public void setActivityid(String activityid) { - this.activityid = activityid; + public void setActivityId(String activityId) { + this.activityId = activityId; } public String getReview() { @@ -48,20 +48,20 @@ public void setRating(Float rating) { } - public String getUpdatedon() { - return updatedon; + public String getUpdatedOn() { + return updatedOn; } - public void setUpdatedon(UUID updatedon) { - this.updatedon = updatedon.toString(); + public void setUpdatedOn(UUID updatedOn) { + this.updatedOn = updatedOn.toString(); } - public String getActivitytype() { - return activitytype; + public String getActivityType() { + return activityType; } - public void setActivitytype(String activitytype) { - this.activitytype = activitytype; + public void setActivityType(String activityType) { + this.activityType = activityType; } public String getUserId() { @@ -75,11 +75,11 @@ public void setUserId(String userId) { @Override public String toString() { return "LookupDataModel{" + - "activityid='" + activityid + '\'' + + "activityId='" + activityId + '\'' + ", review='" + review + '\'' + ", rating=" + rating + - ", updatedon='" + updatedon + '\'' + - ", activitytype='" + activitytype + '\'' + + ", updatedOn='" + updatedOn + '\'' + + ", activityType='" + activityType + '\'' + ", userId='" + userId + '\'' + '}'; } diff --git a/src/main/java/org/sunbird/ratings/service/RatingService.java b/src/main/java/org/sunbird/ratings/service/RatingService.java index c2bfe5ad6..cdbabfa9e 100644 --- a/src/main/java/org/sunbird/ratings/service/RatingService.java +++ b/src/main/java/org/sunbird/ratings/service/RatingService.java @@ -1,5 +1,7 @@ package org.sunbird.ratings.service; +import java.util.Map; + import org.sunbird.common.model.SBApiResponse; import org.sunbird.ratings.model.LookupRequest; import org.sunbird.ratings.model.RequestRating; @@ -12,4 +14,6 @@ public interface RatingService { public SBApiResponse getRatingSummary(String activityId, String activityType); public SBApiResponse ratingLookUp(LookupRequest request); + + public SBApiResponse readRatings(Map request); } diff --git a/src/main/java/org/sunbird/ratings/service/RatingServiceImpl.java b/src/main/java/org/sunbird/ratings/service/RatingServiceImpl.java index 235baa9de..9c3c9b0cb 100644 --- a/src/main/java/org/sunbird/ratings/service/RatingServiceImpl.java +++ b/src/main/java/org/sunbird/ratings/service/RatingServiceImpl.java @@ -1,8 +1,14 @@ package org.sunbird.ratings.service; -import com.datastax.driver.core.utils.UUIDs; -import com.fasterxml.jackson.databind.JsonNode; -import com.fasterxml.jackson.databind.ObjectMapper; +import java.sql.Timestamp; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.UUID; +import java.util.regex.Pattern; + import org.apache.commons.lang.StringUtils; import org.apache.kafka.common.KafkaException; import org.springframework.beans.factory.annotation.Autowired; @@ -10,19 +16,30 @@ import org.springframework.http.HttpStatus; import org.springframework.stereotype.Service; import org.springframework.util.CollectionUtils; +import org.springframework.util.ObjectUtils; import org.sunbird.cassandra.utils.CassandraOperation; import org.sunbird.common.model.SBApiResponse; import org.sunbird.common.util.Constants; +import org.sunbird.common.util.ProjectUtil; import org.sunbird.core.logger.CbExtLogger; import org.sunbird.core.producer.Producer; import org.sunbird.ratings.exception.ValidationException; -import org.sunbird.ratings.model.*; +import org.sunbird.ratings.model.LookupDataModel; +import org.sunbird.ratings.model.LookupRequest; +import org.sunbird.ratings.model.LookupResponse; +import org.sunbird.ratings.model.RatingMessage; +import org.sunbird.ratings.model.RatingModelInfo; +import org.sunbird.ratings.model.RequestRating; +import org.sunbird.ratings.model.SummaryModel; +import org.sunbird.ratings.model.SummaryNodeModel; +import org.sunbird.ratings.model.UserModel; +import org.sunbird.ratings.model.ValidationBody; import org.sunbird.ratings.responsecode.ResponseCode; import org.sunbird.ratings.responsecode.ResponseMessage; -import java.sql.Timestamp; -import java.util.*; -import java.util.regex.Pattern; +import com.datastax.driver.core.utils.UUIDs; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; @Service public class RatingServiceImpl implements RatingService { @@ -57,29 +74,30 @@ public SBApiResponse getRatings(String activityId, String activityType, String u request.put(Constants.ACTIVITY_ID, activityId); request.put(Constants.ACTIVITY_TYPE, activityType); request.put(Constants.RATINGS_USER_ID, userId); - List> existingDataList = cassandraOperation.getRecordsByProperties(Constants.KEYSPACE_SUNBIRD, + List> existingDataList = cassandraOperation.getRecordsByPropertiesWithoutFiltering( + Constants.KEYSPACE_SUNBIRD, Constants.TABLE_RATINGS, request, null); if (!CollectionUtils.isEmpty(existingDataList)) { Map ratingData = existingDataList.get(0); RatingModelInfo ratingModelInfo = new RatingModelInfo(); - ratingModelInfo.setActivityId((String) ratingData.get("activityid")); - ratingModelInfo.setReview((String) ratingData.get("review")); - ratingModelInfo.setRating((Float) ratingData.get("rating")); - ratingModelInfo.setComment(ratingData.get("comment")!=null ?(String) ratingData.get("comment") : null); - ratingModelInfo.setCommentBy(ratingData.get("commentby")!=null ?(String) ratingData.get("commentby") : null); - - if(ratingData.get("commentupdatedon")!=null){ - UUID commentupdatedOn = (UUID) ratingData.get("commentupdatedon"); + ratingModelInfo.setActivityId((String) ratingData.get(Constants.ACTIVITY_ID)); + ratingModelInfo.setReview((String) ratingData.get(Constants.REVIEW)); + ratingModelInfo.setRating((Float) ratingData.get(Constants.RATING)); + ratingModelInfo.setComment(ratingData.get(Constants.COMMENT)!=null ?(String) ratingData.get(Constants.COMMENT) : null); + ratingModelInfo.setCommentBy(ratingData.get(Constants.COMMENT_BY)!=null ?(String) ratingData.get(Constants.COMMENT_BY) : null); + + if(ratingData.get(Constants.COMMENT_UPDATED_ON)!=null){ + UUID commentupdatedOn = (UUID) ratingData.get(Constants.COMMENT_UPDATED_ON); Long CommentUpdatedTime = (commentupdatedOn.timestamp() - 0x01b21dd213814000L) / 10000L; ratingModelInfo.setCommentUpdatedOn(new Timestamp(CommentUpdatedTime)); } - timeBasedUuid = (UUID) ratingData.get("updatedon"); + timeBasedUuid = (UUID) ratingData.get(Constants.UPDATED_ON); Long updatedTime = (timeBasedUuid.timestamp() - 0x01b21dd213814000L) / 10000L; ratingModelInfo.setUpdatedOn(new Timestamp(updatedTime)); - ratingModelInfo.setActivityType((String) ratingData.get("activitytype")); - ratingModelInfo.setUserId((String) ratingData.get("userId")); - timeBasedUuid = (UUID) ratingData.get("createdon"); + ratingModelInfo.setActivityType((String) ratingData.get(Constants.ACTIVITY_TYPE)); + ratingModelInfo.setUserId((String) ratingData.get(Constants.USER_ID)); + timeBasedUuid = (UUID) ratingData.get(Constants.CREATED_ON); Long createdTime = (timeBasedUuid.timestamp() - 0x01b21dd213814000L) / 10000L; ratingModelInfo.setCreatedOn(new Timestamp(createdTime)); response.put(Constants.MESSAGE, Constants.SUCCESSFUL); @@ -111,7 +129,8 @@ public SBApiResponse getRatingSummary(String activityId, String activityType) { request.put(Constants.ACTIVITY_ID, activityId); request.put(Constants.ACTIVITY_TYPE, activityType); - List> existingDataList = cassandraOperation.getRecordsByProperties(Constants.KEYSPACE_SUNBIRD, + List> existingDataList = cassandraOperation.getRecordsByPropertiesWithoutFiltering( + Constants.KEYSPACE_SUNBIRD, Constants.TABLE_RATINGS_SUMMARY, request, null); if (!CollectionUtils.isEmpty(existingDataList)) { @@ -154,8 +173,8 @@ public SBApiResponse getRatingSummary(String activityId, String activityType) { } } SummaryModel summaryModel = new SummaryModel( - summaryData.get(Constants.SUMMARY_ACTIVITY_ID).toString(), - summaryData.get(Constants.SUMMARY_ACTIVITY_TYPE).toString(), + summaryData.get(Constants.ACTIVITY_ID).toString(), + summaryData.get(Constants.ACTIVITY_TYPE).toString(), (Float) summaryData.get(Constants.TOTALCOUNT1STARS), (Float) summaryData.get(Constants.TOTALCOUNT2STARS), (Float) summaryData.get(Constants.TOTALCOUNT3STARS), @@ -198,7 +217,8 @@ public SBApiResponse upsertRating(RequestRating requestRating) { request.put(Constants.RATINGS_USER_ID, requestRating.getUserId()); - List> existingDataList = cassandraOperation.getRecordsByProperties(Constants.KEYSPACE_SUNBIRD, + List> existingDataList = cassandraOperation.getRecordsByPropertiesWithoutFiltering( + Constants.KEYSPACE_SUNBIRD, Constants.TABLE_RATINGS, request, null); if (!CollectionUtils.isEmpty(existingDataList)) { @@ -218,10 +238,10 @@ public SBApiResponse upsertRating(RequestRating requestRating) { cassandraOperation.updateRecord(Constants.KEYSPACE_SUNBIRD, Constants.TABLE_RATINGS, updateRequest, request); ratingMessage = new RatingMessage("ratingUpdate", requestRating.getActivityId(), requestRating.getActivityType(), - requestRating.getUserId(), String.valueOf((prevInfo.get("createdon")))); + requestRating.getUserId(), String.valueOf((prevInfo.get(Constants.CREATED_ON)))); - ratingMessage.setPrevValues(processEventMessage(String.valueOf(prevInfo.get("updatedon")), - (Float) prevInfo.get("rating"), (String) prevInfo.get("review"))); + ratingMessage.setPrevValues(processEventMessage(String.valueOf(prevInfo.get(Constants.UPDATED_ON)), + (Float) prevInfo.get(Constants.RATING), (String) prevInfo.get(Constants.REVIEW))); ratingMessage.setUpdatedValues(processEventMessage(String.valueOf(updateRequest.get(Constants.UPDATED_ON)), requestRating.getRating(), requestRating.getReview())); } else { @@ -286,7 +306,7 @@ public SBApiResponse ratingLookUp(LookupRequest lookupRequest) { } Map existingDataList = cassandraOperation.getRecordsByPropertiesWithPagination(Constants.KEYSPACE_SUNBIRD, - Constants.TABLE_RATINGS_LOOKUP, request, null, lookupRequest.getLimit(), uuid, "userId"); + Constants.TABLE_RATINGS_LOOKUP, request, null, lookupRequest.getLimit(), uuid, Constants.USER_ID); List listOfLookupResponse = new ArrayList<>(); if (!CollectionUtils.isEmpty(existingDataList)) { @@ -301,20 +321,20 @@ public SBApiResponse ratingLookUp(LookupRequest lookupRequest) { fields.add(Constants.USERID); fields.add(Constants.FIRSTNAME); - Map existingUserList = cassandraOperation.getRecordsByProperties(Constants.KEYSPACE_SUNBIRD, + Map existingUserList = cassandraOperation.getRecordsByPropertiesWithoutFiltering(Constants.KEYSPACE_SUNBIRD, Constants.TABLE_USER, userRequest, fields, Constants.ID); for (String user : listOfUserId) { final ObjectMapper mapper = new ObjectMapper(); final UserModel userModel = mapper.convertValue(existingUserList.get(user), UserModel.class); final LookupDataModel lookupModel = mapper.convertValue(existingDataList.get(user), LookupDataModel.class); - Long updatedTime= ((UUID.fromString(lookupModel.getUpdatedon()).timestamp() - 0x01b21dd213814000L) )/ 10000L; - listOfLookupResponse.add(new LookupResponse(lookupModel.getActivityid(), + Long updatedTime= ((UUID.fromString(lookupModel.getUpdatedOn()).timestamp() - 0x01b21dd213814000L) )/ 10000L; + listOfLookupResponse.add(new LookupResponse(lookupModel.getActivityId(), lookupModel.getReview(), lookupModel.getRating().toString(), updatedTime, - lookupModel.getUpdatedon(), - lookupModel.getActivitytype(), + lookupModel.getUpdatedOn(), + lookupModel.getActivityType(), lookupModel.getUserId(), (userModel.getFirstName() != null) ? userModel.getFirstName() : "" )); @@ -418,4 +438,70 @@ public void processExceptionBody(SBApiResponse response, Exception ex, response.setResponseCode(status); } + public SBApiResponse readRatings(Map request) { + SBApiResponse response = ProjectUtil.createDefaultResponse(Constants.API_RATINGS_READ); + String errMsg = ""; + String activityId = ""; + try { + errMsg = validateRequest(request); + if (StringUtils.isNotBlank(errMsg)) { + updateErrorDetails(response, errMsg, HttpStatus.INTERNAL_SERVER_ERROR); + return response; + } + + Map requestBody = (Map) request.get(Constants.REQUEST); + Map compositeKey = new HashMap(); + + activityId = (String) requestBody.get(Constants.ACTIVITY_ID); + compositeKey.put(Constants.ACTIVITY_ID, activityId); + compositeKey.put(Constants.ACTIVITY_TYPE, (String) requestBody.get(Constants.ACTIVITY_TYPE)); + compositeKey.put(Constants.RATINGS_USER_ID, (List) requestBody.get(Constants.USER_ID)); + List> existingDataList = cassandraOperation.getRecordsByPropertiesWithoutFiltering( + Constants.KEYSPACE_SUNBIRD, + Constants.TABLE_RATINGS, compositeKey, null); + if (!CollectionUtils.isEmpty(existingDataList)) { + response.put(Constants.COUNT, existingDataList.size()); + response.put(Constants.CONTENT, existingDataList); + } + } catch (Exception e) { + errMsg = String.format("Failed to read rating for %s Course. Exception: %s", activityId, e.getMessage()); + logger.error("updateRatingTopicName", e); + } + return response; + } + + private String validateRequest(Map request) { + StringBuilder strBuilder = new StringBuilder(); + Map requestBody = (Map) request.get(Constants.REQUEST); + if (ObjectUtils.isEmpty(requestBody)) { + strBuilder.append("Invalid Request Body."); + return strBuilder.toString(); + } + + List missingAttrib = new ArrayList(); + if (!requestBody.containsKey(Constants.ACTIVITY_ID)) { + missingAttrib.add(Constants.ACTIVITY_ID); + } + if (!requestBody.containsKey(Constants.ACTIVITY_TYPE)) { + missingAttrib.add(Constants.ACTIVITY_TYPE); + } + + if (!requestBody.containsKey(Constants.USER_ID)) { + missingAttrib.add(Constants.USER_ID); + } + + if (missingAttrib.size() > 0) { + strBuilder.append("The following parameter(s) are missing. Missing params - [") + .append(missingAttrib.toString()).append("]"); + } + + return strBuilder.toString(); + } + + private void updateErrorDetails(SBApiResponse response, String errMsg, HttpStatus responseCode) { + response.getParams().setStatus(Constants.FAILED); + response.getParams().setErrmsg(errMsg); + response.setResponseCode(responseCode); + } + } diff --git a/src/main/resources/cassandratablecolumn.properties b/src/main/resources/cassandratablecolumn.properties index 64ec9ed0b..11a6a942c 100644 --- a/src/main/resources/cassandratablecolumn.properties +++ b/src/main/resources/cassandratablecolumn.properties @@ -57,3 +57,9 @@ failedrecordscount=failedRecordsCount submitassessmentresponse=submitAssessmentResponse assessmentreadresponse=assessmentReadResponse submitassessmentrequest=submitAssessmentRequest +activityid=activityId +activitytype=activityType +commentby=commentBy +commentupdatedon=commentUpdatedOn +createdon=createdOn +updatedon=updatedOn \ No newline at end of file