Author: tchemit Date: 2012-09-05 23:04:41 +0200 (Wed, 05 Sep 2012) New Revision: 3690 Url: http://chorem.org/repositories/revision/pollen/3690 Log: fixes #804: Can not acces no more to poll where I was admin and voter Modified: trunk/pollen-services/src/main/java/org/chorem/pollen/entities/migration/PollenMigrationCallbackV1_4_5_2.java Modified: trunk/pollen-services/src/main/java/org/chorem/pollen/entities/migration/PollenMigrationCallbackV1_4_5_2.java =================================================================== --- trunk/pollen-services/src/main/java/org/chorem/pollen/entities/migration/PollenMigrationCallbackV1_4_5_2.java 2012-09-05 20:09:16 UTC (rev 3689) +++ trunk/pollen-services/src/main/java/org/chorem/pollen/entities/migration/PollenMigrationCallbackV1_4_5_2.java 2012-09-05 21:04:41 UTC (rev 3690) @@ -8,7 +8,6 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.chorem.pollen.PollenApplicationContext; -import org.chorem.pollen.PollenConfiguration; import org.chorem.pollen.business.persistence.Poll; import org.chorem.pollen.business.persistence.PollAccount; import org.chorem.pollen.business.persistence.PollDAO; @@ -19,13 +18,17 @@ import org.chorem.pollen.services.PollenServiceContext; import org.chorem.pollen.services.PollenServiceFactory; import org.chorem.pollen.services.impl.VoteService; -import org.nuiton.topia.TopiaContext; import org.nuiton.topia.TopiaException; import org.nuiton.topia.framework.TopiaContextImplementor; +import org.nuiton.topia.framework.TopiaSQLQuery; import org.nuiton.topia.migration.TopiaMigrationCallbackByClassNG; import org.nuiton.util.Version; import org.nuiton.util.VersionUtil; +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; import java.util.Collection; import java.util.List; import java.util.Locale; @@ -54,34 +57,49 @@ boolean showSql, boolean showProgression) throws TopiaException { - - PollenServiceFactory serviceFactory = new PollenServiceFactory(); - PollenApplicationContext applicationContext = PollenApplicationContext.get( ActionContext.getContext()); - PollenConfiguration configuration = - applicationContext.getConfiguration(); - - PollenServiceContext sContext = DefaultPollenServiceContext.newContext( Locale.getDefault(), tx, - configuration, - serviceFactory, + applicationContext.getConfiguration(), + new PollenServiceFactory(), applicationContext.getVoteCountingStrategyProvider() ); // clean pollAccount for votes // see http://chorem.org/issues/804 - removeDuplicateVoteWithSameUserAccount(sContext, queries); + removeDuplicateVoteWithSameUserAccount(tx, sContext, queries); } - private void removeDuplicateVoteWithSameUserAccount(PollenServiceContext sContext, + private void removeDuplicateVoteWithSameUserAccount(TopiaContextImplementor tx, + PollenServiceContext sContext, List<String> queries) throws TopiaException { - TopiaContext tx = sContext.getTransaction(); + // get all poll with several votes with same userAccount + TopiaSQLQuery<String> getAllPollIdsQuery = new TopiaSQLQuery<String>() { + @Override + protected PreparedStatement prepareQuery(Connection connection) throws SQLException { + PreparedStatement ps = connection.prepareStatement( + "SELECT p.pollid FROM poll p WHERE " + + "(SELECT COUNT(pa) > COUNT(DISTINCT(pa.useraccount)) " + + "FROM vote v,pollaccount pa WHERE " + + "v.poll = p.topiaid AND v.pollaccount = pa.topiaid " + + "AND pa.useraccount IS NOT NULL);"); + return ps; + } + + @Override + protected String prepareResult(ResultSet set) throws SQLException { + String pollId = set.getString(1); + return pollId; + } + }; + + List<String> pollIds = getAllPollIdsQuery.findMultipleResult(tx); + PollDAO pollDAO = PollenDAOHelper.getPollDAO(tx); VoteService voteService = sContext.newService(VoteService.class); @@ -89,15 +107,17 @@ Multimap<Poll, PollAccount> accountToAnonymous = ArrayListMultimap.create(); Multimap<Poll, Vote> voteToFixByPoll = ArrayListMultimap.create(); - long nbPolls = pollDAO.count(); + long nbPolls = pollIds.size(); int currentPollIndex = 0; - for (Poll poll : pollDAO) { + for (String pollId : pollIds) { if (log.isInfoEnabled()) { - log.info("Scan poll [" + (currentPollIndex++) + "/" + - nbPolls + "]" + poll.getPollId()); + log.info("Treat bad poll [" + (currentPollIndex++) + "/" + + nbPolls + "]" + pollId); } + + Poll poll = pollDAO.findByPollId(pollId); boolean pollAnonymous = poll.isAnonymous(); List<Vote> votes = voteService.getAllVotes(poll); @@ -119,9 +139,8 @@ // ok found a userAccount if (log.isWarnEnabled()) { - log.warn("Found a poll" + (pollAnonymous ? "(anonymous)" : "") + " [" + poll.getPollId() + "] with multi vote for the same userAccount : " + + log.warn("Poll" + (pollAnonymous ? "(anonymous)" : "") + " [" + poll.getPollId() + "] with multi vote for the same userAccount : " + userAccount.getEmail()); - } if (pollAnonymous) { for (Vote vote : votesForUser) { @@ -144,7 +163,7 @@ "poll to fix, will remove the userAccount from " + "anonymous votes"); } - String request = "UPDATE pollaccount set useraccount IS NULL WHERE topiaid='%s';"; + String request = "UPDATE pollaccount set useraccount = NULL WHERE topiaid='%s';"; for (PollAccount account : accountToAnonymous.values()) { queries.add(String.format(request, account.getTopiaId())); } @@ -157,9 +176,12 @@ log.warn("There is " + pollToFix.size() + " other polls " + "to fix, will remove the userAccount from any votes"); } + String request = "UPDATE pollaccount set useraccount = NULL WHERE topiaid='%s';"; + for (Vote account : voteToFixByPoll.values()) { + queries.add(String.format(request, account.getPollAccount().getTopiaId())); + } } - } final Function<Vote, UserAccount> voteToUserAccount = new Function<Vote, UserAccount>() {