public static PostVote getDefaultPostVote() { return new PostVote(getDefaultUser()); }
/** * Adds new vote to the post or overrides existent * * @param vote vote to add or override */ public void putVote(PostVote vote) { vote.setPost(this); for (PostVote storedVote : votes) { if (storedVote.getUser().equals(vote.getUser()) && storedVote.getPost().equals(vote.getPost())) { storedVote.setVotedUp(vote.isVotedUp()); storedVote.setVoteDate(vote.getVoteDate()); return; } } votes.add(vote); }
/** * Determines if user voted down for the post * * @param user user to check * * @return true if user voted down for the post * false if user voted up for the post * false if user not voted for the post */ public boolean isVotedDownBy(JCUser user) { for (PostVote vote : votes) { if (vote.getUser().equals(user)) { return !vote.isVotedUp(); } } return false; }
/** * {@inheritDoc} */ @Override @PreAuthorize("hasPermission(#post.topic.branch.id, 'BRANCH', 'BranchPermission.CREATE_POSTS') " + "and #post.userCreated.username != principal.username") public Post vote(Post post, PostVote vote) { JCUser currentUser = userService.getCurrentUser(); if (!post.canBeVotedBy(currentUser, vote.isVotedUp())) { logger.info("User [{}] tries to vote for post with id={} in same direction more than one time", currentUser.getUsername(), post.getId()); throw new AccessDeniedException("User can't vote in same direction more than one time"); } vote.setUser(currentUser); int ratingChanges = post.calculateRatingChanges(vote); post.putVote(vote); getDao().saveOrUpdate(post); getDao().changeRating(post.getId(), ratingChanges); return post; }
/** * Determines if user voted up for the post * * @param user user to check * * @return true if user voted up for the post * false if user voted down for the post * false if user not voted for the post */ public boolean isVotedUpBy(JCUser user) { for (PostVote vote : votes) { if (vote.getUser().equals(user)) { return vote.isVotedUp(); } } return false; }
/** * Calculates changes in rating of the posh which will be made by specified vote * * @param vote vote for calculating changes; * * @return 0 if user can't vote in direction specified by vote * +/- 1 if user votes first time in up/down direction * +/- 2 if user changes his vote from negative to positive/from positive to negative */ public int calculateRatingChanges(PostVote vote) { if (canBeVotedBy(vote.getUser(), vote.isVotedUp())) { if (isVotedDownBy(vote.getUser())) { return 2; } else if (isVotedUpBy(vote.getUser())) { return -2; } else { return vote.isVotedUp() ? 1 : -1; } } return 0; }
public static PostVote getDefaultPostVote() { return new PostVote(getDefaultUser()); }
/** * Votes up for post with specified id * * @param postId id of a post to vote up * @param request HttpServletRequest * * @return response in JSON format * * @throws NotFoundException if post with specified id not found */ @RequestMapping(method = RequestMethod.GET, value = "/posts/{postId}/voteup") @ResponseBody public JsonResponse voteUp(@PathVariable Long postId, HttpServletRequest request) throws NotFoundException { PostVote vote = new PostVote(true); voteWithSessionLocking(postId, vote, request); return new JsonResponse(JsonResponseStatus.SUCCESS); }
/** * Votes down for post with specified id * * @param postId id of a post to vote down * @param request HttpServletRequest * * @return response in JSON format * * @throws NotFoundException if post with specified id not found */ @RequestMapping(method = RequestMethod.GET, value = "/posts/{postId}/votedown") @ResponseBody public JsonResponse voteDown(@PathVariable Long postId, HttpServletRequest request) throws NotFoundException { PostVote vote = new PostVote(false); voteWithSessionLocking(postId, vote, request); return new JsonResponse(JsonResponseStatus.SUCCESS); }