r3208 - in branches/pollen-1.2.6-struts2: pollen-services/src/main/java/org/chorem/pollen/bean pollen-services/src/main/java/org/chorem/pollen/services/impl pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/poll pollen-ui-struts2/src/main/webapp/WEB-INF/jsp/poll
Author: fdesbois Date: 2012-03-26 19:02:51 +0200 (Mon, 26 Mar 2012) New Revision: 3208 Url: http://chorem.org/repositories/revision/pollen/3208 Log: - Delete vote - Add method for updateVoteUrl Modified: branches/pollen-1.2.6-struts2/pollen-services/src/main/java/org/chorem/pollen/bean/PollUri.java branches/pollen-1.2.6-struts2/pollen-services/src/main/java/org/chorem/pollen/services/impl/PollService.java branches/pollen-1.2.6-struts2/pollen-services/src/main/java/org/chorem/pollen/services/impl/VoteService.java branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/poll/DeleteVote.java branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/webapp/WEB-INF/jsp/poll/confirmDeletePollVote.jsp Modified: branches/pollen-1.2.6-struts2/pollen-services/src/main/java/org/chorem/pollen/bean/PollUri.java =================================================================== --- branches/pollen-1.2.6-struts2/pollen-services/src/main/java/org/chorem/pollen/bean/PollUri.java 2012-03-23 21:32:36 UTC (rev 3207) +++ branches/pollen-1.2.6-struts2/pollen-services/src/main/java/org/chorem/pollen/bean/PollUri.java 2012-03-26 17:02:51 UTC (rev 3208) @@ -23,6 +23,7 @@ */ package org.chorem.pollen.bean; +import com.google.common.base.Preconditions; import org.apache.commons.lang3.StringUtils; /** @@ -47,10 +48,22 @@ return result; } + public static PollUri newPollUri(String pollId, String accountId) { + Preconditions.checkNotNull(pollId); + + PollUri result = new PollUri(); + + result.pollId = pollId; + result.accountId = accountId; + return result; + } + protected String pollId; protected String accountId; + private PollUri() { } + public String getPollId() { return pollId; } Modified: branches/pollen-1.2.6-struts2/pollen-services/src/main/java/org/chorem/pollen/services/impl/PollService.java =================================================================== --- branches/pollen-1.2.6-struts2/pollen-services/src/main/java/org/chorem/pollen/services/impl/PollService.java 2012-03-23 21:32:36 UTC (rev 3207) +++ branches/pollen-1.2.6-struts2/pollen-services/src/main/java/org/chorem/pollen/services/impl/PollService.java 2012-03-26 17:02:51 UTC (rev 3208) @@ -44,7 +44,6 @@ import org.chorem.pollen.business.persistence.PreventRuleDAO; import org.chorem.pollen.business.persistence.UserAccount; import org.chorem.pollen.business.persistence.Vote; -import org.chorem.pollen.business.persistence.VoteDAO; import org.chorem.pollen.business.persistence.VotingList; import org.chorem.pollen.business.persistence.VotingListDAO; import org.chorem.pollen.common.ChoiceType; @@ -53,7 +52,6 @@ import org.chorem.pollen.services.exceptions.PollAccountNotFound; import org.chorem.pollen.services.exceptions.PollChoiceNotFoundException; import org.chorem.pollen.services.exceptions.PollNotFoundException; -import org.chorem.pollen.services.exceptions.PollVoteNotFoundException; import org.chorem.pollen.services.exceptions.UnauthorizedPollAccessException; import org.nuiton.topia.TopiaException; import org.nuiton.topia.persistence.TopiaFilterPagerUtil; @@ -483,26 +481,6 @@ commitTransaction("Could not close poll " + poll.getTitle()); } - public void deleteVote(String pollId, String voteId) - throws PollNotFoundException, PollVoteNotFoundException { - - Poll poll = getPollByPollId(pollId); - - if (poll == null) { - throw new PollNotFoundException(); - } - Vote vote = poll.getVoteByTopiaId(voteId); - - if (vote == null) { - throw new PollVoteNotFoundException(); - } - - VoteDAO dao = getDAO(Vote.class); - delete(dao, vote); - - commitTransaction("Could not delete vote " + vote); - } - public void deleteChoice(String pollId, String choiceId) throws PollNotFoundException, PollChoiceNotFoundException { Modified: branches/pollen-1.2.6-struts2/pollen-services/src/main/java/org/chorem/pollen/services/impl/VoteService.java =================================================================== --- branches/pollen-1.2.6-struts2/pollen-services/src/main/java/org/chorem/pollen/services/impl/VoteService.java 2012-03-23 21:32:36 UTC (rev 3207) +++ branches/pollen-1.2.6-struts2/pollen-services/src/main/java/org/chorem/pollen/services/impl/VoteService.java 2012-03-26 17:02:51 UTC (rev 3208) @@ -25,11 +25,13 @@ import com.google.common.base.Preconditions; import com.google.common.base.Predicate; +import com.google.common.collect.ImmutableList; import com.google.common.collect.Iterables; import com.google.common.collect.Lists; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.chorem.pollen.PollenTechnicalException; +import org.chorem.pollen.bean.PollUri; import org.chorem.pollen.business.persistence.Choice; import org.chorem.pollen.business.persistence.Poll; import org.chorem.pollen.business.persistence.PollAccount; @@ -39,6 +41,7 @@ import org.chorem.pollen.business.persistence.VoteDAO; import org.chorem.pollen.business.persistence.VoteToChoice; import org.chorem.pollen.business.persistence.VoteToChoiceDAO; +import org.chorem.pollen.common.PollType; import org.chorem.pollen.entities.PollenDAOHelper; import org.chorem.pollen.services.PollenServiceSupport; import org.chorem.pollen.services.exceptions.VoteNotFoundException; @@ -46,6 +49,7 @@ import org.nuiton.topia.TopiaException; import org.nuiton.topia.persistence.TopiaFilterPagerUtil; +import java.net.URL; import java.util.List; import java.util.Map; @@ -117,6 +121,20 @@ } return result; } + + /** + * Retrieve the URL to update a vote based on {@link PollUri}. + * + * @param pollUri Uri with pollId and accountId + * @return the URL that allow the voting person to update his vote + */ + public String getUpdateVoteUrl(PollUri pollUri) { + Preconditions.checkNotNull(pollUri.getAccountId()); + URL baseUrl = serviceContext.getApplicationURL(); + String result = + String.format("%s/poll/votefor/%s", baseUrl, pollUri.getUri()); + return result; + } public Vote createVote(Vote vote) { @@ -172,6 +190,7 @@ String choiceId = input.getChoice().getTopiaId(); Choice choiceLoaded = getEntityById(Choice.class, choiceId); voteToChoiceCreated.setChoice(choiceLoaded); + voteToChoiceCreated.setVote(result); result.addChoiceVoteToChoice(voteToChoiceCreated); } @@ -225,6 +244,16 @@ commitTransaction("Could not update vote"); } + /** + * Delete vote referenced by {@code voteId}. This will also delete all + * associated {@link VoteToChoice}. The {@link PollAccount} could also be + * deleted for a {@link PollType#FREE} poll where vote will be removed. This + * remove implied an new execution of poll vote counting if continuous results + * is activated. + * + * @param voteId TopiaId of the vote + * @throws VoteNotFoundException if vote doesn't exist + */ public void deleteVote(String voteId) throws VoteNotFoundException { Vote entityToDelete = getEntityById(Vote.class, voteId); @@ -234,8 +263,28 @@ } VoteDAO dao = getDAO(Vote.class); + VoteToChoiceDAO voteToChoiceDao = getDAO(VoteToChoice.class); + + // Delete all VoteToChoice + List<VoteToChoice> voteToChoices = ImmutableList.copyOf(entityToDelete.getChoiceVoteToChoice()); + for (VoteToChoice voteToChoice : voteToChoices) { + entityToDelete.removeChoiceVoteToChoice(voteToChoice); + delete(voteToChoiceDao, voteToChoice); + } + + // Delete vote PollAccount if the Poll is free and account is not the creator + Poll poll = entityToDelete.getPoll(); + PollAccount voteAccount = entityToDelete.getPollAccount(); + if (PollType.FREE.equals(poll.getPollType()) && !voteAccount.equals(poll.getCreator())) { + PollAccountDAO accountDAO = getDAO(PollAccount.class); + delete(accountDAO, voteAccount); + } + + poll.removeVote(entityToDelete); delete(dao, entityToDelete); + // TODO calculate results if continuous + if (log.isDebugEnabled()) { log.debug("Entity deleted: " + voteId); } Modified: branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/poll/DeleteVote.java =================================================================== --- branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/poll/DeleteVote.java 2012-03-23 21:32:36 UTC (rev 3207) +++ branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/poll/DeleteVote.java 2012-03-26 17:02:51 UTC (rev 3208) @@ -24,7 +24,7 @@ package org.chorem.pollen.ui.actions.poll; import com.google.common.base.Preconditions; -import org.chorem.pollen.services.impl.PollService; +import org.chorem.pollen.services.impl.VoteService; /** * To delete a poll vote. @@ -48,9 +48,9 @@ Preconditions.checkNotNull(pollId); Preconditions.checkNotNull(voteId); - PollService service = newService(PollService.class); + VoteService service = newService(VoteService.class); - service.deleteVote(pollId, voteId); + service.deleteVote(voteId); return SUCCESS; } Modified: branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/webapp/WEB-INF/jsp/poll/confirmDeletePollVote.jsp =================================================================== --- branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/webapp/WEB-INF/jsp/poll/confirmDeletePollVote.jsp 2012-03-23 21:32:36 UTC (rev 3207) +++ branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/webapp/WEB-INF/jsp/poll/confirmDeletePollVote.jsp 2012-03-26 17:02:51 UTC (rev 3208) @@ -27,7 +27,7 @@ <s:form method="POST" namespace="/poll" action="deleteVote"> <s:hidden key="uriId" label=''/> - <s:hidden key="voteId" label=''/> + <s:hidden key="voteId" value="%{vote.topiaId}" label=''/> <s:text name="pollen.information.confirmDeletePollVote"> <s:param>
participants (1)
-
fdesbois@users.chorem.org