r3174 - in branches/pollen-1.2.6-struts2: . pollen-domain pollen-domain/src pollen-domain/src/main pollen-domain/src/main/java pollen-domain/src/main/java/org pollen-domain/src/main/java/org/chorem pollen-domain/src/main/java/org/chorem/poll pollen-domain/src/main/java/org/chorem/poll/domain pollen-domain/src/main/java/org/chorem/poll/domain/poll pollen-domain/src/main/java/org/chorem/poll/domain/strategy pollen-domain/src/main/java/org/chorem/poll/domain/vote pollen-domain/src/main/resourc
Author: tchemit Date: 2012-03-12 09:37:14 +0100 (Mon, 12 Mar 2012) New Revision: 3174 Url: http://chorem.org/repositories/revision/pollen/3174 Log: domain NG module (will be used with version 1.3 Added: branches/pollen-1.2.6-struts2/pollen-domain/ branches/pollen-1.2.6-struts2/pollen-domain/LICENSE.txt branches/pollen-1.2.6-struts2/pollen-domain/README.txt branches/pollen-1.2.6-struts2/pollen-domain/changelog.txt branches/pollen-1.2.6-struts2/pollen-domain/pom.xml branches/pollen-1.2.6-struts2/pollen-domain/src/ branches/pollen-1.2.6-struts2/pollen-domain/src/main/ branches/pollen-1.2.6-struts2/pollen-domain/src/main/java/ branches/pollen-1.2.6-struts2/pollen-domain/src/main/java/org/ branches/pollen-1.2.6-struts2/pollen-domain/src/main/java/org/chorem/ branches/pollen-1.2.6-struts2/pollen-domain/src/main/java/org/chorem/poll/ branches/pollen-1.2.6-struts2/pollen-domain/src/main/java/org/chorem/poll/domain/ branches/pollen-1.2.6-struts2/pollen-domain/src/main/java/org/chorem/poll/domain/ImportExportService.java branches/pollen-1.2.6-struts2/pollen-domain/src/main/java/org/chorem/poll/domain/PollenDomainFunctions.java branches/pollen-1.2.6-struts2/pollen-domain/src/main/java/org/chorem/poll/domain/VoteCountingService.java branches/pollen-1.2.6-struts2/pollen-domain/src/main/java/org/chorem/poll/domain/VoteCountingUtils.java branches/pollen-1.2.6-struts2/pollen-domain/src/main/java/org/chorem/poll/domain/poll/ branches/pollen-1.2.6-struts2/pollen-domain/src/main/java/org/chorem/poll/domain/poll/ChoiceType.java branches/pollen-1.2.6-struts2/pollen-domain/src/main/java/org/chorem/poll/domain/poll/I18nAble.java branches/pollen-1.2.6-struts2/pollen-domain/src/main/java/org/chorem/poll/domain/poll/Poll.java branches/pollen-1.2.6-struts2/pollen-domain/src/main/java/org/chorem/poll/domain/poll/PollChoice.java branches/pollen-1.2.6-struts2/pollen-domain/src/main/java/org/chorem/poll/domain/poll/PollComment.java branches/pollen-1.2.6-struts2/pollen-domain/src/main/java/org/chorem/poll/domain/poll/PollType.java branches/pollen-1.2.6-struts2/pollen-domain/src/main/java/org/chorem/poll/domain/poll/PollVoteCountingChoice.java branches/pollen-1.2.6-struts2/pollen-domain/src/main/java/org/chorem/poll/domain/poll/PollVoteCountingResult.java branches/pollen-1.2.6-struts2/pollen-domain/src/main/java/org/chorem/poll/domain/poll/PollVotingChoice.java branches/pollen-1.2.6-struts2/pollen-domain/src/main/java/org/chorem/poll/domain/poll/PollVotingGroup.java branches/pollen-1.2.6-struts2/pollen-domain/src/main/java/org/chorem/poll/domain/poll/PollVotingPerson.java branches/pollen-1.2.6-struts2/pollen-domain/src/main/java/org/chorem/poll/domain/poll/VoteCountingType.java branches/pollen-1.2.6-struts2/pollen-domain/src/main/java/org/chorem/poll/domain/strategy/ branches/pollen-1.2.6-struts2/pollen-domain/src/main/java/org/chorem/poll/domain/strategy/CondorcetVoteCountingStrategy.java branches/pollen-1.2.6-struts2/pollen-domain/src/main/java/org/chorem/poll/domain/strategy/NumberVoteCountingStrategy.java branches/pollen-1.2.6-struts2/pollen-domain/src/main/java/org/chorem/poll/domain/strategy/PercentageVoteCountingStrategy.java branches/pollen-1.2.6-struts2/pollen-domain/src/main/java/org/chorem/poll/domain/strategy/StandardVoteCountingStrategy.java branches/pollen-1.2.6-struts2/pollen-domain/src/main/java/org/chorem/poll/domain/strategy/VoteCountingStrategy.java branches/pollen-1.2.6-struts2/pollen-domain/src/main/java/org/chorem/poll/domain/vote/ branches/pollen-1.2.6-struts2/pollen-domain/src/main/java/org/chorem/poll/domain/vote/Choice.java branches/pollen-1.2.6-struts2/pollen-domain/src/main/java/org/chorem/poll/domain/vote/Group.java branches/pollen-1.2.6-struts2/pollen-domain/src/main/java/org/chorem/poll/domain/vote/PercentageBehavior.java branches/pollen-1.2.6-struts2/pollen-domain/src/main/java/org/chorem/poll/domain/vote/Vote.java branches/pollen-1.2.6-struts2/pollen-domain/src/main/resources/ branches/pollen-1.2.6-struts2/pollen-domain/src/main/resources/i18n/ branches/pollen-1.2.6-struts2/pollen-domain/src/main/resources/i18n/pollen-domain_en_GB.properties branches/pollen-1.2.6-struts2/pollen-domain/src/main/resources/i18n/pollen-domain_fr_FR.properties branches/pollen-1.2.6-struts2/pollen-domain/src/site/ branches/pollen-1.2.6-struts2/pollen-domain/src/site/rst/ branches/pollen-1.2.6-struts2/pollen-domain/src/test/ branches/pollen-1.2.6-struts2/pollen-domain/src/test/java/ branches/pollen-1.2.6-struts2/pollen-domain/src/test/java/org/ branches/pollen-1.2.6-struts2/pollen-domain/src/test/java/org/chorem/ branches/pollen-1.2.6-struts2/pollen-domain/src/test/java/org/chorem/poll/ branches/pollen-1.2.6-struts2/pollen-domain/src/test/java/org/chorem/poll/domain/ branches/pollen-1.2.6-struts2/pollen-domain/src/test/java/org/chorem/poll/domain/ImportExportServiceTest.java branches/pollen-1.2.6-struts2/pollen-domain/src/test/java/org/chorem/poll/domain/VoteCountingServiceTest.java branches/pollen-1.2.6-struts2/pollen-domain/src/test/java/org/chorem/poll/domain/strategy/ branches/pollen-1.2.6-struts2/pollen-domain/src/test/java/org/chorem/poll/domain/strategy/CondorcetVoteCountingStrategyTest.java branches/pollen-1.2.6-struts2/pollen-domain/src/test/java/org/chorem/poll/domain/strategy/NumberVoteCountingStrategyTest.java branches/pollen-1.2.6-struts2/pollen-domain/src/test/java/org/chorem/poll/domain/strategy/PercentageVoteCountingStrategyTest.java branches/pollen-1.2.6-struts2/pollen-domain/src/test/java/org/chorem/poll/domain/strategy/StandardVoteCountingStrategyTest.java branches/pollen-1.2.6-struts2/pollen-domain/src/test/resources/ branches/pollen-1.2.6-struts2/pollen-domain/src/test/resources/log4j.properties Property changes on: branches/pollen-1.2.6-struts2/pollen-domain ___________________________________________________________________ Added: svn:ignore + target *.iml *.ipr *.iws *.log .project .classpath .settings.xml Added: branches/pollen-1.2.6-struts2/pollen-domain/pom.xml =================================================================== --- branches/pollen-1.2.6-struts2/pollen-domain/pom.xml (rev 0) +++ branches/pollen-1.2.6-struts2/pollen-domain/pom.xml 2012-03-12 08:37:14 UTC (rev 3174) @@ -0,0 +1,66 @@ +<?xml version="1.0" encoding="UTF-8"?> +<project xmlns="http://maven.apache.org/POM/4.0.0" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + + <modelVersion>4.0.0</modelVersion> + + <parent> + <groupId>org.chorem</groupId> + <artifactId>pollen</artifactId> + <version>1.2.6-SNAPSHOT</version> + </parent> + + <groupId>org.chorem.pollen</groupId> + <artifactId>pollen-domain</artifactId> + + <name>Pollen :: Domain</name> + <description>Pollen Domain</description> + + <dependencies> + + <dependency> + <groupId>commons-logging</groupId> + <artifactId>commons-logging</artifactId> + </dependency> + <dependency> + <groupId>org.jdom</groupId> + <artifactId>jdom</artifactId> + </dependency> + <dependency> + <groupId>org.nuiton.i18n</groupId> + <artifactId>nuiton-i18n</artifactId> + </dependency> + <dependency> + <groupId>org.nuiton</groupId> + <artifactId>nuiton-utils</artifactId> + </dependency> + <dependency> + <groupId>com.google.guava</groupId> + <artifactId>guava</artifactId> + </dependency> + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + </dependency> + + </dependencies> + + <build> + <plugins> + <plugin> + <groupId>org.nuiton.i18n</groupId> + <artifactId>maven-i18n-plugin</artifactId> + <executions> + <execution> + <goals> + <goal>parserJava</goal> + <goal>gen</goal> + </goals> + </execution> + </executions> + </plugin> + </plugins> + </build> + +</project> Copied: branches/pollen-1.2.6-struts2/pollen-domain/src/main/java/org/chorem/poll/domain/ImportExportService.java (from rev 3173, branches/pollen-1.2.6-struts2/pollen-votecounting/src/main/java/org/chorem/pollen/votecounting/services/ImportExportService.java) =================================================================== --- branches/pollen-1.2.6-struts2/pollen-domain/src/main/java/org/chorem/poll/domain/ImportExportService.java (rev 0) +++ branches/pollen-1.2.6-struts2/pollen-domain/src/main/java/org/chorem/poll/domain/ImportExportService.java 2012-03-12 08:37:14 UTC (rev 3174) @@ -0,0 +1,605 @@ +/* + * #%L + * Pollen :: Vote Counting + * + * $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.poll.domain; + +import com.google.common.collect.Lists; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.chorem.poll.domain.poll.ChoiceType; +import org.chorem.poll.domain.poll.Poll; +import org.chorem.poll.domain.poll.PollChoice; +import org.chorem.poll.domain.poll.PollComment; +import org.chorem.poll.domain.poll.PollType; +import org.chorem.poll.domain.poll.PollVoteCountingChoice; +import org.chorem.poll.domain.poll.PollVoteCountingResult; +import org.chorem.poll.domain.poll.PollVotingChoice; +import org.chorem.poll.domain.poll.PollVotingGroup; +import org.chorem.poll.domain.poll.PollVotingPerson; +import org.chorem.poll.domain.poll.VoteCountingType; +import org.chorem.poll.domain.strategy.NumberVoteCountingStrategy; +import org.jdom.Document; +import org.jdom.Element; +import org.jdom.JDOMException; +import org.jdom.input.SAXBuilder; +import org.jdom.output.Format; +import org.jdom.output.XMLOutputter; + +import java.io.File; +import java.io.IOException; +import java.util.List; + +/** + * Service to import / export a poll to or from his xml representation. + * + * @author tchemit <chemit@codelutin.com> + * @since 1.2.6 + */ +public class ImportExportService { + + public static final String POLL = "poll"; + + public static final String TITLE1 = "title"; + + public static final String TITLE = TITLE1; + + public static final String CHOICE = "choice"; + + public static final String ID_CHOICE = "idChoice"; + + public static final String VALUE = "value"; + + public static final String PERCENTAGE = "percentage"; + + public static final String NB_VOTES = "nbVotes"; + + public static final String IS_BY_GROUP = "isByGroup"; + + public static final String CHOICES = "choices"; + + public static final String CHOICE_RESULT = "choiceResult"; + + public static final String ID_POLL = "idPoll"; + + public static final String VOTE_COUNTING_RESULT = "voteCountingResult"; + + public static final String DESCRIPTION = "description"; + + public static final String NAME = "name"; + + public static final String POLL_CHOICE = "pollChoice"; + + public static final String VOTING_PERSON = "votingPerson"; + + public static final String VOTING_ID = "votingId"; + + public static final String WEIGHT = "weight"; + + public static final String EMAIL = "email"; + + public static final String COMMENT = "comment"; + + public static final String VOTING_PERSONS = "votingPersons"; + + public static final String ID_GROUP = "idGroup"; + + public static final String GROUP = "group"; + + public static final String POLL_CHOICES = "pollChoices"; + + public static final String VOTE_COUNTING_RESULTS = "voteCountingResults"; + + public static final String GROUPS = "groups"; + + public static final String COMMENTS = "comments"; + + public static final String CREATOR_ID = "creatorId"; + + public static final String CREATOR_EMAIL = "creatorEmail"; + + public static final String MAX_CHOICE_NB = "maxChoiceNb"; + + public static final String POLL_EXPORT = "pollExport"; + + public static final String POLL_ID = "pollId"; + + public static final String POLL_TYPE = "pollType"; + + public static final String CHOICE_TYPE = "choiceType"; + + public static final String VOTE_COUNTING = "voteCounting"; + + public static final String CLOSED = "closed"; + + public static final String CHOICE_ADD_ALLOWED = "choiceAddAllowed"; + + public static final String ANONYMOUS_VOTE_ALLOWED = "anonymousVoteAllowed"; + + public static final String ANONYMOUS = "anonymous"; + + public static final String PUBLIC_RESULTS = "publicResults"; + + public static final String CONTINUOUS_RESULTS = "continuousResults"; + + public static final String VALUE_TRUE = "true"; + + public static final String VALUE_FALSE = "false"; + + /** Logger. */ + private static final Log log = LogFactory.getLog(ImportExportService.class); + + /** + * Export the given poll to an xml representation. + * + * @param exportPoll the poll to export + * @return the xml representation of the given poll + */ + public String exportToXml(Poll exportPoll) { + + Document document = toDOM(exportPoll); + + // Mise en forme classique avec getPrettyFormat() + XMLOutputter sortie = new XMLOutputter(Format.getPrettyFormat()); + String content = sortie.outputString(document); + + if (log.isDebugEnabled()) { + log.debug(content); + } + return content; + } + + /** + * Import into a {@link Poll} his xml representation from the + * given file. + * + * @param file file to load + * @return the loaded dto from the xml representation. + * @throws JDOMException if could not load xml from file + * @throws IOException if could not load the file + */ + public Poll importFromXml(File file) throws JDOMException, IOException { + + // Création d'un nouveau document JDOM avec le fichier XML en argument + Document document = new SAXBuilder().build(file); + + // Initialisation d'un nouvel élément racine + Element racine = document.getRootElement(); + + Poll result = toDTO(racine); + + return result; + } + + protected Poll toDTO(Element racine) { + + // Création du sondage + Element pollElm = racine.getChild(POLL); + + Poll poll = new Poll(); + poll.setPollId(pollElm.getAttributeValue(POLL_ID)); + poll.setTitle(pollElm.getChild(TITLE).getText()); + poll.setDescription(pollElm.getChild(DESCRIPTION).getText()); + poll.setCreatorId(pollElm.getChild(CREATOR_ID).getText()); + poll.setCreatorEmail(pollElm.getChild(CREATOR_EMAIL).getText()); + poll.setMaxChoiceNb(Integer.parseInt( + pollElm.getChild(MAX_CHOICE_NB).getText())); + + poll.setPollType( + PollType.valueOf(pollElm.getAttributeValue(POLL_TYPE))); + poll.setChoiceType(ChoiceType.valueOf( + pollElm.getAttributeValue(CHOICE_TYPE))); + poll.setVoteCounting(VoteCountingType.valueOf( + pollElm.getAttributeValue(VOTE_COUNTING))); + poll.setClosed(Boolean.valueOf(pollElm.getAttributeValue(CLOSED))); + poll.setChoiceAddAllowed(Boolean.valueOf( + pollElm.getAttributeValue(CHOICE_ADD_ALLOWED))); + poll.setAnonymousVoteAllowed(Boolean.valueOf( + pollElm.getAttributeValue(ANONYMOUS_VOTE_ALLOWED))); + poll.setAnonymous(Boolean.valueOf( + pollElm.getAttributeValue(ANONYMOUS))); + poll.setPublicResults(Boolean.valueOf(pollElm.getAttributeValue(PUBLIC_RESULTS))); + poll.setContinuousResults(Boolean.valueOf( + pollElm.getAttributeValue(CONTINUOUS_RESULTS))); + + // Ajout des commentaires + Element commentsElm = pollElm.getChild(COMMENTS); + List<Element> listComments = commentsElm.getChildren(COMMENT); + List vectorComments = toComments(listComments); + + poll.setComments(vectorComments); + + // Ajout des groupes + Element groupsElm = pollElm.getChild(GROUPS); + List<Element> listGroups = groupsElm.getChildren(GROUP); + List vectorGroups = toVotingGroups(listGroups); + + poll.setVotingGroups(vectorGroups); + + // Ajout des choix + Element pollChoicesElm = pollElm.getChild(POLL_CHOICES); + List<Element> listPollChoices = pollChoicesElm.getChildren(POLL_CHOICE); + List<PollChoice> vectorPollChoices = toPollChoices(listPollChoices); + + poll.setChoices(vectorPollChoices); + + // Ajout des résultats + Element voteCountingResultsElm = racine.getChild(VOTE_COUNTING_RESULTS); + List<Element> listVoteCountingResults = + voteCountingResultsElm.getChildren(VOTE_COUNTING_RESULT); + List<PollVoteCountingResult> vectorVoteCountingResults = + toVoteCountingResults(listVoteCountingResults); + + // Création du sondage PollExportDTO + poll.setVoteCountingResult(vectorVoteCountingResults); + + return poll; + } + + protected List<PollComment> toComments(List<Element> listComments) { + List<PollComment> vectorComments = Lists.newArrayList(); + + for (Element commentElm : listComments) { + PollComment comment = new PollComment(); + comment.setVotingID(commentElm.getAttributeValue(VOTING_ID)); + comment.setText(commentElm.getText()); + vectorComments.add(comment); + } + + return vectorComments; + } + + protected List<PollVotingGroup> toVotingGroups(List<Element> listGroups) { + List<PollVotingGroup> vectorGroups = Lists.newArrayList(); + + for (Element groupElm : listGroups) { + PollVotingGroup group = new PollVotingGroup(); + + group.setIdGroup(groupElm.getAttributeValue(ID_GROUP)); + group.setWeight(Double.parseDouble((groupElm.getAttributeValue(WEIGHT)))); + + group.setName(groupElm.getAttributeValue(NAME)); + + Element votingPersonsElm = groupElm.getChild(VOTING_PERSONS); + List<Element> listVotingPersons = votingPersonsElm + .getChildren(VOTING_PERSON); + List vectorVotingPersons = toVotingPersons(listVotingPersons); + + group.setVotingPersons(vectorVotingPersons); + + vectorGroups.add(group); + } + + return vectorGroups; + } + + protected List<PollVotingPerson> toVotingPersons(List<Element> listVotingPersons) { + List<PollVotingPerson> vectorVotingPersons = Lists.newArrayList(); + + for (Element votingPersonElm : listVotingPersons) { + PollVotingPerson votingPerson = new PollVotingPerson(); + votingPerson.setVotingId(votingPersonElm.getAttributeValue(VOTING_ID)); + votingPerson.setWeight(Double.parseDouble(votingPersonElm.getAttributeValue(WEIGHT))); + + votingPerson.setEmail(votingPersonElm.getChild(EMAIL).getValue()); + votingPerson.setComment(votingPersonElm.getChild(COMMENT) + .getValue()); + + Element choicesElm = votingPersonElm.getChild(CHOICES); + + List<Element> listVoteToChoices = choicesElm.getChildren(CHOICE); + List vectorVoteToChoices = toVoteToChoices(listVoteToChoices); + + votingPerson.setChoices(vectorVoteToChoices); + vectorVotingPersons.add(votingPerson); + } + + return vectorVotingPersons; + } + + protected List<PollVotingChoice> toVoteToChoices(List<Element> listVoteToChoices) { + List<PollVotingChoice> vectorVoteToChoices = Lists.newArrayList(); + + for (Element voteToChoiceElm : listVoteToChoices) { + PollVotingChoice voteToChoice = new PollVotingChoice( + voteToChoiceElm.getAttributeValue(ID_CHOICE), + Double.parseDouble(voteToChoiceElm.getAttributeValue(VALUE))); + vectorVoteToChoices.add(voteToChoice); + } + return vectorVoteToChoices; + } + + protected List<PollChoice> toPollChoices(List<Element> listPollChoices) { + List<PollChoice> vectorPollChoices = Lists.newArrayList(); + + for (Element pollChoiceElm : listPollChoices) { + PollChoice pollChoice = new PollChoice(); + pollChoice.setIdChoice(pollChoiceElm.getAttributeValue(ID_CHOICE)); + + String pollChoiceName = pollChoiceElm.getChild(NAME).getValue(); + pollChoice.setName(pollChoiceName); + pollChoice.setHidden(pollChoiceName != null + && pollChoiceName.startsWith(NumberVoteCountingStrategy.HIDDEN_PREFIX)); + + pollChoice.setDescription(pollChoiceElm.getChild(DESCRIPTION) + .getValue()); + + vectorPollChoices.add(pollChoice); + } + + return vectorPollChoices; + } + + protected List<PollVoteCountingResult> toVoteCountingResults(List<Element> listVoteCountingResults) { + List<PollVoteCountingResult> vectorVoteCountingResults = Lists.newArrayList(); + + for (Element voteCountingResultElm : listVoteCountingResults) { + PollVoteCountingResult voteCountingResult = new PollVoteCountingResult(); + voteCountingResult.setIdPoll( + voteCountingResultElm.getAttributeValue(ID_POLL)); + voteCountingResult.setNbVotes( + Integer.parseInt(voteCountingResultElm.getChild(NB_VOTES) + .getValue())); + voteCountingResult.setChoiceResult(voteCountingResultElm.getChild( + CHOICE_RESULT).getValue()); + voteCountingResult.setByGroup( + Boolean.parseBoolean( + voteCountingResultElm.getChild(IS_BY_GROUP).getValue())); + + Element choicesElm = voteCountingResultElm.getChild(CHOICES); + List<Element> listChoices = choicesElm.getChildren(CHOICE); + List vectorChoices = toChoices(listChoices); + voteCountingResult.setChoices(vectorChoices); + vectorVoteCountingResults.add(voteCountingResult); + } + + return vectorVoteCountingResults; + } + + protected List<PollVoteCountingChoice> toChoices(List<Element> listChoices) { + List<PollVoteCountingChoice> vectorChoices = Lists.newArrayList(); + + for (Element choiceElm : listChoices) { + PollVoteCountingChoice choice = new PollVoteCountingChoice( + choiceElm.getAttribute(ID_CHOICE).getName(), + Double.parseDouble(choiceElm.getAttribute(VALUE).getValue()), + Double.parseDouble(choiceElm.getChild(PERCENTAGE).getValue()), + Integer.parseInt((choiceElm.getChild(NB_VOTES).getValue()))); + vectorChoices.add(choice); + } + + return vectorChoices; + } + + protected Document toDOM(Poll poll) { + + List<PollVoteCountingResult> voteCountingResults = + poll.getVoteCountingResult(); + + // Création de la racine et du document + Element racine = new Element(POLL_EXPORT); + Document document = new Document(racine); + + // Création du sondage + Element pollElm = new Element(POLL); + pollElm.setAttribute(POLL_ID, poll.getPollId()); + pollElm.setAttribute(POLL_TYPE, poll.getPollType().name()); + pollElm.setAttribute(CHOICE_TYPE, poll.getChoiceType().name()); + pollElm.setAttribute(VOTE_COUNTING, poll.getVoteCounting().name()); + pollElm.setAttribute(CLOSED, poll.isClosed() ? VALUE_TRUE : VALUE_FALSE); + pollElm.setAttribute(CHOICE_ADD_ALLOWED, + poll.isChoiceAddAllowed() ? VALUE_TRUE : VALUE_FALSE); + pollElm.setAttribute(ANONYMOUS_VOTE_ALLOWED, + poll.isAnonymousVoteAllowed() ? VALUE_TRUE : VALUE_FALSE); + pollElm.setAttribute(ANONYMOUS, + poll.isAnonymous() ? VALUE_TRUE : VALUE_FALSE); + pollElm.setAttribute(PUBLIC_RESULTS, + poll.isPublicResults() ? VALUE_TRUE : VALUE_FALSE); + pollElm.setAttribute(CONTINUOUS_RESULTS, + poll.isContinuousResults() ? VALUE_TRUE : VALUE_FALSE); + + Element titleElm = new Element(TITLE1); + titleElm.setText(poll.getTitle()); + pollElm.addContent(titleElm); + + Element descriptionElm = new Element(DESCRIPTION); + descriptionElm.setText(poll.getDescription()); + pollElm.addContent(descriptionElm); + + Element creatorIdElm = new Element(CREATOR_ID); + creatorIdElm.setText(poll.getCreatorId()); + pollElm.addContent(creatorIdElm); + + Element creatorEmailElm = new Element(CREATOR_EMAIL); + creatorEmailElm.setText(poll.getCreatorEmail()); + pollElm.addContent(creatorEmailElm); + + Element maxChoiceNbElm = new Element(MAX_CHOICE_NB); + maxChoiceNbElm.setText(Integer.toString(poll.getMaxChoiceNb())); + pollElm.addContent(maxChoiceNbElm); + + // Création des commentaires + Element commentsElm = new Element(COMMENTS); + + for (PollComment commentDTO : poll.getComments()) { + Element commentElm = toDOM(commentDTO); + commentsElm.addContent(commentElm); + } + + pollElm.addContent(commentsElm); + + // Création des groupes de votants + Element groupsElm = new Element(GROUPS); + + for (PollVotingGroup votingGroupDTO : poll.getVotingGroups()) { + Element groupElm = toDOM(votingGroupDTO); + groupsElm.addContent(groupElm); + } + + pollElm.addContent(groupsElm); + + // Création des choix + Element pollChoicesElm = new Element(POLL_CHOICES); + + for (PollChoice pollChoiceDTO : poll.getChoices()) { + Element pollChoiceElm = toDOM(pollChoiceDTO); + pollChoicesElm.addContent(pollChoiceElm); + } + pollElm.addContent(pollChoicesElm); + + // Création des résultats + Element voteCountingResultsElm = new Element(VOTE_COUNTING_RESULTS); + + for (PollVoteCountingResult voteCountingResult1 : voteCountingResults) { + Element voteCountingResult = toDOM(voteCountingResult1); + voteCountingResultsElm.addContent(voteCountingResult); + } + + // Ajout du sondage et des résultats à la racine + racine.addContent(pollElm); + + racine.addContent(voteCountingResultsElm); + + return document; + } + + protected Element toDOM(PollComment comment) { + Element commentElm = new Element(COMMENT); + + commentElm.setAttribute(VOTING_ID, comment.getVotingID()); + commentElm.setText(comment.getText()); + + return commentElm; + } + + protected Element toDOM(PollVotingGroup group) { + Element groupElm = new Element(GROUP); + + groupElm.setAttribute(ID_GROUP, group.getIdGroup()); + groupElm.setAttribute(NAME, group.getName()); + groupElm.setAttribute(WEIGHT, Double.toString(group.getWeight())); + + Element votingPersonsElm = new Element(VOTING_PERSONS); + + for (PollVotingPerson votingPersonDTO : group.getVotingPersons()) { + Element votingPersonElm = toDOM(votingPersonDTO); + votingPersonsElm.addContent(votingPersonElm); + } + groupElm.addContent(votingPersonsElm); + + return groupElm; + } + + protected Element toDOM(PollVotingPerson votingPerson) { + Element votingPersonElm = new Element(VOTING_PERSON); + + votingPersonElm.setAttribute(VOTING_ID, votingPerson.getVotingId()); + votingPersonElm.setAttribute(WEIGHT, Double.toString(votingPerson + .getWeight())); + + Element emailElm = new Element(EMAIL); + emailElm.setText(votingPerson.getEmail()); + votingPersonElm.addContent(emailElm); + + Element commentElm = new Element(COMMENT); + commentElm.setText(votingPerson.getComment()); + votingPersonElm.addContent(commentElm); + + Element choicesElm = new Element(CHOICES); + + for (PollVotingChoice voteToChoice : votingPerson.getChoices()) { + Element choiceElm = new Element(CHOICE); + choiceElm.setAttribute(ID_CHOICE, voteToChoice.getIdChoice()); + choiceElm.setAttribute(VALUE, Double.toString(voteToChoice + .getValue())); + choicesElm.addContent(choiceElm); + } + + votingPersonElm.addContent(choicesElm); + + return votingPersonElm; + } + + protected Element toDOM(PollChoice pollChoice) { + Element pollChoiceElm = new Element(POLL_CHOICE); + + pollChoiceElm.setAttribute(ID_CHOICE, pollChoice.getIdChoice()); + + Element nameElm = new Element(NAME); + nameElm.setText(pollChoice.getName()); + pollChoiceElm.addContent(nameElm); + + Element descriptionElm = new Element(DESCRIPTION); + descriptionElm.setText(pollChoice.getDescription()); + pollChoiceElm.addContent(descriptionElm); + + return pollChoiceElm; + } + + protected Element toDOM(PollVoteCountingResult voteCountingResults) { + Element voteCountingResultElm = new Element(VOTE_COUNTING_RESULT); + + voteCountingResultElm.setAttribute(ID_POLL, voteCountingResults + .getIdPoll()); + + Element nbVotesElm = new Element(NB_VOTES); + nbVotesElm.setText(Integer.toString(voteCountingResults.getNbVotes())); + voteCountingResultElm.addContent(nbVotesElm); + + Element choiceResultElm = new Element(CHOICE_RESULT); + choiceResultElm.setText(voteCountingResults.getChoiceResult()); + voteCountingResultElm.addContent(choiceResultElm); + + Element isByGroupElm = new Element(IS_BY_GROUP); + isByGroupElm.setText(Boolean.toString(voteCountingResults.isByGroup())); + voteCountingResultElm.addContent(isByGroupElm); + + Element choicesElm = new Element(CHOICES); + + for (PollVoteCountingChoice choiceDTO : voteCountingResults.getChoices()) { + Element choiceElm = toDOM(choiceDTO); + choicesElm.addContent(choiceElm); + } + + voteCountingResultElm.addContent(choicesElm); + + return voteCountingResultElm; + } + + protected Element toDOM(PollVoteCountingChoice choice) { + Element choiceElm = new Element(CHOICE); + + choiceElm.setAttribute(ID_CHOICE, choice.getIdChoice()); + choiceElm.setAttribute(VALUE, Double.toString(choice.getValue())); + + Element percentageElm = new Element(PERCENTAGE); + percentageElm.setText(Double.toString(choice.getPercentage())); + choiceElm.addContent(percentageElm); + + Element nbVotesElm = new Element(NB_VOTES); + nbVotesElm.setText(Integer.toString(choice.getNbVotes())); + choiceElm.addContent(nbVotesElm); + + return choiceElm; + } + +} Added: branches/pollen-1.2.6-struts2/pollen-domain/src/main/java/org/chorem/poll/domain/PollenDomainFunctions.java =================================================================== --- branches/pollen-1.2.6-struts2/pollen-domain/src/main/java/org/chorem/poll/domain/PollenDomainFunctions.java (rev 0) +++ branches/pollen-1.2.6-struts2/pollen-domain/src/main/java/org/chorem/poll/domain/PollenDomainFunctions.java 2012-03-12 08:37:14 UTC (rev 3174) @@ -0,0 +1,11 @@ +package org.chorem.poll.domain; + +/** + * TODO + * + * @author tchemit <chemit@codelutin.com> + * @since 1.2.6 + */ +public class PollenDomainFunctions { + +} Copied: branches/pollen-1.2.6-struts2/pollen-domain/src/main/java/org/chorem/poll/domain/VoteCountingService.java (from rev 3173, branches/pollen-1.2.6-struts2/pollen-votecounting/src/main/java/org/chorem/pollen/votecounting/services/VoteCountingService.java) =================================================================== --- branches/pollen-1.2.6-struts2/pollen-domain/src/main/java/org/chorem/poll/domain/VoteCountingService.java (rev 0) +++ branches/pollen-1.2.6-struts2/pollen-domain/src/main/java/org/chorem/poll/domain/VoteCountingService.java 2012-03-12 08:37:14 UTC (rev 3174) @@ -0,0 +1,392 @@ +/* + * #%L + * Pollen :: Vote Counting + * + * $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.poll.domain; + +import com.google.common.collect.Iterables; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.chorem.poll.domain.poll.Poll; +import org.chorem.poll.domain.poll.PollChoice; +import org.chorem.poll.domain.poll.PollType; +import org.chorem.poll.domain.poll.PollVoteCountingChoice; +import org.chorem.poll.domain.poll.PollVoteCountingResult; +import org.chorem.poll.domain.poll.PollVotingChoice; +import org.chorem.poll.domain.poll.PollVotingGroup; +import org.chorem.poll.domain.poll.PollVotingPerson; +import org.chorem.poll.domain.strategy.CondorcetVoteCountingStrategy; +import org.chorem.poll.domain.strategy.NumberVoteCountingStrategy; +import org.chorem.poll.domain.strategy.PercentageVoteCountingStrategy; +import org.chorem.poll.domain.strategy.StandardVoteCountingStrategy; +import org.chorem.poll.domain.strategy.VoteCountingStrategy; +import org.chorem.poll.domain.vote.Choice; +import org.chorem.poll.domain.vote.Group; + +import java.util.ArrayList; +import java.util.List; + +/** + * Service qui permet le dépouillement d'un sondage. + * + * @author tchemit <chemit@codelutin.com> + * @since 1.2.6 + */ +public class VoteCountingService { + + /** Logger. */ + private static final Log log = LogFactory.getLog(VoteCountingService.class); + + public PollVoteCountingResult execute(Poll poll) { + boolean byGroup = poll.getPollType() == PollType.GROUP; + PollVoteCountingResult result = execute(poll, byGroup); + return result; + } + + public PollVoteCountingResult executeVoteCounting(Poll poll) { + return execute(poll, false); + } + + public PollVoteCountingResult executeGroupCounting(Poll poll) { + return execute(poll, true); + } + + /** + * Execution du dépouillement + * + * @param poll : sondage + * @return resultat + */ + private PollVoteCountingResult execute(Poll poll, boolean isByGroup) { + if (log.isInfoEnabled()) { + log.info("Dépouillement (byGroup=" + isByGroup + ") du sondage " + + poll.getPollId()); + } + + // Création et remplissage du contexte + VoteCountingContext context = VoteCountingContext.create(poll, + isByGroup); + + // Execution + if (!context.executeCounting()) { + //FIXME tchemit ? Why returning null, prefer use an exception + return null; + } + List<PollVoteCountingChoice> resChoices = context.executeStats(); + + PollVoteCountingResult result = new PollVoteCountingResult(); + result.setNbVotes(calculateNbVotes(poll, isByGroup)); + result.setTypeVoteCounting(poll.getVoteCounting()); + result.setByGroup(isByGroup); + result.setIdPoll(poll.getPollId()); + result.setChoices(resChoices); + return result; + } + + /** + * Contexte du dépouillement du sondage (PATTERN STRATEGY). + * + * @author fdesbois + * @version $Id$ + */ + public static class VoteCountingContext { + + /** + * Booléen permettant de savoir si la méthode doit calculer la valeur des + * choix en fonction des groupes + */ + private boolean groupCounting; + + /** Liste des choix liés au contexte et résultats */ + private List<Choice> choices, hiddenChoices, results; + + /** Méthode de dépouillement choisie pour ce contexte */ + private VoteCountingStrategy method; + + + /** + * Constructeur + * + * @param method : Méthode de dépouillement + * @param groupCounting : Dépouillement pour les groupes ou non + */ + VoteCountingContext(VoteCountingStrategy method, boolean groupCounting) { + this.groupCounting = groupCounting; + this.choices = new ArrayList<Choice>(); + this.hiddenChoices = new ArrayList<Choice>(); + this.results = new ArrayList<Choice>(); + this.method = method; + } + + /** + * Création du contexte en fonction du type de dépouillement + * + * @param poll + * @param isByGroup + * @return + */ + public static VoteCountingContext create(Poll poll, boolean isByGroup) { + + VoteCountingStrategy method; + switch (poll.getVoteCounting()) { + case NORMAL: + method = new StandardVoteCountingStrategy(); + break; + case PERCENTAGE: + method = new PercentageVoteCountingStrategy(); + break; + case CONDORCET: + method = new CondorcetVoteCountingStrategy(); + break; + case NUMBER: + method = new NumberVoteCountingStrategy(); + break; + default: + method = new StandardVoteCountingStrategy(); + } + VoteCountingContext result = new VoteCountingContext(method, + isByGroup); + + for (PollChoice choice : poll.getChoices()) { + if (choice.isHidden()) { + result.addHiddenChoice(choice); + } else { + result.addChoice(choice); + } + } + for (PollVotingGroup votingGroup : poll.getVotingGroups()) { + result.addGroup(votingGroup.getIdGroup(), votingGroup.getWeight()); + for (PollVotingPerson person : votingGroup.getVotingPersons()) { + String currentIdGroup = votingGroup.getIdGroup(); + for (PollVotingChoice vote : person.getChoices()) { + + Choice choice = result.getChoice(vote.getIdChoice()); + + Group group = choice.getGroup(currentIdGroup); + + group.addVote(vote.getValue(), + person.getWeight(), + person.getVotingId()); + } + } + } + return result; + } + + /** + * Renvoie un choix de la map + * + * @param idChoice : identifiant du choix + * @return choix + */ + public Choice getChoice(String idChoice) { + for (Choice choice : this.choices) { + if (choice.getIdChoice().equals(idChoice)) { + return choice; + } + } + for (Choice choice : this.hiddenChoices) { + if (choice.getIdChoice().equals(idChoice)) { + return choice; + } + } + return null; + } + + /** + * Renvoie la liste des choix + * + * @return liste des choix + */ + public List<Choice> getChoices() { + return this.choices; + } + + /** + * Renvoie la liste des choix cachés + * + * @return liste des choix cachés + */ + public List<Choice> getHiddenChoices() { + return this.hiddenChoices; + } + + /** + * Renvoie la liste des résultats + * + * @return liste des choix résultats + */ + public List<Choice> getResults() { + return this.results; + } + + /** + * Ajout d'un nouveau choix au contexte + * + * @param choice choix à ajouter + */ + public void addChoice(PollChoice choice) { + this.choices.add(new Choice(choice.getIdChoice(), choice.getName())); + } + + /** + * Ajout d'un nouveau choix au contexte + * + * @param choice choix à ajouter + */ + public void addHiddenChoice(PollChoice choice) { + this.hiddenChoices.add(new Choice(choice.getIdChoice(), choice.getName())); + } + + /** + * Ajout d'un groupe pour l'ensemble des choix du contexte + * + * @param idGroup : identifiant du groupe + * @param weight : poids du groupe + */ + public void addGroup(String idGroup, double weight) { + for (Choice choice : this.choices) { + choice.addGroup(idGroup, weight); + } + for (Choice choice : this.hiddenChoices) { + choice.addGroup(idGroup, weight); + } + } + + /** + * Test si le choix est un résultat d'après le contexte A utiliser après + * dépouillement sinon aucun résultat + * + * @param choice : choix à tester + * @return true si le choix est un résultat, false sinon + */ + private boolean isChoiceResult(Choice choice) { + for (Choice res : results) { + if (choice.getIdChoice().equals(res.getIdChoice())) { + return true; + } + } + return false; + } + + /** + * Execution de la méthode de dépouillement et calcul des choix résultats + * + * @return liste de choix résultats + */ + public boolean executeCounting() { + + method.executeCounting(choices, groupCounting); + method.executeCounting(hiddenChoices, groupCounting); + + this.results.clear(); + Choice result = new Choice(""); + + for (Choice curr : this.choices) { + if (curr.value() > result.value()) { + result = curr; + this.results.clear(); + } else if (curr.value() == result.value()) { + this.results.add(curr); + } + } + + this.results.add(result); + return true; + } + + public List<PollVoteCountingChoice> executeStats() { + List<PollVoteCountingChoice> resChoices = new ArrayList<PollVoteCountingChoice>(); + + try { + for (Choice choice : choices) { + PollVoteCountingChoice choiceDTO = new PollVoteCountingChoice(); + + choiceDTO.setIdChoice(choice.getIdChoice()); + choiceDTO.setName(choice.getName()); + choiceDTO.setValue(choice.getValue()); + choiceDTO.setResult(isChoiceResult(choice)); + choiceDTO.setNbVotes(calculateNbVotes(choice, groupCounting)); + + method.executeStats(choices, groupCounting, choice, choiceDTO); + resChoices.add(choiceDTO); + } + + for (Choice choice : hiddenChoices) { + PollVoteCountingChoice choiceDTO = new PollVoteCountingChoice(); + choiceDTO.setIdChoice(choice.getIdChoice()); + choiceDTO.setName(choice.getName()); + choiceDTO.setValue(choice.getValue()); + resChoices.add(choiceDTO); + } + + } catch (Exception e) { + log.error("L'exécution des statistiques a échoué", e); + resChoices = new ArrayList<PollVoteCountingChoice>(); + } + + return resChoices; + } + } + + /** + * Calcul le nombre de votes d'une liste + * + * @param poll + * @param byGroup + * @return nombre de votes + */ + public static int calculateNbVotes(Poll poll, boolean byGroup) { + int result; + List<PollVotingGroup> list = poll.getVotingGroups(); + if (byGroup) { + result = 0; + for (PollVotingGroup curr : list) { + result += Iterables.size(curr); + } + } else { + result = list.size(); + } + return result; + } + + /** + * Calcul le nombre de votes d'une liste + * + * @param choice + * @param byGroup + * @return nombre de votes + */ + public static int calculateNbVotes(Choice choice, boolean byGroup) { + int result; + List<Group> list = choice.getGroups(); + if (byGroup) { + result = 0; + for (Group curr : list) { + result += Iterables.size(curr); + } + } else { + result = list.size(); + } + return result; + } +} Copied: branches/pollen-1.2.6-struts2/pollen-domain/src/main/java/org/chorem/poll/domain/VoteCountingUtils.java (from rev 3173, branches/pollen-1.2.6-struts2/pollen-votecounting/src/main/java/org/chorem/pollen/votecounting/utils/Utils.java) =================================================================== --- branches/pollen-1.2.6-struts2/pollen-domain/src/main/java/org/chorem/poll/domain/VoteCountingUtils.java (rev 0) +++ branches/pollen-1.2.6-struts2/pollen-domain/src/main/java/org/chorem/poll/domain/VoteCountingUtils.java 2012-03-12 08:37:14 UTC (rev 3174) @@ -0,0 +1,88 @@ +/* + * #%L + * Pollen :: Vote Counting + * + * $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% + */ +package org.chorem.poll.domain; + +import org.chorem.poll.domain.vote.PercentageBehavior; + +import java.util.List; + +/** + * Classe utilitaire pour les algorithmes de dépouillement. + * + * @version $Id$ + */ +public class VoteCountingUtils { + + public static double calculatePercentage(PercentageBehavior in, + List<PercentageBehavior> list) { + double total = 0.; + for (PercentageBehavior curr : list) { + total += curr.value(); + } + return (in.value() / total * 100); + } + +// /** +// * Calcul le nombre de votes d'une liste +// * +// * @param poll +// * @param byGroup +// * @return nombre de votes +// */ +// public static int calculateNbVotes(Poll poll, boolean byGroup) { +// int result; +// List<PollVotingGroup> list = poll.getVotingGroups(); +// if (byGroup) { +// result = 0; +// for (PollVotingGroup curr : list) { +// result += Iterables.size(curr); +// } +// } else { +// result = list.size(); +// } +// return result; +// } +// +// /** +// * Calcul le nombre de votes d'une liste +// * +// * @param choice +// * @param byGroup +// * @return nombre de votes +// */ +// public static int calculateNbVotes(Choice choice, boolean byGroup) { +// int result; +// List<Group> list = choice.getGroups(); +// if (byGroup) { +// result = 0; +// for (Group curr : list) { +// result += Iterables.size(curr); +// } +// } else { +// result = list.size(); +// } +// return result; +// } + +} \ No newline at end of file Added: branches/pollen-1.2.6-struts2/pollen-domain/src/main/java/org/chorem/poll/domain/poll/ChoiceType.java =================================================================== --- branches/pollen-1.2.6-struts2/pollen-domain/src/main/java/org/chorem/poll/domain/poll/ChoiceType.java (rev 0) +++ branches/pollen-1.2.6-struts2/pollen-domain/src/main/java/org/chorem/poll/domain/poll/ChoiceType.java 2012-03-12 08:37:14 UTC (rev 3174) @@ -0,0 +1,55 @@ +/* + * #%L + * Pollen :: Vote Counting + * + * $Id: ChoiceType.java 3122 2012-01-30 20:43:30Z tchemit $ + * $HeadURL: http://svn.chorem.org/svn/pollen/branches/pollen-1.2.6-struts2/pollen-voteco... $ + * %% + * 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% + */ +package org.chorem.poll.domain.poll; + +import static org.nuiton.i18n.I18n.n_; + +public enum ChoiceType implements I18nAble { + TEXT(n_("pollen.choiceType.text")), + DATE(n_("pollen.choiceType.date")), + IMAGE(n_("pollen.choiceType.image")); + + private final String i18nKey; + + ChoiceType(String i18nKey) { + this.i18nKey = i18nKey; + } + + @Override + public String getI18nKey() { + return i18nKey; + } + + public static ChoiceType valueOf(int ordinal) { + + ChoiceType result = null; + for (ChoiceType curr : ChoiceType.values()) { + if (curr.ordinal() == ordinal) { + result = curr; + break; + } + } + return result; + } +} \ No newline at end of file Added: branches/pollen-1.2.6-struts2/pollen-domain/src/main/java/org/chorem/poll/domain/poll/I18nAble.java =================================================================== --- branches/pollen-1.2.6-struts2/pollen-domain/src/main/java/org/chorem/poll/domain/poll/I18nAble.java (rev 0) +++ branches/pollen-1.2.6-struts2/pollen-domain/src/main/java/org/chorem/poll/domain/poll/I18nAble.java 2012-03-12 08:37:14 UTC (rev 3174) @@ -0,0 +1,35 @@ +/* + * #%L + * Pollen :: Vote Counting + * + * $Id: I18nAble.java 3122 2012-01-30 20:43:30Z tchemit $ + * $HeadURL: http://svn.chorem.org/svn/pollen/branches/pollen-1.2.6-struts2/pollen-voteco... $ + * %% + * 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% + */ +package org.chorem.poll.domain.poll; + +/** + * Contract to expose a i18n key. + * + * @author tchemit <chemit@codelutin.com> + * @since 1.2.6 + */ +public interface I18nAble { + + String getI18nKey(); +} Copied: branches/pollen-1.2.6-struts2/pollen-domain/src/main/java/org/chorem/poll/domain/poll/Poll.java (from rev 3173, branches/pollen-1.2.6-struts2/pollen-votecounting/src/main/java/org/chorem/pollen/votecounting/dto/PollDTO.java) =================================================================== --- branches/pollen-1.2.6-struts2/pollen-domain/src/main/java/org/chorem/poll/domain/poll/Poll.java (rev 0) +++ branches/pollen-1.2.6-struts2/pollen-domain/src/main/java/org/chorem/poll/domain/poll/Poll.java 2012-03-12 08:37:14 UTC (rev 3174) @@ -0,0 +1,257 @@ +/* + * #%L + * Pollen :: Vote Counting + * + * $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% + */ +package org.chorem.poll.domain.poll; + +import com.google.common.collect.Lists; + +import java.util.Date; +import java.util.List; + +/** + * Classe DTO représentant un sondage. + * + * @version $Id$ + */ +public class Poll { + + private String pollId = ""; + + private String title = ""; + + private String description = ""; + + private Date beginDate = new Date(); + + private Date endDate = null; + + private Date beginChoiceDate = null; + + private int maxChoiceNb = 0; + + private boolean closed = true; + + private boolean choiceAddAllowed = false; + + private boolean anonymousVoteAllowed = false; + + private boolean anonymous = false; + + private boolean publicResults = false; + + private boolean continuousResults = false; + + private PollType pollType = null; + + private VoteCountingType voteCounting = null; + + private ChoiceType choiceType = null; + + private String creatorId = ""; + + private String creatorEmail = ""; + + private List<PollVotingGroup> votingGroups = Lists.newArrayList(); + + private List<PollChoice> choices = Lists.newArrayList(); + + private List<PollComment> comments = Lists.newArrayList(); + + private List<PollVoteCountingResult> voteCountingResult; + + public String getPollId() { + return pollId; + } + + public void setPollId(String pollId) { + this.pollId = pollId; + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + public Date getBeginDate() { + return beginDate; + } + + public void setBeginDate(Date beginDate) { + this.beginDate = beginDate; + } + + public Date getEndDate() { + return endDate; + } + + public void setEndDate(Date endDate) { + this.endDate = endDate; + } + + public Date getBeginChoiceDate() { + return beginChoiceDate; + } + + public void setBeginChoiceDate(Date beginChoiceDate) { + this.beginChoiceDate = beginChoiceDate; + } + + public int getMaxChoiceNb() { + return maxChoiceNb; + } + + public void setMaxChoiceNb(int maxChoiceNb) { + this.maxChoiceNb = maxChoiceNb; + } + + public boolean isClosed() { + return closed; + } + + public void setClosed(boolean closed) { + this.closed = closed; + } + + public boolean isChoiceAddAllowed() { + return choiceAddAllowed; + } + + public void setChoiceAddAllowed(boolean choiceAddAllowed) { + this.choiceAddAllowed = choiceAddAllowed; + } + + public boolean isAnonymousVoteAllowed() { + return anonymousVoteAllowed; + } + + public void setAnonymousVoteAllowed(boolean anonymousVoteAllowed) { + this.anonymousVoteAllowed = anonymousVoteAllowed; + } + + public boolean isAnonymous() { + return anonymous; + } + + public void setAnonymous(boolean anonymous) { + this.anonymous = anonymous; + } + + public boolean isPublicResults() { + return publicResults; + } + + public void setPublicResults(boolean publicResults) { + this.publicResults = publicResults; + } + + public boolean isContinuousResults() { + return continuousResults; + } + + public void setContinuousResults(boolean continuousResults) { + this.continuousResults = continuousResults; + } + + public PollType getPollType() { + return pollType; + } + + public void setPollType(PollType pollType) { + this.pollType = pollType; + } + + public VoteCountingType getVoteCounting() { + return voteCounting; + } + + public void setVoteCounting(VoteCountingType voteCounting) { + this.voteCounting = voteCounting; + } + + public ChoiceType getChoiceType() { + return choiceType; + } + + public void setChoiceType(ChoiceType choiceType) { + this.choiceType = choiceType; + } + + public String getCreatorId() { + return creatorId; + } + + public void setCreatorId(String creatorId) { + this.creatorId = creatorId; + } + + public String getCreatorEmail() { + return creatorEmail; + } + + public void setCreatorEmail(String creatorEmail) { + this.creatorEmail = creatorEmail; + } + + public List<PollVotingGroup> getVotingGroups() { + return votingGroups; + } + + public void setVotingGroups(List<PollVotingGroup> votingGroups) { + this.votingGroups = votingGroups; + } + + public List<PollChoice> getChoices() { + return choices; + } + + public void setChoices(List<PollChoice> choices) { + this.choices = choices; + } + + public List<PollComment> getComments() { + return comments; + } + + public void setComments(List<PollComment> comments) { + this.comments = comments; + } + + public List<PollVoteCountingResult> getVoteCountingResult() { + return voteCountingResult; + } + + public void setVoteCountingResult(List<PollVoteCountingResult> voteCountingResult) { + this.voteCountingResult = voteCountingResult; + } +} \ No newline at end of file Property changes on: branches/pollen-1.2.6-struts2/pollen-domain/src/main/java/org/chorem/poll/domain/poll/Poll.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Copied: branches/pollen-1.2.6-struts2/pollen-domain/src/main/java/org/chorem/poll/domain/poll/PollChoice.java (from rev 3173, branches/pollen-1.2.6-struts2/pollen-votecounting/src/main/java/org/chorem/pollen/votecounting/dto/PollChoiceDTO.java) =================================================================== --- branches/pollen-1.2.6-struts2/pollen-domain/src/main/java/org/chorem/poll/domain/poll/PollChoice.java (rev 0) +++ branches/pollen-1.2.6-struts2/pollen-domain/src/main/java/org/chorem/poll/domain/poll/PollChoice.java 2012-03-12 08:37:14 UTC (rev 3174) @@ -0,0 +1,72 @@ +/* + * #%L + * Pollen :: Vote Counting + * + * $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% + */ +package org.chorem.poll.domain.poll; + +/** + * Classe DTO représentant un choix de vote. + * + * @version $Id$ + */ +public class PollChoice { + + private String idChoice; + + private String name; + + private String description; + + private boolean hidden; + + public String getIdChoice() { + return idChoice; + } + + public void setIdChoice(String idChoice) { + this.idChoice = idChoice; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + public boolean isHidden() { + return hidden; + } + + public void setHidden(boolean hidden) { + this.hidden = hidden; + } +} \ No newline at end of file Property changes on: branches/pollen-1.2.6-struts2/pollen-domain/src/main/java/org/chorem/poll/domain/poll/PollChoice.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Copied: branches/pollen-1.2.6-struts2/pollen-domain/src/main/java/org/chorem/poll/domain/poll/PollComment.java (from rev 3173, branches/pollen-1.2.6-struts2/pollen-votecounting/src/main/java/org/chorem/pollen/votecounting/dto/CommentDTO.java) =================================================================== --- branches/pollen-1.2.6-struts2/pollen-domain/src/main/java/org/chorem/poll/domain/poll/PollComment.java (rev 0) +++ branches/pollen-1.2.6-struts2/pollen-domain/src/main/java/org/chorem/poll/domain/poll/PollComment.java 2012-03-12 08:37:14 UTC (rev 3174) @@ -0,0 +1,52 @@ +/* + * #%L + * Pollen :: Vote Counting + * + * $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% + */ +package org.chorem.poll.domain.poll; + +/** + * Classe DTO représentant un commentaire. + * + * @version $Id$ + */ +public class PollComment { + + private String votingID; + + private String text; + + public String getVotingID() { + return votingID; + } + + public void setVotingID(String votingID) { + this.votingID = votingID; + } + + public String getText() { + return text; + } + + public void setText(String text) { + this.text = text; + } +} \ No newline at end of file Property changes on: branches/pollen-1.2.6-struts2/pollen-domain/src/main/java/org/chorem/poll/domain/poll/PollComment.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Added: branches/pollen-1.2.6-struts2/pollen-domain/src/main/java/org/chorem/poll/domain/poll/PollType.java =================================================================== --- branches/pollen-1.2.6-struts2/pollen-domain/src/main/java/org/chorem/poll/domain/poll/PollType.java (rev 0) +++ branches/pollen-1.2.6-struts2/pollen-domain/src/main/java/org/chorem/poll/domain/poll/PollType.java 2012-03-12 08:37:14 UTC (rev 3174) @@ -0,0 +1,55 @@ +/* + * #%L + * Pollen :: Vote Counting + * + * $Id: PollType.java 3122 2012-01-30 20:43:30Z tchemit $ + * $HeadURL: http://svn.chorem.org/svn/pollen/branches/pollen-1.2.6-struts2/pollen-voteco... $ + * %% + * 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% + */ +package org.chorem.poll.domain.poll; + +import static org.nuiton.i18n.I18n.n_; + +public enum PollType implements I18nAble { + + FREE(n_("pollen.pollType.free")), + RESTRICTED(n_("pollen.pollType.restricted")), + GROUP(n_("pollen.pollType.group")); + + private final String i18nKey; + + PollType(String i18nKey) { + this.i18nKey = i18nKey; + } + + @Override + public String getI18nKey() { + return i18nKey; + } + + public static PollType valueOf(int ordinal) { + PollType result = null; + for (PollType curr : PollType.values()) { + if (curr.ordinal() == ordinal) { + result = curr; + break; + } + } + return result; + } +} Copied: branches/pollen-1.2.6-struts2/pollen-domain/src/main/java/org/chorem/poll/domain/poll/PollVoteCountingChoice.java (from rev 3173, branches/pollen-1.2.6-struts2/pollen-votecounting/src/main/java/org/chorem/pollen/votecounting/dto/ChoiceDTO.java) =================================================================== --- branches/pollen-1.2.6-struts2/pollen-domain/src/main/java/org/chorem/poll/domain/poll/PollVoteCountingChoice.java (rev 0) +++ branches/pollen-1.2.6-struts2/pollen-domain/src/main/java/org/chorem/poll/domain/poll/PollVoteCountingChoice.java 2012-03-12 08:37:14 UTC (rev 3174) @@ -0,0 +1,124 @@ +/* + * #%L + * Pollen :: Vote Counting + * + * $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% + */ + +package org.chorem.poll.domain.poll; + +/** + * Classe DTO représentant un choix et les valeurs du vote correspondant. + * + * @version $Id$ + */ +public class PollVoteCountingChoice { + + private String idChoice; + + private String name; + + private double value; + + private double percentage; + + private double average; + + private int nbVotes; + + private int nbBlankVotes; + + private boolean result; + + public PollVoteCountingChoice() { + } + + public PollVoteCountingChoice(String idChoice, double value, double percentage, + int nbVotes) { + this.idChoice = idChoice; + this.value = value; + this.percentage = percentage; + this.nbVotes = nbVotes; + } + + public String getIdChoice() { + return idChoice; + } + + public void setIdChoice(String idChoice) { + this.idChoice = idChoice; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public double getValue() { + return value; + } + + public void setValue(double value) { + this.value = value; + } + + public double getPercentage() { + return percentage; + } + + public void setPercentage(double percentage) { + this.percentage = percentage; + } + + public double getAverage() { + return average; + } + + public void setAverage(double average) { + this.average = average; + } + + public int getNbVotes() { + return nbVotes; + } + + public void setNbVotes(int nbVotes) { + this.nbVotes = nbVotes; + } + + public int getNbBlankVotes() { + return nbBlankVotes; + } + + public void setNbBlankVotes(int nbBlankVotes) { + this.nbBlankVotes = nbBlankVotes; + } + + public boolean isResult() { + return result; + } + + public void setResult(boolean result) { + this.result = result; + } +} \ No newline at end of file Copied: branches/pollen-1.2.6-struts2/pollen-domain/src/main/java/org/chorem/poll/domain/poll/PollVoteCountingResult.java (from rev 3173, branches/pollen-1.2.6-struts2/pollen-votecounting/src/main/java/org/chorem/pollen/votecounting/dto/VoteCountingResultDTO.java) =================================================================== --- branches/pollen-1.2.6-struts2/pollen-domain/src/main/java/org/chorem/poll/domain/poll/PollVoteCountingResult.java (rev 0) +++ branches/pollen-1.2.6-struts2/pollen-domain/src/main/java/org/chorem/poll/domain/poll/PollVoteCountingResult.java 2012-03-12 08:37:14 UTC (rev 3174) @@ -0,0 +1,99 @@ +/* + * #%L + * Pollen :: Vote Counting + * + * $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% + */ +package org.chorem.poll.domain.poll; + +import java.util.List; + +/** + * Classe DTO représentant un résultat de sondage. + * + * @version $Id$ + */ +public class PollVoteCountingResult { + + private int nbVotes; + + private String choiceResult; + + private String idPoll; + + private VoteCountingType typeVoteCounting; + + private boolean byGroup; + + private List<PollVoteCountingChoice> choices; + + public PollVoteCountingResult() { + + } + + public int getNbVotes() { + return nbVotes; + } + + public void setNbVotes(int nbVotes) { + this.nbVotes = nbVotes; + } + + public String getChoiceResult() { + return choiceResult; + } + + public void setChoiceResult(String choiceResult) { + this.choiceResult = choiceResult; + } + + public String getIdPoll() { + return idPoll; + } + + public void setIdPoll(String idPoll) { + this.idPoll = idPoll; + } + + public VoteCountingType getTypeVoteCounting() { + return typeVoteCounting; + } + + public void setTypeVoteCounting(VoteCountingType typeVoteCounting) { + this.typeVoteCounting = typeVoteCounting; + } + + public boolean isByGroup() { + return byGroup; + } + + public void setByGroup(boolean byGroup) { + this.byGroup = byGroup; + } + + public List<PollVoteCountingChoice> getChoices() { + return choices; + } + + public void setChoices(List<PollVoteCountingChoice> choices) { + this.choices = choices; + } + +} \ No newline at end of file Copied: branches/pollen-1.2.6-struts2/pollen-domain/src/main/java/org/chorem/poll/domain/poll/PollVotingChoice.java (from rev 3173, branches/pollen-1.2.6-struts2/pollen-votecounting/src/main/java/org/chorem/pollen/votecounting/dto/VoteToChoiceDTO.java) =================================================================== --- branches/pollen-1.2.6-struts2/pollen-domain/src/main/java/org/chorem/poll/domain/poll/PollVotingChoice.java (rev 0) +++ branches/pollen-1.2.6-struts2/pollen-domain/src/main/java/org/chorem/poll/domain/poll/PollVotingChoice.java 2012-03-12 08:37:14 UTC (rev 3174) @@ -0,0 +1,63 @@ +/* + * #%L + * Pollen :: Vote Counting + * + * $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% + */ +package org.chorem.poll.domain.poll; + +/** + * Classe DTO représentant un vote. + * + * @version $Id$ + */ +public class PollVotingChoice { + + private double value; + + private String idChoice; + + public PollVotingChoice(int idChoice, double value) { + this.idChoice = String.valueOf(idChoice); + this.value = value; + } + + public PollVotingChoice(String idChoice, double value) { + this.idChoice = idChoice; + this.value = value; + } + + public double getValue() { + return value; + } + + public void setValue(double value) { + this.value = value; + } + + public String getIdChoice() { + return idChoice; + } + + public void setIdChoice(String idChoice) { + this.idChoice = idChoice; + } + +} \ No newline at end of file Copied: branches/pollen-1.2.6-struts2/pollen-domain/src/main/java/org/chorem/poll/domain/poll/PollVotingGroup.java (from rev 3173, branches/pollen-1.2.6-struts2/pollen-votecounting/src/main/java/org/chorem/pollen/votecounting/dto/VotingGroupDTO.java) =================================================================== --- branches/pollen-1.2.6-struts2/pollen-domain/src/main/java/org/chorem/poll/domain/poll/PollVotingGroup.java (rev 0) +++ branches/pollen-1.2.6-struts2/pollen-domain/src/main/java/org/chorem/poll/domain/poll/PollVotingGroup.java 2012-03-12 08:37:14 UTC (rev 3174) @@ -0,0 +1,83 @@ +/* + * #%L + * Pollen :: Vote Counting + * + * $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% + */ +package org.chorem.poll.domain.poll; + +import com.google.common.collect.Lists; + +import java.util.Iterator; +import java.util.List; + +/** + * Classe DTO représentant un groupe de votants. + * + * @version $Id$ + */ +public class PollVotingGroup implements Iterable<PollVotingPerson> { + + private String name; + + private double weight; + + private String idGroup; + + private List<PollVotingPerson> votingPersons = Lists.newArrayList(); + + @Override + public Iterator<PollVotingPerson> iterator() { + return votingPersons.iterator(); + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public double getWeight() { + return weight; + } + + public void setWeight(double weight) { + this.weight = weight; + } + + public String getIdGroup() { + return idGroup; + } + + public void setIdGroup(String idGroup) { + this.idGroup = idGroup; + } + + public List<PollVotingPerson> getVotingPersons() { + return votingPersons; + } + + public void setVotingPersons(List<PollVotingPerson> votingPersons) { + this.votingPersons = votingPersons; + } + +} \ No newline at end of file Property changes on: branches/pollen-1.2.6-struts2/pollen-domain/src/main/java/org/chorem/poll/domain/poll/PollVotingGroup.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Copied: branches/pollen-1.2.6-struts2/pollen-domain/src/main/java/org/chorem/poll/domain/poll/PollVotingPerson.java (from rev 3173, branches/pollen-1.2.6-struts2/pollen-votecounting/src/main/java/org/chorem/pollen/votecounting/dto/VotingPersonDTO.java) =================================================================== --- branches/pollen-1.2.6-struts2/pollen-domain/src/main/java/org/chorem/poll/domain/poll/PollVotingPerson.java (rev 0) +++ branches/pollen-1.2.6-struts2/pollen-domain/src/main/java/org/chorem/poll/domain/poll/PollVotingPerson.java 2012-03-12 08:37:14 UTC (rev 3174) @@ -0,0 +1,108 @@ +/* + * #%L + * Pollen :: Vote Counting + * + * $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% + */ +package org.chorem.poll.domain.poll; + +import com.google.common.collect.Lists; + +import java.util.Date; +import java.util.List; + +/** + * Classe DTO représentant un votant. + * + * @version $Id$ + */ +public class PollVotingPerson { + + private String votingId; + + private double weight; + + private String comment; + + private String email; + + private String login; + + private Date votingDate; + + private List<PollVotingChoice> choices = Lists.newArrayList(); + + public String getVotingId() { + return votingId; + } + + public void setVotingId(String votingId) { + this.votingId = votingId; + } + + public double getWeight() { + return weight; + } + + public void setWeight(double weight) { + this.weight = weight; + } + + public String getComment() { + return comment; + } + + public void setComment(String comment) { + this.comment = comment; + } + + public String getEmail() { + return email; + } + + public void setEmail(String email) { + this.email = email; + } + + public String getLogin() { + return login; + } + + public void setLogin(String login) { + this.login = login; + } + + public Date getVotingDate() { + return votingDate; + } + + public void setVotingDate(Date votingDate) { + this.votingDate = votingDate; + } + + public List<PollVotingChoice> getChoices() { + return choices; + } + + public void setChoices(List<PollVotingChoice> choices) { + this.choices = choices; + } + +} \ No newline at end of file Property changes on: branches/pollen-1.2.6-struts2/pollen-domain/src/main/java/org/chorem/poll/domain/poll/PollVotingPerson.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Added: branches/pollen-1.2.6-struts2/pollen-domain/src/main/java/org/chorem/poll/domain/poll/VoteCountingType.java =================================================================== --- branches/pollen-1.2.6-struts2/pollen-domain/src/main/java/org/chorem/poll/domain/poll/VoteCountingType.java (rev 0) +++ branches/pollen-1.2.6-struts2/pollen-domain/src/main/java/org/chorem/poll/domain/poll/VoteCountingType.java 2012-03-12 08:37:14 UTC (rev 3174) @@ -0,0 +1,55 @@ +/* + * #%L + * Pollen :: Vote Counting + * + * $Id: VoteCountingType.java 3122 2012-01-30 20:43:30Z tchemit $ + * $HeadURL: http://svn.chorem.org/svn/pollen/branches/pollen-1.2.6-struts2/pollen-voteco... $ + * %% + * 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% + */ +package org.chorem.poll.domain.poll; + +import static org.nuiton.i18n.I18n.n_; + +public enum VoteCountingType implements I18nAble { + NORMAL(n_("pollen.voteCountingType.normal")), + PERCENTAGE(n_("pollen.voteCountingType.percentage")), + CONDORCET(n_("pollen.voteCountingType.condorcet")), + NUMBER(n_("pollen.voteCountingType.number")); + + private final String i18nKey; + + VoteCountingType(String i18nKey) { + this.i18nKey = i18nKey; + } + + @Override + public String getI18nKey() { + return i18nKey; + } + + public static VoteCountingType valueOf(int ordinal) { + VoteCountingType result = null; + for (VoteCountingType curr : VoteCountingType.values()) { + if (curr.ordinal() == ordinal) { + result = curr; + break; + } + } + return result; + } +} \ No newline at end of file Copied: branches/pollen-1.2.6-struts2/pollen-domain/src/main/java/org/chorem/poll/domain/strategy/CondorcetVoteCountingStrategy.java (from rev 3173, branches/pollen-1.2.6-struts2/pollen-votecounting/src/main/java/org/chorem/pollen/votecounting/business/CondorcetMethod.java) =================================================================== --- branches/pollen-1.2.6-struts2/pollen-domain/src/main/java/org/chorem/poll/domain/strategy/CondorcetVoteCountingStrategy.java (rev 0) +++ branches/pollen-1.2.6-struts2/pollen-domain/src/main/java/org/chorem/poll/domain/strategy/CondorcetVoteCountingStrategy.java 2012-03-12 08:37:14 UTC (rev 3174) @@ -0,0 +1,337 @@ +/* + * #%L + * Pollen :: Vote Counting + * + * $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% + */ +package org.chorem.poll.domain.strategy; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.chorem.poll.domain.VoteCountingUtils; +import org.chorem.poll.domain.poll.PollVoteCountingChoice; +import org.chorem.poll.domain.vote.Choice; +import org.chorem.poll.domain.vote.Group; +import org.chorem.poll.domain.vote.PercentageBehavior; +import org.chorem.poll.domain.vote.Vote; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.TreeSet; + +/** + * Méthode de dépouillement Condorcet. + * + * @author rannou + * @version $Id$ + */ +public class CondorcetVoteCountingStrategy implements VoteCountingStrategy { + + /** Victoires pour chaque choix. */ + private Map<Choice, Double> victories; + + /** Choix de chaque personne. (personne >> choix >> classement) */ + private Map<String, Map<Choice, Double>> personVotes; + + /** Choix de chaque groupe. (groupe >> choix >> classement) */ + private Map<String, Map<Choice, Double>> groupVotes; + + /** log. */ + private static final Log log = + LogFactory.getLog(CondorcetVoteCountingStrategy.class); + + @Override + public void executeCounting(List<Choice> choices, boolean byGroup) { + if (byGroup) { + initPersonVotes(choices); + initGroupVotes(choices); + calculateVictories(choices, groupVotes, byGroup); + updateChoices(choices); + } else { + initPersonVotes(choices); + calculateVictories(choices, personVotes, byGroup); + updateChoices(choices); + } + } + + /** + * Initialisation des votes par personne. Les votes sont initialisés avec + * les valeurs des choix. + * + * @param choices Les choix utilisés pour le dépouillement. + */ + private void initPersonVotes(List<Choice> choices) { + personVotes = new HashMap<String, Map<Choice, Double>>(); + for (Choice choice : choices) { + for (Group group : choice.getGroups()) { + for (Vote vote : group.getVotes()) { + Map<Choice, Double> choicesMap = personVotes.get(vote + .getVotingID()); + if (choicesMap == null) { + choicesMap = new HashMap<Choice, Double>(); + } + choicesMap.put(choice, vote.getValue()); + personVotes.put(vote.getVotingID(), choicesMap); + } + } + } + } + + /** + * Initialisation des votes par groupes. Les votes sont initialisés avec les + * victoires des membres du groupe. Des dépouillements sont donc effectués + * au sein de chaque groupe avant d'effectuer le dépouillement global. + * + * @param choices Les choix utilisés pour le dépouillement. + */ + private void initGroupVotes(List<Choice> choices) { + groupVotes = new HashMap<String, Map<Choice, Double>>(); + + // Initialisation de la liste des membres de chaque groupe (groupMembers) + Map<String, Set<String>> groupMembers = new HashMap<String, Set<String>>(); + for (Choice choice : choices) { + for (Group group : choice.getGroups()) { + Set<String> members = groupMembers.get(group.getIdGroup()); + if (members == null) { + members = new HashSet<String>(); + } + for (Vote vote : group.getVotes()) { + members.add(vote.getVotingID()); + } + groupMembers.put(group.getIdGroup(), members); + } + } + + // Initialisation des votes de chaque groupe (groupVotes) + for (String groupID : groupMembers.keySet()) { + + // Initialisation des votes par membre à partir des personVotes + // membersVotes : liste des votes de chaque votant du groupe courant + Map<String, Map<Choice, Double>> membersVotes = new HashMap<String, Map<Choice, Double>>(); + for (String votingID : groupMembers.get(groupID)) { + Map<Choice, Double> choicesMap = personVotes.get(votingID); + membersVotes.put(votingID, choicesMap); + } + + // Calcul des victoires pour le groupe + calculateVictories(choices, membersVotes, false); + groupVotes.put(groupID, victories2votes(victories)); + + //TODO joindre ce résultat (Map) au group.value (Double) ??? + TreeSet<Double> treeVictories = new TreeSet<Double>(victories + .values()); + for (Choice choice : choices) { + for (Group group : choice.getGroups()) { + if (group.getIdGroup().equals(groupID)) { + group.setValue(treeVictories.last()); + } + } + } + + // Affichage des victoires et des groupVotes + if (log.isDebugEnabled()) { + log.debug("> groupe " + groupID); + for (Choice choice : victories.keySet()) { + log.debug(">> choix= " + choice.getIdChoice() + + " _ victoires= " + victories.get(choice) + + " _ classement= " + + groupVotes.get(groupID).get(choice)); + } + } + } + } + + /** + * Calcul des victoires. Parcours des votants (personnes ou groupes) et de + * leurs votes. Pour chaque vote, calcul des victoires par rapport aux + * autres votes du votant et mise à jour du nombre de victoires pour ce + * vote. + * + * @param choices Les choix utilisés pour le dépouillement. + * @param votes Les votes de chaque votant. + * @param byGroup Calcul sur des groupes. + * @see #getVictories(String, Choice, Map) + */ + private void calculateVictories(List<Choice> choices, + Map<String, Map<Choice, Double>> votes, boolean byGroup) { + // Initialisation des victoires à 0. + victories = new HashMap<Choice, Double>(); + for (Choice choice : choices) { + victories.put(choice, 0.); + } + + // Calcul des victoires + for (String keyVotant : votes.keySet()) { + for (Choice keyChoice : votes.get(keyVotant).keySet()) { + + // Victoires pour le votant + application du poids du votant + Double v = getVictories(keyVotant, keyChoice, votes) + .doubleValue(); + if (byGroup) { + v *= getGroupWeight(choices, keyVotant); + } else { + v *= getPersonWeight(choices, keyVotant); + } + + victories.put(keyChoice, victories.get(keyChoice) + v); + } + } + } + + /** + * Calcul du nombre de duels gagnés par un choix par rapport aux autres + * choix d'un votant (personne ou groupe). + * + * @param id L'identifiant du votant + * @param choice Le choix de référence pour le calcul + * @param votes Les votes de chaque votant. + * @return Le nombre de duels gagnés + */ + private Integer getVictories(String id, Choice choice, + Map<String, Map<Choice, Double>> votes) { + int res = 0; // résultat : nombre de victoires + + Double ref = votes.get(id).get(choice); // valeur de référence + Double val; // valeur courante + + for (Choice keyChoice : victories.keySet()) { + if (votes.get(id).containsKey(keyChoice)) { + val = votes.get(id).get(keyChoice); + if (ref < val) { + res++; + } + } else { // val == null (choix non renseigné par le votant) + res++; + } + } + return res; + } + + /** + * Transformation des victoires en votes. Les victoires listent le nombre de + * duels gagnés pour chaque choix alors que les votes donnent leur + * classement. + * + * @param victories Les victoires. + * @return Les votes. + */ + private Map<Choice, Double> victories2votes(Map<Choice, Double> victories) { + Map<Choice, Double> victories_ = new HashMap<Choice, Double>(victories); + Map<Choice, Double> votes_ = new HashMap<Choice, Double>(); + + Integer d = 1; + Map<Choice, Double> victoriesMax = getMax(victories_); + while (!victoriesMax.isEmpty()) { + for (Choice choice : victoriesMax.keySet()) { + votes_.put(choice, d.doubleValue()); + victories_.remove(choice); + } + d++; + victoriesMax = getMax(victories_); + } + + return votes_; + } + + /** + * Retourne le ou les couples choix-valeur de valeur maxmimale. + * + * @param victories Les victoires. + * @return Le ou les choix de valeur maximale. + */ + private Map<Choice, Double> getMax(Map<Choice, Double> victories) { + Map<Choice, Double> result = new HashMap<Choice, Double>(); + if (victories.isEmpty()) { + return result; + } + + TreeSet<Double> treeVictories = new TreeSet<Double>(victories.values()); + Double max = treeVictories.last(); + for (Choice choice : victories.keySet()) { + if (victories.get(choice).equals(max)) { + result.put(choice, victories.get(choice)); + } + } + return result; + } + + /** + * Récupération du poids d'une personne. + * + * @param choices Les choix utilisés pour le dépouillement. + * @param id L'identifiant de la personne. + * @return Le poids de la personne. + */ + private Double getPersonWeight(List<Choice> choices, String id) { + for (Choice choice : choices) { + for (Group group : choice.getGroups()) { + for (Vote vote : group.getVotes()) { + if (id.equals(vote.getVotingID())) { + return vote.getWeight(); + } + } + } + } + return null; + } + + /** + * Récupération du poids d'un groupe. + * + * @param choices Les choix utilisés pour le dépouillement. + * @param id L'identifiant du groupe. + * @return Le poids du groupe. + */ + private Double getGroupWeight(List<Choice> choices, String id) { + for (Choice choice : choices) { + for (Group group : choice.getGroups()) { + if (id.equals(group.getIdGroup())) { + return group.getWeight(); + } + } + } + return null; + } + + /** + * Mise à jour des valeurs des choix. + * + * @param choices Les choix utilisés pour le dépouillement. + */ + private void updateChoices(List<Choice> choices) { + for (Choice choice : choices) { + choice.setValue(victories.get(choice)); + /*for (Group group : choice.getGroups()) { + group.setValue(victories.get(choice)); + }*/ + } + } + + @Override + public void executeStats(List<Choice> choices, boolean groupCounting, + Choice choice, PollVoteCountingChoice choiceDTO) { + List<PercentageBehavior> list = new ArrayList<PercentageBehavior>(choices); + choiceDTO.setPercentage(VoteCountingUtils.calculatePercentage(choice, list)); + } +} \ No newline at end of file Copied: branches/pollen-1.2.6-struts2/pollen-domain/src/main/java/org/chorem/poll/domain/strategy/NumberVoteCountingStrategy.java (from rev 3173, branches/pollen-1.2.6-struts2/pollen-votecounting/src/main/java/org/chorem/pollen/votecounting/business/NumberMethod.java) =================================================================== --- branches/pollen-1.2.6-struts2/pollen-domain/src/main/java/org/chorem/poll/domain/strategy/NumberVoteCountingStrategy.java (rev 0) +++ branches/pollen-1.2.6-struts2/pollen-domain/src/main/java/org/chorem/poll/domain/strategy/NumberVoteCountingStrategy.java 2012-03-12 08:37:14 UTC (rev 3174) @@ -0,0 +1,97 @@ +/* + * #%L + * Pollen :: Vote Counting + * + * $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% + */ +package org.chorem.poll.domain.strategy; + +import org.chorem.poll.domain.poll.PollVoteCountingChoice; +import org.chorem.poll.domain.vote.Choice; +import org.chorem.poll.domain.vote.Group; +import org.chorem.poll.domain.vote.Vote; + +import java.util.List; + +/** + * Méthode de dépouillement pour vote de type number. + * + * @version $Revision$ $Date$ + * @since 1.2.2 + */ +public class NumberVoteCountingStrategy implements VoteCountingStrategy { + + /** Permet de rajouter des choix cachés */ + public static final String HIDDEN_PREFIX = "HIDDEN_"; + + @Override + public void executeCounting(List<Choice> choices, boolean byGroup) { + + for (Choice choice : choices) { + double totalChoice = 0.; + + for (Group group : choice.getGroups()) { + double totalGroup = 0.; + + for (Vote vote : group.getVotes()) { + double value = vote.getValue(); + + if (value >= 0) { + if (byGroup) { + totalGroup += vote.getValue() * vote.getWeight() * group.getWeight(); + } else { + totalGroup += vote.getValue() * vote.getWeight(); + } + } + } + + group.setValue(totalGroup); + totalChoice += totalGroup; + } + + choice.setValue(totalChoice); + } + } + + @Override + public void executeStats(List<Choice> choices, boolean groupCounting, + Choice choice, PollVoteCountingChoice choiceDTO) { + + int nbBlankVotes = 0; + double average = 0; + int nbVotes = 0; + + for (Group group : choice.getGroups()) { + for (Vote vote : group.getVotes()) { + nbVotes = nbVotes + 1; + if (vote.getValue() < 0) { + nbBlankVotes = nbBlankVotes + 1; + } + } + } + + if (nbVotes - nbBlankVotes > 0) { + average = choice.value() / (nbVotes - nbBlankVotes); + } + + choiceDTO.setAverage(average); + choiceDTO.setNbBlankVotes(nbBlankVotes); + } +} Copied: branches/pollen-1.2.6-struts2/pollen-domain/src/main/java/org/chorem/poll/domain/strategy/PercentageVoteCountingStrategy.java (from rev 3173, branches/pollen-1.2.6-struts2/pollen-votecounting/src/main/java/org/chorem/pollen/votecounting/business/PercentageMethod.java) =================================================================== --- branches/pollen-1.2.6-struts2/pollen-domain/src/main/java/org/chorem/poll/domain/strategy/PercentageVoteCountingStrategy.java (rev 0) +++ branches/pollen-1.2.6-struts2/pollen-domain/src/main/java/org/chorem/poll/domain/strategy/PercentageVoteCountingStrategy.java 2012-03-12 08:37:14 UTC (rev 3174) @@ -0,0 +1,52 @@ +/* + * #%L + * Pollen :: Vote Counting + * + * $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% + */ +package org.chorem.poll.domain.strategy; + +import org.chorem.poll.domain.vote.Choice; + +import java.util.List; + +/** + * Méthode de dépouillement par pourcentage (pondération des choix). Cette + * méthode étend la méthode standard en ajoutant la pondération des choix. + * + * @author rannou + * @version $Id$ + */ + +public class PercentageVoteCountingStrategy extends StandardVoteCountingStrategy { + + @Override + public double getValueForChoiceByGroup(Choice choice, String idGroup, + List<String> choicesId) { + for (String idChoice : choicesId) { + if (choice.getIdChoice().equals(idChoice)) { + return choice.getGroup(idGroup).getValue() + * choice.getGroup(idGroup).getWeight(); // choice.getGroup(idGroup).getValue() remplace 1 + + } + } + return 0; + } +} \ No newline at end of file Copied: branches/pollen-1.2.6-struts2/pollen-domain/src/main/java/org/chorem/poll/domain/strategy/StandardVoteCountingStrategy.java (from rev 3173, branches/pollen-1.2.6-struts2/pollen-votecounting/src/main/java/org/chorem/pollen/votecounting/business/StandardMethod.java) =================================================================== --- branches/pollen-1.2.6-struts2/pollen-domain/src/main/java/org/chorem/poll/domain/strategy/StandardVoteCountingStrategy.java (rev 0) +++ branches/pollen-1.2.6-struts2/pollen-domain/src/main/java/org/chorem/poll/domain/strategy/StandardVoteCountingStrategy.java 2012-03-12 08:37:14 UTC (rev 3174) @@ -0,0 +1,132 @@ +/* + * #%L + * Pollen :: Vote Counting + * + * $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% + */ +package org.chorem.poll.domain.strategy; + +import org.chorem.poll.domain.VoteCountingUtils; +import org.chorem.poll.domain.poll.PollVoteCountingChoice; +import org.chorem.poll.domain.vote.Choice; +import org.chorem.poll.domain.vote.Group; +import org.chorem.poll.domain.vote.PercentageBehavior; +import org.chorem.poll.domain.vote.Vote; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * Méthode de dépouillement standard. + * + * @version $Id$ + */ +public class StandardVoteCountingStrategy implements VoteCountingStrategy { + + @Override + public void executeCounting(List<Choice> choices, boolean byGroup) { + calculateGroups(choices); + if (byGroup) { + calculateChoicesByGroup(choices); + } + } + + // Pre traitement de la valeur des groupes + totalChoice (tout le monde) + public void calculateGroups(List<Choice> choices) { + for (Choice choice : choices) { + double totalChoice = 0.; + for (Group group : choice.getGroups()) { + this.getValueForGroup(group); + totalChoice += group.getValue(); + } + choice.setValue(totalChoice); + } + } + + public void getValueForGroup(Group group) { + double total = 0.; + for (Vote vote : group.getVotes()) { + total += vote.getValue() * vote.getWeight(); + } + group.setValue(total); + } + + // Calcul avec prise en compte des groupes + public void calculateChoicesByGroup(List<Choice> choices) { + Map<String, List<String>> mapGroupsChoosenChoices = this + .getChoosenChoicesByGroup(choices); + + for (Choice choice : choices) { + double totalChoice = 0.; + for (String idGroup : mapGroupsChoosenChoices.keySet()) { + totalChoice += this.getValueForChoiceByGroup(choice, idGroup, + mapGroupsChoosenChoices.get(idGroup)); + } + choice.setValue(totalChoice); + } + } + + // Récupération du choix de chaque groupe (plusieurs choix si égalité) + public Map<String, List<String>> getChoosenChoicesByGroup( + List<Choice> choices) { + Map<String, List<String>> mapGroupsChoosenChoices = new HashMap<String, List<String>>(); + + if (choices != null && choices.size() > 0) { + for (Group group : choices.get(0).getGroups()) { + double maxValue = 0.; + List<String> choosenChoicesId = new ArrayList<String>(); + + for (Choice choice : choices) { + Group curr = choice.getGroup(group.getIdGroup()); + if (curr.getValue() > maxValue) { + maxValue = curr.getValue(); + choosenChoicesId.clear(); + } + if (curr.getValue() != 0 && maxValue == curr.getValue()) { + choosenChoicesId.add(choice.getIdChoice()); + } + } + + mapGroupsChoosenChoices.put(group.getIdGroup(), choosenChoicesId); + } + } + + return mapGroupsChoosenChoices; + } + + public double getValueForChoiceByGroup(Choice choice, String idGroup, + List<String> choicesId) { + for (String idChoice : choicesId) { + if (choice.getIdChoice().equals(idChoice)) { + return 1 * choice.getGroup(idGroup).getWeight(); // 1 a remplacer suivant l'algo + } + } + return 0; + } + + @Override + public void executeStats(List<Choice> choices, boolean groupCounting, + Choice choice, PollVoteCountingChoice choiceDTO) { + List<PercentageBehavior> list = new ArrayList<PercentageBehavior>(choices); + choiceDTO.setPercentage(VoteCountingUtils.calculatePercentage(choice, list)); + } +} \ No newline at end of file Property changes on: branches/pollen-1.2.6-struts2/pollen-domain/src/main/java/org/chorem/poll/domain/strategy/StandardVoteCountingStrategy.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Added: branches/pollen-1.2.6-struts2/pollen-domain/src/main/java/org/chorem/poll/domain/strategy/VoteCountingStrategy.java =================================================================== --- branches/pollen-1.2.6-struts2/pollen-domain/src/main/java/org/chorem/poll/domain/strategy/VoteCountingStrategy.java (rev 0) +++ branches/pollen-1.2.6-struts2/pollen-domain/src/main/java/org/chorem/poll/domain/strategy/VoteCountingStrategy.java 2012-03-12 08:37:14 UTC (rev 3174) @@ -0,0 +1,37 @@ +package org.chorem.poll.domain.strategy; + +import org.chorem.poll.domain.poll.PollVoteCountingChoice; +import org.chorem.poll.domain.vote.Choice; + +import java.util.List; + +/** + * Contract of a strategy of a vote countig. + * + * @author tchemit <chemit@codelutin.com> + * @since 1.2.6 + */ +public interface VoteCountingStrategy { + + /** + * Execute le dépouillement de l'ensemble des choix spécifiés. + * + * @param pollchoices les choix à dépouiller + * @param groupCounting si le dépouillement doit être groupé + */ + public void executeCounting(List<Choice> pollchoices, boolean groupCounting); + + /** + * Effectue des statistiques sur le <code>choice</code> specifié. + * + * La fonction <code>executeCounting</code> doit avoir été + * executée avant d'appeler cette fonction. + * + * @param choices l'ensemble des choix du dépouillement + * @param groupCounting si le dépouillement doit être groupé + * @param choice le choix sur lequel effectuer des statistiques + * @param choiceDTO le choix contenant les resultats statistiques + */ + public void executeStats(List<Choice> choices, boolean groupCounting, + Choice choice, PollVoteCountingChoice choiceDTO); +} Copied: branches/pollen-1.2.6-struts2/pollen-domain/src/main/java/org/chorem/poll/domain/vote/Choice.java (from rev 3173, branches/pollen-1.2.6-struts2/pollen-votecounting/src/main/java/org/chorem/pollen/votecounting/business/Choice.java) =================================================================== --- branches/pollen-1.2.6-struts2/pollen-domain/src/main/java/org/chorem/poll/domain/vote/Choice.java (rev 0) +++ branches/pollen-1.2.6-struts2/pollen-domain/src/main/java/org/chorem/poll/domain/vote/Choice.java 2012-03-12 08:37:14 UTC (rev 3174) @@ -0,0 +1,157 @@ +/* + * #%L + * Pollen :: Vote Counting + * + * $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% + */ +package org.chorem.poll.domain.vote; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +/** + * Cette classe décrit le choix d'un sondage. Sa valeur sera modifiée au moment + * de l'exécution de la méthode de dépouillement. + * + * @author fdesbois + * @version $Id$ + */ +public class Choice implements Iterable<Group>, PercentageBehavior { + /** identifiant du choix */ + private String idChoice; + + /** nom du choix */ + private String name; + + /** valeur du choix déterminé par le dépouillement */ + private double value; + + /** Map des groupes liés au choix */ + private List<Group> groups; + + /** + * Constructeur + * + * @param id : identifiant du choix + */ + public Choice(String id) { + this.idChoice = id; + this.groups = new ArrayList<Group>(); + this.value = 0.; + } + + /** + * Constructeur + * + * @param id : identifiant du choix + * @param name : nom du choix + */ + public Choice(String id, String name) { + this(id); + this.name = name; + } + + /** + * setValue : Mise à jour de la valeur du choix + * + * @param value : nouvelle valeur + */ + public void setValue(double value) { + this.value = value; + } + + /** + * getValue : Renvoie la valeur du choix + * + * @return valeur courante du choix + */ + public double getValue() { + return this.value; + } + + /** + * getId : Renvoie l'identifiant du choix + * + * @return identifiant du choix + */ + public String getIdChoice() { + return this.idChoice; + } + + /** + * getName : Renvoie le nom du choix + * + * @return nom du choix + */ + public String getName() { + return this.name; + } + + /** + * getGroups : Renvoie la map des groupes + * + * @return map des groupes pour ce choix + */ + public List<Group> getGroups() { + return this.groups; + } + + /** + * addGroup : Ajout d'un groupe lié au choix + * + * @param weight : poids du groupe dans le sondage + * @param idGroup : identifiant du groupe + */ + public void addGroup(String idGroup, double weight) { + this.groups.add(new Group(idGroup, weight)); + } + + /** + * getGroup : Renvoie un groupe existant dans la map + * + * @param idGroup : identifiant du groupe dans la map + * @return le groupe ayant l'identifiant idGroup + */ + public Group getGroup(String idGroup) { + for (Group g : this.groups) { + if (g.getIdGroup().equals(idGroup)) { + return g; + } + } + return null; + } + + @Override + public double value() { + return getValue(); + } + + + @Override + public Iterator<Group> iterator() { + return groups.iterator(); + } + + @Override + public String toString() { + return "Choice id=" + idChoice + " _ value=" + value; + } +} \ No newline at end of file Copied: branches/pollen-1.2.6-struts2/pollen-domain/src/main/java/org/chorem/poll/domain/vote/Group.java (from rev 3173, branches/pollen-1.2.6-struts2/pollen-votecounting/src/main/java/org/chorem/pollen/votecounting/business/Group.java) =================================================================== --- branches/pollen-1.2.6-struts2/pollen-domain/src/main/java/org/chorem/poll/domain/vote/Group.java (rev 0) +++ branches/pollen-1.2.6-struts2/pollen-domain/src/main/java/org/chorem/poll/domain/vote/Group.java 2012-03-12 08:37:14 UTC (rev 3174) @@ -0,0 +1,122 @@ +/* + * #%L + * Pollen :: Vote Counting + * + * $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% + */ +package org.chorem.poll.domain.vote; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +/** + * Cette classe décrit un groupe du sondage. Sa valeur dépend du choix et sera + * calculée au moment du dépouillement. + * + * @author fdesbois + * @version $Id$ + */ +public class Group implements Iterable<Vote> { + /** Identifiant du groupe */ + private String idGroup; + + /** Poids du groupe */ + private double weight; + + /** Valeur du groupe pour le choix dans lequel il appartient */ + private double value; + + /** Liste des votes liés au groupe */ + private List<Vote> votes; + + /** + * Constructeur + * + * @param id : identifiant du groupe + * @param weight : poids du groupe + */ + public Group(String id, double weight) { + this.idGroup = id; + this.weight = weight; + this.value = 0.; + this.votes = new ArrayList<Vote>(); + } + + /** + * getIdGroup : Identifiant du groupe + * + * @return identifiant du groupe + */ + public String getIdGroup() { + return this.idGroup; + } + + /** + * getWeight : Renvoie le poids du groupe + * + * @return poids du groupe + */ + public double getWeight() { + return this.weight; + } + + /** + * setValue : Mise à jour de la valeur du groupe + * + * @param value : nouvelle valeur du groupe + */ + public void setValue(double value) { + this.value = value; + } + + /** + * getValue : Renvoie la valeur du groupe pour le choix auquel il appartient + * + * @return la valeur du groupe + */ + public double getValue() { + return this.value; + } + + /** + * getVotes : Renvoie la liste des votes liés au groupe + * + * @return liste des votes + */ + public List<Vote> getVotes() { + return this.votes; + } + + /** + * addVote : Ajout d'un nouveau vote au groupe + * + * @param value : valeur du vote + * @param weight : poids du vote + */ + public void addVote(double value, double weight, String votingID) { + this.votes.add(new Vote(value, weight, votingID)); + } + + @Override + public Iterator<Vote> iterator() { + return votes.iterator(); + } +} \ No newline at end of file Copied: branches/pollen-1.2.6-struts2/pollen-domain/src/main/java/org/chorem/poll/domain/vote/PercentageBehavior.java (from rev 3173, branches/pollen-1.2.6-struts2/pollen-votecounting/src/main/java/org/chorem/pollen/votecounting/utils/PercentageBehavior.java) =================================================================== --- branches/pollen-1.2.6-struts2/pollen-domain/src/main/java/org/chorem/poll/domain/vote/PercentageBehavior.java (rev 0) +++ branches/pollen-1.2.6-struts2/pollen-domain/src/main/java/org/chorem/poll/domain/vote/PercentageBehavior.java 2012-03-12 08:37:14 UTC (rev 3174) @@ -0,0 +1,33 @@ +/* + * #%L + * Pollen :: Vote Counting + * + * $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% + */ +package org.chorem.poll.domain.vote; + +/** + * Interface pour le comportement pourcentage. + * + * @version $Id$ + */ +public interface PercentageBehavior { + public double value(); +} \ No newline at end of file Copied: branches/pollen-1.2.6-struts2/pollen-domain/src/main/java/org/chorem/poll/domain/vote/Vote.java (from rev 3173, branches/pollen-1.2.6-struts2/pollen-votecounting/src/main/java/org/chorem/pollen/votecounting/business/Vote.java) =================================================================== --- branches/pollen-1.2.6-struts2/pollen-domain/src/main/java/org/chorem/poll/domain/vote/Vote.java (rev 0) +++ branches/pollen-1.2.6-struts2/pollen-domain/src/main/java/org/chorem/poll/domain/vote/Vote.java 2012-03-12 08:37:14 UTC (rev 3174) @@ -0,0 +1,82 @@ +/* + * #%L + * Pollen :: Vote Counting + * + * $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% + */ +package org.chorem.poll.domain.vote; + +/** + * Cette classe décrit un vote du sondage. Le vote récupère l'id et le poids du + * votant. + * + * @author fdesbois + * @version $Id$ + */ +public class Vote { + /** Valeur du vote (ex : 1, 2, 3 pour condorcet ou 15 pour pourcentage) */ + private double value; + + /** Poids du vote dans le sondage */ + private double weight; + + /** Identifiant du votant */ + private String votingID; + + /** + * Constructeur + * + * @param value : valeur du vote connue pour le choix + * @param weight : poids du vote + * @param votingID : identifiant du votant + */ + public Vote(double value, double weight, String votingID) { + this.value = value; + this.weight = weight; + this.votingID = votingID; + } + + /** + * getValue : Renvoie la valeur du vote + * + * @return valeur du vote + */ + public double getValue() { + return this.value; + } + + /** + * getWeight : Renvoie le poids du vote + * + * @return poids du vote + */ + public double getWeight() { + return this.weight; + } + + /** + * getVotingID : Renvoie l'identifiant du votant + * + * @return identifiant du votant + */ + public String getVotingID() { + return this.votingID; + } +} \ No newline at end of file Added: branches/pollen-1.2.6-struts2/pollen-domain/src/main/resources/i18n/pollen-domain_en_GB.properties =================================================================== --- branches/pollen-1.2.6-struts2/pollen-domain/src/main/resources/i18n/pollen-domain_en_GB.properties (rev 0) +++ branches/pollen-1.2.6-struts2/pollen-domain/src/main/resources/i18n/pollen-domain_en_GB.properties 2012-03-12 08:37:14 UTC (rev 3174) @@ -0,0 +1,10 @@ +pollen.choiceType.date=Date +pollen.choiceType.image=Image +pollen.choiceType.text=Text +pollen.pollType.free=Free +pollen.pollType.group=Group +pollen.pollType.restricted=Restricted +pollen.voteCountingType.condorcet=Condorcet +pollen.voteCountingType.normal=Normal +pollen.voteCountingType.number=Number +pollen.voteCountingType.percentage=Percentage Added: branches/pollen-1.2.6-struts2/pollen-domain/src/main/resources/i18n/pollen-domain_fr_FR.properties =================================================================== --- branches/pollen-1.2.6-struts2/pollen-domain/src/main/resources/i18n/pollen-domain_fr_FR.properties (rev 0) +++ branches/pollen-1.2.6-struts2/pollen-domain/src/main/resources/i18n/pollen-domain_fr_FR.properties 2012-03-12 08:37:14 UTC (rev 3174) @@ -0,0 +1,10 @@ +pollen.choiceType.date=Date +pollen.choiceType.image=Image +pollen.choiceType.text=Texte +pollen.pollType.free=Libre +pollen.pollType.group=Groupe +pollen.pollType.restricted=Restreint +pollen.voteCountingType.condorcet=Condorcet +pollen.voteCountingType.normal=Normal +pollen.voteCountingType.number=Nombre +pollen.voteCountingType.percentage=Pourcentage Added: branches/pollen-1.2.6-struts2/pollen-domain/src/test/java/org/chorem/poll/domain/ImportExportServiceTest.java =================================================================== --- branches/pollen-1.2.6-struts2/pollen-domain/src/test/java/org/chorem/poll/domain/ImportExportServiceTest.java (rev 0) +++ branches/pollen-1.2.6-struts2/pollen-domain/src/test/java/org/chorem/poll/domain/ImportExportServiceTest.java 2012-03-12 08:37:14 UTC (rev 3174) @@ -0,0 +1,22 @@ +package org.chorem.poll.domain; + +import org.junit.Test; + +/** + * TODO + * + * @author tchemit <chemit@codelutin.com> + * @since 1.2.6 + */ +public class ImportExportServiceTest { + + @Test + public void exportToXml() throws Exception { + + } + + @Test + public void importFromXml() throws Exception { + + } +} Added: branches/pollen-1.2.6-struts2/pollen-domain/src/test/java/org/chorem/poll/domain/VoteCountingServiceTest.java =================================================================== --- branches/pollen-1.2.6-struts2/pollen-domain/src/test/java/org/chorem/poll/domain/VoteCountingServiceTest.java (rev 0) +++ branches/pollen-1.2.6-struts2/pollen-domain/src/test/java/org/chorem/poll/domain/VoteCountingServiceTest.java 2012-03-12 08:37:14 UTC (rev 3174) @@ -0,0 +1,21 @@ +package org.chorem.poll.domain; + +import org.junit.Test; + +/** + * TODO + * + * @author tchemit <chemit@codelutin.com> + * @since 1.2.6 + */ +public class VoteCountingServiceTest { + @Test + public void executeVoteCounting() throws Exception { + + } + + @Test + public void executeGroupCounting() throws Exception { + + } +} Added: branches/pollen-1.2.6-struts2/pollen-domain/src/test/java/org/chorem/poll/domain/strategy/CondorcetVoteCountingStrategyTest.java =================================================================== --- branches/pollen-1.2.6-struts2/pollen-domain/src/test/java/org/chorem/poll/domain/strategy/CondorcetVoteCountingStrategyTest.java (rev 0) +++ branches/pollen-1.2.6-struts2/pollen-domain/src/test/java/org/chorem/poll/domain/strategy/CondorcetVoteCountingStrategyTest.java 2012-03-12 08:37:14 UTC (rev 3174) @@ -0,0 +1,22 @@ +package org.chorem.poll.domain.strategy; + +import org.junit.Test; + +/** + * TODO + * + * @author tchemit <chemit@codelutin.com> + * @since 1.2.6 + */ +public class CondorcetVoteCountingStrategyTest { + + @Test + public void executeCounting() throws Exception { + + } + + @Test + public void executeStats() throws Exception { + + } +} Added: branches/pollen-1.2.6-struts2/pollen-domain/src/test/java/org/chorem/poll/domain/strategy/NumberVoteCountingStrategyTest.java =================================================================== --- branches/pollen-1.2.6-struts2/pollen-domain/src/test/java/org/chorem/poll/domain/strategy/NumberVoteCountingStrategyTest.java (rev 0) +++ branches/pollen-1.2.6-struts2/pollen-domain/src/test/java/org/chorem/poll/domain/strategy/NumberVoteCountingStrategyTest.java 2012-03-12 08:37:14 UTC (rev 3174) @@ -0,0 +1,22 @@ +package org.chorem.poll.domain.strategy; + +import org.junit.Test; + +/** + * TODO + * + * @author tchemit <chemit@codelutin.com> + * @since 1.2.6 + */ +public class NumberVoteCountingStrategyTest { + + @Test + public void executeCounting() throws Exception { + + } + + @Test + public void executeStats() throws Exception { + + } +} Added: branches/pollen-1.2.6-struts2/pollen-domain/src/test/java/org/chorem/poll/domain/strategy/PercentageVoteCountingStrategyTest.java =================================================================== --- branches/pollen-1.2.6-struts2/pollen-domain/src/test/java/org/chorem/poll/domain/strategy/PercentageVoteCountingStrategyTest.java (rev 0) +++ branches/pollen-1.2.6-struts2/pollen-domain/src/test/java/org/chorem/poll/domain/strategy/PercentageVoteCountingStrategyTest.java 2012-03-12 08:37:14 UTC (rev 3174) @@ -0,0 +1,22 @@ +package org.chorem.poll.domain.strategy; + +import org.junit.Test; + +/** + * TODO + * + * @author tchemit <chemit@codelutin.com> + * @since 1.2.6 + */ +public class PercentageVoteCountingStrategyTest { + + @Test + public void executeCounting() throws Exception { + + } + + @Test + public void executeStats() throws Exception { + + } +} Added: branches/pollen-1.2.6-struts2/pollen-domain/src/test/java/org/chorem/poll/domain/strategy/StandardVoteCountingStrategyTest.java =================================================================== --- branches/pollen-1.2.6-struts2/pollen-domain/src/test/java/org/chorem/poll/domain/strategy/StandardVoteCountingStrategyTest.java (rev 0) +++ branches/pollen-1.2.6-struts2/pollen-domain/src/test/java/org/chorem/poll/domain/strategy/StandardVoteCountingStrategyTest.java 2012-03-12 08:37:14 UTC (rev 3174) @@ -0,0 +1,22 @@ +package org.chorem.poll.domain.strategy; + +import org.junit.Test; + +/** + * TODO + * + * @author tchemit <chemit@codelutin.com> + * @since 1.2.6 + */ +public class StandardVoteCountingStrategyTest { + + @Test + public void executeCounting() throws Exception { + + } + + @Test + public void executeStats() throws Exception { + + } +} Copied: branches/pollen-1.2.6-struts2/pollen-domain/src/test/resources/log4j.properties (from rev 3173, branches/pollen-1.2.6-struts2/pollen-votecounting/src/main/resources/log4j.properties) =================================================================== --- branches/pollen-1.2.6-struts2/pollen-domain/src/test/resources/log4j.properties (rev 0) +++ branches/pollen-1.2.6-struts2/pollen-domain/src/test/resources/log4j.properties 2012-03-12 08:37:14 UTC (rev 3174) @@ -0,0 +1,8 @@ +# Global logging configuration +log4j.rootLogger=WARN, stdout +# Console output... +log4j.appender.stdout=org.apache.log4j.ConsoleAppender +log4j.appender.stdout.layout=org.apache.log4j.PatternLayout +log4j.appender.stdout.layout.ConversionPattern=%d %5p [%t] (%F:%L) %M - %m%n +# package level +log4j.logger.org.chorem.pollen=INFO
participants (1)
-
tchemit@users.chorem.org