Author: tchemit Date: 2012-09-05 22:09:16 +0200 (Wed, 05 Sep 2012) New Revision: 3689 Url: http://chorem.org/repositories/revision/pollen/3689 Log: refs #804: Can not acces no more to poll where I was admin and voter (fix by migration for anonymous poll) Added: trunk/pollen-services/src/main/java/org/chorem/pollen/entities/migration/PollenMigrationCallbackV1_4_5_2.java Modified: trunk/pollen-persistence/src/main/xmi/pollen.properties trunk/pollen-services/src/main/resources/META-INF/services/org.nuiton.topia.migration.TopiaMigrationCallbackByClassNG$MigrationCallBackForVersion trunk/pom.xml Modified: trunk/pollen-persistence/src/main/xmi/pollen.properties =================================================================== --- trunk/pollen-persistence/src/main/xmi/pollen.properties 2012-09-05 17:49:33 UTC (rev 3688) +++ trunk/pollen-persistence/src/main/xmi/pollen.properties 2012-09-05 20:09:16 UTC (rev 3689) @@ -24,7 +24,7 @@ #model.tagvalue.dbSchema=Pollen model.tagvalue.constantPrefix=PROPERTY_ model.tagvalue.java.lang.String=text -model.tagvalue.version=1.4.5 +model.tagvalue.version=1.4.5.2 model.tagvalue.doNotGenerateBooleanGetMethods=true model.tagvalue.indexForeignKeys=true Added: 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 (rev 0) +++ 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) @@ -0,0 +1,173 @@ +package org.chorem.pollen.entities.migration; + +import com.google.common.base.Function; +import com.google.common.collect.ArrayListMultimap; +import com.google.common.collect.Multimap; +import com.opensymphony.xwork2.ActionContext; +import org.apache.commons.collections.CollectionUtils; +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; +import org.chorem.pollen.business.persistence.UserAccount; +import org.chorem.pollen.business.persistence.Vote; +import org.chorem.pollen.entities.PollenDAOHelper; +import org.chorem.pollen.services.DefaultPollenServiceContext; +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.migration.TopiaMigrationCallbackByClassNG; +import org.nuiton.util.Version; +import org.nuiton.util.VersionUtil; + +import java.util.Collection; +import java.util.List; +import java.util.Locale; +import java.util.Set; + +/** + * Migration for version {@code 1.4.5.2}. + * + * @author tchemit <chemit@codelutin.com> + * @since 1.4.5.2 + */ +public class PollenMigrationCallbackV1_4_5_2 extends TopiaMigrationCallbackByClassNG.MigrationCallBackForVersion { + + /** Logger. */ + private static final Log log = + LogFactory.getLog(PollenMigrationCallbackV1_4_5_2.class); + + @Override + public Version getVersion() { + return VersionUtil.valueOf("1.4.5.2"); + } + + @Override + protected void prepareMigrationScript(TopiaContextImplementor tx, + List<String> queries, + 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.getVoteCountingStrategyProvider() + ); + + // clean pollAccount for votes + // see http://chorem.org/issues/804 + removeDuplicateVoteWithSameUserAccount(sContext, queries); + } + + private void removeDuplicateVoteWithSameUserAccount(PollenServiceContext sContext, + List<String> queries) throws TopiaException { + + TopiaContext tx = sContext.getTransaction(); + + PollDAO pollDAO = PollenDAOHelper.getPollDAO(tx); + + VoteService voteService = sContext.newService(VoteService.class); + + Multimap<Poll, PollAccount> accountToAnonymous = ArrayListMultimap.create(); + Multimap<Poll, Vote> voteToFixByPoll = ArrayListMultimap.create(); + + long nbPolls = pollDAO.count(); + int currentPollIndex = 0; + + for (Poll poll : pollDAO) { + + if (log.isInfoEnabled()) { + log.info("Scan poll [" + (currentPollIndex++) + "/" + + nbPolls + "]" + poll.getPollId()); + } + boolean pollAnonymous = poll.isAnonymous(); + + List<Vote> votes = voteService.getAllVotes(poll); + + Multimap<UserAccount, Vote> voteByUserAccount = ArrayListMultimap.create(); + for (Vote vote : votes) { + UserAccount useraccount = voteToUserAccount.apply(vote); + if (useraccount != null) { + voteByUserAccount.put(useraccount, vote); + } + } + + for (UserAccount userAccount : voteByUserAccount.keySet()) { + + if (userAccount != null) { + + Collection<Vote> votesForUser = voteByUserAccount.get(userAccount); + if (votesForUser.size() > 1) { + + // ok found a userAccount + if (log.isWarnEnabled()) { + log.warn("Found a poll" + (pollAnonymous ? "(anonymous)" : "") + " [" + poll.getPollId() + "] with multi vote for the same userAccount : " + + userAccount.getEmail()); + + } + if (pollAnonymous) { + for (Vote vote : votesForUser) { + accountToAnonymous.put(poll, vote.getPollAccount()); + } + } else { + voteToFixByPoll.putAll(poll, votesForUser); + } + } + } + } + } + + // treat anonymous vote + Set<Poll> anonymousPollToFix = accountToAnonymous.keySet(); + + if (CollectionUtils.isNotEmpty(anonymousPollToFix)) { + if (log.isWarnEnabled()) { + log.warn("There is " + anonymousPollToFix.size() + " anonymous " + + "poll to fix, will remove the userAccount from " + + "anonymous votes"); + } + String request = "UPDATE pollaccount set useraccount IS NULL WHERE topiaid='%s';"; + for (PollAccount account : accountToAnonymous.values()) { + queries.add(String.format(request, account.getTopiaId())); + } + } + + // treat other votes + Set<Poll> pollToFix = voteToFixByPoll.keySet(); + if (CollectionUtils.isNotEmpty(pollToFix)) { + if (log.isWarnEnabled()) { + log.warn("There is " + pollToFix.size() + " other polls " + + "to fix, will remove the userAccount from any votes"); + } + } + + + } + + final Function<Vote, UserAccount> voteToUserAccount = new Function<Vote, UserAccount>() { + @Override + public UserAccount apply(Vote input) { + PollAccount pollAccount = input.getPollAccount(); + return pollAccount == null ? null : pollAccount.getUserAccount(); + } + }; + +} \ No newline at end of file Property changes on: trunk/pollen-services/src/main/java/org/chorem/pollen/entities/migration/PollenMigrationCallbackV1_4_5_2.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision Added: svn:eol-style + native Modified: trunk/pollen-services/src/main/resources/META-INF/services/org.nuiton.topia.migration.TopiaMigrationCallbackByClassNG$MigrationCallBackForVersion =================================================================== --- trunk/pollen-services/src/main/resources/META-INF/services/org.nuiton.topia.migration.TopiaMigrationCallbackByClassNG$MigrationCallBackForVersion 2012-09-05 17:49:33 UTC (rev 3688) +++ trunk/pollen-services/src/main/resources/META-INF/services/org.nuiton.topia.migration.TopiaMigrationCallbackByClassNG$MigrationCallBackForVersion 2012-09-05 20:09:16 UTC (rev 3689) @@ -3,4 +3,5 @@ org.chorem.pollen.entities.migration.PollenMigrationCallbackV1_3 org.chorem.pollen.entities.migration.PollenMigrationCallbackV1_3_1 org.chorem.pollen.entities.migration.PollenMigrationCallbackV1_4 -org.chorem.pollen.entities.migration.PollenMigrationCallbackV1_4_5 \ No newline at end of file +org.chorem.pollen.entities.migration.PollenMigrationCallbackV1_4_5 +org.chorem.pollen.entities.migration.PollenMigrationCallbackV1_4_5_2 \ No newline at end of file Modified: trunk/pom.xml =================================================================== --- trunk/pom.xml 2012-09-05 17:49:33 UTC (rev 3688) +++ trunk/pom.xml 2012-09-05 20:09:16 UTC (rev 3689) @@ -153,7 +153,7 @@ <projectId>pollen</projectId> <!-- customized versions --> - <topiaVersion>2.6.13</topiaVersion> + <topiaVersion>2.6.14-SNAPSHOT</topiaVersion> <eugenePluginVersion>2.5</eugenePluginVersion> <nuitonI18nVersion>2.5</nuitonI18nVersion>