Author: tchemit Date: 2012-09-23 16:37:50 +0200 (Sun, 23 Sep 2012) New Revision: 3705 Url: http://chorem.org/repositories/revision/pollen/3705 Log: fixes #814: There is some pollAccount with no accountId Modified: trunk/pollen-services/src/main/java/org/chorem/pollen/entities/migration/PollenMigrationCallbackV1_5.java trunk/pollen-services/src/main/java/org/chorem/pollen/services/impl/VoteService.java Modified: trunk/pollen-services/src/main/java/org/chorem/pollen/entities/migration/PollenMigrationCallbackV1_5.java =================================================================== --- trunk/pollen-services/src/main/java/org/chorem/pollen/entities/migration/PollenMigrationCallbackV1_5.java 2012-09-23 14:37:19 UTC (rev 3704) +++ trunk/pollen-services/src/main/java/org/chorem/pollen/entities/migration/PollenMigrationCallbackV1_5.java 2012-09-23 14:37:50 UTC (rev 3705) @@ -94,6 +94,10 @@ // clean pollAccount for votes // see http://chorem.org/issues/804 removeDuplicateVoteWithSameUserAccount(tx, sContext, queries); + + // generate missing accountId + // see http://chorem.org/issues/814 + generateMissingAccountId(tx, sContext, queries); } private void removeDuplicateVoteWithSameUserAccount(TopiaContextImplementor tx, @@ -207,6 +211,36 @@ } + private void generateMissingAccountId(TopiaContextImplementor tx, + PollenServiceContext sContext, + List<String> queries) throws TopiaException { + // get all pollAccount with no accountId + TopiaSQLQuery<String> sqlQuery = new TopiaSQLQuery<String>() { + + @Override + protected PreparedStatement prepareQuery(Connection connection) throws SQLException { + PreparedStatement ps = connection.prepareStatement( + "SELECT p.topiaId FROM pollAccount p, vote v WHERE " + + "p.accountId = '' AND v.pollaccount = p.topiaid"); + return ps; + } + + @Override + protected String prepareResult(ResultSet set) throws SQLException { + String pollAccountId = set.getString(1); + return pollAccountId; + } + }; + + List<String> accountIds = sqlQuery.findMultipleResult(tx); + + String request = + "UPDATE pollaccount set accountId = '%s' WHERE topiaid='%s';"; + for (String id : accountIds) { + queries.add(String.format(request, sContext.generateId(), id)); + } + } + final Function<Vote, UserAccount> voteToUserAccount = new Function<Vote, UserAccount>() { @Override public UserAccount apply(Vote input) { 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-23 14:37:19 UTC (rev 3704) +++ trunk/pollen-services/src/main/java/org/chorem/pollen/services/impl/VoteService.java 2012-09-23 14:37:50 UTC (rev 3705) @@ -25,6 +25,7 @@ import com.google.common.base.Preconditions; import com.google.common.collect.ImmutableList; import com.google.common.collect.Lists; +import org.apache.commons.lang3.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.chorem.pollen.PollenTechnicalException; @@ -61,11 +62,12 @@ VoteDAO dao = getDAO(Vote.class); Vote voteLoaded = null; - if (accountEditable.getAccountId() != null) { + if (StringUtils.isNotBlank(accountEditable.getAccountId())) { // if pollAccount has no accoutId, so not yet voted, no need to // seek for something is db. try { - voteLoaded = dao.findByAccountId(accountEditable.getAccountId()); + voteLoaded = dao.findByPollAndAccountId( + poll, accountEditable.getAccountId()); } catch (TopiaException e) { throw new PollenTechnicalException(e); } @@ -153,7 +155,7 @@ // Create new account PollAccountDAO pollAccountDAO = getDAO(PollAccount.class); pollAccountLoaded = createWithProperties( - pollAccountDAO, PollAccount.PROPERTY_ACCOUNT_ID, generateId()); + pollAccountDAO, PollAccount.PROPERTY_ACCOUNT_ID, serviceContext.generateId()); } else { // Load existing account (restricted poll)