Author: fdesbois Date: 2010-03-09 17:44:39 +0100 (Tue, 09 Mar 2010) New Revision: 2912 Log: Regression problem on existing vote : use a new method from serviceVote "hasAlreadyVoted" Modified: branches/pollen-1.2.3-1.2.x/pollen-business/src/main/java/org/chorem/pollen/business/services/ServiceVote.java branches/pollen-1.2.3-1.2.x/pollen-business/src/main/java/org/chorem/pollen/business/services/ServiceVoteImpl.java branches/pollen-1.2.3-1.2.x/pollen-ui/src/main/java/org/chorem/pollen/ui/pages/poll/VoteForPoll.java Modified: branches/pollen-1.2.3-1.2.x/pollen-business/src/main/java/org/chorem/pollen/business/services/ServiceVote.java =================================================================== --- branches/pollen-1.2.3-1.2.x/pollen-business/src/main/java/org/chorem/pollen/business/services/ServiceVote.java 2010-03-09 16:02:39 UTC (rev 2911) +++ branches/pollen-1.2.3-1.2.x/pollen-business/src/main/java/org/chorem/pollen/business/services/ServiceVote.java 2010-03-09 16:44:39 UTC (rev 2912) @@ -69,6 +69,15 @@ int startIndex, int endIndex); /** + * Test if the {@code votingId} has already voted for the {@code poll}. + * + * @param votingId to test existing vote + * @param poll where the votingId can have already voted + * @return true if the votingId has already voted or false otherwise + */ + public boolean hasAlreadyVoted(String votingId, PollDTO poll); + + /** * Retourne les votes d'un sondage * * @param properties La HashMap pour sélectionner les votes d'un sondage. Il Modified: branches/pollen-1.2.3-1.2.x/pollen-business/src/main/java/org/chorem/pollen/business/services/ServiceVoteImpl.java =================================================================== --- branches/pollen-1.2.3-1.2.x/pollen-business/src/main/java/org/chorem/pollen/business/services/ServiceVoteImpl.java 2010-03-09 16:02:39 UTC (rev 2911) +++ branches/pollen-1.2.3-1.2.x/pollen-business/src/main/java/org/chorem/pollen/business/services/ServiceVoteImpl.java 2010-03-09 16:44:39 UTC (rev 2912) @@ -178,6 +178,7 @@ } } + @Override public List<VoteDTO> getVotesByPoll(PollDTO poll, int startIndex, int endIndex) { TopiaContext transaction = null; @@ -213,15 +214,9 @@ if (log.isDebugEnabled()) { log.debug("Nb votes found : " + votes.size()); } + converter.setTransaction(transaction); + results = converter.createVoteDTOs(votes); -// PollDAO dao = PollenModelDAOHelper.getPollDAO(transaction); -// Poll ePoll = dao.findByPollId(poll.getPollId()); - -// if (ePoll != null) { - converter.setTransaction(transaction); - results = converter.createVoteDTOs(votes); -// } - } catch (Exception eee) { ContextUtil.doCatch(eee, transaction, "Unable to load votes from poll with uid = " + @@ -233,6 +228,39 @@ } @Override + public boolean hasAlreadyVoted(String votingId, PollDTO poll) { + TopiaContext transaction = null; + boolean result = false; + try { + + transaction = rootContext.beginTransaction(); + + // Test using a count(*) on votes + List<Long> tmp = transaction.find( + "SELECT COUNT(*)" + + " FROM " + Vote.class.getName() + + " WHERE poll.pollId = :pollUId" + + " AND pollAccount.votingId = :votingId", + "pollUId", poll.getPollId(), + "votingId", votingId); + + int count = tmp.get(0).intValue(); + + // If the count is greater than 0, there is an existing votingId + // who has already voted for the poll + result = count > 0; + + } catch (Exception eee) { + ContextUtil.doCatch(eee, transaction, + "Unable test vote existing for account with votingId = " + + votingId + " and poll with uid = " + poll.getPollId()); + } finally { + ContextUtil.doFinally(transaction); + } + return result; + } + + @Override public List<VoteDTO> selectVotes(Map<String, Object> properties) { TopiaContext transaction = null; List<VoteDTO> results = null; Modified: branches/pollen-1.2.3-1.2.x/pollen-ui/src/main/java/org/chorem/pollen/ui/pages/poll/VoteForPoll.java =================================================================== --- branches/pollen-1.2.3-1.2.x/pollen-ui/src/main/java/org/chorem/pollen/ui/pages/poll/VoteForPoll.java 2010-03-09 16:02:39 UTC (rev 2911) +++ branches/pollen-1.2.3-1.2.x/pollen-ui/src/main/java/org/chorem/pollen/ui/pages/poll/VoteForPoll.java 2010-03-09 16:44:39 UTC (rev 2912) @@ -500,7 +500,12 @@ } // Contrôle si le votant n'a pas déjà voté - alreadyVoted = hasAlreadyVoted(pollAccount.getVotingId()); + if (log.isInfoEnabled()) { + log.info("BUSINESS REQUEST [hasAlreadyVoted]"); + } + alreadyVoted = serviceVote.hasAlreadyVoted( + pollAccount.getVotingId(), poll); + modifAllowed = isModifAllowed(pollAccount.getVotingId()); if (alreadyVoted && !modifAllowed) { voteForm.recordError(nameField, messages.format("alreadyVoted", @@ -523,16 +528,19 @@ private boolean hasAlreadyVoted(String votingId) { // comparaison du votant (pollAccount) avec les votants des votes existants - for (VoteDTO vote : getVotes()) { - PollAccountDTO account = servicePollAccount - .findPollAccountById(vote.getPollAccountId()); +// for (VoteDTO vote : getVotes()) { +// PollAccountDTO account = servicePollAccount +// .findPollAccountById(vote.getPollAccountId()); +// +// if (account.getVotingId().equals(votingId)) { +// return true; +// } +// } - if (account.getVotingId().equals(votingId)) { - return true; - } + if (log.isDebugEnabled()) { + log.debug("BUSINESS REQUEST [hasAlreadyVoted]"); } - - return false; + return serviceVote.hasAlreadyVoted(votingId, poll); } /** @@ -1185,7 +1193,14 @@ public void setPage(int page) { this.page = page; } - + + /** + * Retrieve votes of the current poll from business module. + * The votes are ordered by creation date, only the ones to display are + * loaded depends on current page using pager component. + * + * @return the list of votes to display + */ public List<VoteDTO> getVotes() { if (votes == null) { if (log.isInfoEnabled()) {