Search in sources :

Example 1 with UserPratilipi

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

the class CommentDataUtil method saveCommentData.

public static CommentData saveCommentData(CommentData commentData) throws InvalidArgumentException, InsufficientAccessException {
    _validateCommentDataForSave(commentData);
    boolean isNew = commentData.getId() == null;
    DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
    Comment comment = isNew ? dataAccessor.newComment() : dataAccessor.getComment(commentData.getId());
    if (isNew && !hasAccessToAddCommentData(commentData))
        throw new InsufficientAccessException();
    if (!isNew && !hasAccessToUpdateCommentData(comment))
        throw new InsufficientAccessException();
    AuditLog auditLog = dataAccessor.newAuditLog(AccessTokenFilter.getAccessToken(), isNew ? AccessType.COMMENT_ADD : AccessType.COMMENT_UPDATE, comment);
    if (isNew) {
        comment.setUserId(commentData.getUserId());
        comment.setParentType(commentData.getParentType());
        comment.setParentId(commentData.getParentId());
        if (commentData.getParentType() == CommentParentType.REVIEW) {
            UserPratilipi userPratilipi = dataAccessor.getUserPratilipi(commentData.getParentId());
            comment.setReferenceType(ReferenceType.PRATILIPI);
            comment.setReferenceId(userPratilipi.getPratilipiId());
        }
        comment.setCreationDate(new Date());
    } else {
        comment.setLastUpdated(new Date());
    }
    if (commentData.hasContent())
        comment.setContent(commentData.getContent());
    if (commentData.hasState())
        comment.setState(commentData.getState());
    comment = dataAccessor.createOrUpdateComment(comment, auditLog);
    User user = dataAccessor.getUser(commentData.getUserId());
    UserData userData = UserDataUtil.createUserData(user);
    commentData = createCommentData(comment);
    commentData.setUser(userData);
    return commentData;
}
Also used : Comment(com.pratilipi.data.type.Comment) UserPratilipi(com.pratilipi.data.type.UserPratilipi) User(com.pratilipi.data.type.User) UserData(com.pratilipi.data.client.UserData) DataAccessor(com.pratilipi.data.DataAccessor) InsufficientAccessException(com.pratilipi.common.exception.InsufficientAccessException) AuditLog(com.pratilipi.data.type.AuditLog) Date(java.util.Date)

Example 2 with UserPratilipi

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

the class VoteDataUtil method saveVoteData.

public static VoteData saveVoteData(Long userId, VoteParentType parentType, String parentId, VoteType type) throws InsufficientAccessException {
    DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
    Vote vote = dataAccessor.getVote(userId, parentType, parentId);
    boolean isNew = vote == null;
    if (isNew)
        vote = dataAccessor.newVote();
    AuditLog auditLog = dataAccessor.newAuditLog(AccessTokenFilter.getAccessToken(), AccessType.VOTE, vote);
    if (isNew) {
        vote.setUserId(userId);
        vote.setParentType(parentType);
        vote.setParentId(parentId);
        if (parentType == VoteParentType.REVIEW) {
            UserPratilipi userPratilipi = dataAccessor.getUserPratilipi(parentId);
            vote.setReferenceType(ReferenceType.PRATILIPI);
            vote.setReferenceId(userPratilipi.getPratilipiId());
        } else if (parentType == VoteParentType.COMMENT) {
            Comment comment = dataAccessor.getComment(Long.parseLong(parentId));
            UserPratilipi userPratilipi = dataAccessor.getUserPratilipi(comment.getParentId());
            vote.setReferenceType(ReferenceType.PRATILIPI);
            vote.setReferenceId(userPratilipi.getPratilipiId());
        }
        vote.setCreationDate(new Date());
    } else {
        vote.setLastUpdated(new Date());
    }
    vote.setType(type);
    if (!hasAccessToAddOrUpdateData(vote))
        throw new InsufficientAccessException();
    vote = dataAccessor.createOrUpdateVote(vote, auditLog);
    return createVoteData(vote);
}
Also used : UserPratilipi(com.pratilipi.data.type.UserPratilipi) Comment(com.pratilipi.data.type.Comment) Vote(com.pratilipi.data.type.Vote) DataAccessor(com.pratilipi.data.DataAccessor) InsufficientAccessException(com.pratilipi.common.exception.InsufficientAccessException) AuditLog(com.pratilipi.data.type.AuditLog) Date(java.util.Date)

Example 3 with UserPratilipi

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

the class UserPratilipiDataUtil method getUserPratilipi.

public static UserPratilipiData getUserPratilipi(Long userId, Long pratilipiId) throws UnexpectedServerException {
    if (userId.equals(0L))
        return null;
    DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
    UserPratilipi userPratilipi = dataAccessor.getUserPratilipi(userId, pratilipiId);
    if (userPratilipi == null) {
        userPratilipi = dataAccessor.newUserPratilipi();
        userPratilipi.setUserId(userId);
        userPratilipi.setPratilipiId(pratilipiId);
    }
    return createUserPratilipiData(userPratilipi);
}
Also used : UserPratilipi(com.pratilipi.data.type.UserPratilipi) DataAccessor(com.pratilipi.data.DataAccessor)

Example 4 with UserPratilipi

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

the class DataStoreCleanupUtil method blockReview.

public static void blockReview(Long userId, Long pratilipiId) {
    UserPratilipi userPratilipi = ObjectifyService.ofy().load().type(UserPratilipiEntity.class).id(userId + "-" + pratilipiId).now();
    if (userPratilipi.getRating() != null || userPratilipi.getReviewState() != UserReviewState.BLOCKED) {
        userPratilipi.setRating(null);
        userPratilipi.setReviewState(UserReviewState.BLOCKED);
        ObjectifyService.ofy().save().entity(userPratilipi).now();
        Task task = TaskQueueFactory.newTask().setUrl("/pratilipi/process").addParam("pratilipiId", pratilipiId.toString()).addParam("updateReviewsDoc", "true").addParam("updateUserPratilipiStats", "true");
        TaskQueueFactory.getPratilipiTaskQueue().add(task);
    }
    System.out.println();
    System.out.println("Blocked review: " + userPratilipi.getReview());
}
Also used : UserPratilipi(com.pratilipi.data.type.UserPratilipi) Task(com.pratilipi.taskqueue.Task)

Example 5 with UserPratilipi

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

Aggregations

UserPratilipi (com.pratilipi.data.type.UserPratilipi)14 DataAccessor (com.pratilipi.data.DataAccessor)11 Date (java.util.Date)7 AuditLog (com.pratilipi.data.type.AuditLog)6 Comment (com.pratilipi.data.type.Comment)5 InsufficientAccessException (com.pratilipi.common.exception.InsufficientAccessException)4 Vote (com.pratilipi.data.type.Vote)4 ArrayList (java.util.ArrayList)4 Author (com.pratilipi.data.type.Author)3 Pratilipi (com.pratilipi.data.type.Pratilipi)3 LinkedList (java.util.LinkedList)3 AccessToken (com.pratilipi.data.type.AccessToken)2 Page (com.pratilipi.data.type.Page)2 UserAuthor (com.pratilipi.data.type.UserAuthor)2 UserPratilipiEntity (com.pratilipi.data.type.gae.UserPratilipiEntity)2 Task (com.pratilipi.taskqueue.Task)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Get (com.pratilipi.api.annotation.Get)1 GenericResponse (com.pratilipi.api.shared.GenericResponse)1