Author: fdesbois Date: 2012-04-30 12:45:14 +0200 (Mon, 30 Apr 2012) New Revision: 3329 Url: http://chorem.org/repositories/revision/pollen/3329 Log: fixes #539 : - repair problem with id for result page - improve polls loading and retrieve also PollAccount for participated and invited cases Modified: 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/GetCreatedPolls.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 trunk/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/json/GetPolls.java trunk/pollen-ui-struts2/src/main/webapp/WEB-INF/jsp/pollListHelper.jsp 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-04-27 10:24:54 UTC (rev 3328) +++ trunk/pollen-persistence/src/main/java/org/chorem/pollen/business/persistence/PollDAOImpl.java 2012-04-30 10:45:14 UTC (rev 3329) @@ -24,7 +24,7 @@ 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.logging.Log; import org.apache.commons.logging.LogFactory; import org.chorem.pollen.entities.PollenDAOHelper; @@ -33,6 +33,7 @@ import org.nuiton.topia.persistence.TopiaFilterPagerUtil; import java.util.List; +import java.util.Map; public class PollDAOImpl<E extends Poll> extends PollDAOAbstract<E> { @@ -71,8 +72,9 @@ return result; } - public List<E> getInvitedPolls(TopiaFilterPagerUtil.FilterPagerBean pager, - UserAccount userToUse) throws TopiaException { + public Map<Poll, PollAccount> getInvitedPolls( + TopiaFilterPagerUtil.FilterPagerBean pager, + UserAccount userToUse) throws TopiaException { Preconditions.checkNotNull(pager); Preconditions.checkNotNull(userToUse); @@ -80,33 +82,47 @@ //FIXME Use a query to do this to avoid in memory pagination String email = userToUse.getEmail(); - 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(); + 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); - if (pollAccount != null - && pollAccount.getEmail() != null - && pollAccount.getEmail().equals( - email)) { - allPolls.add(poll); - } - } - } - } - } + return findAllWithPollAccounts(query, + "p", + "l." + PersonToList.PROPERTY_POLL_ACCOUNT); - int records = allPolls.size(); - pager.setRecords(records); +// 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; - List<E> result = TopiaFilterPagerUtil.getPageFromList(allPolls, pager); - return result; - // try { // PollDAO dao = PollenDAOHelper.getPollDAO(getTransaction()); // TopiaQuery countQuery = dao.createQuery("e"); @@ -126,8 +142,9 @@ // } } - public List<E> getParticipatedPolls(TopiaFilterPagerUtil.FilterPagerBean pager, - UserAccount userToUse) throws TopiaException { + public Map<Poll, PollAccount> getParticipatedPolls( + TopiaFilterPagerUtil.FilterPagerBean pager, + UserAccount userToUse) throws TopiaException { Preconditions.checkNotNull(pager); Preconditions.checkNotNull(userToUse); @@ -144,9 +161,27 @@ TopiaQuery query = TopiaFilterPagerUtil.addPagerToQuery(countQuery, pager); - List<E> result = findAllByQuery(query); + return findAllWithPollAccounts(query, + "e." + Vote.PROPERTY_POLL, + "e." + Vote.PROPERTY_POLL_ACCOUNT); + } + + protected Map<Poll, PollAccount> findAllWithPollAccounts(TopiaQuery query, + String pollAlias, + String pollAccountAlias) + throws TopiaException { + + // Select both poll and pollAccount + query.setSelect(pollAlias, pollAccountAlias); + + List<Object[]> queryResults = getContext().findByQuery(query); + Map<Poll, PollAccount> result = Maps.newLinkedHashMap(); + for (Object[] row : queryResults) { + Poll poll = (Poll) row[0]; + PollAccount pollAccount = (PollAccount) row[1]; + result.put(poll, pollAccount); + } return result; - } public List<E> getRunningPolls(boolean withEndDate) throws TopiaException { 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-04-27 10:24:54 UTC (rev 3328) +++ trunk/pollen-services/src/main/java/org/chorem/pollen/services/impl/PollService.java 2012-04-30 10:45:14 UTC (rev 3329) @@ -467,8 +467,9 @@ } } - public List<Poll> getInvitedPolls(TopiaFilterPagerUtil.FilterPagerBean pager, - UserAccount user) { + public Map<Poll, PollAccount> getInvitedPolls( + TopiaFilterPagerUtil.FilterPagerBean pager, + UserAccount user) { Preconditions.checkNotNull(pager); Preconditions.checkNotNull(user); @@ -480,7 +481,8 @@ Preconditions.checkNotNull(userToUse); try { PollDAO pollDao = getDAO(Poll.class); - List<Poll> result = pollDao.getInvitedPolls(pager, userToUse); + Map<Poll, PollAccount> result = + pollDao.getInvitedPolls(pager, userToUse); return result; } catch (TopiaException e) { throw new PollenTechnicalException("Could not obtain invited polls", e); @@ -505,8 +507,9 @@ // } } - public List<Poll> getParticipatedPolls(TopiaFilterPagerUtil.FilterPagerBean pager, - UserAccount user) { + public Map<Poll, PollAccount> getParticipatedPolls( + TopiaFilterPagerUtil.FilterPagerBean pager, + UserAccount user) { Preconditions.checkNotNull(pager); Preconditions.checkNotNull(user); @@ -519,7 +522,8 @@ try { PollDAO pollDao = getDAO(Poll.class); - List<Poll> result = pollDao.getParticipatedPolls(pager, userToUse); + Map<Poll, PollAccount> result = + pollDao.getParticipatedPolls(pager, userToUse); return result; } catch (TopiaException e) { throw new PollenTechnicalException("Could not obtain running polls", e); Modified: trunk/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/json/GetCreatedPolls.java =================================================================== --- trunk/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/json/GetCreatedPolls.java 2012-04-27 10:24:54 UTC (rev 3328) +++ trunk/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/json/GetCreatedPolls.java 2012-04-30 10:45:14 UTC (rev 3329) @@ -25,6 +25,7 @@ import com.google.common.collect.Sets; import org.chorem.pollen.business.persistence.Poll; +import org.chorem.pollen.common.PollType; import org.chorem.pollen.entities.PollenBinderHelper; import org.chorem.pollen.services.impl.PollService; import org.nuiton.util.beans.Binder; @@ -91,8 +92,12 @@ Poll.PROPERTY_BEGIN_DATE, Poll.PROPERTY_END_DATE ); + map.put("id", poll.getTopiaId()); + map.put("voteId", getVoteId(poll)); + map.put("resultId", getResultId(poll)); map.put("adminId", poll.getAdminId()); + Set<String> functions = getPollFunctions(poll); map.put("functions", functions); polls[index++] = map; @@ -115,4 +120,34 @@ result.add("delete"); return result; } + + protected String getVoteId(Poll poll) { + String result; + if (PollType.FREE == poll.getPollType()) { + result = poll.getPollId(); + + } else { + result = poll.getAdminId(); + } + return result; + } + + /** + * Retrieve the id for result page depends on {@code poll}. + * It's not necessary to use {@code adminId} if the poll results are public. + * But the {@code adminId} is mandatory if the poll is restricted (security). + * + * @param poll Poll + * @return the pollId if results are public or the adminId if not + */ + protected String getResultId(Poll poll) { + String result; + if (PollType.FREE == poll.getPollType() && poll.isPublicResults()) { + result = poll.getPollId(); + + } else { + result = poll.getAdminId(); + } + return result; + } } \ No newline at end of file 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-04-27 10:24:54 UTC (rev 3328) +++ trunk/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/json/GetInvitedPolls.java 2012-04-30 10:45:14 UTC (rev 3329) @@ -24,15 +24,13 @@ package org.chorem.pollen.ui.actions.json; import com.google.common.collect.Sets; -import org.chorem.pollen.business.persistence.PersonToList; +import org.chorem.pollen.bean.PollUri; import org.chorem.pollen.business.persistence.Poll; import org.chorem.pollen.business.persistence.PollAccount; -import org.chorem.pollen.business.persistence.VotingList; import org.chorem.pollen.entities.PollenBinderHelper; 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; @@ -77,15 +75,16 @@ PollService pollService = newService(PollService.class); - List<Poll> pollList = pollService.getInvitedPolls(pager, + Map<Poll, PollAccount> invitedPolls = pollService.getInvitedPolls(pager, getPollenUserAccount()); - polls = new Map[pollList.size()]; + polls = new Map[invitedPolls.size()]; Binder<Poll, Poll> binder = PollenBinderHelper.getSimpleTopiaBinder(Poll.class); int index = 0; - for (Poll poll : pollList) { + for (Map.Entry<Poll, PollAccount> entry : invitedPolls.entrySet()) { + Poll poll = entry.getKey(); Map<String, Object> map = binder.obtainProperties( poll, Poll.PROPERTY_TITLE, @@ -94,10 +93,15 @@ Poll.PROPERTY_BEGIN_DATE, Poll.PROPERTY_END_DATE ); - String userEmail = getPollenUserAccount().getEmail(); - map.put("adminId", poll.getAdminId()); - map.put("voteId", getVoteId(poll, userEmail)); + + 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; @@ -115,19 +119,4 @@ } return result; } - - protected String getVoteId(Poll poll, String userEmail) { - String result = poll.getPollId(); - for (VotingList list : poll.getVotingList()) { - for (PersonToList account : list.getPollAccountPersonToList()) { - PollAccount pollAccount = account.getPollAccount(); - if (userEmail.equals(pollAccount.getEmail())) { - result += ":" + pollAccount.getAccountId(); - break; - } - } - } - - return result; - } } \ No newline at end of file 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-04-27 10:24:54 UTC (rev 3328) +++ trunk/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/json/GetParticipatedPolls.java 2012-04-30 10:45:14 UTC (rev 3329) @@ -24,12 +24,14 @@ package org.chorem.pollen.ui.actions.json; import com.google.common.collect.Sets; +import org.chorem.pollen.bean.PollUri; import org.chorem.pollen.business.persistence.Poll; +import org.chorem.pollen.business.persistence.PollAccount; +import org.chorem.pollen.common.PollType; import org.chorem.pollen.entities.PollenBinderHelper; 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; @@ -74,15 +76,16 @@ PollService pollService = newService(PollService.class); - List<Poll> pollList = pollService.getParticipatedPolls( - pager, getPollenUserAccount()); + Map<Poll, PollAccount> participatedPolls = + pollService.getParticipatedPolls(pager, getPollenUserAccount()); - polls = new Map[pollList.size()]; + polls = new Map[participatedPolls.size()]; Binder<Poll, Poll> binder = PollenBinderHelper.getSimpleTopiaBinder(Poll.class); int index = 0; - for (Poll poll : pollList) { + for (Map.Entry<Poll, PollAccount> entry : participatedPolls.entrySet()) { + Poll poll = entry.getKey(); Map<String, Object> map = binder.obtainProperties( poll, Poll.PROPERTY_TITLE, @@ -91,7 +94,22 @@ 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 + map.put("voteId", pollUri.getUri()); + + // For Free poll, it's not necessary to keep the accountId + String resultId = poll.getPollType() == PollType.FREE + ? pollUri.getPollId() + : pollUri.getUri(); + map.put("resultId", resultId); + 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/GetPolls.java =================================================================== --- trunk/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/json/GetPolls.java 2012-04-27 10:24:54 UTC (rev 3328) +++ trunk/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/json/GetPolls.java 2012-04-30 10:45:14 UTC (rev 3329) @@ -92,7 +92,10 @@ Poll.PROPERTY_END_DATE ); map.put("id", poll.getTopiaId()); + map.put("voteId", poll.getAdminId()); + map.put("resultId", poll.getAdminId()); map.put("adminId", poll.getAdminId()); + Set<String> functions = getPollFunctions(poll); map.put("functions", functions); polls[index++] = map; Modified: trunk/pollen-ui-struts2/src/main/webapp/WEB-INF/jsp/pollListHelper.jsp =================================================================== --- trunk/pollen-ui-struts2/src/main/webapp/WEB-INF/jsp/pollListHelper.jsp 2012-04-27 10:24:54 UTC (rev 3328) +++ trunk/pollen-ui-struts2/src/main/webapp/WEB-INF/jsp/pollListHelper.jsp 2012-04-30 10:45:14 UTC (rev 3329) @@ -83,19 +83,17 @@ function pollFunctions(cellvalue, options, rowObject) { var id = rowObject.pollId; + var voteId = rowObject.voteId; + var resultId = rowObject.resultId; var adminId = rowObject.adminId; var result = ""; - if (rowObject['voteId']) { - id = rowObject['voteId']; - } - if (cellvalue.indexOf('vote') > -1) { - result += formatLink("${voteUrl}" + id, "${voteImg}", "Vote", "${voteTitle}") + result += formatLink("${voteUrl}" + voteId, "${voteImg}", "Vote", "${voteTitle}") } if (cellvalue.indexOf('result') > -1) { - result += formatLink("${resultUrl}" + adminId, "${resultImg}", "Result", "${resultTitle}") + result += formatLink("${resultUrl}" + resultId, "${resultImg}", "Result", "${resultTitle}") } if (cellvalue.indexOf('edit') > -1) { result += formatLink("${editUrl}" + adminId, "${editImg}", "Edit", "${editTitle}")