Author: tchemit Date: 2012-09-30 14:57:25 +0200 (Sun, 30 Sep 2012) New Revision: 3712 Url: http://chorem.org/repositories/revision/pollen/3712 Log: fixes #816: Remove hidden choice mecanism fixes #815: Do not persist any longer results fix logging dependencies (doomed by slf4j :() remove PollResultService Added: trunk/pollen-services/src/main/java/org/chorem/pollen/services/impl/PollExportService.java Removed: trunk/pollen-services/src/main/java/org/chorem/pollen/bean/PollResult.java trunk/pollen-services/src/main/java/org/chorem/pollen/services/impl/PollResultsService.java Modified: trunk/pollen-persistence/pom.xml trunk/pollen-persistence/src/main/java/org/chorem/pollen/business/persistence/PollImpl.java trunk/pollen-persistence/src/main/java/org/chorem/pollen/business/persistence/Polls.java trunk/pollen-persistence/src/main/xmi/pollen.zargo trunk/pollen-services/pom.xml trunk/pollen-services/src/main/java/org/chorem/pollen/entities/migration/PollenMigrationCallbackV1_4.java trunk/pollen-services/src/main/java/org/chorem/pollen/entities/migration/PollenMigrationCallbackV1_5.java trunk/pollen-services/src/main/java/org/chorem/pollen/services/PollenServiceFunctions.java trunk/pollen-services/src/main/java/org/chorem/pollen/services/impl/PollFeedService.java trunk/pollen-services/src/main/java/org/chorem/pollen/services/impl/PollVoteCountingService.java trunk/pollen-services/src/main/java/org/chorem/pollen/services/impl/VoteService.java trunk/pollen-ui-struts2/pom.xml trunk/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/PollenActionSupport.java trunk/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/poll/admin/ExportPoll.java trunk/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/poll/vote/AbstractVoteAction.java trunk/pollen-ui-struts2/src/main/webapp/WEB-INF/jsp/poll/vote.jsp trunk/pollen-votecounting-api/src/main/java/org/chorem/pollen/votecounting/model/VoteCountingResult.java trunk/pom.xml Modified: trunk/pollen-persistence/pom.xml =================================================================== --- trunk/pollen-persistence/pom.xml 2012-09-29 21:19:04 UTC (rev 3711) +++ trunk/pollen-persistence/pom.xml 2012-09-30 12:57:25 UTC (rev 3712) @@ -75,6 +75,12 @@ </dependency> <dependency> + <groupId>org.slf4j</groupId> + <artifactId>slf4j-jcl</artifactId> + <scope>test</scope> + </dependency> + + <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <scope>test</scope> Modified: trunk/pollen-persistence/src/main/java/org/chorem/pollen/business/persistence/PollImpl.java =================================================================== --- trunk/pollen-persistence/src/main/java/org/chorem/pollen/business/persistence/PollImpl.java 2012-09-29 21:19:04 UTC (rev 3711) +++ trunk/pollen-persistence/src/main/java/org/chorem/pollen/business/persistence/PollImpl.java 2012-09-30 12:57:25 UTC (rev 3712) @@ -38,6 +38,16 @@ } @Override + public boolean isPollGroup() { + return PollType.GROUP == getPollType(); + } + + @Override + public boolean isPollRestricted() { + return PollType.RESTRICTED == getPollType(); + } + + @Override public boolean isAnonymous() { return PollVoteVisibility.NOBODY == getPollVoteVisibility(); } @@ -59,14 +69,6 @@ } @Override - public List<Result> getResult() { - if (result == null) { - result = Lists.newArrayList(); - } - return result; - } - - @Override public List<Comment> getComment() { if (comment == null) { comment = Lists.newArrayList(); 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-29 21:19:04 UTC (rev 3711) +++ trunk/pollen-persistence/src/main/java/org/chorem/pollen/business/persistence/Polls.java 2012-09-30 12:57:25 UTC (rev 3712) @@ -94,7 +94,8 @@ public static GroupOfVoter toGroupOfVoters(Poll poll) { Preconditions.checkNotNull(poll); Preconditions.checkArgument( - isGroupPoll(poll), "Can only use this method for a group poll"); + poll.isPollGroup(), + "Can only use this method for a group poll"); GroupOfVoterBuilder builder = new GroupOfVoterBuilder(); Modified: trunk/pollen-persistence/src/main/xmi/pollen.zargo =================================================================== (Binary files differ) Modified: trunk/pollen-services/pom.xml =================================================================== --- trunk/pollen-services/pom.xml 2012-09-29 21:19:04 UTC (rev 3711) +++ trunk/pollen-services/pom.xml 2012-09-30 12:57:25 UTC (rev 3712) @@ -105,6 +105,12 @@ </dependency> <dependency> + <groupId>org.slf4j</groupId> + <artifactId>slf4j-jcl</artifactId> + <scope>test</scope> + </dependency> + + <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <scope>test</scope> Deleted: trunk/pollen-services/src/main/java/org/chorem/pollen/bean/PollResult.java =================================================================== --- trunk/pollen-services/src/main/java/org/chorem/pollen/bean/PollResult.java 2012-09-29 21:19:04 UTC (rev 3711) +++ trunk/pollen-services/src/main/java/org/chorem/pollen/bean/PollResult.java 2012-09-30 12:57:25 UTC (rev 3712) @@ -1,126 +0,0 @@ -/* - * #%L - * Pollen :: Services - * $Id$ - * $HeadURL$ - * %% - * Copyright (C) 2009 - 2012 CodeLutin, Tony Chemit - * %% - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - * #L% - */ -package org.chorem.pollen.bean; - -import org.apache.commons.lang3.builder.ToStringBuilder; -import org.chorem.pollen.business.persistence.ChoiceType; - -import java.io.Serializable; - -/** - * Classe DTO représentant un résultat de sondage. - * - * @since 1.3 - */ -public class PollResult implements Serializable { - - private static final long serialVersionUID = 1L; - - private String id; - - private String name; - - private String value; - - private String pollId; - - private ChoiceType choiceType; - - private int voteCountingType; - - private boolean byGroup; - - private boolean hidden; - - public String getId() { - return id; - } - - public void setId(String id) { - this.id = id; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public String getValue() { - return value; - } - - public void setValue(String value) { - this.value = value; - } - - public String getPollId() { - return pollId; - } - - public void setPollId(String pollId) { - this.pollId = pollId; - } - - public ChoiceType getChoiceType() { - return choiceType; - } - - public void setChoiceType(ChoiceType choiceType) { - this.choiceType = choiceType; - } - - public int getVoteCountingType() { - return voteCountingType; - } - - public void setVoteCountingType(int voteCountingType) { - this.voteCountingType = voteCountingType; - } - - public boolean isByGroup() { - return byGroup; - } - - public void setByGroup(boolean byGroup) { - this.byGroup = byGroup; - } - - public boolean isHidden() { - return hidden; - } - - public void setHidden(boolean hidden) { - this.hidden = hidden; - } - - @Override - public String toString() { - return new ToStringBuilder(this). - append("name", name). - append("value", value). - toString(); - } -} Modified: trunk/pollen-services/src/main/java/org/chorem/pollen/entities/migration/PollenMigrationCallbackV1_4.java =================================================================== --- trunk/pollen-services/src/main/java/org/chorem/pollen/entities/migration/PollenMigrationCallbackV1_4.java 2012-09-29 21:19:04 UTC (rev 3711) +++ trunk/pollen-services/src/main/java/org/chorem/pollen/entities/migration/PollenMigrationCallbackV1_4.java 2012-09-30 12:57:25 UTC (rev 3712) @@ -241,8 +241,7 @@ } private void addResultUptodateToPoll(List<String> queries) { - queries.add("ALTER TABLE poll ADD COLUMN " + - Poll.PROPERTY_RESULT_UPTODATE + " boolean default false;"); + queries.add("ALTER TABLE poll ADD COLUMN resultuptodate boolean default false;"); } 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-29 21:19:04 UTC (rev 3711) +++ trunk/pollen-services/src/main/java/org/chorem/pollen/entities/migration/PollenMigrationCallbackV1_5.java 2012-09-30 12:57:25 UTC (rev 3712) @@ -98,8 +98,27 @@ // generate missing accountId // see http://chorem.org/issues/814 generateMissingAccountId(tx, sContext, queries); + + // remove result from persistance + // see http://chorem.org/issues/815 + removeResults(tx, queries); + + // remove hidden choices + // see http://chorem.org/issues/816 + removeHiddenChoices(tx, queries); } + private void removeResults(TopiaContextImplementor tx, List<String> queries) { + queries.add("DROP TABLE result;"); + queries.add("ALTER TABLE poll DROP COLUMN resultUptodate"); + } + + private void removeHiddenChoices(TopiaContextImplementor tx, List<String> queries) { + + queries.add("DELETE FROM votetochoice WHERE choice in (SELECT topiaid FROM choice WHERE name LIKE 'HIDDEN_%');"); + queries.add("DELETE FROM choice WHERE name LIKE 'HIDDEN_%';"); + } + private void removeDuplicateVoteWithSameUserAccount(TopiaContextImplementor tx, PollenServiceContext sContext, List<String> queries) throws TopiaException { Modified: trunk/pollen-services/src/main/java/org/chorem/pollen/services/PollenServiceFunctions.java =================================================================== --- trunk/pollen-services/src/main/java/org/chorem/pollen/services/PollenServiceFunctions.java 2012-09-29 21:19:04 UTC (rev 3711) +++ trunk/pollen-services/src/main/java/org/chorem/pollen/services/PollenServiceFunctions.java 2012-09-30 12:57:25 UTC (rev 3712) @@ -26,19 +26,15 @@ import com.google.common.collect.Lists; import org.chorem.pollen.bean.PollDateChoice; import org.chorem.pollen.bean.PollImageChoice; -import org.chorem.pollen.bean.PollResult; import org.chorem.pollen.business.persistence.Choice; import org.chorem.pollen.business.persistence.ChoiceImpl; import org.chorem.pollen.business.persistence.ChoiceType; import org.chorem.pollen.business.persistence.PersonToList; import org.chorem.pollen.business.persistence.PersonToListImpl; -import org.chorem.pollen.business.persistence.Poll; import org.chorem.pollen.business.persistence.PollAccount; import org.chorem.pollen.business.persistence.PollAccountImpl; -import org.chorem.pollen.business.persistence.Result; import org.chorem.pollen.business.persistence.VotingList; import org.chorem.pollen.business.persistence.VotingListImpl; -import org.nuiton.topia.persistence.TopiaEntity; import java.util.Date; import java.util.List; @@ -72,28 +68,6 @@ } } -// public static final Function<Poll, Set<Voter>> POLL_TO_VOTERS = new Function<Poll, Set<Voter>>() { -// @Override -// public Set<Voter> apply(Poll input) { -// -// VoterBuilder builder = new SimpleVoterBuilder(); -// for (Vote vote : input.getVote()) { -// -// String voterId = vote.getPollAccount().getAccountId(); -// double voteWeight = vote.getWeight(); -// builder.newVoter(voterId, voteWeight); -// -// for (VoteToChoice voteToChoice : vote.getChoiceVoteToChoice()) { -// String choiceId = voteToChoice.getChoice().getTopiaId(); -// Double voteValue = Double.valueOf(voteToChoice.getVoteValue()); -// builder.addVoteForChoice(choiceId, voteValue); -// } -// } -// Set<Voter> result = builder.getVoters(); -// return result; -// } -// }; - public static final Function<Choice, String> CHOICE_TO_NAME = new Function<Choice, String>() { @Override public String apply(Choice input) { @@ -120,10 +94,6 @@ } }; - public static TopiaIdExtractor newTopiaIdExtractor() { - return new TopiaIdExtractor(); - } - public static TextChoiceCreator newTextChoiceCreator() { return new TextChoiceCreator(); } @@ -164,14 +134,6 @@ return new PersonToListCreator(); } - public static class TopiaIdExtractor implements Function<TopiaEntity, String> { - - @Override - public String apply(TopiaEntity input) { - return input.getTopiaId(); - } - } - public static class TextChoiceCreator implements Function<Choice, Choice> { @Override @@ -274,26 +236,4 @@ } } - public static Function<Result, PollResult> newResultToBeanFunction(final Poll poll) { - return new Function<Result, PollResult>() { - - @Override - public PollResult apply(Result res) { - PollResult dto = new PollResult(); - dto.setId(res.getTopiaId()); - dto.setPollId(poll.getPollId()); - String resName = res.getName(); - dto.setName(resName); -// dto.setHidden(resName != null && -// resName.startsWith(NumberMethod.HIDDEN_PREFIX)); - dto.setHidden(false); - dto.setValue(res.getResultValue()); - dto.setByGroup(res.isByGroup()); - dto.setChoiceType(poll.getChoiceType()); - dto.setVoteCountingType(poll.getVoteCountingType()); - return dto; - } - }; - } - } Added: trunk/pollen-services/src/main/java/org/chorem/pollen/services/impl/PollExportService.java =================================================================== --- trunk/pollen-services/src/main/java/org/chorem/pollen/services/impl/PollExportService.java (rev 0) +++ trunk/pollen-services/src/main/java/org/chorem/pollen/services/impl/PollExportService.java 2012-09-30 12:57:25 UTC (rev 3712) @@ -0,0 +1,49 @@ +package org.chorem.pollen.services.impl; + +/* + * #%L + * Pollen :: Services + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2009 - 2012 CodeLutin + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * #L% + */ + +import org.chorem.pollen.business.persistence.Poll; +import org.chorem.pollen.services.PollenServiceSupport; +import org.chorem.pollen.services.exceptions.PollNotFoundException; + +/** + * Service to export poll as xml format (or later other formats). + * + * @author tchemit <chemit@codelutin.com> + * @since 1.5 + */ +public class PollExportService extends PollenServiceSupport { + + //FIXME must be done soon + public String exportToXml(String pollId) throws PollNotFoundException { + + // Recherche du sondage + + PollService pollService = newService(PollService.class); + + Poll poll = pollService.getExistingPollByPollId(pollId); + + return "<strong>TODO in Pollen 2.0</strong>"; + } +} Property changes on: trunk/pollen-services/src/main/java/org/chorem/pollen/services/impl/PollExportService.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Modified: trunk/pollen-services/src/main/java/org/chorem/pollen/services/impl/PollFeedService.java =================================================================== --- trunk/pollen-services/src/main/java/org/chorem/pollen/services/impl/PollFeedService.java 2012-09-29 21:19:04 UTC (rev 3711) +++ trunk/pollen-services/src/main/java/org/chorem/pollen/services/impl/PollFeedService.java 2012-09-30 12:57:25 UTC (rev 3712) @@ -145,10 +145,10 @@ public void onVoteAdded(Poll poll, Vote vote) { - PollResultsService pollResultsService = - newService(PollResultsService.class); + PollVoteCountingService service = + newService(PollVoteCountingService.class); - String pollResult = pollResultsService.getResultsAsString(poll); + String pollResult = service.getResultsAsString(poll); String userId; @@ -168,10 +168,10 @@ public void onVoteUpdated(Poll poll, Vote vote) { - PollResultsService pollResultsService = - newService(PollResultsService.class); + PollVoteCountingService service = + newService(PollVoteCountingService.class); - String pollResult = pollResultsService.getResultsAsString(poll); + String pollResult = service.getResultsAsString(poll); String userId; @@ -192,10 +192,10 @@ public void onVoteDeleted(Poll poll, Vote vote, PollAccount voteAccount, String reason) { - PollResultsService pollResultsService = - newService(PollResultsService.class); + PollVoteCountingService service = + newService(PollVoteCountingService.class); - String pollResult = pollResultsService.getResultsAsString(poll); + String pollResult = service.getResultsAsString(poll); String userId; if (vote.isAnonymous()) { Deleted: trunk/pollen-services/src/main/java/org/chorem/pollen/services/impl/PollResultsService.java =================================================================== --- trunk/pollen-services/src/main/java/org/chorem/pollen/services/impl/PollResultsService.java 2012-09-29 21:19:04 UTC (rev 3711) +++ trunk/pollen-services/src/main/java/org/chorem/pollen/services/impl/PollResultsService.java 2012-09-30 12:57:25 UTC (rev 3712) @@ -1,235 +0,0 @@ -/* - * #%L - * Pollen :: Services - * $Id$ - * $HeadURL$ - * %% - * Copyright (C) 2009 - 2012 CodeLutin, Tony Chemit - * %% - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - * #L% - */ -package org.chorem.pollen.services.impl; - -import com.google.common.base.Preconditions; -import com.google.common.collect.Lists; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.chorem.pollen.bean.PollResult; -import org.chorem.pollen.business.persistence.Choice; -import org.chorem.pollen.business.persistence.ChoiceType; -import org.chorem.pollen.business.persistence.Poll; -import org.chorem.pollen.business.persistence.Polls; -import org.chorem.pollen.business.persistence.Result; -import org.chorem.pollen.business.persistence.ResultDAO; -import org.chorem.pollen.services.PollenServiceFunctions; -import org.chorem.pollen.services.PollenServiceSupport; -import org.chorem.pollen.services.exceptions.PollNotFoundException; -import org.chorem.pollen.votecounting.model.ChoiceScore; -import org.chorem.pollen.votecounting.model.VoteCountingResult; - -import java.text.DateFormat; -import java.text.SimpleDateFormat; -import java.util.Collection; -import java.util.Date; -import java.util.Iterator; -import java.util.List; - -/** - * Deals with poll results. - * - * @author tchemit <chemit@codelutin.com> - * @since 1.3 - */ -public class PollResultsService extends PollenServiceSupport { - - /** log. */ - private static final Log log = LogFactory.getLog(PollResultsService.class); - - /** - * Génère les résultats d'un sondage (= un dépouillement) et les stoque en - * base. - * <p/> - * <strong>Note importante :</strong> Le commite n'est pas effectuée dans - * cette méthode car d'autres services vont appeller cette méthode au sein - * d'une transaction et on ne veut pas faire deux commits. - * - * @param poll le sondage à dépouiller - * @since 1.4 - */ - public void generateResult(Poll poll) { - Preconditions.checkNotNull(poll); - - if (log.isInfoEnabled()) { - log.info("Will regenerate results for poll " + poll.getPollId()); - } - - - PollVoteCountingService service = - newService(PollVoteCountingService.class); - - VoteCountingResult mainResult; - - if (Polls.isGroupPoll(poll)) { - mainResult = service.getGroupResult(poll).getMainResult(); - } else { - mainResult = service.getSimpleResult(poll); - } - - boolean groupPoll = Polls.isGroupPoll(poll); - int voteCountingType = poll.getVoteCountingType(); - - // clear result - poll.clearResult(); - - ResultDAO daoResult = getDAO(Result.class); - - for (ChoiceScore choice : mainResult.getScores()) { - - Result eResult = create(daoResult); - - Choice eChoice = poll.getChoiceByTopiaId(choice.getChoiceId()); - - eResult.setName(eChoice.getName()); - eResult.setByGroup(groupPoll); - eResult.setResultValue(String.valueOf(choice.getScoreValue())); - eResult.setVoteCountingType(voteCountingType); - - poll.addResult(eResult); - } - poll.setResultUptodate(true); - } - - /** - * Récurpération des résultats d'un sondage sous forme de - * {@link PollResult}. - * <p/> - * Si les résultats ne sont pas à jour ({@link Poll#isResultUptodate()}, - * alors ils seront regénérés. - * - * @param poll le sondage à traiter - * @return les résultats du sondage. - */ - public List<PollResult> getResults(Poll poll) { - - Preconditions.checkNotNull(poll); - - if (!poll.isResultUptodate()) { - - //must regenerate results - - generateResult(poll); - - commitTransaction("Could not generate poll result"); - } - - // Conversion des résultats - List<PollResult> list = Lists.transform( - poll.getResult(), - PollenServiceFunctions.newResultToBeanFunction(poll)); - return list; - } - - public String exportPolltoXml(String pollId) throws PollNotFoundException { - - // Recherche du sondage - - PollService pollService = newService(PollService.class); - - Poll poll = pollService.getExistingPollByPollId(pollId); - - return "TODO in Pollen 2.0"; -// PollDTO dto = PollenFunctions.POLL_TO_BEAN.apply(poll); -// -// VoteCountingService service = new VoteCountingService(); -// -// List<VoteCountingResultDTO> results = Lists.newArrayList(); -// -// VoteCountingResultDTO result = service.executeVoteCounting(dto); -// -// results.add(result); -// -// if (poll.getPollType() == PollType.GROUP) { -// VoteCountingResultDTO groupResult = -// service.executeGroupCounting(dto); -// results.add(groupResult); -// } -// -// // Transformation du sondage -// PollExportDTO pollExport = new PollExportDTO(); -// pollExport.setPollId(pollId); -// pollExport.setPoll(dto); -// pollExport.setVoteCountingResults(results); -// -// // Export du sondage -// ImportExportService serviceExport = new ImportExportService(); -// String content = serviceExport.exportToXml(pollExport); -// -// return content; - } - - public String getResultValue(Choice choice, - Collection<PollResult> results) { - - String val = ""; - for (PollResult result : results) { - if (result.getName().equals(choice.getName())) { - val = removeTrailing0(result.getValue()); - break; - } - } - return val; - } - - /** - * Supprime le 0 final d'un nombre à virgule. Le résultat peut-être un - * double : 1,0 -> 1 et 1,2 -> 1,2. - * - * @param val le nombre - * @return le nombre sans 0 final - */ - protected String removeTrailing0(String val) { - if (val.endsWith(".0")) { - val = val.substring(0, val.indexOf('.')); - } - return val; - } - - /** - * Retourne une chaîne contenant les résultats du sondage. - * - * @param poll le sondage - * @return les résultats sous forme de chaine de caractères - */ - public String getResultsAsString(Poll poll) { - - DateFormat dateFormat = new SimpleDateFormat(getDateTimePattern()); - StringBuilder res = new StringBuilder(""); - Iterator<Result> it = poll.getResult().iterator(); - while (it.hasNext()) { - Result result = it.next(); - if (poll.getChoiceType() == ChoiceType.DATE) { - Date date = new Date(Long.parseLong(result.getName())); - res.append(dateFormat.format(date)); - } else { - res.append(result.getName()); - } - res.append("=").append(removeTrailing0(result.getResultValue())); - if (it.hasNext()) { - res.append(", "); - } - } - return res.toString(); - } -} 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-29 21:19:04 UTC (rev 3711) +++ trunk/pollen-services/src/main/java/org/chorem/pollen/services/impl/PollVoteCountingService.java 2012-09-30 12:57:25 UTC (rev 3712) @@ -22,18 +22,32 @@ */ package org.chorem.pollen.services.impl; +import com.google.common.base.Joiner; import com.google.common.base.Preconditions; +import com.google.common.collect.ImmutableMap; +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.business.persistence.Choice; +import org.chorem.pollen.business.persistence.ChoiceType; import org.chorem.pollen.business.persistence.Poll; import org.chorem.pollen.business.persistence.Polls; import org.chorem.pollen.services.PollenServiceSupport; +import org.chorem.pollen.votecounting.VoteCounting; +import org.chorem.pollen.votecounting.VoteCountingStrategy; +import org.chorem.pollen.votecounting.model.ChoiceScore; import org.chorem.pollen.votecounting.model.GroupOfVoter; import org.chorem.pollen.votecounting.model.GroupVoteCountingResult; import org.chorem.pollen.votecounting.model.VoteCountingResult; -import org.chorem.pollen.votecounting.VoteCounting; -import org.chorem.pollen.votecounting.VoteCountingStrategy; +import org.nuiton.topia.persistence.TopiaId; +import java.math.BigDecimal; +import java.text.DateFormat; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.List; + /** * New Poll vote counting service. * @@ -46,6 +60,17 @@ private static final Log log = LogFactory.getLog(PollVoteCountingService.class); + public VoteCountingResult getMainResult(Poll poll) { + VoteCountingResult mainResult; + if (poll.isPollGroup()) { + GroupVoteCountingResult groupResult = getGroupResult(poll); + mainResult = groupResult.getMainResult(); + } else { + mainResult = getSimpleResult(poll); + } + return mainResult; + } + /** * Execute a simple vote counting for a given poll (if the poll * is a group poll, none of his group will be used). @@ -85,5 +110,72 @@ return result; } + public String getResultValue(Choice choice, + List<ChoiceScore> results) { + String val = ""; + for (ChoiceScore result : results) { + if (result.getChoiceId().equals(choice.getTopiaId())) { + val = getResultValue(result); + break; + } + } + return val; + } + + public String getResultValue(ChoiceScore result) { + + BigDecimal scoreValue = result.getScoreValue(); + String val = removeTrailing0(scoreValue == null ? "" : + String.valueOf(scoreValue)); + return val; + } + + /** + * Retourne une chaîne contenant les résultats du sondage. + * + * @param poll le sondage + * @return les résultats sous forme de chaine de caractères + */ + public String getResultsAsString(Poll poll) { + + VoteCountingResult mainResult = getMainResult(poll); + List<ChoiceScore> results = mainResult.getTopRanking(); + + ImmutableMap<String, Choice> choicesById = Maps.uniqueIndex( + poll.getChoice(), TopiaId.GET_TOPIA_ID); + + DateFormat dateFormat = new SimpleDateFormat(getDateTimePattern()); + List<String> resultsAsString = Lists.newArrayList(); + for (ChoiceScore result : results) { + String choiceResult; + Choice choice = choicesById.get(result.getChoiceId()); + if (poll.getChoiceType() == ChoiceType.DATE) { + Date date = new Date(Long.parseLong(choice.getName())); + choiceResult = dateFormat.format(date); + } else { + choiceResult = choice.getName(); + } + choiceResult += "=" + removeTrailing0(getResultValue(result)); + resultsAsString.add(choiceResult); + } + String result = Joiner.on(",").join(resultsAsString); + return result; + } + + /** + * Supprime le 0 final d'un nombre à virgule. Le résultat peut-être un + * double : 1,0 -> 1 et 1,2 -> 1,2. + * + * @param val le nombre + * @return le nombre sans 0 final + */ + protected String removeTrailing0(String val) { + if (val.endsWith(".0")) { + val = val.substring(0, val.indexOf('.')); + } + return val; + } + + } 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-29 21:19:04 UTC (rev 3711) +++ trunk/pollen-services/src/main/java/org/chorem/pollen/services/impl/VoteService.java 2012-09-30 12:57:25 UTC (rev 3712) @@ -201,7 +201,7 @@ // regnenerate poll results - generatePollResult(pollToUpdate); +// generatePollResult(pollToUpdate); commitTransaction("Could not create vote"); @@ -263,7 +263,7 @@ log.debug("Entity updated: " + result.getTopiaId()); } - generatePollResult(poll); +// generatePollResult(poll); commitTransaction("Could not update vote"); @@ -333,7 +333,7 @@ log.debug("Entity deleted: " + voteId); } - generatePollResult(poll); +// generatePollResult(poll); commitTransaction("Could not delete vote"); @@ -407,9 +407,4 @@ return result; } - protected void generatePollResult(Poll poll) { - PollResultsService pollResultsService = newService(PollResultsService.class); - pollResultsService.generateResult(poll); - } - } Modified: trunk/pollen-ui-struts2/pom.xml =================================================================== --- trunk/pollen-ui-struts2/pom.xml 2012-09-29 21:19:04 UTC (rev 3711) +++ trunk/pollen-ui-struts2/pom.xml 2012-09-30 12:57:25 UTC (rev 3712) @@ -209,6 +209,18 @@ <scope>runtime</scope> </dependency> + <dependency> + <groupId>org.slf4j</groupId> + <artifactId>slf4j-jcl</artifactId> + <scope>runtime</scope> + </dependency> + + <dependency> + <groupId>log4j</groupId> + <artifactId>log4j</artifactId> + <scope>runtime</scope> + </dependency> + <!-- Test dependencies --> <dependency> @@ -259,12 +271,6 @@ <scope>provided</scope> </dependency> - <dependency> - <groupId>log4j</groupId> - <artifactId>log4j</artifactId> - <scope>test</scope> - </dependency> - </dependencies> <build> Modified: trunk/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/PollenActionSupport.java =================================================================== --- trunk/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/PollenActionSupport.java 2012-09-29 21:19:04 UTC (rev 3711) +++ trunk/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/PollenActionSupport.java 2012-09-30 12:57:25 UTC (rev 3712) @@ -43,10 +43,11 @@ import org.chorem.pollen.services.PollenServiceSupport; import org.chorem.pollen.services.impl.FavoriteService; import org.chorem.pollen.services.impl.PollCommentService; +import org.chorem.pollen.services.impl.PollExportService; import org.chorem.pollen.services.impl.PollFeedService; -import org.chorem.pollen.services.impl.PollResultsService; import org.chorem.pollen.services.impl.PollService; import org.chorem.pollen.services.impl.PollUrlService; +import org.chorem.pollen.services.impl.PollVoteCountingService; import org.chorem.pollen.services.impl.PreventRuleService; import org.chorem.pollen.services.impl.SecurityService; import org.chorem.pollen.services.impl.UserService; @@ -362,10 +363,14 @@ return getService(PollUrlService.class); } - protected final PollResultsService getPollResultsService() { - return getService(PollResultsService.class); + protected final PollExportService getPollExportService() { + return getService(PollExportService.class); } + protected final PollVoteCountingService getPollVoteCountingService() { + return getService(PollVoteCountingService.class); + } + protected final VoteService getVoteService() { return getService(VoteService.class); } Modified: trunk/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/poll/admin/ExportPoll.java =================================================================== --- trunk/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/poll/admin/ExportPoll.java 2012-09-29 21:19:04 UTC (rev 3711) +++ trunk/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/poll/admin/ExportPoll.java 2012-09-30 12:57:25 UTC (rev 3712) @@ -61,7 +61,7 @@ String pollId = getUserSecurityContext().getPoll().getPollId(); - String exportContent = getPollResultsService().exportPolltoXml(pollId); + String exportContent = getPollExportService().exportToXml(pollId); filename = "exportPoll-" + pollId + ".xml"; Modified: trunk/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/poll/vote/AbstractVoteAction.java =================================================================== --- trunk/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/poll/vote/AbstractVoteAction.java 2012-09-29 21:19:04 UTC (rev 3711) +++ trunk/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/poll/vote/AbstractVoteAction.java 2012-09-30 12:57:25 UTC (rev 3712) @@ -28,7 +28,6 @@ import org.apache.commons.logging.LogFactory; import org.apache.struts2.interceptor.ParameterAware; import org.chorem.pollen.PollenTechnicalException; -import org.chorem.pollen.bean.PollResult; import org.chorem.pollen.bean.PollUrl; import org.chorem.pollen.business.persistence.Choice; import org.chorem.pollen.business.persistence.ChoiceType; @@ -49,6 +48,8 @@ import org.chorem.pollen.ui.actions.PollenActionSupport; import org.chorem.pollen.ui.actions.PollenUserSecurityAware; import org.chorem.pollen.votecounting.VoteCounting; +import org.chorem.pollen.votecounting.model.ChoiceScore; +import org.chorem.pollen.votecounting.model.VoteCountingResult; import java.io.UnsupportedEncodingException; import java.net.URLEncoder; @@ -114,11 +115,11 @@ private String commentAuthor; /** - * Results of the poll. + * Top ranking result. * - * @since 1.3 + * @since 1.5 */ - private List<PollResult> results; + List<ChoiceScore> results; /** * List of comments to display. @@ -153,7 +154,7 @@ return vote; } - public List<PollResult> getResults() { + public List<ChoiceScore> getResults() { return results; } @@ -377,7 +378,7 @@ public String getResultValue(Choice choice) { - String val = getPollResultsService().getResultValue(choice, results); + String val = getPollVoteCountingService().getResultValue(choice, results); return val; } @@ -422,13 +423,14 @@ if (isResultAllowed()) { // load poll results - results = getPollResultsService().getResults(poll); + VoteCountingResult result = + getPollVoteCountingService().getMainResult(poll); + results = result.getScores(); + if (log.isDebugEnabled()) { - for (PollResult res : results) { - log.debug(res.getName() + ": " + res.getValue() - + ", (voteCounting=" + res.getVoteCountingType() - + ", byGroup=" + res.isByGroup() + ")"); + for (ChoiceScore res : results) { + log.debug(res.getChoiceId() + ": " + res.getScoreValue()); } } } Modified: trunk/pollen-ui-struts2/src/main/webapp/WEB-INF/jsp/poll/vote.jsp =================================================================== --- trunk/pollen-ui-struts2/src/main/webapp/WEB-INF/jsp/poll/vote.jsp 2012-09-29 21:19:04 UTC (rev 3711) +++ trunk/pollen-ui-struts2/src/main/webapp/WEB-INF/jsp/poll/vote.jsp 2012-09-30 12:57:25 UTC (rev 3712) @@ -114,7 +114,6 @@ <h1 class="titleVote"><s:property value="poll.title"/></h1> <!-- Informations sur le sondage --> -<!-- Informations sur le sondage --> <s:set name="showVoteUrl" value="false"/> <s:set name="showResultUrl" value="true"/> <%@include file="pollInformation.jsp" %> @@ -157,91 +156,84 @@ <%--</s:else>--%> <s:if test="textType"> <s:iterator value="poll.choice" var="choice"> - <s:if test="!isChoiceHidden(#choice)"> - <th> - <s:if test="!isDescNull(#choice)"> + <th> + <s:if test="!isDescNull(#choice)"> - <span - title='<s:property value="escapeLineBreak(#choice.description)"/>'> - <s:property value="name"/> - </span> + <span + title='<s:property value="escapeLineBreak(#choice.description)"/>'> + <s:property value="name"/> + </span> + </s:if> + <s:else> + <s:property value="name"/> + </s:else> + <s:if test="pollChoiceRunning"> + <s:if test="creatorOrAdmin"> + <s:a action="deleteChoice/%{pollUri}" namespace="/poll" + onclick="return openDeleteChoiceDialog('%{#choice.topiaId}');"> + <s:param name="choiceId"><s:property value="id"/></s:param> + <img src="<s:url value="/img/delete.png"/>" + title="<s:text name="pollen.action.deleteChoice"/>" + alt="<s:text name="pollen.action.deleteChoice"/>"/> + </s:a> </s:if> - <s:else> - <s:property value="name"/> - </s:else> - <s:if test="pollChoiceRunning"> - <s:if test="creatorOrAdmin"> - <s:a action="deleteChoice/%{pollUri}" namespace="/poll" - onclick="return openDeleteChoiceDialog('%{#choice.topiaId}');"> - <s:param name="choiceId"><s:property value="id"/></s:param> - <img src="<s:url value="/img/delete.png"/>" - title="<s:text name="pollen.action.deleteChoice"/>" - alt="<s:text name="pollen.action.deleteChoice"/>"/> - </s:a> - </s:if> - </s:if> - </th> - </s:if> + </s:if> + </th> </s:iterator> </s:if> <s:if test="dateType"> <s:iterator value="poll.choice" var="choice"> - <s:if test="!isChoiceHidden(#choice)"> - <s:if test="!isDescNull(#choice)"> + <s:if test="!isDescNull(#choice)"> - <th class="desc"> - <span - title='<s:property value="escapeLineBreak(#choice.description)"/>'> - <s:property value="%{getChoiceAsDate(#choice)}"/> - </span> - </th> - </s:if> - <s:else> - <th> - <s:property value="%{getChoiceAsDate(#choice)}"/> - </th> - </s:else> + <th class="desc"> + <span + title='<s:property value="escapeLineBreak(#choice.description)"/>'> + <s:property value="%{getChoiceAsDate(#choice)}"/> + </span> + </th> </s:if> + <s:else> + <th> + <s:property value="%{getChoiceAsDate(#choice)}"/> + </th> + </s:else> </s:iterator> </s:if> <s:if test="imageType"> <s:iterator value="poll.choice" var="choice"> - <s:if test="!isChoiceHidden(#choice)"> + <s:url id="imageUrl" namespace="/io" action="getPollChoiceImage" + escapeAmp="false"> + <s:param name="choiceId" value="%{getImageChoiceName(#choice)}"/> + <s:param name="pollId" value="poll.pollId"/> + <s:param name="thumb" value="false"/> + </s:url> + <s:url id="imageUrlThumb" namespace="/io" action="getPollChoiceImage" + escapeAmp="false"> + <s:param name="choiceId" value="%{getImageChoiceName(#choice)}"/> + <s:param name="pollId" value="poll.pollId"/> + <s:param name="thumb" value="true"/> + </s:url> + <s:if test="!isDescNull(#choice)"> - <s:url id="imageUrl" namespace="/io" action="getPollChoiceImage" - escapeAmp="false"> - <s:param name="choiceId" value="%{getImageChoiceName(#choice)}"/> - <s:param name="pollId" value="poll.pollId"/> - <s:param name="thumb" value="false"/> - </s:url> - <s:url id="imageUrlThumb" namespace="/io" action="getPollChoiceImage" - escapeAmp="false"> - <s:param name="choiceId" value="%{getImageChoiceName(#choice)}"/> - <s:param name="pollId" value="poll.pollId"/> - <s:param name="thumb" value="true"/> - </s:url> - <s:if test="!isDescNull(#choice)"> - - <th class="desc"> - <a href="<s:property value='imageUrl'/>" rel="lightbox"> - <img alt="<s:property value='name'/>" - title='<s:property value="escapeLineBreak(#choice.description)"/>' - src="<s:property value='imageUrlThumb'/>"> - </a> - </th> - </s:if> - <s:else> - <th> - <a href="<s:property value='imageUrl'/>" rel="lightbox"> - <img alt="<s:property value='name'/>" - title='<s:property value="escapeLineBreak(#choice.description)"/>' - src="<s:property value='imageUrlThumb'/>"> - </a> - </th> - </s:else> + <th class="desc"> + <a href="<s:property value='imageUrl'/>" rel="lightbox"> + <img alt="<s:property value='name'/>" + title='<s:property value="escapeLineBreak(#choice.description)"/>' + src="<s:property value='imageUrlThumb'/>"> + </a> + </th> </s:if> + <s:else> + <th> + <a href="<s:property value='imageUrl'/>" rel="lightbox"> + <img alt="<s:property value='name'/>" + title='<s:property value="escapeLineBreak(#choice.description)"/>' + src="<s:property value='imageUrlThumb'/>"> + </a> + </th> + </s:else> </s:iterator> </s:if> </tr> @@ -260,12 +252,9 @@ </s:else> <s:set name="template" value="%{choiceFragment}"/> <s:iterator value="poll.choice" var="choice" status="status"> - - <s:if test="!isChoiceHidden(#choice)"> - <th> - <jsp:include page="${template}"/> - </th> - </s:if> + <th> + <jsp:include page="${template}"/> + </th> </s:iterator> </tr> </tfoot> @@ -302,23 +291,21 @@ </s:if> </td> <s:iterator value="poll.choice" var="choice"> - <s:if test="!isChoiceHidden(#choice)"> - <s:set name="currentVoteChoice" - value="%{#vote.getChoiceVoteToChoice(#choice)}"/> - <%--s:if test="poll.anonymous"> - <td class="anonymous">?</td> - </s:if> - <s:else--%> - <s:if test="isChoiceInVote(#currentVoteChoice)"> - <td class="voted"> - <s:property value="%{getChoiceValue(#currentVoteChoice)}"/> - </td> - </s:if> - <s:else> - <td class="notVoted"></td> - </s:else> - <%--</s:else>--%> + <s:set name="currentVoteChoice" + value="%{#vote.getChoiceVoteToChoice(#choice)}"/> + <%--s:if test="poll.anonymous"> + <td class="anonymous">?</td> </s:if> + <s:else--%> + <s:if test="isChoiceInVote(#currentVoteChoice)"> + <td class="voted"> + <s:property value="%{getChoiceValue(#currentVoteChoice)}"/> + </td> + </s:if> + <s:else> + <td class="notVoted"></td> + </s:else> + <%--</s:else>--%> </s:iterator> </tr> </s:iterator> @@ -332,11 +319,9 @@ </td> <s:iterator value="poll.choice" var="choice"> - <s:if test="!isChoiceHidden(#choice)"> - <td class="result"> - <s:property value="%{getResultValue(#choice)}"/> - </td> - </s:if> + <td class="result"> + <s:property value="%{getResultValue(#choice)}"/> + </td> </s:iterator> </tr> </s:if> @@ -368,23 +353,18 @@ enctype="multipart/form-data"> <h4><s:text name="pollen.action.addChoice"/></h4> <s:if test="textType"> - <s:textfield key="choice.name" - label="%{getText('pollen.common.choice')}" - required="true"/> + <s:textfield key="choice.name" required="true" + label="%{getText('pollen.common.choice')}" /> </s:if> <s:elseif test="dateType"> - <sj:datepicker key="choice.date" + <sj:datepicker key="choice.date" required="true" label="%{getText('pollen.common.choice')}" - changeMonth="true" changeYear="true" - timepicker="true" - displayFormat="%{getText('pollen.common.datePickerPattern')}" - required="true"/> + changeMonth="true" changeYear="true" timepicker="true" + displayFormat="%{getText('pollen.common.datePickerPattern')}" /> </s:elseif> <s:elseif test="imageType"> - <s:file key="imageChoice[0]" - label="%{getText('pollen.common.choice')}" - cssClass="nameField" - required="true"/> + <s:file key="imageChoice[0]" required="true" cssClass="nameField" + label="%{getText('pollen.common.choice')}" /> </s:elseif> <br/> <s:textarea cols="36" key="choice.description" Modified: trunk/pollen-votecounting-api/src/main/java/org/chorem/pollen/votecounting/model/VoteCountingResult.java =================================================================== --- trunk/pollen-votecounting-api/src/main/java/org/chorem/pollen/votecounting/model/VoteCountingResult.java 2012-09-29 21:19:04 UTC (rev 3711) +++ trunk/pollen-votecounting-api/src/main/java/org/chorem/pollen/votecounting/model/VoteCountingResult.java 2012-09-30 12:57:25 UTC (rev 3712) @@ -22,13 +22,11 @@ */ package org.chorem.pollen.votecounting.model; +import com.google.common.collect.ArrayListMultimap; import com.google.common.collect.Lists; -import com.google.common.collect.Multimap; import com.google.common.collect.Multimaps; import java.io.Serializable; -import java.util.Collection; -import java.util.Collections; import java.util.List; /** @@ -54,7 +52,7 @@ * * @see ChoiceScore#getScoreOrder() */ - private transient Multimap<Integer, ChoiceScore> scoresByRank; + private transient ArrayListMultimap<Integer, ChoiceScore> scoresByRank; public static VoteCountingResult newResult(List<ChoiceScore> scores) { VoteCountingResult result = new VoteCountingResult(); @@ -81,13 +79,14 @@ this.scores = Lists.newArrayList(scores); } - public Collection<ChoiceScore> getTopRanking() { + public List<ChoiceScore> getTopRanking() { return getScoresByRank().get(0); } - public Multimap<Integer, ChoiceScore> getScoresByRank() { + public ArrayListMultimap<Integer, ChoiceScore> getScoresByRank() { if (scoresByRank == null) { - scoresByRank = Multimaps.index(scores, ChoiceScore.SCORE_BY_ORDER); + scoresByRank = ArrayListMultimap.create( + Multimaps.index(scores, ChoiceScore.SCORE_BY_ORDER)); } return scoresByRank; } Modified: trunk/pom.xml =================================================================== --- trunk/pom.xml 2012-09-29 21:19:04 UTC (rev 3711) +++ trunk/pom.xml 2012-09-30 12:57:25 UTC (rev 3712) @@ -343,11 +343,18 @@ <!-- Logging --> - <dependency> + <!--dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>${slf4jVersion}</version> <scope>compile</scope> + </dependency--> + + <dependency> + <groupId>org.slf4j</groupId> + <artifactId>slf4j-jcl</artifactId> + <version>${slf4jVersion}</version> + <scope>runtime</scope> </dependency> <!-- Others -->