Author: tchemit Date: 2012-09-04 18:00:18 +0200 (Tue, 04 Sep 2012) New Revision: 3674 Url: http://chorem.org/repositories/revision/pollen/3674 Log: fixes #801: Unicity of vote id is not correct Modified: trunk/pollen-persistence/src/main/java/org/chorem/pollen/business/persistence/VoteDAOImpl.java trunk/pollen-persistence/src/test/java/org/chorem/pollen/business/persistence/VoteDAOImplTest.java trunk/pollen-services/src/main/java/org/chorem/pollen/services/impl/VoteService.java trunk/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/poll/vote/VoteForPoll.java Modified: trunk/pollen-persistence/src/main/java/org/chorem/pollen/business/persistence/VoteDAOImpl.java =================================================================== --- trunk/pollen-persistence/src/main/java/org/chorem/pollen/business/persistence/VoteDAOImpl.java 2012-09-04 15:40:30 UTC (rev 3673) +++ trunk/pollen-persistence/src/main/java/org/chorem/pollen/business/persistence/VoteDAOImpl.java 2012-09-04 16:00:18 UTC (rev 3674) @@ -65,14 +65,14 @@ return result; } - public boolean hasAlreadyVoted(Poll poll, String votingId) throws TopiaException { + public Vote findVoteByPollAndVotingId(Poll poll, String votingId) throws TopiaException { - String hql = "SELECT COUNT(*) FROM VoteImpl e, PollImpl p WHERE " + + String hql = "SELECT e FROM VoteImpl e, PollImpl p WHERE " + "p = :p AND " + "e in elements(p.vote) AND " + "e.pollAccount.votingId = :v"; - boolean result = existsByQuery(hql, "p", poll, "v", votingId); + Vote result = findByQuery(Vote.class, hql, "p", poll, "v", votingId); return result; } Modified: trunk/pollen-persistence/src/test/java/org/chorem/pollen/business/persistence/VoteDAOImplTest.java =================================================================== --- trunk/pollen-persistence/src/test/java/org/chorem/pollen/business/persistence/VoteDAOImplTest.java 2012-09-04 15:40:30 UTC (rev 3673) +++ trunk/pollen-persistence/src/test/java/org/chorem/pollen/business/persistence/VoteDAOImplTest.java 2012-09-04 16:00:18 UTC (rev 3674) @@ -128,7 +128,7 @@ } @Test - public void hasAlreadyVoted() throws Exception { + public void findVoteByPollAndVotingId() throws Exception { TopiaContext tx = beginTransaction(); @@ -158,16 +158,16 @@ tx.commitTransaction(); - boolean actual; - actual = voteDAO.hasAlreadyVoted(poll1, votingId1); - Assert.assertTrue(actual); - actual = voteDAO.hasAlreadyVoted(poll2, votingId1); - Assert.assertFalse(actual); + Vote actual; + actual = voteDAO.findVoteByPollAndVotingId(poll1, votingId1); + Assert.assertNotNull(actual); + actual = voteDAO.findVoteByPollAndVotingId(poll2, votingId1); + Assert.assertNull(actual); - actual = voteDAO.hasAlreadyVoted(poll1, votingId2); - Assert.assertFalse(actual); - actual = voteDAO.hasAlreadyVoted(poll2, votingId2); - Assert.assertTrue(actual); + actual = voteDAO.findVoteByPollAndVotingId(poll1, votingId2); + Assert.assertNull(actual); + actual = voteDAO.findVoteByPollAndVotingId(poll2, votingId2); + Assert.assertNotNull(actual); } @Test Modified: trunk/pollen-services/src/main/java/org/chorem/pollen/services/impl/VoteService.java =================================================================== --- trunk/pollen-services/src/main/java/org/chorem/pollen/services/impl/VoteService.java 2012-09-04 15:40:30 UTC (rev 3673) +++ trunk/pollen-services/src/main/java/org/chorem/pollen/services/impl/VoteService.java 2012-09-04 16:00:18 UTC (rev 3674) @@ -364,10 +364,23 @@ } } - public boolean hasAlreadyVoted(Poll poll, String votingId) { + public boolean isVotingIdFree(Poll poll, + String pollAccountId, + String votingId) { try { VoteDAO dao = getDAO(Vote.class); - boolean result = dao.hasAlreadyVoted(poll, votingId); + Vote existingVote = dao.findVoteByPollAndVotingId(poll, votingId); + boolean result; + if (existingVote == null) { + // no such vote + // votingId is free + result = true; + } else { + + // there is a vote using this votingId + // votingId is free only if the pollAccount is the incoming one + result = existingVote.getPollAccount().getTopiaId().equals(pollAccountId); + } return result; } catch (Exception e) { throw new PollenTechnicalException( Modified: trunk/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/poll/vote/VoteForPoll.java =================================================================== --- trunk/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/poll/vote/VoteForPoll.java 2012-09-04 15:40:30 UTC (rev 3673) +++ trunk/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/poll/vote/VoteForPoll.java 2012-09-04 16:00:18 UTC (rev 3674) @@ -98,9 +98,8 @@ _("pollen.error.pollAccount.votingId.required")); } - // check if the new pollAccount (topiaId = null) has already voted - if (pollAccount.getTopiaId() == null && - getVoteService().hasAlreadyVoted(poll, name)) { + // check if the votingId is available + if (!getVoteService().isVotingIdFree(poll, pollAccount.getTopiaId(),name)) { addFieldError("pollAccount.votingId", _("pollen.error.user.alreadyVoted", name)); }