Search in sources :

Example 1 with CommentDoc

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;
}
Also used : CommentDoc(com.pratilipi.data.type.CommentDoc) DataListCursorTuple(com.pratilipi.data.DataListCursorTuple) DocAccessor(com.pratilipi.data.DocAccessor) UserPratilipiDoc(com.pratilipi.data.type.UserPratilipiDoc) CommentData(com.pratilipi.data.client.CommentData) PratilipiReviewsDoc(com.pratilipi.data.type.PratilipiReviewsDoc)

Example 2 with CommentDoc

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);
}
Also used : HashMap(java.util.HashMap) DataAccessor(com.pratilipi.data.DataAccessor) DocAccessor(com.pratilipi.data.DocAccessor) ArrayList(java.util.ArrayList) CommentDoc(com.pratilipi.data.type.CommentDoc) UserPratilipi(com.pratilipi.data.type.UserPratilipi) UserPratilipiDoc(com.pratilipi.data.type.UserPratilipiDoc) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) Comment(com.pratilipi.data.type.Comment) Vote(com.pratilipi.data.type.Vote) LinkedList(java.util.LinkedList) PratilipiReviewsDoc(com.pratilipi.data.type.PratilipiReviewsDoc)

Example 3 with CommentDoc

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;
}
Also used : CommentDoc(com.pratilipi.data.type.CommentDoc) UserData(com.pratilipi.data.client.UserData) ArrayList(java.util.ArrayList) CommentData(com.pratilipi.data.client.CommentData)

Aggregations

CommentDoc (com.pratilipi.data.type.CommentDoc)3 DocAccessor (com.pratilipi.data.DocAccessor)2 CommentData (com.pratilipi.data.client.CommentData)2 PratilipiReviewsDoc (com.pratilipi.data.type.PratilipiReviewsDoc)2 UserPratilipiDoc (com.pratilipi.data.type.UserPratilipiDoc)2 ArrayList (java.util.ArrayList)2 DataAccessor (com.pratilipi.data.DataAccessor)1 DataListCursorTuple (com.pratilipi.data.DataListCursorTuple)1 UserData (com.pratilipi.data.client.UserData)1 Comment (com.pratilipi.data.type.Comment)1 UserPratilipi (com.pratilipi.data.type.UserPratilipi)1 Vote (com.pratilipi.data.type.Vote)1 HashMap (java.util.HashMap)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1