// find answers question
question = questionService.findAnswersQuestion(answer.getId());
answerService.loadAnswers(question);
userAnswer = question.lookupAnswerById(answer.getId());
Vote vote;
// find user vote if exist
if (userSession != null) {
// read vote from db, ignore request parameter
userVote = answerService.findUserVoteForQuestion(userSession.getUser(), question);
} else {
// read votes from param
if ((userVote != null) && (userVote.getId() != null)) {
userVote = answerService.findVoteById(userVote.getId());
if (userVote == null) {
log.info("user vote can't be found.");
}
} else {
userVote = null;
}
}
// update or save a vote
if (userVote != null) {
log.info("update vote {}", userVote.getId());
Answer currentAnswer = question.lookupAnswerById(userVote.getAnswerId());
if (currentAnswer.equals(userAnswer)) {
log.debug("vote equals to previous one.");
statsService.calcAnswerDistribution(question);
return OK;
}
vote = answerService.changeVoteAnswer(userVote, currentAnswer, userAnswer, requestRemoteAddr);
} else {
log.info("add new vote {}");
vote = answerService.voteForAnswer(userAnswer, requestRemoteAddr, userSession == null ? null : userSession.getUser());
}
userVoteId = vote.getId();
// recalc
statsService.calcAnswerDistribution(question);
// add cookie