r3021 - in trunk/pollen-business/src: main/java/org/chorem/pollen/service main/xmi test/java/org/chorem/pollen/service test/java/org/chorem/pollen/test
Author: fdesbois Date: 2010-05-31 17:57:48 +0200 (Mon, 31 May 2010) New Revision: 3021 Url: http://chorem.org/repositories/revision/pollen/3021 Log: Implement deleteVote method + tests Modified: trunk/pollen-business/src/main/java/org/chorem/pollen/service/ServiceVoteImpl.java trunk/pollen-business/src/main/xmi/pollen.properties trunk/pollen-business/src/main/xmi/pollen.zargo trunk/pollen-business/src/test/java/org/chorem/pollen/service/ServiceVoteImplTest.java trunk/pollen-business/src/test/java/org/chorem/pollen/test/AbstractServiceTest.java Modified: trunk/pollen-business/src/main/java/org/chorem/pollen/service/ServiceVoteImpl.java =================================================================== --- trunk/pollen-business/src/main/java/org/chorem/pollen/service/ServiceVoteImpl.java 2010-05-28 13:21:09 UTC (rev 3020) +++ trunk/pollen-business/src/main/java/org/chorem/pollen/service/ServiceVoteImpl.java 2010-05-31 15:57:48 UTC (rev 3021) @@ -146,11 +146,11 @@ // Add link between the participant to find and his parent query.addInElements(participantProperty.name(), parentCollectionProperty). - addEquals(participantProperty.name(), participant). - addWhere(pollProperty.nameProperty(Poll.CREATOR), - TopiaQuery.Op.NEQ, - participant - ); + addEquals(participantProperty.name(), participant). + addWhere(pollProperty.nameProperty(Poll.CREATOR), + TopiaQuery.Op.NEQ, + participant + ); if (log.isDebugEnabled()) { log.debug("Query : " + query); @@ -209,10 +209,51 @@ } @Override - protected void executeDeleteVote(TopiaContext transaction, Poll poll, PollAccount participant) throws Exception { + protected void executeDeleteVote(TopiaContext transaction, Poll poll, + PollAccount participant) throws TopiaException { // For free poll, delete the participant // For other types, delete all votes and set voteDate to null + + if (poll.getPollType().isFree()) { + + PollAccountDAO dao = PollenDAOHelper.getPollAccountDAO(transaction); + + PollAccount participantToDelete = + dao.findByTopiaId(participant.getTopiaId()); + + dao.delete(participantToDelete); + + // Next code has equivalent results -> remove the entity from the poll + // then delete it because of delete-orphan + +// PollDAO dao = PollenDAOHelper.getPollDAO(transaction); +// +// Poll pollToUpdate = dao.findByTopiaId(poll.getTopiaId()); +// +// // participant will be deleted because of composition on pollAccount +// // collection +// pollToUpdate.removePollAccount(participant); +// +// dao.update(pollToUpdate); + + } else if (poll.getPollType().isRestrictedOrGroup()) { + + PollAccountDAO dao = PollenDAOHelper.getPollAccountDAO(transaction); + + PollAccount participantToUpdate = + dao.findByTopiaId(participant.getTopiaId()); + + // Remove all votes deleted by cascade + participantToUpdate.clearChoiceVote(); + // Reset date, the participant is considered like he has not alerady + // voted + participantToUpdate.setVoteDate(null); + + dao.update(participantToUpdate); + } + + transaction.commitTransaction(); } @Override Modified: trunk/pollen-business/src/main/xmi/pollen.properties =================================================================== --- trunk/pollen-business/src/main/xmi/pollen.properties 2010-05-28 13:21:09 UTC (rev 3020) +++ trunk/pollen-business/src/main/xmi/pollen.properties 2010-05-31 15:57:48 UTC (rev 3021) @@ -2,6 +2,7 @@ model.tagvalue.copyright=/* *##%\n Copyright (C) 2009 Pollen\n *##%*/ # toString are particularly difficult to use because of lazy initialization model.tagvalue.notGenerateToString=true +#model.tagvalue.useLegacyDAO=true #model.tagvalue.dbSchema=Pollen model.tagvalue.java.lang.String=text model.tagvalue.exceptionClass=org.chorem.pollen.PollenException Modified: trunk/pollen-business/src/main/xmi/pollen.zargo =================================================================== (Binary files differ) Modified: trunk/pollen-business/src/test/java/org/chorem/pollen/service/ServiceVoteImplTest.java =================================================================== --- trunk/pollen-business/src/test/java/org/chorem/pollen/service/ServiceVoteImplTest.java 2010-05-28 13:21:09 UTC (rev 3020) +++ trunk/pollen-business/src/test/java/org/chorem/pollen/service/ServiceVoteImplTest.java 2010-05-31 15:57:48 UTC (rev 3021) @@ -11,8 +11,10 @@ import org.chorem.pollen.entity.PollAccount; import org.chorem.pollen.entity.PollAccountDAO; import org.chorem.pollen.entity.PollAccountImpl; +import org.chorem.pollen.entity.PollDAO; import org.chorem.pollen.entity.UserAccount; import org.chorem.pollen.entity.Vote; +import org.chorem.pollen.entity.VoteDAO; import org.chorem.pollen.entity.VoteImpl; import org.chorem.pollen.test.AbstractServiceTest; import org.junit.Assert; @@ -21,7 +23,10 @@ import org.nuiton.topia.TopiaException; import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; import java.util.List; +import java.util.Map; /** * Created: 27 mai 2010 @@ -33,9 +38,13 @@ private static final Log log = LogFactory.getLog(ServiceVoteImplTest.class); + protected UserAccount user; + @Override protected void init() throws Exception { getServiceVote(); + + user = createUser(false); } @Test @@ -43,7 +52,6 @@ throws PollenBusinessException, TopiaException { /** PREPARE DATA **/ - UserAccount user = createUser(false); Poll pollCreated = createGroupPoll("Poll", user, null, 3, "Group1", "Group2"); @@ -80,8 +88,6 @@ @Test public void testSaveVoteForFreePoll() throws PollenBusinessException, TopiaException { /** PREPARE DATA **/ - UserAccount user = createUser(false); - Poll pollCreated = createFreePoll("Poll", user, VoteCountingType.NORMAL); List<Choice> choices = addChoicesToPoll(pollCreated, ChoiceType.TEXT, @@ -200,8 +206,140 @@ } finally { transaction.closeContext(); } + } - + @Test + public void testDeleteVoteForFreePoll() + throws TopiaException, PollenBusinessException { + /** PREPARE DATA */ + Poll pollCreated = createFreePoll("Poll", user, VoteCountingType.NORMAL); + + List<Choice> choices = + addChoicesToPoll(pollCreated, ChoiceType.TEXT, + "choice1", "choice2", "choice3"); + + PollAccount participant = new PollAccountImpl(); + participant.setName("participant"); + participant.setEmail("participant@domain.org"); + participant.setUserAccount(user); + + for (Choice choice : choices) { + Vote vote = new VoteImpl(); + vote.setChoice(choices.get(0)); // choice1 + vote.setVoteValue(1); // TRUE + participant.addChoiceVote(vote); + } + + String uid = serviceVote.saveVote(pollCreated, participant); + TopiaContext transaction = beginTransaction(); + PollAccount account = null; + Collection<Vote> votes = null; + try { + PollAccountDAO dao = PollenDAOHelper.getPollAccountDAO(transaction); + + account = dao.findByNaturalId(uid); + votes = account.getChoiceVote(); + + } finally { + transaction.closeContext(); + } + + Poll poll = loadEntity(pollCreated); + + /** EXEC METHOD */ + + serviceVote.deleteVote(poll, account); + + transaction = beginTransaction(); + try { + PollAccountDAO dao = PollenDAOHelper.getPollAccountDAO(transaction); + + // Check account are correctly deleted + account = dao.findByNaturalId(uid); + Assert.assertNull(account); + + VoteDAO voteDAO = PollenDAOHelper.getVoteDAO(transaction); + // Check all votes are not in database anymore + for (Vote vote : votes) { + Vote voteFound = voteDAO.findByTopiaId(vote.getTopiaId()); + Assert.assertNull(voteFound); + } + + PollDAO pollDAO = PollenDAOHelper.getPollDAO(transaction); + + Poll pollFound = pollDAO.findByTopiaId(poll.getTopiaId()); + Assert.assertEquals(0, pollFound.getPollAccount().size()); + Assert.assertNotNull(pollFound.getCreator()); + + } finally { + transaction.closeContext(); + } } + + @Test + public void testDeleteVoteForGroupPoll() + throws TopiaException, PollenBusinessException { + + /** PREPARE DATA */ + Poll pollCreated = createGroupPoll("Poll", user, + VoteCountingType.NORMAL, 3, "group1", "group2"); + + PollAccount group1 = pollCreated.getPollAccount().iterator().next(); + PollAccount existingParticipant = group1.getChild().iterator().next(); + + List<Choice> choices = + addChoicesToPoll(pollCreated, ChoiceType.TEXT, + "choice1", "choice2", "choice3"); + + for (Choice choice : choices) { + Vote vote = new VoteImpl(); + vote.setChoice(choices.get(0)); // choice1 + vote.setVoteValue(1); // TRUE + existingParticipant.addChoiceVote(vote); + } + + String uid = serviceVote.saveVote(pollCreated, existingParticipant); + TopiaContext transaction = beginTransaction(); + PollAccount account = null; + Collection<Vote> votes = null; + try { + PollAccountDAO dao = PollenDAOHelper.getPollAccountDAO(transaction); + + account = dao.findByNaturalId(uid); + votes = account.getChoiceVote(); + + } finally { + transaction.closeContext(); + } + + Poll poll = loadEntity(pollCreated); + + /** EXEC METHOD */ + + serviceVote.deleteVote(poll, account); + + transaction = beginTransaction(); + try { + PollAccountDAO dao = PollenDAOHelper.getPollAccountDAO(transaction); + + // Check account are correctly deleted + account = dao.findByNaturalId(uid); + Assert.assertFalse(account.isList()); + Assert.assertNotNull(account); + Assert.assertNull(account.getVoteDate()); + + VoteDAO voteDAO = PollenDAOHelper.getVoteDAO(transaction); + // Check all votes are not in database anymore + for (Vote vote : votes) { + Vote voteFound = voteDAO.findByTopiaId(vote.getTopiaId()); + Assert.assertNull(voteFound); + } + + } finally { + transaction.closeContext(); + } + + + } } Modified: trunk/pollen-business/src/test/java/org/chorem/pollen/test/AbstractServiceTest.java =================================================================== --- trunk/pollen-business/src/test/java/org/chorem/pollen/test/AbstractServiceTest.java 2010-05-28 13:21:09 UTC (rev 3020) +++ trunk/pollen-business/src/test/java/org/chorem/pollen/test/AbstractServiceTest.java 2010-05-31 15:57:48 UTC (rev 3021) @@ -21,6 +21,8 @@ import org.chorem.pollen.entity.PollDAO; import org.chorem.pollen.entity.UserAccount; import org.chorem.pollen.entity.UserAccountDAO; +import org.chorem.pollen.entity.Vote; +import org.chorem.pollen.entity.VoteDAO; import org.chorem.pollen.service.ServiceFavoriteImpl; import org.chorem.pollen.service.ServicePollImpl; import org.chorem.pollen.service.ServiceUserImpl; @@ -43,6 +45,7 @@ import java.util.Date; import java.util.GregorianCalendar; import java.util.List; +import java.util.Map; import java.util.Properties; /**
participants (1)
-
fdesbois@users.chorem.org