Author: tchemit Date: 2012-03-08 01:20:00 +0100 (Thu, 08 Mar 2012) New Revision: 3171 Url: http://chorem.org/repositories/revision/pollen/3171 Log: add some usefull function for binding + poll firs method Added: branches/pollen-1.2.6-struts2/pollen-persistence/src/main/java/org/chorem/pollen/business/persistence/PollImpl.java Modified: branches/pollen-1.2.6-struts2/pollen-persistence/src/main/java/org/chorem/pollen/PollenFunctions.java branches/pollen-1.2.6-struts2/pollen-persistence/src/main/xmi/pollen.zargo Modified: branches/pollen-1.2.6-struts2/pollen-persistence/src/main/java/org/chorem/pollen/PollenFunctions.java =================================================================== --- branches/pollen-1.2.6-struts2/pollen-persistence/src/main/java/org/chorem/pollen/PollenFunctions.java 2012-03-07 23:02:51 UTC (rev 3170) +++ branches/pollen-1.2.6-struts2/pollen-persistence/src/main/java/org/chorem/pollen/PollenFunctions.java 2012-03-08 00:20:00 UTC (rev 3171) @@ -24,9 +24,26 @@ package org.chorem.pollen; import com.google.common.base.Function; +import com.google.common.collect.Lists; +import org.chorem.pollen.business.persistence.Choice; +import org.chorem.pollen.business.persistence.Comment; +import org.chorem.pollen.business.persistence.PersonToList; +import org.chorem.pollen.business.persistence.Poll; import org.chorem.pollen.business.persistence.UserAccount; +import org.chorem.pollen.business.persistence.Vote; +import org.chorem.pollen.business.persistence.VoteToChoice; +import org.chorem.pollen.business.persistence.VotingList; +import org.chorem.pollen.votecounting.business.NumberMethod; +import org.chorem.pollen.votecounting.dto.CommentDTO; +import org.chorem.pollen.votecounting.dto.PollChoiceDTO; +import org.chorem.pollen.votecounting.dto.PollDTO; +import org.chorem.pollen.votecounting.dto.VoteToChoiceDTO; +import org.chorem.pollen.votecounting.dto.VotingGroupDTO; +import org.chorem.pollen.votecounting.dto.VotingPersonDTO; import org.nuiton.topia.persistence.TopiaEntity; +import java.util.List; + /** * Usefull pollen functions. * @@ -49,4 +66,121 @@ } }; + public static final Function<Poll, PollDTO> POLL_TO_BEAN = new Function<Poll, PollDTO>() { + @Override + public PollDTO apply(Poll input) { + PollDTO result = new PollDTO(input.getPollId()); + result.setTitle(input.getTitle()); + result.setDescription(input.getDescription()); + result.setBeginChoiceDate(input.getBeginChoiceDate()); + result.setBeginDate(input.getBeginDate()); + result.setEndDate(input.getEndDate()); + result.setAnonymous(input.isAnonymous()); + result.setAnonymousVoteAllowed(input.isAnonymousVoteAllowed()); + result.setPublicResults(input.isPublicResults()); + result.setContinuousResults(input.isContinuousResults()); + result.setChoiceAddAllowed(input.isChoiceAddAllowed()); + result.setClosed(input.isClosed()); + result.setCreatorId(input.getCreator().getVotingId()); + result.setCreatorEmail(input.getCreator().getEmail()); + result.setMaxChoiceNb(input.getMaxChoiceNb()); + result.setPollType(input.getPollType()); + result.setChoiceType(input.getChoiceType()); + result.setVoteCounting(input.getVoteCountingType()); + + result.setComments(Lists.transform(input.getComment(), + COMMENT_TO_BEAN)); + + result.setChoices(Lists.transform(input.getChoice(), + CHOICE_TO_BEAN)); + + if (!input.isVotingListEmpty()) { + + result.setVotingGroups(Lists.transform(input.getVotingList(), + VOTING_LIST_TO_BEAN + )); + } else { + // un groupe par défaut si il y en a pas + VotingGroupDTO group = new VotingGroupDTO("unique", 1); + group.setName("unique"); + List<VotingPersonDTO> votes = Lists.transform(input.getVote(), + VOTE_TO_BEAN); + group.setVotingPersons(votes); + result.getVotingGroups().add(group); + } + return result; + } + }; + + public static final Function<Comment, CommentDTO> COMMENT_TO_BEAN = new Function<Comment, CommentDTO>() { + @Override + public CommentDTO apply(Comment input) { + CommentDTO result = new CommentDTO( + input.getPollAccount().getVotingId(), input.getText()); + return result; + } + }; + + public static final Function<Choice, PollChoiceDTO> CHOICE_TO_BEAN = new Function<Choice, PollChoiceDTO>() { + @Override + public PollChoiceDTO apply(Choice input) { + PollChoiceDTO result = new PollChoiceDTO(input.getTopiaId()); + String choiceName = input.getName(); + result.setName(choiceName); + result.setHidden(choiceName != null && + choiceName.startsWith(NumberMethod.HIDDEN_PREFIX)); + result.setDescription(input.getDescription()); + return result; + } + }; + + public static final Function<VotingList, VotingGroupDTO> VOTING_LIST_TO_BEAN = new Function<VotingList, VotingGroupDTO>() { + @Override + public VotingGroupDTO apply(VotingList input) { + VotingGroupDTO result = new VotingGroupDTO( + input.getTopiaId(), + input.getWeight()); + + result.setName(input.getName()); + + for (PersonToList pToList : input.getPollAccountPersonToList()) { + Vote vote = input.getPoll().getVoteByPollAccount(pToList.getPollAccount()); + if (vote != null) { + // Pas de vote pour cette personne : doit engendrer erreur ?!? + VotingPersonDTO bean = VOTE_TO_BEAN.apply(vote); + result.getVotingPersons().add(bean); + } + } + return result; + } + }; + + public static final Function<Vote, VotingPersonDTO> VOTE_TO_BEAN = new Function<Vote, VotingPersonDTO>() { + @Override + public VotingPersonDTO apply(Vote input) { + VotingPersonDTO result = + new VotingPersonDTO(input.getPollAccount().getVotingId(), input.getWeight()); + result.setEmail(input.getPollAccount().getEmail()); + + for (VoteToChoice vToChoice : input.getChoiceVoteToChoice()) { + if (vToChoice != null && vToChoice.getChoice() != null) { + VoteToChoiceDTO bean = VOTE_TO_CHOICE_TO_BEAN.apply(vToChoice); + result.getChoices().add(bean); + } + } + return result; + } + }; + + public static final Function<VoteToChoice, VoteToChoiceDTO> VOTE_TO_CHOICE_TO_BEAN = new Function<VoteToChoice, VoteToChoiceDTO>() { + @Override + public VoteToChoiceDTO apply(VoteToChoice input) { + VoteToChoiceDTO result = new VoteToChoiceDTO( + input.getChoice().getTopiaId(), + input.getVoteValue()); + + return result; + } + }; + } Added: branches/pollen-1.2.6-struts2/pollen-persistence/src/main/java/org/chorem/pollen/business/persistence/PollImpl.java =================================================================== --- branches/pollen-1.2.6-struts2/pollen-persistence/src/main/java/org/chorem/pollen/business/persistence/PollImpl.java (rev 0) +++ branches/pollen-1.2.6-struts2/pollen-persistence/src/main/java/org/chorem/pollen/business/persistence/PollImpl.java 2012-03-08 00:20:00 UTC (rev 3171) @@ -0,0 +1,22 @@ +package org.chorem.pollen.business.persistence; + +import com.google.common.base.Preconditions; + +public class PollImpl extends PollAbstract { + + private static final long serialVersionUID = 1L; + + public Vote getVoteByPollAccount(PollAccount account) { + Preconditions.checkNotNull(account); + Vote resultVote = null; + if (!isVoteEmpty()) { + for (Vote vote : this.getVote()) { + if (account.equals(vote.getPollAccount())) { + resultVote = vote; + break; + } + } + } + return resultVote; + } +} //PollImpl Modified: branches/pollen-1.2.6-struts2/pollen-persistence/src/main/xmi/pollen.zargo =================================================================== (Binary files differ)