Search in sources :

Example 1 with PratilipiReviewsDoc

use of com.pratilipi.data.type.PratilipiReviewsDoc 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 PratilipiReviewsDoc

use of com.pratilipi.data.type.PratilipiReviewsDoc in project pratilipi by Pratilipi.

the class UserPratilipiDataUtil method createUserPratilipiData.

public static UserPratilipiData createUserPratilipiData(UserPratilipi userPratilipi) throws UnexpectedServerException {
    DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
    DocAccessor docAccessor = DataAccessorFactory.getDocAccessor();
    User user = dataAccessor.getUser(userPratilipi.getUserId());
    UserData userData = UserDataUtil.createUserData(user);
    PratilipiReviewsDoc reviewsDoc = docAccessor.getPratilipiReviewsDoc(userPratilipi.getPratilipiId());
    UserPratilipiData userPratilipiData = new UserPratilipiData();
    userPratilipiData.setId(userPratilipi.getId());
    userPratilipiData.setUserId(userPratilipi.getUserId());
    userPratilipiData.setPratilipiId(userPratilipi.getPratilipiId());
    userPratilipiData.setUser(userData);
    userPratilipiData.setUserName(userData.getDisplayName());
    userPratilipiData.setUserImageUrl(userData.getProfileImageUrl());
    userPratilipiData.setUserProfilePageUrl(userData.getProfilePageUrl());
    userPratilipiData.setRating(userPratilipi.getRating());
    userPratilipiData.setReview(processReview(userPratilipi.getReviewTitle(), userPratilipi.getReview()));
    userPratilipiData.setReviewState(userPratilipi.getReviewState());
    userPratilipiData.setReviewDate(userPratilipi.getReviewDate());
    for (UserPratilipiDoc review : reviewsDoc.getReviews()) {
        if (review.getId().equals(userPratilipi.getId())) {
            userPratilipiData.setCommentCount(review.getCommentCount());
            userPratilipiData.setLikeCount(review.getLikeCount());
            userPratilipiData.setLiked(review.getLikedByUserIds().contains(AccessTokenFilter.getAccessToken().getUserId()));
            break;
        }
    }
    userPratilipiData.setAddedToLib(userPratilipi.isAddedToLib());
    userPratilipiData.setAccessToReview(hasAccessToUpdateUserPratilipiData(userPratilipi, AccessType.USER_PRATILIPI_REVIEW));
    return userPratilipiData;
}
Also used : User(com.pratilipi.data.type.User) UserData(com.pratilipi.data.client.UserData) DataAccessor(com.pratilipi.data.DataAccessor) DocAccessor(com.pratilipi.data.DocAccessor) UserPratilipiDoc(com.pratilipi.data.type.UserPratilipiDoc) UserPratilipiData(com.pratilipi.data.client.UserPratilipiData) PratilipiReviewsDoc(com.pratilipi.data.type.PratilipiReviewsDoc)

Example 3 with PratilipiReviewsDoc

use of com.pratilipi.data.type.PratilipiReviewsDoc in project pratilipi by Pratilipi.

the class UserPratilipiDataUtil method getPratilipiReviewList.

public static DataListCursorTuple<UserPratilipiData> getPratilipiReviewList(Long pratilipiId, String cursor, Integer offset, Integer resultCount) throws UnexpectedServerException {
    DocAccessor docAccessor = DataAccessorFactory.getDocAccessor();
    PratilipiReviewsDoc reviewsDoc = docAccessor.getPratilipiReviewsDoc(pratilipiId);
    List<UserPratilipiDoc> reviewDocList = reviewsDoc.getReviews();
    // Reverse Sort
    Collections.reverse(reviewDocList);
    // Using next UserPratilipiId as the cursor
    int fromIndex = 0;
    if (cursor != null && !cursor.equals("-1")) {
        for (int i = 0; i < reviewDocList.size(); i++) {
            if (reviewDocList.get(i).getId().equals(cursor)) {
                fromIndex = i;
                break;
            }
        }
        if (offset != null)
            fromIndex = fromIndex + offset;
    }
    if (fromIndex >= reviewDocList.size() || (cursor != null && cursor.equals("-1"))) {
        return new DataListCursorTuple<UserPratilipiData>(new ArrayList<UserPratilipiData>(), "-1", (long) (int) reviewsDoc.getReviews().size());
    }
    int toIndex = resultCount == null ? reviewDocList.size() : Math.min(fromIndex + resultCount, reviewDocList.size());
    // Next cursor
    cursor = toIndex == reviewDocList.size() ? "-1" : reviewDocList.get(toIndex).getId();
    reviewDocList = reviewDocList.subList(fromIndex, toIndex);
    List<UserPratilipiData> userPratilipiDataList = new ArrayList<>(reviewDocList.size());
    for (UserPratilipiDoc review : reviewDocList) userPratilipiDataList.add(createUserPratilipiData(review));
    return new DataListCursorTuple<UserPratilipiData>(userPratilipiDataList, cursor, (long) (int) reviewsDoc.getReviews().size());
}
Also used : DataListCursorTuple(com.pratilipi.data.DataListCursorTuple) DocAccessor(com.pratilipi.data.DocAccessor) UserPratilipiDoc(com.pratilipi.data.type.UserPratilipiDoc) ArrayList(java.util.ArrayList) UserPratilipiData(com.pratilipi.data.client.UserPratilipiData) PratilipiReviewsDoc(com.pratilipi.data.type.PratilipiReviewsDoc)

Example 4 with PratilipiReviewsDoc

use of com.pratilipi.data.type.PratilipiReviewsDoc 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 5 with PratilipiReviewsDoc

use of com.pratilipi.data.type.PratilipiReviewsDoc in project pratilipi by Pratilipi.

the class PratilipiDataUtil method updateUserPratilipiStats.

public static void updateUserPratilipiStats(Long pratilipiId) throws UnexpectedServerException {
    DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
    DocAccessor docAccessor = DataAccessorFactory.getDocAccessor();
    Pratilipi pratilipi = dataAccessor.getPratilipi(pratilipiId);
    if (pratilipi.getState() != PratilipiState.PUBLISHED)
        return;
    PratilipiReviewsDoc reviewsDoc = docAccessor.getPratilipiReviewsDoc(pratilipiId);
    if (pratilipi.getRatingCount().equals(reviewsDoc.getRatingCount()) && pratilipi.getTotalRating().equals(reviewsDoc.getTotalRating()) && pratilipi.getReviewCount().equals(reviewsDoc.getReviewCount()))
        return;
    AuditLog auditLog = dataAccessor.newAuditLog(AccessTokenFilter.getAccessToken(), AccessType.PRATILIPI_UPDATE, pratilipi);
    pratilipi.setRatingCount(reviewsDoc.getRatingCount());
    pratilipi.setTotalRating(reviewsDoc.getTotalRating());
    pratilipi.setReviewCount(reviewsDoc.getReviewCount());
    pratilipi = dataAccessor.createOrUpdatePratilipi(pratilipi, auditLog);
}
Also used : DataAccessor(com.pratilipi.data.DataAccessor) DocAccessor(com.pratilipi.data.DocAccessor) Pratilipi(com.pratilipi.data.type.Pratilipi) UserPratilipi(com.pratilipi.data.type.UserPratilipi) AuditLog(com.pratilipi.data.type.AuditLog) PratilipiReviewsDoc(com.pratilipi.data.type.PratilipiReviewsDoc)

Aggregations

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