r3707 - in trunk: pollen-persistence/src/main/java/org/chorem/pollen/business/persistence pollen-services/src/main/java/org/chorem/pollen/services/impl pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/poll pollen-ui-struts2/src/main/resources/i18n pollen-ui-struts2/src/main/webapp/WEB-INF/jsp/poll pollen-votecounting-api/src/main/java/org/chorem/pollen/votecounting/model
Author: tchemit Date: 2012-09-25 17:32:42 +0200 (Tue, 25 Sep 2012) New Revision: 3707 Url: http://chorem.org/repositories/revision/pollen/3707 Log: refs #590: Refactor votecounting module (make group vote counting working again) Modified: trunk/pollen-persistence/src/main/java/org/chorem/pollen/business/persistence/Polls.java trunk/pollen-services/src/main/java/org/chorem/pollen/services/impl/PollVoteCountingService.java trunk/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/poll/ResultForPoll.java trunk/pollen-ui-struts2/src/main/resources/i18n/pollen-ui-struts2_en_GB.properties trunk/pollen-ui-struts2/src/main/resources/i18n/pollen-ui-struts2_fr_FR.properties trunk/pollen-ui-struts2/src/main/webapp/WEB-INF/jsp/poll/result.jsp trunk/pollen-votecounting-api/src/main/java/org/chorem/pollen/votecounting/model/GroupVoteCountingResult.java Modified: trunk/pollen-persistence/src/main/java/org/chorem/pollen/business/persistence/Polls.java =================================================================== --- trunk/pollen-persistence/src/main/java/org/chorem/pollen/business/persistence/Polls.java 2012-09-23 19:07:20 UTC (rev 3706) +++ trunk/pollen-persistence/src/main/java/org/chorem/pollen/business/persistence/Polls.java 2012-09-25 15:32:42 UTC (rev 3707) @@ -57,8 +57,21 @@ return PollType.FREE == poll.getPollType(); } - public static GroupOfVoter toSimpleGroupOfVoters(Poll poll) { + /** + * Collect all vote for all voters of the givne poll. + * <p/> + * The result (a set of simple voters) are stored in a fictiv group of voter. + * <p/> + * <strong>Note:</strong> Even if the poll is a group type, then no of his + * group are used (just collect simple voters with no group linkage). + * + * @param poll the poll to scan + * @return the aggregate group of voter containing simple voters with + * their votes. + */ + public static GroupOfVoter toSimpleVotersGroup(Poll poll) { Preconditions.checkNotNull(poll); + SimpleVoterBuilder builder = new SimpleVoterBuilder(); for (Vote vote : poll.getVote()) { @@ -69,8 +82,20 @@ return result; } + /** + * Build the group of voter of the poll, using his group to build sub + * group of the poll. + * <p/> + * <strong>Note:</strong> This method can only be used for a group poll. + * + * @param poll the poll to scan + * @return the aggregate group of group of voters with their votes + */ public static GroupOfVoter toGroupOfVoters(Poll poll) { Preconditions.checkNotNull(poll); + Preconditions.checkArgument( + isGroupPoll(poll), "Can only use this method for a group poll"); + GroupOfVoterBuilder builder = new GroupOfVoterBuilder(); // create group of voter Modified: trunk/pollen-services/src/main/java/org/chorem/pollen/services/impl/PollVoteCountingService.java =================================================================== --- trunk/pollen-services/src/main/java/org/chorem/pollen/services/impl/PollVoteCountingService.java 2012-09-23 19:07:20 UTC (rev 3706) +++ trunk/pollen-services/src/main/java/org/chorem/pollen/services/impl/PollVoteCountingService.java 2012-09-25 15:32:42 UTC (rev 3707) @@ -46,7 +46,8 @@ LogFactory.getLog(PollVoteCountingService.class); /** - * Execute a vote counting for a given poll. + * Execute a simple vote counting for a given poll (if the poll + * is a group poll, none of his group will be used). * * @param poll poll on wich vote count * @return vote counting result @@ -59,7 +60,7 @@ Preconditions.checkNotNull(strategy); // create vote counting model - GroupOfVoter group = Polls.toSimpleGroupOfVoters(poll); + GroupOfVoter group = Polls.toSimpleVotersGroup(poll); // compute result by the strategy GroupVoteCountingResult result = strategy.votecount(group); Modified: trunk/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/poll/ResultForPoll.java =================================================================== --- trunk/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/poll/ResultForPoll.java 2012-09-23 19:07:20 UTC (rev 3706) +++ trunk/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/poll/ResultForPoll.java 2012-09-25 15:32:42 UTC (rev 3707) @@ -36,6 +36,7 @@ import org.chorem.pollen.business.persistence.Poll; import org.chorem.pollen.business.persistence.PollAccount; import org.chorem.pollen.business.persistence.Polls; +import org.chorem.pollen.business.persistence.VotingList; import org.chorem.pollen.services.impl.PollVoteCountingService; import org.chorem.pollen.ui.PollenUIUtils; import org.chorem.pollen.ui.actions.PageSkin; @@ -82,20 +83,29 @@ */ private Poll poll; + /** Poll main votecounting result. */ + private VoteCountingResult pollResult; + + /** Group result (if poll is group and {]code byGroup} flag is on). */ private GroupVoteCountingResult groupResult; - private VoteCountingResult pollResult; - + /** choices of the poll indexed by ther id. */ private Map<String, Choice> choicesById; + /** group of the poll indexed by ther id. */ + private Map<String, VotingList> groupsById; + /** * Url helper (to build new url) * * @since 1.3.1 */ @Inject(required = true) - private UrlHelper urlHelper; + private transient UrlHelper urlHelper; + /** Flag to votecount by group (only used if a poll is group). */ + private Boolean byGroup; + public ResultForPoll() { super(PageSkin.RESULT); } @@ -112,6 +122,14 @@ return feedFileExisting; } + public Boolean getByGroup() { + return byGroup; + } + + public void setByGroup(Boolean byGroup) { + this.byGroup = byGroup; + } + public String getVoteCountingTypeName() { VoteCountingStrategy strategy = getVoteCountingStrategy(getPoll()); String result = strategy.getStrategyName(getLocale()); @@ -178,6 +196,10 @@ return result; } + public Set<String> getGroupIds() { + return groupResult.getGroupIds(); + } + public GroupVoteCountingResult getGroupResult() { return groupResult; } @@ -216,6 +238,12 @@ public String execute() throws Exception { poll = getUserSecurityContext().getPoll(); + if (byGroup == null) { + + // nothing defined, use default value + byGroup = isGroupPoll(); + } + choicesById = Maps.uniqueIndex(poll.getChoice(), TopiaId.GET_TOPIA_ID); feedFileExisting = getPollFeedService().isFeedExists(poll); @@ -223,8 +251,14 @@ PollVoteCountingService voteCountingService = getService(PollVoteCountingService.class); - if (isGroupPoll()) { + if (isGroupPoll() && getByGroup()) { + groupsById = Maps.newHashMap(); + + for (VotingList votingList : poll.getVotingList()) { + groupsById.put(votingList.getTopiaId(), votingList); + } + // load group result groupResult = voteCountingService.getGroupResult(poll); @@ -269,12 +303,23 @@ } public String getNormalPollResultChartUrl(int width, int height) { - return getPollResultChartUrl(pollResult, width, height); + String title; + if (isGroupPoll() && !getByGroup()) { + + // means poll votecount as anot group poll + title = _("pollen.common.pollResult.forPollAsNotGroupPoll"); + } else { + title = _("pollen.common.pollResult.forPoll"); + + } + return getPollResultChartUrl(pollResult, title, width, height); } public String getGroupPollResultChartUrl(String groupId, int width, int height) { VoteCountingResult result = groupResult.getGroupResult(groupId); - return getPollResultChartUrl(result, width, height); + String groupName = getGroupName(groupId); + String title = _("pollen.common.pollResult.forGroup", groupName); + return getPollResultChartUrl(result, title, width, height); } public String getChoiceName(String id) { @@ -282,7 +327,13 @@ return result; } + public String getGroupName(String id) { + String result = groupsById.get(id).getName(); + return result; + } + protected String getPollResultChartUrl(VoteCountingResult results, + String title, int width, int height) { List<String> choiceValues = Lists.newArrayList(); @@ -310,7 +361,7 @@ Map<String, Object> params = Maps.newHashMap(); params.put("width", width); params.put("height", height); - params.put("title", ""); + params.put("title", title); params.put("values", choiceValues); urlHelper.buildParametersString(params, url, "&"); Modified: trunk/pollen-ui-struts2/src/main/resources/i18n/pollen-ui-struts2_en_GB.properties =================================================================== --- trunk/pollen-ui-struts2/src/main/resources/i18n/pollen-ui-struts2_en_GB.properties 2012-09-23 19:07:20 UTC (rev 3706) +++ trunk/pollen-ui-struts2/src/main/resources/i18n/pollen-ui-struts2_en_GB.properties 2012-09-25 15:32:42 UTC (rev 3707) @@ -140,6 +140,9 @@ pollen.common.pollOption.reminder=Send reminder emails pollen.common.pollOption.reminder.help=An email is sent to remind persons who have not yet voted N hours before poll end pollen.common.pollOption.reminderHourCountdown=Hours before end +pollen.common.pollResult.forGroup=Poll result for group %s +pollen.common.pollResult.forPoll=Poll result +pollen.common.pollResult.forPollAsNotGroupPoll=Poll result (without taking account of groups) pollen.common.pollType=Who can vote ? pollen.common.pollType.help=Free\: accessible to everyone <br/><br/> Restricted\: accessible only to a list of persons <br/><br/> Group\: accessible to several lists of persons pollen.common.pollVoteVisibility=Vote visibility Modified: trunk/pollen-ui-struts2/src/main/resources/i18n/pollen-ui-struts2_fr_FR.properties =================================================================== --- trunk/pollen-ui-struts2/src/main/resources/i18n/pollen-ui-struts2_fr_FR.properties 2012-09-23 19:07:20 UTC (rev 3706) +++ trunk/pollen-ui-struts2/src/main/resources/i18n/pollen-ui-struts2_fr_FR.properties 2012-09-25 15:32:42 UTC (rev 3707) @@ -141,6 +141,9 @@ pollen.common.pollOption.reminder=Envoyer des emails de rappel pollen.common.pollOption.reminder.help=Un email de rappel est envoyé aux votants qui n'ont pas encore voté N heures avant la fin du sondage pollen.common.pollOption.reminderHourCountdown=heures avant la fin du sondage +pollen.common.pollResult.forGroup=Résultat du sondage pour le groupe %s +pollen.common.pollResult.forPoll=Résultat du sondage +pollen.common.pollResult.forPollAsNotGroupPoll=Résultat du sondage (sans tenir compte des groupes) pollen.common.pollType=Qui peut voter ? pollen.common.pollType.help=Libre \: accessible à tout le monde <br/><br/> Restreint \: accessible uniquement à une liste de votants <br/><br/> Groupe \: accessible à plusieurs listes de votants pollen.common.pollVoteVisibility=Visibilité des votes Modified: trunk/pollen-ui-struts2/src/main/webapp/WEB-INF/jsp/poll/result.jsp =================================================================== --- trunk/pollen-ui-struts2/src/main/webapp/WEB-INF/jsp/poll/result.jsp 2012-09-23 19:07:20 UTC (rev 3706) +++ trunk/pollen-ui-struts2/src/main/webapp/WEB-INF/jsp/poll/result.jsp 2012-09-25 15:32:42 UTC (rev 3707) @@ -47,11 +47,17 @@ var chartUrls = {}; chartUrls['chart_normal'] = "<s:url value='%{getNormalPollResultChartUrl(400, 300)}' escapeAmp='false'/>"; - <s:if test="numberVoteCounting"> - <s:iterator value="choicesResultNames" var="choicesResultName" status="status"> - chartUrls['chart_<s:property value="#status.index"/>'] = "<s:url value='%{getChoicesResultCharUrl(400,300, #choicesResultName)}' escapeAmp='false'/>"; + <s:if test="byGroup"> + //Add all result by group + <s:iterator value="groupIds" var="groupId" status="status"> + chartUrls['chart_Group_<s:property value="#status.index"/>'] = "<s:url value='%{getGroupPollResultChartUrl(#groupId, 400, 300)}' escapeAmp='false'/>"; </s:iterator> - </s:if> + </s:if> + <%--<s:if test="numberVoteCounting">--%> + <%--<s:iterator value="choicesResultNames" var="choicesResultName" status="status">--%> + <%--chartUrls['chart_<s:property value="#status.index"/>'] = "<s:url value='%{getChoicesResultCharUrl(400,300, #choicesResultName)}' escapeAmp='false'/>";--%> + <%--</s:iterator>--%> + <%--</s:if>--%> jQuery(document).ready(function () { @@ -132,28 +138,44 @@ </sj:a> </div> - <%--<div class="displayTypeDiv">--%> - <%--<s:if test="groupPoll">--%> - <%--<s:text name="pollen.common.results"/>--%> - <%--<s:if test="byGroup">--%> - <%--<s:a href="%{getResultUrl(false)}">--%> - <%--<s:text name="pollen.common.normal"/>--%> - <%--</s:a>--%> - <%--- <s:text name="pollen.common.group"/>--%> - <%--</s:if>--%> - <%--<s:else>--%> - <%--<s:text name="pollen.common.normal"/> ---%> - <%--<s:a href="%{getResultUrl(true)}">--%> - <%--<s:text name="pollen.common.group"/>--%> - <%--</s:a>--%> - <%--</s:else>--%> - <%--</s:if>--%> - <%--</div>--%> + <div class="displayTypeDiv"> + <s:if test="groupPoll"> + <s:text name="pollen.common.results"/> + <s:if test="byGroup"> + <s:a href="%{getResultUrl(false)}"> + <s:text name="pollen.common.normal"/> + </s:a> + - <s:text name="pollen.common.group"/> + </s:if> + <s:else> + <s:text name="pollen.common.normal"/> - + <s:a href="%{getResultUrl(true)}"> + <s:text name="pollen.common.group"/> + </s:a> + </s:else> + </s:if> + </div> <sj:div id='chartContainer'> <img id='chart_normal'/> </sj:div> + <%-- result by group --%> + <s:if test="byGroup"> + <div id="resultChart"> + <div> + <s:iterator value="groupIds" var="groupId" status="status"> + <hr/> + <s:set name="groupName" value="%{getGroupName(#groupId)}"/> + <img id='chart_Group_<s:property value="#status.index"/>' + alt="<s:property value='%{#groupName}'/>" + title="<s:property value='%{#groupName}'/>"/> + </s:iterator> + </div> + <br/> + </div> + </s:if> + <%-- Diagramme par choix --%> <%--<s:if test="choicesResults != null">--%> <%--<div id="resultChart">--%> Modified: trunk/pollen-votecounting-api/src/main/java/org/chorem/pollen/votecounting/model/GroupVoteCountingResult.java =================================================================== --- trunk/pollen-votecounting-api/src/main/java/org/chorem/pollen/votecounting/model/GroupVoteCountingResult.java 2012-09-23 19:07:20 UTC (rev 3706) +++ trunk/pollen-votecounting-api/src/main/java/org/chorem/pollen/votecounting/model/GroupVoteCountingResult.java 2012-09-25 15:32:42 UTC (rev 3707) @@ -24,6 +24,7 @@ import com.google.common.base.Function; import com.google.common.collect.Maps; +import com.google.common.collect.Sets; import java.io.Serializable; import java.util.Map; @@ -56,6 +57,15 @@ return voter.getResult(); } + public Set<String> getGroupIds() { + return Sets.newHashSet(groups.keySet()); + } + public VoteCountingResult getGroupResult(String groupId) { + GroupOfVoter groupOfVoter = groups.get(groupId); + VoteCountingResult result = groupOfVoter.getResult(); + return result; + } + public void setVoter(GroupOfVoter voter) { this.voter = voter; } @@ -68,10 +78,4 @@ } }); } - - public VoteCountingResult getGroupResult(String groupId) { - GroupOfVoter groupOfVoter = groups.get(groupId); - VoteCountingResult result = groupOfVoter.getResult(); - return result; - } }
participants (1)
-
tchemit@users.chorem.org