Search in sources :

Example 1 with Comment

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

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

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

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

the class AuditLogProcessApi method get.

@Get
public GenericResponse get(GenericRequest request) throws UnexpectedServerException {
    DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
    // Fetching AppProperty
    String appPropertyId = "Api.AuditLogProcess";
    AppProperty appProperty = dataAccessor.getAppProperty(appPropertyId);
    if (appProperty == null)
        appProperty = dataAccessor.newAppProperty(appPropertyId);
    // Fetching list of audit logs
    DataListCursorTuple<AuditLog> auditLogDataListCursorTuple = dataAccessor.getAuditLogList(// Tuesday, 1 August 2017 00:00:00 GMT+05:30
    new Date(1501525800000L), (String) appProperty.getValue(), 5000);
    // Make sets of PrimaryContent ids
    Map<Long, Set<Long>> pratilipiUpdateIds = new HashMap<>();
    Set<String> userPratilipiUpdateIds = new HashSet<>();
    Set<String> userAuthorUpdateIds = new HashSet<>();
    Map<String, UserAuthor> userAuthors = new HashMap<>();
    Set<Long> commentUpdateIds = new HashSet<>();
    Set<String> voteUpdateIds = new HashSet<>();
    Gson gson = new GsonBuilder().registerTypeAdapter(Date.class, new GsonLongDateAdapter()).create();
    for (AuditLog auditLog : auditLogDataListCursorTuple.getDataList()) {
        // TODO: Delete following condition as soon as 'legacy' module is removed
        if (auditLog.getUserId() == null || auditLog.getPrimaryContentId() == null) {
            continue;
        }
        if (auditLog.getUserId().equals(SystemProperty.SYSTEM_USER_ID)) {
            continue;
        }
        if (auditLog.getAccessType() == AccessType.PRATILIPI_UPDATE) {
            Pratilipi oldPratilipi = gson.fromJson(auditLog.getEventDataOld(), PratilipiEntity.class);
            Pratilipi newPratilipi = gson.fromJson(auditLog.getEventDataNew(), PratilipiEntity.class);
            if (oldPratilipi.getState() == PratilipiState.DRAFTED && newPratilipi.getState() == PratilipiState.PUBLISHED) {
                Set<Long> userIdSet = pratilipiUpdateIds.get(auditLog.getPrimaryContentIdLong());
                if (userIdSet == null) {
                    userIdSet = new HashSet<>();
                    pratilipiUpdateIds.put(auditLog.getPrimaryContentIdLong(), userIdSet);
                }
                userIdSet.add(auditLog.getUserId());
            }
        } else if (auditLog.getAccessType() == AccessType.USER_PRATILIPI_REVIEW) {
            UserPratilipi oldUserPratilipi = gson.fromJson(auditLog.getEventDataOld(), UserPratilipiEntity.class);
            UserPratilipi newUserPratilipi = gson.fromJson(auditLog.getEventDataNew(), UserPratilipiEntity.class);
            if (oldUserPratilipi.getRating() == null && oldUserPratilipi.getReview() == null && (newUserPratilipi.getRating() != null || newUserPratilipi.getReview() != null))
                userPratilipiUpdateIds.add(auditLog.getPrimaryContentId());
        } else if (auditLog.getAccessType() == AccessType.USER_AUTHOR_FOLLOWING) {
            UserAuthor oldUserAuthor = gson.fromJson(auditLog.getEventDataOld(), UserAuthorEntity.class);
            UserAuthor newUserAuthor = gson.fromJson(auditLog.getEventDataNew(), UserAuthorEntity.class);
            if (oldUserAuthor.getFollowState() == null && newUserAuthor.getFollowState() == UserFollowState.FOLLOWING) {
                userAuthorUpdateIds.add(auditLog.getPrimaryContentId());
                userAuthors.put(auditLog.getPrimaryContentId(), newUserAuthor);
            }
        } else if (auditLog.getAccessType() == AccessType.COMMENT_ADD) {
            commentUpdateIds.add(auditLog.getPrimaryContentIdLong());
        } else if (auditLog.getAccessType() == AccessType.VOTE) {
            Vote newVote = gson.fromJson(auditLog.getEventDataNew(), VoteEntity.class);
            if (newVote.getType() == VoteType.LIKE)
                voteUpdateIds.add(auditLog.getPrimaryContentId());
        }
    }
    // Batch get Vote entities
    logger.log(Level.INFO, "Fetching " + voteUpdateIds.size() + " Vote Entities.");
    Map<String, Vote> votes = dataAccessor.getVotes(voteUpdateIds);
    // Batch get Comment and entities
    Set<Long> commentIds = new HashSet<>(commentUpdateIds);
    for (Vote vote : votes.values()) if (vote.getParentType() == VoteParentType.COMMENT)
        commentIds.add(vote.getParentIdLong());
    logger.log(Level.INFO, "Fetching " + commentIds.size() + " Comment Entities.");
    Map<Long, Comment> comments = dataAccessor.getComments(commentIds);
    // Batch get UserPratilipi entities
    Set<String> userPratilipiIds = new HashSet<>(userPratilipiUpdateIds);
    for (Comment comment : comments.values()) if (comment.getParentType() == CommentParentType.REVIEW)
        userPratilipiIds.add(comment.getParentId());
    for (Vote vote : votes.values()) if (vote.getParentType() == VoteParentType.REVIEW)
        userPratilipiIds.add(vote.getParentId());
    logger.log(Level.INFO, "Fetching " + userPratilipiIds.size() + " UserPratilipi Entities.");
    Map<String, UserPratilipi> userPratilipis = dataAccessor.getUserPratilipis(userPratilipiIds);
    // Batch get Pratilipi entities
    Set<Long> pratilipiIds = new HashSet<>(pratilipiUpdateIds.keySet());
    for (UserPratilipi userPratilipi : userPratilipis.values()) pratilipiIds.add(userPratilipi.getPratilipiId());
    logger.log(Level.INFO, "Fetching " + pratilipiIds.size() + " Pratilipi Entities.");
    Map<Long, Pratilipi> pratilipis = dataAccessor.getPratilipis(pratilipiIds);
    // Batch get UserAuthor entities
    logger.log(Level.INFO, "Fetching " + userAuthorUpdateIds.size() + " UserAuthor Entities.");
    // Batch get Author entities
    Set<Long> authorIds = new HashSet<>();
    for (Pratilipi pratilipi : pratilipis.values()) authorIds.add(pratilipi.getAuthorId());
    for (UserAuthor userAuthor : userAuthors.values()) authorIds.add(userAuthor.getAuthorId());
    logger.log(Level.INFO, "Fetching " + authorIds.size() + " Author Entities.");
    Map<Long, Author> authors = dataAccessor.getAuthors(authorIds);
    List<Email> totalEmailList = new ArrayList<>();
    // Get userIds of followers for authors who had published
    List<Long> pratilipiUpdateAuthorIds = new ArrayList<>();
    for (Long pratilipiId : pratilipiUpdateIds.keySet()) pratilipiUpdateAuthorIds.add(pratilipis.get(pratilipiId).getAuthorId());
    Map<Long, List<Long>> authorsFollowersMap = _getUserAuthorFollowList(pratilipiUpdateAuthorIds);
    // auditLog.getAccessType() == AccessType.PRATILIPI_UPDATE
    for (Long pratilipiId : pratilipiUpdateIds.keySet()) {
        Pratilipi pratilipi = pratilipis.get(pratilipiId);
        Set<Long> followerUserIdList = new HashSet<>(authorsFollowersMap.get(pratilipi.getAuthorId()));
        Email email = _createPratilipiPublishedEmail(pratilipi, authors.get(pratilipi.getAuthorId()));
        if (email != null)
            totalEmailList.add(email);
        totalEmailList.addAll(_createPratilipiPublishedEmails(pratilipi, followerUserIdList));
        // Send notification to all AEEs as well
        // only if the content is self-published
        List<Long> aeeUserIdList = _getAeeUserIdList(pratilipi.getLanguage());
        Set<Long> userIdSet = pratilipiUpdateIds.get(pratilipiId);
        for (Long userId : userIdSet) {
            if (!aeeUserIdList.contains(userId)) {
                followerUserIdList.addAll(aeeUserIdList);
                break;
            }
        }
        _createPratilipiPublishedNotification(pratilipi, authors.get(pratilipi.getAuthorId()));
        _createPratilipiPublishedNotifications(pratilipi, followerUserIdList);
    }
    // auditLog.getAccessType() == AccessType.USER_PRATILIPI_REVIEW
    for (String userPratilipiId : userPratilipiUpdateIds) {
        UserPratilipi userPratilipi = userPratilipis.get(userPratilipiId);
        if (userPratilipi.getReviewState() != UserReviewState.PUBLISHED)
            continue;
        Long pratilipiId = userPratilipi.getPratilipiId();
        Email email = _createUserPratilipiReviewEmail(userPratilipi, authors.get(pratilipis.get(pratilipiId).getAuthorId()));
        if (email != null)
            totalEmailList.add(email);
    }
    // auditLog.getAccessType() == AccessType.USER_AUTHOR_FOLLOWING
    for (String userAuthorId : userAuthorUpdateIds) {
        UserAuthor userAuthor = userAuthors.get(userAuthorId);
        _createUserAuthorFollowingNotifications(userAuthor, authors.get(userAuthor.getAuthorId()));
        Email email = _createUserAuthorFollowingEmail(userAuthor, authors.get(userAuthor.getAuthorId()));
        if (email != null)
            totalEmailList.add(email);
    }
    // auditLog.getAccessType() == AccessType.COMMENT_ADD
    for (Long commentId : commentUpdateIds) {
        Comment comment = comments.get(commentId);
        if (comment.getParentType() != CommentParentType.REVIEW)
            continue;
        UserPratilipi userPratilipi = userPratilipis.get(comment.getParentId());
        Email email = _createCommentAddedReviewerEmail(userPratilipi, comment);
        if (email != null)
            totalEmailList.add(email);
    /*	// Business call - Not to send CommentAddedAuthorEmail.
			Pratilipi pratilipi = pratilipis.get( userPratilipi.getPratilipiId() );
			Author author = authors.get( pratilipi.getAuthorId() );
			email = _createCommentAddedAuthorEmail( author, comment );
			if( email != null )
				totalEmailList.add( email );
			*/
    }
    // auditLog.getAccessType() == AccessType.VOTE
    for (String voteId : voteUpdateIds) {
        Vote vote = votes.get(voteId);
        if (vote.getParentType() == VoteParentType.REVIEW) {
            UserPratilipi userPratilipi = userPratilipis.get(vote.getParentId());
            // To the reviewer
            Email email = _createVoteOnReviewReviewerEmail(userPratilipi, vote);
            if (email != null)
                totalEmailList.add(email);
        } else if (vote.getParentType() == VoteParentType.COMMENT) {
            // To the commentor
            Email email = _createVoteOnCommentCommentorEmail(comments.get(vote.getParentIdLong()), vote);
            if (email != null)
                totalEmailList.add(email);
        }
    }
    _updateEmailTable(totalEmailList);
    // Updating AppProperty.
    if (auditLogDataListCursorTuple.getDataList().size() > 0) {
        appProperty.setValue(auditLogDataListCursorTuple.getCursor());
        appProperty = dataAccessor.createOrUpdateAppProperty(appProperty);
    }
    return new GenericResponse();
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) Email(com.pratilipi.data.type.Email) HashMap(java.util.HashMap) DataAccessor(com.pratilipi.data.DataAccessor) ArrayList(java.util.ArrayList) AuditLog(com.pratilipi.data.type.AuditLog) UserPratilipi(com.pratilipi.data.type.UserPratilipi) UserAuthor(com.pratilipi.data.type.UserAuthor) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) HashSet(java.util.HashSet) Comment(com.pratilipi.data.type.Comment) Vote(com.pratilipi.data.type.Vote) GenericResponse(com.pratilipi.api.shared.GenericResponse) Date(java.util.Date) UserAuthor(com.pratilipi.data.type.UserAuthor) Author(com.pratilipi.data.type.Author) UserPratilipi(com.pratilipi.data.type.UserPratilipi) Pratilipi(com.pratilipi.data.type.Pratilipi) AppProperty(com.pratilipi.data.type.AppProperty) GsonLongDateAdapter(com.pratilipi.common.util.GsonLongDateAdapter) UserPratilipiEntity(com.pratilipi.data.type.gae.UserPratilipiEntity) Get(com.pratilipi.api.annotation.Get)

Example 5 with Comment

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

the class DataStoreCleanupUtil method delete.

public static void delete(User user, boolean preview) {
    System.out.println();
    System.out.println("User id: " + user.getId() + ", state: " + user.getState() + ", signUpDate:" + user.getSignUpDate() + ", signUpSource:" + user.getSignUpSource());
    if (!preview && user.getState() != UserState.DELETED && user.getState() != UserState.BLOCKED) {
        user.setState(UserState.DELETED);
        // Save
        ObjectifyService.ofy().save().entity(user).now();
    }
    // ACCESS_TOKEN Table
    List<AccessTokenEntity> accessTokenList = ObjectifyService.ofy().load().type(AccessTokenEntity.class).filter("USER_ID", user.getId()).filter("EXPIRY >", new Date()).list();
    System.out.println("AccessTokenEntity # " + accessTokenList.size());
    if (!preview) {
        for (AccessToken accessToken : accessTokenList) {
            accessToken.setExpiry(new Date());
            // Save
            ObjectifyService.ofy().save().entity(accessToken).now();
        }
    }
    // USER_PRATILIPI Table
    List<UserPratilipiEntity> userPratilipiList = ObjectifyService.ofy().load().type(UserPratilipiEntity.class).filter("USER_ID", user.getId()).list();
    System.out.println("UserPratilipiEntity # " + userPratilipiList.size());
    int reviewCount = 0;
    int ratingCount = 0;
    for (UserPratilipi userPratilipi : userPratilipiList) {
        if (userPratilipi.getReview() != null && userPratilipi.getReviewState() != UserReviewState.DELETED && userPratilipi.getReviewState() != UserReviewState.BLOCKED)
            reviewCount++;
        if (userPratilipi.getRating() != null && userPratilipi.getRating() != 0)
            ratingCount++;
    }
    System.out.println("Review ## " + reviewCount);
    System.out.println("Rating ## " + ratingCount);
    if (!preview) {
        for (UserPratilipi userPratilipi : userPratilipiList) {
            boolean save = false;
            if (userPratilipi.getReviewState() != UserReviewState.DELETED && userPratilipi.getReviewState() != UserReviewState.BLOCKED) {
                userPratilipi.setReviewState(UserReviewState.DELETED);
                save = true;
            }
            if (userPratilipi.getRating() != null && userPratilipi.getRating() != 0) {
                userPratilipi.setRating(null);
                save = true;
            }
            if (save) {
                // Save
                ObjectifyService.ofy().save().entity(userPratilipi).now();
                Task task = TaskQueueFactory.newTask().setUrl("/pratilipi/process").addParam("pratilipiId", userPratilipi.getPratilipiId().toString()).addParam("updateReviewsDoc", "true").addParam("updateUserPratilipiStats", "true");
                TaskQueueFactory.getPratilipiTaskQueue().add(task);
            }
        }
    }
    // USER_AUTHOR Table
    List<UserAuthorEntity> userAuthorList = ObjectifyService.ofy().load().type(UserAuthorEntity.class).filter("USER_ID", user.getId()).list();
    System.out.println("UserAuthorEntity # " + userAuthorList.size());
    int followCount = 0;
    for (UserAuthor userAuthor : userAuthorList) if (userAuthor.getFollowState() == UserFollowState.FOLLOWING)
        followCount++;
    System.out.println("Follow ## " + followCount);
    if (!preview) {
        for (UserAuthor userAuthor : userAuthorList) {
            if (userAuthor.getFollowState() != null) {
                userAuthor.setFollowState(null);
                // Save
                ObjectifyService.ofy().save().entity(userAuthor).now();
                Task task = TaskQueueFactory.newTask().setUrl("/author/process").addParam("authorId", userAuthor.getAuthorId().toString()).addParam("updateUserAuthorStats", "true");
                TaskQueueFactory.getUserAuthorTaskQueue().add(task);
            }
        }
        Task task = TaskQueueFactory.newTask().setUrl("/user/process").addParam("userId", user.getId().toString()).addParam("updateFollowsDoc", "true").addParam("updateUserAuthorStats", "true");
        TaskQueueFactory.getUserAuthorTaskQueue().add(task);
    }
    // COMMENT Table
    List<CommentEntity> commentList = ObjectifyService.ofy().load().type(CommentEntity.class).filter("USER_ID", user.getId()).list();
    System.out.println("CommentEntity # " + commentList.size());
    int commentCount = 0;
    for (Comment comment : commentList) if (comment.getState() == CommentState.ACTIVE)
        commentCount++;
    System.out.println("Comment ## " + commentCount);
    if (!preview) {
        for (Comment comment : commentList) {
            if (comment.getState() == CommentState.ACTIVE) {
                comment.setState(CommentState.DELETED);
                // Save
                ObjectifyService.ofy().save().entity(comment).now();
                if (comment.getReferenceType() == ReferenceType.PRATILIPI) {
                    Task task = TaskQueueFactory.newTask().setUrl("/pratilipi/process").addParam("pratilipiId", comment.getReferenceId().toString()).addParam("updateReviewsDoc", "true").addParam("updateUserPratilipiStats", "true");
                    TaskQueueFactory.getPratilipiTaskQueue().add(task);
                }
            }
        }
    }
    // VOTE Table
    List<VoteEntity> voteList = ObjectifyService.ofy().load().type(VoteEntity.class).filter("USER_ID", user.getId()).list();
    System.out.println("VoteEntity # " + voteList.size());
    int voteCount = 0;
    for (Vote vote : voteList) if (vote.getType() != VoteType.NONE)
        voteCount++;
    System.out.println("Vote ## " + voteCount);
    if (!preview) {
        for (Vote vote : voteList) {
            // Delete
            ObjectifyService.ofy().delete().entity(vote).now();
            if (vote.getReferenceType() == ReferenceType.PRATILIPI) {
                Task task = TaskQueueFactory.newTask().setUrl("/pratilipi/process").addParam("pratilipiId", vote.getReferenceId().toString()).addParam("updateReviewsDoc", "true").addParam("updateUserPratilipiStats", "true");
                TaskQueueFactory.getPratilipiTaskQueue().add(task);
            }
        }
    }
    // AUTHOR Table
    List<AuthorEntity> authorList = ObjectifyService.ofy().load().type(AuthorEntity.class).filter("USER_ID", user.getId()).list();
    System.out.println();
    System.out.println("AuthorEntity # " + authorList.size());
    if (authorList.size() == 0)
        return;
    for (Author author : authorList) delete(author, preview);
}
Also used : UserAuthorEntity(com.pratilipi.data.type.gae.UserAuthorEntity) Comment(com.pratilipi.data.type.Comment) Task(com.pratilipi.taskqueue.Task) Vote(com.pratilipi.data.type.Vote) AccessTokenEntity(com.pratilipi.data.type.gae.AccessTokenEntity) Date(java.util.Date) UserAuthorEntity(com.pratilipi.data.type.gae.UserAuthorEntity) AuthorEntity(com.pratilipi.data.type.gae.AuthorEntity) CommentEntity(com.pratilipi.data.type.gae.CommentEntity) UserPratilipi(com.pratilipi.data.type.UserPratilipi) VoteEntity(com.pratilipi.data.type.gae.VoteEntity) AccessToken(com.pratilipi.data.type.AccessToken) UserAuthor(com.pratilipi.data.type.UserAuthor) UserAuthor(com.pratilipi.data.type.UserAuthor) Author(com.pratilipi.data.type.Author) UserPratilipiEntity(com.pratilipi.data.type.gae.UserPratilipiEntity)

Aggregations

Comment (com.pratilipi.data.type.Comment)5 UserPratilipi (com.pratilipi.data.type.UserPratilipi)5 DataAccessor (com.pratilipi.data.DataAccessor)4 Vote (com.pratilipi.data.type.Vote)4 Date (java.util.Date)4 AuditLog (com.pratilipi.data.type.AuditLog)3 InsufficientAccessException (com.pratilipi.common.exception.InsufficientAccessException)2 Author (com.pratilipi.data.type.Author)2 UserAuthor (com.pratilipi.data.type.UserAuthor)2 UserPratilipiEntity (com.pratilipi.data.type.gae.UserPratilipiEntity)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 LinkedList (java.util.LinkedList)2 List (java.util.List)2 Get (com.pratilipi.api.annotation.Get)1 GenericResponse (com.pratilipi.api.shared.GenericResponse)1 GsonLongDateAdapter (com.pratilipi.common.util.GsonLongDateAdapter)1 DocAccessor (com.pratilipi.data.DocAccessor)1 UserData (com.pratilipi.data.client.UserData)1 AccessToken (com.pratilipi.data.type.AccessToken)1