Author: tchemit Date: 2012-05-24 19:12:47 +0200 (Thu, 24 May 2012) New Revision: 3383 Url: http://chorem.org/repositories/revision/pollen/3383 Log: fixes #566: Toutes les donn?\195?\169es ne sont pas affich?\195?\169es sur les sondages particip?\195?\169s Modified: trunk/pollen-persistence/src/main/java/org/chorem/pollen/PollenFunctions.java trunk/pollen-persistence/src/main/java/org/chorem/pollen/business/persistence/PollDAOImpl.java trunk/pollen-services/src/main/java/org/chorem/pollen/services/impl/PollService.java trunk/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/json/GetInvitedPolls.java trunk/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/json/GetParticipatedPolls.java Modified: trunk/pollen-persistence/src/main/java/org/chorem/pollen/PollenFunctions.java =================================================================== --- trunk/pollen-persistence/src/main/java/org/chorem/pollen/PollenFunctions.java 2012-05-24 12:59:21 UTC (rev 3382) +++ trunk/pollen-persistence/src/main/java/org/chorem/pollen/PollenFunctions.java 2012-05-24 17:12:47 UTC (rev 3383) @@ -25,10 +25,12 @@ import com.google.common.base.Function; import com.google.common.collect.Lists; +import org.apache.commons.lang3.tuple.Pair; import org.chorem.pollen.business.persistence.Choice; import org.chorem.pollen.business.persistence.Comment; import org.chorem.pollen.business.persistence.PersonToList; import org.chorem.pollen.business.persistence.Poll; +import org.chorem.pollen.business.persistence.PollAccount; import org.chorem.pollen.business.persistence.UserAccount; import org.chorem.pollen.business.persistence.Vote; import org.chorem.pollen.business.persistence.VoteToChoice; @@ -43,6 +45,7 @@ import org.nuiton.topia.persistence.TopiaEntity; import java.util.List; +import java.util.Map; /** * Usefull pollen functions. @@ -183,4 +186,10 @@ } }; + public static final Function<Map.Entry<Poll,PollAccount>,Pair<Poll,PollAccount>> MAP_ENTRY_TO_PAIR_FUNCTION = new Function<Map.Entry<Poll, PollAccount>, Pair<Poll, PollAccount>>() { + @Override + public Pair<Poll, PollAccount> apply(Map.Entry<Poll, PollAccount> input) { + return Pair.of(input.getKey(), input.getValue()); + } + }; } Modified: trunk/pollen-persistence/src/main/java/org/chorem/pollen/business/persistence/PollDAOImpl.java =================================================================== --- trunk/pollen-persistence/src/main/java/org/chorem/pollen/business/persistence/PollDAOImpl.java 2012-05-24 12:59:21 UTC (rev 3382) +++ trunk/pollen-persistence/src/main/java/org/chorem/pollen/business/persistence/PollDAOImpl.java 2012-05-24 17:12:47 UTC (rev 3383) @@ -24,9 +24,12 @@ package org.chorem.pollen.business.persistence; import com.google.common.base.Preconditions; +import com.google.common.collect.Lists; import com.google.common.collect.Maps; +import org.apache.commons.lang3.tuple.Pair; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.chorem.pollen.PollenFunctions; import org.chorem.pollen.entities.PollenDAOHelper; import org.nuiton.topia.TopiaException; import org.nuiton.topia.framework.TopiaQuery; @@ -72,77 +75,28 @@ return result; } - public Map<Poll, PollAccount> getInvitedPolls( + public List<Pair<Poll, PollAccount>> getInvitedPolls( TopiaFilterPagerUtil.FilterPagerBean pager, UserAccount userToUse) throws TopiaException { Preconditions.checkNotNull(pager); Preconditions.checkNotNull(userToUse); - //FIXME Use a query to do this to avoid in memory pagination String email = userToUse.getEmail(); - TopiaQuery countQuery = createQuery("p"); - countQuery.addLeftJoin("p." + Poll.PROPERTY_VOTING_LIST, "v", false); - countQuery.addLeftJoin("v." + VotingList.PROPERTY_POLL_ACCOUNT_PERSON_TO_LIST, "l", false); - countQuery.addEquals("l." + PersonToList.PROPERTY_POLL_ACCOUNT - + "." + PollAccount.PROPERTY_EMAIL, email); - long records = countByQuery(countQuery); - pager.setRecords((int) records); - - TopiaQuery query = TopiaFilterPagerUtil.addPagerToQuery(countQuery, pager); + TopiaQuery query = createQuery("p"). + setSelect("p", "l." + PersonToList.PROPERTY_POLL_ACCOUNT). + addLeftJoin("p." + Poll.PROPERTY_VOTING_LIST, "v", false). + addLeftJoin("v." + VotingList.PROPERTY_POLL_ACCOUNT_PERSON_TO_LIST, "l", false). + addEquals("l." + PersonToList.PROPERTY_POLL_ACCOUNT + + "." + PollAccount.PROPERTY_EMAIL, email); - return findAllWithPollAccounts(query, - "p", - "l." + PersonToList.PROPERTY_POLL_ACCOUNT); - -// List<E> polls = findAll(); -// List<E> allPolls = Lists.newLinkedList(); -// for (E poll : polls) { -// for (VotingList votingList : poll.getVotingList()) { -// for (PersonToList personToList : votingList -// .getPollAccountPersonToList()) { -// if (!personToList.isHasVoted()) { -// PollAccount pollAccount = personToList -// .getPollAccount(); -// -// if (pollAccount != null -// && pollAccount.getEmail() != null -// && pollAccount.getEmail().equals( -// email)) { -// allPolls.add(poll); -// } -// } -// } -// } -// } -// -// int records = allPolls.size(); -// pager.setRecords(records); -// -// List<E> result = TopiaFilterPagerUtil.getPageFromList(allPolls, pager); -// return result; - -// try { -// PollDAO dao = PollenDAOHelper.getPollDAO(getTransaction()); -// TopiaQuery countQuery = dao.createQuery("e"); -// countQuery.addWhere("e." + Poll.PROPERTY_CREATOR + "." + PollAccount.PROPERTY_USER_ACCOUNT, TopiaQuery.Op.EQ, user); -// long records = dao.countByQuery(countQuery); -// pager.setRecords((int) records); -// -// PagerUtil.computeRecordIndexesAndPagesNumber(pager); -// TopiaQuery query = dao.createQuery("e"); -// TopiaFilterPagerUtil.addPagerToQuery(pager, query); -// query.addWhere("e." + Poll.PROPERTY_CREATOR + "." + PollAccount.PROPERTY_USER_ACCOUNT, TopiaQuery.Op.EQ, user); -// -// List<Poll> result = dao.findAllByQuery(query); -// return result; -// } catch (TopiaException e) { -// throw new PollenTechnicalException(e); -// } + List<Pair<Poll, PollAccount>> result = findAllWithPollAccounts( + query, pager); + return result; } - public Map<Poll, PollAccount> getParticipatedPolls( + public List<Pair<Poll, PollAccount>> getParticipatedPolls( TopiaFilterPagerUtil.FilterPagerBean pager, UserAccount userToUse) throws TopiaException { @@ -151,36 +105,37 @@ VoteDAO voteDao = PollenDAOHelper.getVoteDAO(context); - TopiaQuery countQuery = voteDao.createQuery("e"). - addDistinct(). - setSelect("e." + Vote.PROPERTY_POLL). - addWhere("e." + Vote.PROPERTY_POLL_ACCOUNT + "." + PollAccount.PROPERTY_USER_ACCOUNT, TopiaQuery.Op.EQ, userToUse); + TopiaQuery query = voteDao.createQuery("e"). + setSelect("e." + Vote.PROPERTY_POLL, "e." + Vote.PROPERTY_POLL_ACCOUNT). + addWhere("e." + Vote.PROPERTY_POLL_ACCOUNT + "." + + PollAccount.PROPERTY_USER_ACCOUNT, + TopiaQuery.Op.EQ, userToUse); - long records = countByQuery(countQuery); - pager.setRecords((int) records); - - TopiaQuery query = TopiaFilterPagerUtil.addPagerToQuery(countQuery, pager); - - return findAllWithPollAccounts(query, - "e." + Vote.PROPERTY_POLL, - "e." + Vote.PROPERTY_POLL_ACCOUNT); + List<Pair<Poll, PollAccount>> result = findAllWithPollAccounts( + query, pager); + return result; } - - protected Map<Poll, PollAccount> findAllWithPollAccounts(TopiaQuery query, - String pollAlias, - String pollAccountAlias) + + protected List<Pair<Poll, PollAccount>> findAllWithPollAccounts(TopiaQuery query, + TopiaFilterPagerUtil.FilterPagerBean pager) throws TopiaException { - // Select both poll and pollAccount - query.setSelect(pollAlias, pollAccountAlias); - List<Object[]> queryResults = getContext().findByQuery(query); - Map<Poll, PollAccount> result = Maps.newLinkedHashMap(); + Map<Poll, PollAccount> mapResult = Maps.newLinkedHashMap(); for (Object[] row : queryResults) { Poll poll = (Poll) row[0]; PollAccount pollAccount = (PollAccount) row[1]; - result.put(poll, pollAccount); + mapResult.put(poll, pollAccount); } + + long records = mapResult.size(); + pager.setRecords((int) records); + TopiaFilterPagerUtil.computeRecordIndexesAndPagesNumber(pager); + List<Map.Entry<Poll, PollAccount>> entries = + Lists.newLinkedList(mapResult.entrySet()); + List<Pair<Poll, PollAccount>> result = + Lists.transform(TopiaFilterPagerUtil.getPageFromList(entries, pager), + PollenFunctions.MAP_ENTRY_TO_PAIR_FUNCTION); return result; } Modified: trunk/pollen-services/src/main/java/org/chorem/pollen/services/impl/PollService.java =================================================================== --- trunk/pollen-services/src/main/java/org/chorem/pollen/services/impl/PollService.java 2012-05-24 12:59:21 UTC (rev 3382) +++ trunk/pollen-services/src/main/java/org/chorem/pollen/services/impl/PollService.java 2012-05-24 17:12:47 UTC (rev 3383) @@ -30,6 +30,7 @@ import com.google.common.collect.Lists; import org.apache.commons.io.FileUtils; import org.apache.commons.lang3.StringUtils; +import org.apache.commons.lang3.tuple.Pair; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.chorem.pollen.PollenConfiguration; @@ -452,7 +453,7 @@ } } - public Map<Poll, PollAccount> getInvitedPolls( + public List<Pair<Poll, PollAccount>> getInvitedPolls( TopiaFilterPagerUtil.FilterPagerBean pager, UserAccount user) { @@ -466,7 +467,7 @@ Preconditions.checkNotNull(userToUse); try { PollDAO pollDao = getDAO(Poll.class); - Map<Poll, PollAccount> result = + List<Pair<Poll, PollAccount>> result = pollDao.getInvitedPolls(pager, userToUse); return result; } catch (TopiaException e) { @@ -474,7 +475,7 @@ } } - public Map<Poll, PollAccount> getParticipatedPolls( + public List<Pair<Poll, PollAccount>> getParticipatedPolls( TopiaFilterPagerUtil.FilterPagerBean pager, UserAccount user) { @@ -489,7 +490,7 @@ try { PollDAO pollDao = getDAO(Poll.class); - Map<Poll, PollAccount> result = + List<Pair<Poll,PollAccount>> result = pollDao.getParticipatedPolls(pager, userToUse); return result; } catch (TopiaException e) { Modified: trunk/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/json/GetInvitedPolls.java =================================================================== --- trunk/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/json/GetInvitedPolls.java 2012-05-24 12:59:21 UTC (rev 3382) +++ trunk/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/json/GetInvitedPolls.java 2012-05-24 17:12:47 UTC (rev 3383) @@ -24,6 +24,7 @@ package org.chorem.pollen.ui.actions.json; import com.google.common.collect.Sets; +import org.apache.commons.lang3.tuple.Pair; import org.chorem.pollen.bean.PollUri; import org.chorem.pollen.business.persistence.Poll; import org.chorem.pollen.business.persistence.PollAccount; @@ -31,6 +32,7 @@ import org.chorem.pollen.services.impl.PollService; import org.nuiton.util.beans.Binder; +import java.util.List; import java.util.Map; import java.util.Set; @@ -75,14 +77,14 @@ PollService pollService = newService(PollService.class); - Map<Poll, PollAccount> invitedPolls = pollService.getInvitedPolls(pager, - getPollenUserAccount()); + List<Pair<Poll, PollAccount>> invitedPolls = pollService.getInvitedPolls(pager, + getPollenUserAccount()); polls = new Map[invitedPolls.size()]; Binder<Poll, Poll> binder = PollenBinderHelper.getSimpleTopiaBinder(Poll.class); int index = 0; - for (Map.Entry<Poll, PollAccount> entry : invitedPolls.entrySet()) { + for (Pair<Poll, PollAccount> entry : invitedPolls) { Poll poll = entry.getKey(); Map<String, Object> map = binder.obtainProperties( @@ -95,13 +97,13 @@ ); PollAccount account = entry.getValue(); - + PollUri pollUri = PollUri.newPollUri(poll.getPollId(), account.getAccountId()); map.put("id", poll.getTopiaId()); map.put("voteId", pollUri.getUri()); map.put("resultId", pollUri.getUri()); - + Set<String> functions = getPollFunctions(poll); map.put("functions", functions); polls[index++] = map; Modified: trunk/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/json/GetParticipatedPolls.java =================================================================== --- trunk/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/json/GetParticipatedPolls.java 2012-05-24 12:59:21 UTC (rev 3382) +++ trunk/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/json/GetParticipatedPolls.java 2012-05-24 17:12:47 UTC (rev 3383) @@ -24,6 +24,7 @@ package org.chorem.pollen.ui.actions.json; import com.google.common.collect.Sets; +import org.apache.commons.lang3.tuple.Pair; import org.chorem.pollen.bean.PollUri; import org.chorem.pollen.business.persistence.Poll; import org.chorem.pollen.business.persistence.PollAccount; @@ -32,6 +33,7 @@ import org.chorem.pollen.services.impl.PollService; import org.nuiton.util.beans.Binder; +import java.util.List; import java.util.Map; import java.util.Set; @@ -76,14 +78,14 @@ PollService pollService = newService(PollService.class); - Map<Poll, PollAccount> participatedPolls = + List<Pair<Poll, PollAccount>> participatedPolls = pollService.getParticipatedPolls(pager, getPollenUserAccount()); polls = new Map[participatedPolls.size()]; Binder<Poll, Poll> binder = PollenBinderHelper.getSimpleTopiaBinder(Poll.class); int index = 0; - for (Map.Entry<Poll, PollAccount> entry : participatedPolls.entrySet()) { + for (Pair<Poll, PollAccount> entry : participatedPolls) { Poll poll = entry.getKey(); Map<String, Object> map = binder.obtainProperties( @@ -94,11 +96,11 @@ Poll.PROPERTY_BEGIN_DATE, Poll.PROPERTY_END_DATE ); - + PollAccount account = entry.getValue(); PollUri pollUri = PollUri.newPollUri(poll.getPollId(), account.getAccountId()); - + map.put("id", poll.getTopiaId()); // Keep accountId to allow vote update @@ -109,7 +111,7 @@ ? pollUri.getPollId() : pollUri.getUri(); map.put("resultId", resultId); - + Set<String> functions = getPollFunctions(poll); map.put("functions", functions); polls[index++] = map;