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;
}
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);
}
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);
}
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());
}
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);
}
Aggregations