use of com.pratilipi.data.type.CommentDoc in project pratilipi by Pratilipi.
the class CommentDataUtil method getCommentList.
public static DataListCursorTuple<CommentData> getCommentList(CommentParentType parentType, String parentId, String cursor, Integer offset, Integer resultCount) throws UnexpectedServerException {
DocAccessor docAccessor = DataAccessorFactory.getDocAccessor();
if (parentType == CommentParentType.REVIEW) {
Long userId = Long.parseLong(parentId.substring(0, parentId.indexOf('-')));
Long pratilipiId = Long.parseLong(parentId.substring(parentId.indexOf('-') + 1));
PratilipiReviewsDoc reviewsDoc = docAccessor.getPratilipiReviewsDoc(pratilipiId);
List<CommentDoc> commentDocList = null;
for (UserPratilipiDoc reviewDoc : reviewsDoc.getReviews()) {
if (reviewDoc.getUserId().equals(userId)) {
commentDocList = reviewDoc.getComments();
break;
}
}
int fromIndex = (cursor == null ? 0 : Integer.parseInt(cursor)) + (offset == null ? 0 : offset);
int toIndex = resultCount == null ? commentDocList.size() : fromIndex + resultCount;
commentDocList = commentDocList.subList(Math.min(fromIndex, commentDocList.size()), Math.min(toIndex, commentDocList.size()));
List<CommentData> commentDataList = createCommentDataList(commentDocList);
return new DataListCursorTuple<CommentData>(commentDataList, toIndex + "", (long) (int) reviewsDoc.getReviews().size());
}
return null;
}
use of com.pratilipi.data.type.CommentDoc in project pratilipi by Pratilipi.
the class PratilipiDocUtil method updatePratilipiReviews.
public static void updatePratilipiReviews(Long pratilipiId) throws UnexpectedServerException {
DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
DocAccessor docAccessor = DataAccessorFactory.getDocAccessor();
List<Comment> commentList = dataAccessor.getCommentListByReference(ReferenceType.PRATILIPI, pratilipiId);
Map<String, List<Comment>> reviewIdCommentListMap = new HashMap<>();
for (Comment comment : commentList) {
if (comment.getParentType() != CommentParentType.REVIEW)
continue;
List<Comment> reviewCommentList = reviewIdCommentListMap.get(comment.getParentId());
if (reviewCommentList == null) {
reviewCommentList = new LinkedList<>();
reviewIdCommentListMap.put(comment.getParentId(), reviewCommentList);
}
reviewCommentList.add(comment);
}
List<Vote> voteList = dataAccessor.getVoteListByReference(ReferenceType.PRATILIPI, pratilipiId);
Map<String, List<Vote>> reviewIdLikeVotesMap = new HashMap<>();
Map<String, List<Long>> commentIdLikedByUserIdsMap = new HashMap<>();
for (Vote vote : voteList) {
if (vote.getType() == VoteType.NONE) {
continue;
} else if (vote.getParentType() == VoteParentType.REVIEW) {
List<Vote> reviewLikeVoteList = reviewIdLikeVotesMap.get(vote.getParentId());
if (reviewLikeVoteList == null) {
reviewLikeVoteList = new LinkedList<>();
reviewIdLikeVotesMap.put(vote.getParentId(), reviewLikeVoteList);
}
reviewLikeVoteList.add(vote);
} else if (vote.getParentType() == VoteParentType.COMMENT) {
List<Long> userIdList = commentIdLikedByUserIdsMap.get(vote.getParentId());
if (userIdList == null) {
userIdList = new LinkedList<>();
commentIdLikedByUserIdsMap.put(vote.getParentId(), userIdList);
}
userIdList.add(vote.getUserId());
}
}
List<UserPratilipi> userPratilipiList = dataAccessor.getUserPratilipiList(null, pratilipiId, null, null, true).getDataList();
long ratingCount = 0;
long totalRating = 0;
List<UserPratilipiDoc> reviewDocList = new ArrayList<>();
for (UserPratilipi userPratilipi : userPratilipiList) {
if (userPratilipi.getRating() != null && userPratilipi.getRating() > 0) {
ratingCount++;
totalRating += userPratilipi.getRating();
}
if (userPratilipi.getReviewState() != UserReviewState.PUBLISHED)
continue;
if ((userPratilipi.getReviewTitle() == null || userPratilipi.getReviewTitle().trim().isEmpty()) && (userPratilipi.getReview() == null || userPratilipi.getReview().trim().isEmpty()))
continue;
UserPratilipiDoc reviewDoc = docAccessor.newUserPratilipiDoc();
reviewDoc.setId(userPratilipi.getId());
reviewDoc.setUserId(userPratilipi.getUserId());
reviewDoc.setRating(userPratilipi.getRating());
reviewDoc.setReviewTitle(userPratilipi.getReviewTitle() == null || userPratilipi.getReviewTitle().trim().isEmpty() ? null : userPratilipi.getReviewTitle().trim());
reviewDoc.setReview(userPratilipi.getReview() == null || userPratilipi.getReview().trim().isEmpty() ? null : userPratilipi.getReview().trim());
reviewDoc.setReviewDate(userPratilipi.getReviewDate());
reviewDocList.add(reviewDoc);
List<Vote> reviewLikeVoteList = reviewIdLikeVotesMap.get(userPratilipi.getId());
if (reviewLikeVoteList != null) {
List<Long> userIdList = new ArrayList<>(reviewLikeVoteList.size());
for (Vote vote : reviewLikeVoteList) {
if (vote.getLastUpdated().before(userPratilipi.getReviewDate()))
continue;
userIdList.add(vote.getUserId());
}
reviewDoc.setLikedByUserIds(userIdList);
}
List<Comment> reviewCommentList = reviewIdCommentListMap.get(userPratilipi.getId());
if (reviewCommentList != null) {
List<CommentDoc> commentDocList = new ArrayList<>(reviewCommentList.size());
for (Comment comment : reviewCommentList) {
if (comment.getState() == CommentState.DELETED)
continue;
if (comment.getCreationDate().before(userPratilipi.getReviewDate()))
continue;
CommentDoc commentDoc = docAccessor.newCommentDoc();
commentDoc.setId(comment.getId());
commentDoc.setUserId(comment.getUserId());
commentDoc.setContent(comment.getContent());
commentDoc.setCreationDate(comment.getCreationDate());
commentDoc.setLastUpdated(comment.getLastUpdated());
commentDoc.setLikedByUserIds(commentIdLikedByUserIdsMap.get(comment.getId().toString()));
commentDocList.add(commentDoc);
}
reviewDoc.setComments(commentDocList);
}
}
PratilipiReviewsDoc reviewsDoc = docAccessor.newPratilipiReviewsDoc();
reviewsDoc.setRatingCount(ratingCount);
reviewsDoc.setTotalRating(totalRating);
reviewsDoc.setReviews(reviewDocList);
docAccessor.save(pratilipiId, reviewsDoc);
}
use of com.pratilipi.data.type.CommentDoc in project pratilipi by Pratilipi.
the class CommentDataUtil method createCommentDataList.
public static List<CommentData> createCommentDataList(List<CommentDoc> commentDocList) {
List<Long> userIdList = new ArrayList<>(commentDocList.size());
for (CommentDoc commentDoc : commentDocList) if (!userIdList.contains(commentDoc.getUserId()))
userIdList.add(commentDoc.getUserId());
Map<Long, UserData> userDataMap = UserDataUtil.createUserDataList(userIdList, true);
List<CommentData> commentDataList = new ArrayList<>(commentDocList.size());
for (CommentDoc commentDoc : commentDocList) {
CommentData commentData = createCommentData(commentDoc);
commentData.setUser(userDataMap.get(commentDoc.getUserId()));
commentDataList.add(commentData);
}
return commentDataList;
}
Aggregations