This is an automated email from the git hooks/post-receive script. New commit to branch feature/april in repository pollen. See https://gitlab.nuiton.org/chorem/pollen.git commit 662dccaeed3ef827831a888e4f51092dddd2803d Author: jcouteau <couteau@codelutin.com> Date: Tue Oct 2 15:09:07 2018 +0200 refs #221 - Vote associatif - VoteService should be ok --- .../pollen/services/service/VoteService.java | 148 +++++++++------------ 1 file changed, 66 insertions(+), 82 deletions(-) diff --git a/pollen-services/src/main/java/org/chorem/pollen/services/service/VoteService.java b/pollen-services/src/main/java/org/chorem/pollen/services/service/VoteService.java index 0f780f74..bfb73bcc 100644 --- a/pollen-services/src/main/java/org/chorem/pollen/services/service/VoteService.java +++ b/pollen-services/src/main/java/org/chorem/pollen/services/service/VoteService.java @@ -22,20 +22,7 @@ package org.chorem.pollen.services.service; */ import org.apache.commons.collections4.CollectionUtils; -import org.chorem.pollen.persistence.entity.Choice; -import org.chorem.pollen.persistence.entity.Poll; -import org.chorem.pollen.persistence.entity.PollType; -import org.chorem.pollen.persistence.entity.PollenPrincipal; -import org.chorem.pollen.persistence.entity.PollenPrincipalTopiaDao; -import org.chorem.pollen.persistence.entity.PollenUser; -import org.chorem.pollen.persistence.entity.Polls; -import org.chorem.pollen.persistence.entity.Vote; -import org.chorem.pollen.persistence.entity.VoteToChoice; -import org.chorem.pollen.persistence.entity.VoteToChoiceTopiaDao; -import org.chorem.pollen.persistence.entity.VoteToChoices; -import org.chorem.pollen.persistence.entity.VoteTopiaDao; -import org.chorem.pollen.persistence.entity.VoterList; -import org.chorem.pollen.persistence.entity.VoterListMember; +import org.chorem.pollen.persistence.entity.*; import org.chorem.pollen.services.bean.ChoiceBean; import org.chorem.pollen.services.bean.PaginationParameterBean; import org.chorem.pollen.services.bean.PaginationResultBean; @@ -116,7 +103,7 @@ public class VoteService extends PollenServiceSupport { bean.setVoterAvatar(null); } - int maxVoters = getPollService().getMaxVoters(entity.getPoll()); + int maxVoters = getPollService().getMaxVoters(entity.getQuestion().getPoll()); if (maxVoters > 0 && getVoteDao().getVoteIndex(entity) >= maxVoters) { bean.setIgnored(true); bean.getChoice().forEach(vc -> vc.setVoteValue(null)); @@ -140,11 +127,11 @@ public class VoteService extends PollenServiceSupport { return bean; } - public VoteBean getNewVote(String pollId) { + public VoteBean getNewVote(String questionId) { checkIsConnectedRequired(); - Poll poll = getPollService().getPoll0(pollId); - checkPermission(PollenPermissions.addVote(poll)); + Question question = getQuestionService().getQuestion0(questionId); + checkPermission(PollenPermissions.addVote(question)); VoteBean voteBean = new VoteBean(); voteBean.setWeight(1d); @@ -152,15 +139,15 @@ public class VoteService extends PollenServiceSupport { PollenPrincipal mainPrincipal = getSecurityContext().getMainPrincipal(); PollenUser connectedUser = getConnectedUser(); - if (poll.getPollType() == PollType.RESTRICTED) { + if (question.getPoll().getPollType() == PollType.RESTRICTED) { // si si le mainPrincipal a voté List<VoterListMember> voterListMembers = getVoterListMemberDao() .forEquals(VoterListMember.PROPERTY_MEMBER + "." + PollenPrincipal.PROPERTY_EMAIL, mainPrincipal.getEmail()) - .addEquals(VoterListMember.PROPERTY_VOTER_LIST + "." + VoterList.PROPERTY_POLL + "." + Poll.PROPERTY_TOPIA_ID, pollId) + .addEquals(VoterListMember.PROPERTY_VOTER_LIST + "." + VoterList.PROPERTY_POLL + "." + Poll.PROPERTY_TOPIA_ID, question.getPoll().getTopiaId()) .findAll(); - if (CollectionUtils.isNotEmpty(voterListMembers) && !getVoteDao().forPollEquals(poll).addEquals(Vote.PROPERTY_VOTER, mainPrincipal).exists()) { + if (CollectionUtils.isNotEmpty(voterListMembers) && !getVoteDao().forQuestionEquals(question).addEquals(Vote.PROPERTY_VOTER, mainPrincipal).exists()) { voteBean.setVoterName(mainPrincipal.getName()); voteBean.setVoterListMembers(voterListMembers); } else if (connectedUser != null) { @@ -175,7 +162,7 @@ public class VoteService extends PollenServiceSupport { List<VoterListMember> voterListMembers = getVoterListMemberDao() .forEquals(VoterListMember.PROPERTY_MEMBER + "." + PollenPrincipal.PROPERTY_EMAIL, mainPrincipal.getEmail()) - .addEquals(VoterListMember.PROPERTY_VOTER_LIST + "." + VoterList.PROPERTY_POLL + "." + Poll.PROPERTY_TOPIA_ID, pollId) + .addEquals(VoterListMember.PROPERTY_VOTER_LIST + "." + VoterList.PROPERTY_POLL + "." + Poll.PROPERTY_TOPIA_ID, question.getPoll().getTopiaId()) .findAll(); if (voterListMembers != null) { voteBean.setVoterListMembers(voterListMembers); @@ -193,13 +180,13 @@ public class VoteService extends PollenServiceSupport { } - public PaginationResultBean<VoteBean> getVotes(String pollId, PaginationParameterBean paginationParameter) { + public PaginationResultBean<VoteBean> getVotes(String questionId, PaginationParameterBean paginationParameter) { checkIsConnectedRequired(); - checkNotNull(pollId); + checkNotNull(questionId); - Poll poll = getPollService().getPoll0(pollId); - List<Vote> votes = getVotes0(poll).stream() + Question question = getQuestionService().getQuestion0(questionId); + List<Vote> votes = getVotes0(question).stream() .filter(vote -> isPermitted(PollenPermissions.read(vote))) .collect(Collectors.toList()); @@ -209,47 +196,48 @@ public class VoteService extends PollenServiceSupport { } - public VoteBean getVote(String pollId, String voteId) { + public VoteBean getVote(String questionId, String voteId) { checkIsConnectedRequired(); checkNotNull(voteId); - Poll poll = getPollService().getPoll0(pollId); + Question question = getQuestionService().getQuestion0(questionId); - Vote vote = getVote(poll, voteId); + Vote vote = getVote(question, voteId); checkPermission(PollenPermissions.read(vote)); return toVoteBean(vote); } - public PollenEntityRef<Vote> addVote(String pollId, VoteBean vote) throws InvalidFormException { + public PollenEntityRef<Vote> addVote(String questionId, VoteBean vote) throws InvalidFormException { checkIsConnectedRequired(); - checkNotNull(pollId); + checkNotNull(questionId); checkNotNull(vote); checkIsNotPersisted(vote); - Poll poll = getPollService().getPoll0(pollId); - checkPermission(PollenPermissions.addVote(poll)); + Question question = getQuestionService().getQuestion0(questionId); - List<ChoiceBean> choices = getChoiceService().getChoices(pollId); + checkPermission(PollenPermissions.addVote(question)); - ErrorMap errorMap = checkVote(poll, vote, choices); + List<ChoiceBean> choices = getChoiceService().getChoices(questionId); + + ErrorMap errorMap = checkVote(question, vote, choices); errorMap.failIfNotEmpty(); - Vote result = saveVote(poll, vote); + Vote result = saveVote(question, vote); commit(); - getNotificationService().onVoteAdded(poll, result); - getFeedService().onVoteAdded(poll, result); + getNotificationService().onVoteAdded(question, result); + getFeedService().onVoteAdded(question, result); - int maxVoters = getPollService().getMaxVoters(poll); - if (maxVoters > 0 && getVoteCount(poll) > maxVoters) { - boolean notificationSend = getPollDao().setFlagNotificationMaxVoterSend(poll); + int maxVoters = getPollService().getMaxVoters(question.getPoll()); + if (maxVoters > 0 && getVoteCount(question) > maxVoters) { + boolean notificationSend = getPollDao().setFlagNotificationMaxVoterSend(question.getPoll()); if (notificationSend) { commit(); - getNotificationService().onExceedingMaxVoters(poll, maxVoters); + getNotificationService().onExceedingMaxVoters(question.getPoll(), maxVoters); } } @@ -257,49 +245,49 @@ public class VoteService extends PollenServiceSupport { } - public VoteBean editVote(String pollId, VoteBean vote) throws InvalidFormException { + public VoteBean editVote(String questionId, VoteBean vote) throws InvalidFormException { checkIsConnectedRequired(); checkNotNull(vote); checkIsPersisted(vote); - Poll poll = getPollService().getPoll0(pollId); - Vote voteBd = getVote(poll, vote.getEntityId()); + Question question = getQuestionService().getQuestion0(questionId); + Vote voteBd = getVote(question, vote.getEntityId()); checkPermission(PollenPermissions.edit(voteBd)); - List<ChoiceBean> choices = getChoiceService().getChoices(pollId); + List<ChoiceBean> choices = getChoiceService().getChoices(questionId); - ErrorMap errorMap = checkVote(poll, vote, choices); + ErrorMap errorMap = checkVote(question, vote, choices); errorMap.failIfNotEmpty(); - Vote result = saveVote(poll, vote); + Vote result = saveVote(question, vote); commit(); - getNotificationService().onVoteEdited(poll, result); - getFeedService().onVoteEdited(poll, result); + getNotificationService().onVoteEdited(question, result); + getFeedService().onVoteEdited(question, result); return toVoteBean(result); } - public void deleteVote(String pollId, String voteId) throws InvalidFormException { + public void deleteVote(String questionId, String voteId) throws InvalidFormException { checkIsConnectedRequired(); - checkNotNull(pollId); + checkNotNull(questionId); checkNotNull(voteId); - Poll poll = getPollService().getPoll0(pollId); - Vote vote = getVote(poll, voteId); + Question question = getQuestionService().getQuestion0(questionId); + Vote vote = getVote(question, voteId); checkPermission(PollenPermissions.delete(vote)); - ErrorMap errorMap = checkPoll(poll); + ErrorMap errorMap = checkPoll(question.getPoll()); errorMap.failIfNotEmpty(); getVoteDao().delete(vote); commit(); - getNotificationService().onVoteDeleted(poll, vote); - getFeedService().onVoteDeleted(poll, vote); + getNotificationService().onVoteDeleted(question, vote); + getFeedService().onVoteDeleted(question, vote); } @@ -315,19 +303,19 @@ public class VoteService extends PollenServiceSupport { return errors; } - protected <C extends VoteCountingConfig> ErrorMap checkVote(Poll poll, VoteBean vote, List<ChoiceBean> choices) { + protected <C extends VoteCountingConfig> ErrorMap checkVote(Question question, VoteBean vote, List<ChoiceBean> choices) { ErrorMap errors = new ErrorMap(); Set<String> voterNames = new HashSet<>(); - errors.addAllErrors(checkPoll(poll)); + errors.addAllErrors(checkPoll(question.getPoll())); boolean voteExists = vote.isPersisted(); boolean voteNameNotBlank = checkNotBlank(errors, "voter.name", vote.getVoterName(), l(getLocale(), "pollen.error.vote.voterName.mandatory")); if (voteNameNotBlank) { - List<Vote> existingVote = getVoteDao().findAll(poll); + List<Vote> existingVote = getVoteDao().findAll(question); if (CollectionUtils.isNotEmpty(existingVote)) { @@ -349,8 +337,8 @@ public class VoteService extends PollenServiceSupport { } - VoteCounting<?, C> voteCounting = getVoteCountingService().getVoteCounting(poll); - C config = getVoteCountingService().getVoteCountingConfig(poll); + VoteCounting<?, C> voteCounting = getVoteCountingService().getVoteCounting(question); + C config = getVoteCountingService().getVoteCountingConfig(question); ErrorMap valueErrors = getVoteCountingService().checkVote(vote, voteCounting, choices, config); @@ -360,7 +348,7 @@ public class VoteService extends PollenServiceSupport { } - protected Vote saveVote(Poll poll, VoteBean vote) { + protected Vote saveVote(Question question, VoteBean vote) { boolean voteExist = vote.isPersisted(); @@ -369,24 +357,23 @@ public class VoteService extends PollenServiceSupport { //TODO Finish save if (voteExist) { - toSave = getVote(poll, vote.getEntityId()); + toSave = getVote(question, vote.getEntityId()); } else { toSave = getVoteDao().create(); -// toSave.setPostDate(serviceContext.getNow()); PollenPrincipal mainPrincipal = getSecurityContext().getMainPrincipal(); - if (Polls.isPollRestricted(poll)) { + if (Polls.isPollRestricted(question.getPoll())) { // si si le mainPrincipal a voté List<VoterListMember> voterListMembers = getVoterListMemberDao() .forEquals(VoterListMember.PROPERTY_MEMBER + "." + PollenPrincipal.PROPERTY_EMAIL, mainPrincipal.getEmail()) - .addEquals(VoterListMember.PROPERTY_VOTER_LIST + "." + VoterList.PROPERTY_POLL, poll) + .addEquals(VoterListMember.PROPERTY_VOTER_LIST + "." + VoterList.PROPERTY_POLL, question.getPoll().getTopiaId()) .findAll(); - if (CollectionUtils.isNotEmpty(voterListMembers) && !getVoteDao().forPollEquals(poll).addEquals(Vote.PROPERTY_VOTER, mainPrincipal).exists()) { + if (CollectionUtils.isNotEmpty(voterListMembers) && !getVoteDao().forQuestionEquals(question).addEquals(Vote.PROPERTY_VOTER, mainPrincipal).exists()) { // vote pour le mainPrincipal toSave.setVoterListMember(voterListMembers); toSave.setVoter(mainPrincipal); @@ -394,7 +381,7 @@ public class VoteService extends PollenServiceSupport { // vote pour le connectedUser voterListMembers = getVoterListMemberDao() .forEquals(VoterListMember.PROPERTY_MEMBER + "." + PollenPrincipal.PROPERTY_POLLEN_USER, getConnectedUser()) - .addEquals(VoterListMember.PROPERTY_VOTER_LIST + "." + VoterList.PROPERTY_POLL, poll) + .addEquals(VoterListMember.PROPERTY_VOTER_LIST + "." + VoterList.PROPERTY_POLL, question.getPoll().getTopiaId()) .findAll(); toSave.setVoterListMember(voterListMembers); toSave.setVoter(voterListMembers.get(0).getMember()); @@ -404,7 +391,7 @@ public class VoteService extends PollenServiceSupport { if (mainPrincipal != null) { List<VoterListMember> voterListMembers = getVoterListMemberDao().forMemberEquals(mainPrincipal) - .addEquals(VoterListMember.PROPERTY_VOTER_LIST + "." + VoterList.PROPERTY_POLL, poll) + .addEquals(VoterListMember.PROPERTY_VOTER_LIST + "." + VoterList.PROPERTY_POLL, question.getPoll().getTopiaId()) .findAll(); if (!voterListMembers.isEmpty()) { toSave.setVoterListMember(voterListMembers); @@ -423,17 +410,14 @@ public class VoteService extends PollenServiceSupport { } } - toSave.setPoll(poll); + toSave.setQuestion(question); } - toSave.setAnonymous(poll.isAnonymousVoteAllowed()); + toSave.setAnonymous(question.getPoll().isAnonymousVoteAllowed()); // -- author -- // toSave.getVoter().setName(vote.getVoterName()); -// if (StringUtils.isNotBlank(vote.getVoterEmail())) { -// toSave.getVoter().setEmail(getCleanMail(vote.getVoter().getEmail())); -// } PollenUser connectedUser = getConnectedUser(); @@ -479,13 +463,13 @@ public class VoteService extends PollenServiceSupport { } - protected Vote getVote(Poll poll, String voteId) { + protected Vote getVote(Question question, String voteId) { Vote result = getVoteDao().forTopiaIdEquals(voteId).findUnique(); - if (!poll.equals(result.getPoll())) { + if (!question.equals(result.getQuestion())) { - throw new InvalidEntityLinkException(Vote.PROPERTY_POLL, result, poll); + throw new InvalidEntityLinkException(Vote.PROPERTY_QUESTION, result, question); } @@ -493,9 +477,9 @@ public class VoteService extends PollenServiceSupport { } - protected List<Vote> getVotes0(Poll poll) { + protected List<Vote> getVotes0(Question question) { - return getVoteDao().findAll(poll); + return getVoteDao().findAll(question); } @@ -536,10 +520,10 @@ public class VoteService extends PollenServiceSupport { } - public long getVoteCount(Poll poll) { + public long getVoteCount(Question question) { checkIsConnectedRequired(); - return getVoteDao().forPollEquals(poll).count(); + return getVoteDao().forQuestionEquals(question).count(); } public void purgeOldVotes() { -- To stop receiving notification emails like this one, please contact chorem.org SCM administrator <admin+scm@chorem.org>.