Author: tchemit Date: 2012-02-26 18:19:55 +0100 (Sun, 26 Feb 2012) New Revision: 3147 Url: http://chorem.org/repositories/revision/pollen/3147 Log: add PollResultsService, improve servive tests skeleton continue stuff... Added: branches/pollen-1.2.6-struts2/pollen-services/src/main/java/org/chorem/pollen/bean/PollResultListBean.java branches/pollen-1.2.6-struts2/pollen-services/src/main/java/org/chorem/pollen/services/exceptions/VoteNotFoundException.java branches/pollen-1.2.6-struts2/pollen-services/src/main/java/org/chorem/pollen/services/impl/PollResultsService.java Modified: branches/pollen-1.2.6-struts2/pollen-services/src/main/java/org/chorem/pollen/services/PollenServiceSupport.java branches/pollen-1.2.6-struts2/pollen-services/src/main/java/org/chorem/pollen/services/impl/PollFeedService.java branches/pollen-1.2.6-struts2/pollen-services/src/main/java/org/chorem/pollen/services/impl/PollService.java branches/pollen-1.2.6-struts2/pollen-services/src/main/java/org/chorem/pollen/services/impl/VoteService.java branches/pollen-1.2.6-struts2/pollen-services/src/test/java/org/chorem/pollen/services/AbstractPollenServiceTest.java branches/pollen-1.2.6-struts2/pollen-services/src/test/java/org/chorem/pollen/services/FakeServiceContext.java branches/pollen-1.2.6-struts2/pollen-services/src/test/java/org/chorem/pollen/services/impl/EmailServiceTest.java branches/pollen-1.2.6-struts2/pollen-services/src/test/java/org/chorem/pollen/services/impl/FavoriteServiceTest.java branches/pollen-1.2.6-struts2/pollen-services/src/test/java/org/chorem/pollen/services/impl/PollServiceTest.java branches/pollen-1.2.6-struts2/pollen-services/src/test/java/org/chorem/pollen/services/impl/UserServiceTest.java branches/pollen-1.2.6-struts2/pollen-services/src/test/java/org/chorem/pollen/services/impl/VoteServiceTest.java Added: branches/pollen-1.2.6-struts2/pollen-services/src/main/java/org/chorem/pollen/bean/PollResultListBean.java =================================================================== --- branches/pollen-1.2.6-struts2/pollen-services/src/main/java/org/chorem/pollen/bean/PollResultListBean.java (rev 0) +++ branches/pollen-1.2.6-struts2/pollen-services/src/main/java/org/chorem/pollen/bean/PollResultListBean.java 2012-02-26 17:19:55 UTC (rev 3147) @@ -0,0 +1,38 @@ +package org.chorem.pollen.bean; + +import org.chorem.pollen.business.persistence.Result; +import org.chorem.pollen.votecounting.dto.VoteCountingResultDTO; + +import java.util.List; + +/** + * TODO + * + * @author tchemit <chemit@codelutin.com> + * @since TODO + */ +public class PollResultListBean { + + /** Liste des résultats */ + private List<Result> resultDTOs = null; + + /** Résultat issu du dépouillement du vote */ + private VoteCountingResultDTO voteCountingResultDTO = null; + + public List<Result> getResultDTOs() { + return resultDTOs; + } + + public void setResultDTOs(List<Result> resultDTOs) { + this.resultDTOs = resultDTOs; + } + + public VoteCountingResultDTO getVoteCountingResultDTO() { + return voteCountingResultDTO; + } + + public void setVoteCountingResultDTO(VoteCountingResultDTO voteCountingResultDTO) { + this.voteCountingResultDTO = voteCountingResultDTO; + } + +} Modified: branches/pollen-1.2.6-struts2/pollen-services/src/main/java/org/chorem/pollen/services/PollenServiceSupport.java =================================================================== --- branches/pollen-1.2.6-struts2/pollen-services/src/main/java/org/chorem/pollen/services/PollenServiceSupport.java 2012-02-26 17:19:03 UTC (rev 3146) +++ branches/pollen-1.2.6-struts2/pollen-services/src/main/java/org/chorem/pollen/services/PollenServiceSupport.java 2012-02-26 17:19:55 UTC (rev 3147) @@ -110,6 +110,14 @@ } } + protected <T extends TopiaEntity, D extends TopiaDAO<? super T>> T create(D dao) { + try { + return (T) dao.create(); + } catch (TopiaException e) { + throw new PollenTechnicalException("Could not create entity ", e); + } + } + protected TopiaContext getTransaction() { return serviceContext.getTransaction(); } Added: branches/pollen-1.2.6-struts2/pollen-services/src/main/java/org/chorem/pollen/services/exceptions/VoteNotFoundException.java =================================================================== --- branches/pollen-1.2.6-struts2/pollen-services/src/main/java/org/chorem/pollen/services/exceptions/VoteNotFoundException.java (rev 0) +++ branches/pollen-1.2.6-struts2/pollen-services/src/main/java/org/chorem/pollen/services/exceptions/VoteNotFoundException.java 2012-02-26 17:19:55 UTC (rev 3147) @@ -0,0 +1,34 @@ +/* + * #%L + * Pollen :: Services + * + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2009 - 2012 CodeLutin + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * #L% + */ +package org.chorem.pollen.services.exceptions; + +/** + * When a vote is not found. + * + * @author tchemit <chemit@codelutin.com> + * @since 1.2.6 + */ +public class VoteNotFoundException extends Exception { + private static final long serialVersionUID = 1L; +} Property changes on: branches/pollen-1.2.6-struts2/pollen-services/src/main/java/org/chorem/pollen/services/exceptions/VoteNotFoundException.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Modified: branches/pollen-1.2.6-struts2/pollen-services/src/main/java/org/chorem/pollen/services/impl/PollFeedService.java =================================================================== --- branches/pollen-1.2.6-struts2/pollen-services/src/main/java/org/chorem/pollen/services/impl/PollFeedService.java 2012-02-26 17:19:03 UTC (rev 3146) +++ branches/pollen-1.2.6-struts2/pollen-services/src/main/java/org/chorem/pollen/services/impl/PollFeedService.java 2012-02-26 17:19:55 UTC (rev 3147) @@ -193,7 +193,8 @@ String title = _("pollen.feed.createPollContent", poll.getCreator().getVotingId()); - String content = _("pollen.feed.createPollContent", poll.getDescription()); + String content = _("pollen.feed.createPollContent", + poll.getDescription()); feedFeed(poll, title, content); } @@ -211,14 +212,20 @@ feedFeed(comment.getPoll(), title, content); } - public void addFeedEntryWhenAddVote(Vote vote) { + public void addFeedEntryWhenAddVote(Vote vote, + boolean anonymousVote, + String pollResult) { + String userId; + if (anonymousVote) { + userId = _("pollen.common.anonymous"); + } else { + userId = vote.getPollAccount().getVotingId(); - String title = _("pollen.feed.addVoteTitle", - vote.getPollAccount().getVotingId()); + } + String title = _("pollen.feed.addVoteTitle", userId); - // TODO Add result of poll ? And if poll is not public ? - String content = _("pollen.feed.addVoteContent", "TODO"); + String content = _("pollen.feed.addVoteContent", pollResult); feedFeed(vote.getPoll(), title, content); } Added: branches/pollen-1.2.6-struts2/pollen-services/src/main/java/org/chorem/pollen/services/impl/PollResultsService.java =================================================================== --- branches/pollen-1.2.6-struts2/pollen-services/src/main/java/org/chorem/pollen/services/impl/PollResultsService.java (rev 0) +++ branches/pollen-1.2.6-struts2/pollen-services/src/main/java/org/chorem/pollen/services/impl/PollResultsService.java 2012-02-26 17:19:55 UTC (rev 3147) @@ -0,0 +1,344 @@ +package org.chorem.pollen.services.impl; + +import com.google.common.collect.Lists; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.chorem.pollen.bean.PollResultListBean; +import org.chorem.pollen.business.persistence.Choice; +import org.chorem.pollen.business.persistence.Poll; +import org.chorem.pollen.business.persistence.Result; +import org.chorem.pollen.business.persistence.ResultDAO; +import org.chorem.pollen.common.ChoiceType; +import org.chorem.pollen.common.PollType; +import org.chorem.pollen.common.VoteCountingType; +import org.chorem.pollen.services.PollenServiceSupport; +import org.chorem.pollen.votecounting.dto.ChoiceDTO; +import org.chorem.pollen.votecounting.dto.PollDTO; +import org.chorem.pollen.votecounting.dto.VoteCountingResultDTO; +import org.chorem.pollen.votecounting.services.ServiceVoteCounting; +import org.chorem.pollen.votecounting.services.ServiceVoteCountingImpl; + +import java.text.DateFormat; +import java.text.SimpleDateFormat; +import java.util.Collection; +import java.util.Date; +import java.util.Iterator; +import java.util.List; + +/** + * Deals with poll results. + * + * @author tchemit <chemit@codelutin.com> + * @since 1.2.6 + */ +public class PollResultsService extends PollenServiceSupport { + + /** log. */ + private static final Log log = LogFactory.getLog(PollResultsService.class); + + public PollResultListBean getAllResults(String pollId) { + return getResults(pollId, null, false, false); + } + + public PollResultListBean getResultsByVoteCounting(String pollId, + VoteCountingType voteCounting) { + return getResults(pollId, voteCounting, false, false); + } + + public PollResultListBean getGroupResults(String pollId) { + return getResults(pollId, null, true, true); + } + + public PollResultListBean getNormalResults(String pollId) { + return getResults(pollId, null, true, false); + } + + /** + * Récupère les résultats d'un sondage. + * + * @param pollId le sondage + * @param voteCounting type de dépouillement + * @param byGroup résultats par groupe + * @param groupOnly résultats uniquement par groupe + * @return les résultats du sondage + */ + private PollResultListBean getResults(String pollId, + VoteCountingType voteCounting, + boolean byGroup, + boolean groupOnly) { + + PollService pollService = newService(PollService.class); + Poll poll = pollService.getPollByPollId(pollId); + +// DataResultConverter converter = new DataResultConverter(transaction); + if (log.isDebugEnabled()) { + log.debug(poll.getPollId() + " (" + voteCounting + + ") has results: " + hasResults(poll, voteCounting)); + } + + // Dépouillement du sondage. + // S'il n'existe pas de résultats ou si le sondage est encore ouvert. + //if (!hasResults(ePoll, voteCounting) || !ePoll.getIsClosed()) { + PollDTO dto = pollService.createPollDTOForVoteCounting(poll); + + if (voteCounting != null) { + dto.setVoteCounting(voteCounting); + } + + // clear result (they will be regenerated ! (a big shame)) + poll.clearResult(); + + ServiceVoteCounting service = new ServiceVoteCountingImpl(); + + VoteCountingResultDTO result; + + if (PollType.GROUP == poll.getPollType()) { + result = service.executeGroupCounting(dto); + } else { + result = service.executeVoteCounting(dto); + } + for (ChoiceDTO choice : result.getChoices()) { + + ResultDAO daoResult = getDAO(Result.class); + Result eResult = create(daoResult); + + Choice eChoice = poll.getChoiceByTopiaId(choice.getIdChoice()); + + poll.addResult(eResult); + + eResult.setName(eChoice.getName()); + eResult.setByGroup(result.isByGroup()); + eResult.setPoll(poll); + eResult.setResultValue(String.valueOf(choice.getValue())); + eResult.setVoteCountingType(result.getTypeVoteCounting()); + } + + // Conversion et trie des résultats + List<Result> list = Lists.newArrayList(poll.getResult()); + + VoteCountingType voteCountingType = poll.getVoteCountingType(); + + Iterator<Result> it = list.iterator(); + while (it.hasNext()) { + Result currentResult = it.next(); + + // Cas d'un dépouillement particulier + // Suppression des resultats qui ne sont pas de se dépouillement + if (voteCounting != null + && currentResult.getVoteCountingType() != voteCounting) { + it.remove(); + } + + // Cas d'un sondage de type GROUP avec filtre + if (byGroup) { + + // Filtre group : Suppression resultats non group + if (groupOnly && !currentResult.getByGroup()) { + it.remove(); + } + + // Filtre non group : Suppression resultats group + else if (!groupOnly && currentResult.getByGroup()) { + it.remove(); + } + + // Suppression des autres resultats de depouillements differents + if (currentResult.getVoteCountingType() != voteCountingType) { + it.remove(); + } + } + } + + PollResultListBean resultListDTO = new PollResultListBean(); + resultListDTO.setResultDTOs(list); + resultListDTO.setVoteCountingResultDTO(result); + + return resultListDTO; + } + + /** + * Retourne une chaîne contenant les résultats du sondage. + * + * @param poll le sondage + * @param results les résultats du sondage + * @return les résultats sous forme de chaine de caractères + */ + public String getResultsAsString(Poll poll, Collection<Result> results) { + + DateFormat dateFormat = new SimpleDateFormat(); + StringBuilder res = new StringBuilder(""); + Iterator<Result> it = results.iterator(); + while (it.hasNext()) { + Result result = it.next(); + if (poll.getChoiceType() == ChoiceType.DATE) { + Date date = new Date(Long.parseLong(result.getName())); + res.append(dateFormat.format(date)); + } else { + res.append(result.getName()); + } + res.append("=").append(removeTrailing0(result.getResultValue())); + if (it.hasNext()) { + res.append(", "); + } + } + return res.toString(); + } + + public String getResultValue(Choice choice, Collection<Result> results) { + + String val = ""; + for (Result result : results) { + if (result.getName().equals(choice.getName())) { + val = removeTrailing0(result.getResultValue()); + break; + } + } + return val; + } + + + /** + * Supprime le 0 final d'un nombre à virgule. Le résultat peut-être un + * double : 1,0 -> 1 et 1,2 -> 1,2. + * + * @param val le nombre + * @return le nombre sans 0 final + */ + private String removeTrailing0(String val) { + if (val.endsWith(".0")) { + val = val.substring(0, val.indexOf('.')); + } + return val; + } + + + /** + * Création des résultats de sondage à partir d'un dto de résultats de + * sondage. + * + * @param ePoll poll à remplir + * @param dto le dto des résultats de sondage. + */ + protected void populateResultEntities(Poll ePoll, VoteCountingResultDTO dto) { + for (ChoiceDTO choice : dto.getChoices()) { + + ResultDAO daoResult = getDAO(Result.class); + Result eResult = create(daoResult); + + Choice eChoice = ePoll.getChoiceByTopiaId(choice.getIdChoice()); + + ePoll.addResult(eResult); + + eResult.setName(eChoice.getName()); + eResult.setByGroup(dto.isByGroup()); + eResult.setPoll(ePoll); + eResult.setResultValue(String.valueOf(choice.getValue())); + eResult.setVoteCountingType(dto.getTypeVoteCounting()); + } + } + + /** + * Retourne vrai si le sondage a des résultats. + * + * @param poll le sondage + * @param type type de dépouillement + * @return vrai si le sondage a des résultats + */ + private boolean hasResults(Poll poll, VoteCountingType type) { + boolean hasresult = false; + + if (!poll.isResultEmpty()) { + + if (type == null) { + hasresult = true; + } else { + for (Result result : poll.getResult()) { + if (type.equals(result.getVoteCountingType())) { + hasresult = true; + break; + } + } + } + } + + return hasresult; + } + +// public String exportPoll(String pollId) { +// TopiaContext transaction = getTransaction(); +// try { +// +// // Recherche du sondage +// PollDAO daoPoll = PollenDAOHelper.getPollDAO(transaction); +// Poll ePoll = daoPoll.findByPollId(pollId); +// +// // Dépouillement du sondage. +// DataResultConverter converter = new DataResultConverter(transaction); +// PollDTO dto = DataVoteCountingConverter +// .createPollDTOForVoteCounting(ePoll); +// +// ePoll.clearResult(); +// +// ServiceVoteCounting service = new ServiceVoteCountingImpl(); +// List<VoteCountingResultDTO> results = new ArrayList<VoteCountingResultDTO>(); +// VoteCountingResultDTO result = service.executeVoteCounting(dto); +// converter.populateResultEntities(result); +// results.add(result); +// +// if (EnumController.isGroupType(ePoll)) { +// VoteCountingResultDTO groupResult = service +// .executeGroupCounting(dto); +// converter.populateResultEntities(groupResult); +// results.add(groupResult); +// } +// +// // Transformation du sondage +// PollExportDTO pollExport = new PollExportDTO(); +// pollExport.setPollId(pollId); +// pollExport.setPoll(dto); +// pollExport.setVoteCountingResults(results); +// +// // Export du sondage +// ServiceExport serviceExport = new ServiceExportImpl(); +// String filename = serviceExport.executeExport(pollExport); +// +// if (log.isInfoEnabled()) { +// log.info("Poll exported: " + pollId); +// } +// +// return filename; +// } catch (Exception e) { +// throw new PollenTechnicalException("Could not export results", e); +// } +// } + +// public String importPoll(String filePath) { +// return importPoll(filePath, null); +// } +// +// public String importPoll(String filePath, UserAccount user) { +// String topiaId = ""; +// +// // Import du sondage +// ServiceExport serviceExport = new ServiceExportImpl(); +// PollExportDTO pollExportDTO = serviceExport.executeImport(filePath); +// +// // Transformation du sondage +// // FIXME do not call a Service from an other one +// PollService servicePoll = newService(PollService.class); +// org.chorem.pollen.business.dto.PollDTO poll = DataVoteCountingConverter +// .createPollDTO(pollExportDTO.getPoll()); +// +// // Enregistrement du sondage +// if (user != null) { +// poll.setUserId(user.getId()); +// } +// topiaId = servicePoll.createPoll(poll); +// +// if (log.isInfoEnabled()) { +// log.info("Poll imported: " + topiaId); +// } +// +// return topiaId; +// } +} Modified: branches/pollen-1.2.6-struts2/pollen-services/src/main/java/org/chorem/pollen/services/impl/PollService.java =================================================================== --- branches/pollen-1.2.6-struts2/pollen-services/src/main/java/org/chorem/pollen/services/impl/PollService.java 2012-02-26 17:19:03 UTC (rev 3146) +++ branches/pollen-1.2.6-struts2/pollen-services/src/main/java/org/chorem/pollen/services/impl/PollService.java 2012-02-26 17:19:55 UTC (rev 3147) @@ -31,6 +31,7 @@ import org.chorem.pollen.PollenTechnicalException; import org.chorem.pollen.business.persistence.Choice; import org.chorem.pollen.business.persistence.ChoiceDAO; +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.PollAccount; @@ -39,11 +40,19 @@ import org.chorem.pollen.business.persistence.UserAccount; import org.chorem.pollen.business.persistence.Vote; import org.chorem.pollen.business.persistence.VoteDAO; +import org.chorem.pollen.business.persistence.VoteToChoice; import org.chorem.pollen.business.persistence.VotingList; import org.chorem.pollen.services.PollenServiceSupport; import org.chorem.pollen.services.exceptions.PollChoiceNotFoundException; import org.chorem.pollen.services.exceptions.PollNotFoundException; import org.chorem.pollen.services.exceptions.PollVoteNotFoundException; +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.TopiaException; import org.nuiton.topia.framework.TopiaQuery; import org.nuiton.web.struts2.FilterPagerUtil; @@ -982,4 +991,134 @@ ChoiceDAO dao = getDAO(Choice.class); delete(dao, choice); } + + public PollAccount getRestrictedAccount(String pollId, + String accountId) throws PollNotFoundException { + Poll poll = getPollByPollId(pollId); + + if (poll == null) { + throw new PollNotFoundException(); + } + + if (log.isInfoEnabled()) { + log.info("getRestrictedAccount : accountId = " + accountId + + " _ pollId = " + poll.getPollId()); + } + + // Use PersonToList association entity to find coherence between + // the poll and votingId + PollAccountDAO dao = getDAO(PollAccount.class); + + try { + PollAccount result = dao.getRestrictedPollAccount(pollId, accountId); + return result; + } catch (TopiaException e) { + throw new PollenTechnicalException( + "Could not obtain restricted pollAccount", e); + } + } + + public PollDTO createPollDTOForVoteCounting(Poll ePoll) { + PollDTO pollDTO = new PollDTO(ePoll.getPollId()); + pollDTO.setTitle(ePoll.getTitle()); + pollDTO.setDescription(ePoll.getDescription()); + pollDTO.setBeginChoiceDate(ePoll.getBeginChoiceDate()); + pollDTO.setBeginDate(ePoll.getBeginDate()); + pollDTO.setEndDate(ePoll.getEndDate()); + pollDTO.setAnonymous(ePoll.getAnonymous()); + pollDTO.setAnonymousVoteAllowed(ePoll.getAnonymousVoteAllowed()); + pollDTO.setPublicResults(ePoll.getPublicResults()); + pollDTO.setContinuousResults(ePoll.getContinuousResults()); + pollDTO.setChoiceAddAllowed(ePoll.getChoiceAddAllowed()); + pollDTO.setClosed(ePoll.getClosed()); + pollDTO.setCreatorId(ePoll.getCreator().getVotingId()); + pollDTO.setCreatorEmail(ePoll.getCreator().getEmail()); + pollDTO.setMaxChoiceNb(ePoll.getMaxChoiceNb()); + pollDTO.setPollType(ePoll.getPollType()); + pollDTO.setChoiceType(ePoll.getChoiceType()); + pollDTO.setVoteCounting(ePoll.getVoteCountingType()); + + for (Comment comment : ePoll.getComment()) { + pollDTO.getComments().add(createPollCommentDTO(comment)); + } + for (Choice choice : ePoll.getChoice()) { + pollDTO.getChoices().add(createPollChoiceDTO(choice)); + } + if (ePoll.getVotingList() != null && !ePoll.getVotingList().isEmpty()) { + for (VotingList list : ePoll.getVotingList()) { + pollDTO.getVotingGroups().add(createVotingGroupDTO(list)); + } + } else { + // un groupe par défaut si il y en a pas + VotingGroupDTO group = new VotingGroupDTO("unique", 1); + group.setName("unique"); + for (Vote vote : ePoll.getVote()) { + group.getVotingPersons().add(createVotingPersonDTO(vote)); + } + pollDTO.getVotingGroups().add(group); + } + return pollDTO; + } + + private CommentDTO createPollCommentDTO(Comment comment) { + CommentDTO commentDTO = new CommentDTO(comment.getPollAccount() + .getVotingId(), comment.getText()); + return commentDTO; + } + + private PollChoiceDTO createPollChoiceDTO(Choice choice) { + PollChoiceDTO choiceDTO = new PollChoiceDTO(choice.getTopiaId()); + String choiceName = choice.getName(); + choiceDTO.setName(choiceName); + choiceDTO.setHidden(choiceName != null && + choiceName.startsWith(NumberMethod.HIDDEN_PREFIX)); + choiceDTO.setDescription(choice.getDescription()); + return choiceDTO; + } + + private VotingGroupDTO createVotingGroupDTO(VotingList list) { + VotingGroupDTO group = new VotingGroupDTO(list.getTopiaId(), list + .getWeight()); + group.setName(list.getName()); + + for (PersonToList pToList : list.getPollAccountPersonToList()) { + Vote vote = getPollVoteByPollAccount(list.getPoll(), pToList + .getPollAccount()); + if (vote != null) { // Pas de vote pour cette personne : doit engendrer erreur ?!? + group.getVotingPersons().add(createVotingPersonDTO(vote)); + } + } + return group; + } + + private VotingPersonDTO createVotingPersonDTO(Vote vote) { + VotingPersonDTO votingPerson = new VotingPersonDTO(vote + .getPollAccount().getVotingId(), vote.getWeight()); + votingPerson.setEmail(vote.getPollAccount().getEmail()); + + for (VoteToChoice vToChoice : vote.getChoiceVoteToChoice()) { + if (vToChoice != null && vToChoice.getChoice() != null) { + votingPerson.getChoices().add(createVoteToChoiceDTO(vToChoice)); + } + } + return votingPerson; + } + + private VoteToChoiceDTO createVoteToChoiceDTO(VoteToChoice vToChoice) { + String topiaId = vToChoice.getChoice().getTopiaId(); + VoteToChoiceDTO vote = new VoteToChoiceDTO(topiaId, vToChoice + .getVoteValue()); + + return vote; + } + + /* A Deplacer au bon endroit (PollImpl) si c'est possible */ + private static Vote getPollVoteByPollAccount(Poll poll, PollAccount account) { + for (Vote vote : poll.getVote()) { + if (account.getTopiaId().equals(vote.getPollAccount().getTopiaId())) { + return vote; + } + } + return null; + } } Modified: branches/pollen-1.2.6-struts2/pollen-services/src/main/java/org/chorem/pollen/services/impl/VoteService.java =================================================================== --- branches/pollen-1.2.6-struts2/pollen-services/src/main/java/org/chorem/pollen/services/impl/VoteService.java 2012-02-26 17:19:03 UTC (rev 3146) +++ branches/pollen-1.2.6-struts2/pollen-services/src/main/java/org/chorem/pollen/services/impl/VoteService.java 2012-02-26 17:19:55 UTC (rev 3147) @@ -23,411 +23,237 @@ */ package org.chorem.pollen.services.impl; +import com.google.common.collect.Lists; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.chorem.pollen.PollenTechnicalException; +import org.chorem.pollen.business.persistence.Poll; +import org.chorem.pollen.business.persistence.PollAccount; +import org.chorem.pollen.business.persistence.PollAccountDAO; +import org.chorem.pollen.business.persistence.Vote; +import org.chorem.pollen.business.persistence.VoteDAO; +import org.chorem.pollen.entities.PollenDAOHelper; import org.chorem.pollen.services.PollenServiceSupport; +import org.chorem.pollen.services.exceptions.VoteNotFoundException; +import org.nuiton.topia.TopiaContext; +import org.nuiton.topia.TopiaException; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + public class VoteService extends PollenServiceSupport { /** Logger. */ private static final Log log = LogFactory.getLog(VoteService.class); - /* - private void addFeedEntry(PollAction pollAction, String titleStr, - String contentStr) { - String voteURL = siteURL + "poll/VoteFor/" + poll.getPollId(); - File feedFile = getFeedContext().getFile(poll.getPollId()); - String title = null; - String content = null; + public String createVote(Vote vote, PollAccount pollAccount) { + TopiaContext transaction = getTransaction(); - switch (pollAction) { - case ADDVOTE: - if (isVoteAnonymous()) { - titleStr = messages.get("anonymous"); + String topiaId; + try { + + // création du compte associé au vote s'il n'existe pas + // sinon mise à jour du compte + //ServicePollAccount spa = new ServicePollAccountImpl(); + if (pollAccount == null) { + vote.setPollAccount(null); + } else { +// pollAccount.setHasVoted(true); + PollAccountDAO pollAccountDAO = + PollenDAOHelper.getPollAccountDAO(transaction); + + PollAccount pollAccountEntity = + pollAccountDAO.findByTopiaId(pollAccount.getTopiaId()); + +// PollAccountDTO account = spa.findPollAccountById(pollAccountDTO +// .getId()); + if (pollAccountEntity == null) { + log.debug("Nouveau compte associé au vote"); + pollAccountEntity = pollAccountDAO.create(); +// voteDTO.setPollAccountId(spa +// .createPollAccount(pollAccountDTO)); + } else { + log.debug("Compte associé au vote : " + pollAccountEntity.getTopiaId()); + //spa.updatePollAccount(pollAccountDTO); + //voteDTO.setPollAccountId(account.getId()); + + } + // Fill pollAccountEntity from DTO in argument +// DataPollAccountConverter pollAccountConverter = new DataPollAccountConverter(); +// pollAccountConverter.setTransaction(transaction); +// pollAccountConverter.populatePollAccountEntity(pollAccountDTO, +// pollAccountEntity); + // Update pollAccountEntity + pollAccountDAO.update(pollAccountEntity); + + vote.setPollAccount(pollAccountEntity); +// vote.setVotingListId(pollAccount.getVotingListId()); +// vote.setWeight(pollAccount.getWeight()); + log.debug("Poids du vote : " + vote.getWeight()); } - title = messages.format("pollFeed_voteTitle", titleStr); - content = messages.format("pollFeed_voteContent", contentStr); - break; - case ADDCHOICE: - title = messages.format("pollFeed_choiceTitle", titleStr); - content = messages.format("pollFeed_choiceContent", contentStr); - break; - case ADDCOMMENT: - title = messages.format("pollFeed_commentTitle", titleStr); - content = messages.format("pollFeed_commentContent", contentStr); - break; + + VoteDAO voteDAO = PollenDAOHelper.getVoteDAO(transaction); + + Vote voteEntity = voteDAO.create(); +// converter.setTransaction(transaction); +// converter.populateVoteEntity(vote, voteEntity); + +// converter.populateChoiceVote(vote, voteEntity); + + topiaId = voteEntity.getTopiaId(); + + if (log.isDebugEnabled()) { + log.debug("Entity created: " + topiaId); + } + + return topiaId; + } catch (Exception e) { + throw new PollenTechnicalException("Could not obtain votes", e); } + } - if (!feedFile.exists()) { - FeedUtil.createFeed(feedFile, "atom_1.0", messages.format( - "pollFeed_title", poll.getTitle()), siteURL, messages - .format("pollFeed_desc", poll.getDescription())); + public boolean updateVote(Vote vote) { + TopiaContext transaction = getTransaction(); + try { + + VoteDAO voteDAO = PollenDAOHelper.getVoteDAO(transaction); + Vote voteEntity = voteDAO.findByTopiaId(vote.getTopiaId()); +// converter.setTransaction(transaction); +// converter.populateVoteEntity(voteDTO, voteEntity); + voteDAO.update(voteEntity); + + if (log.isDebugEnabled()) { + log.debug("Entity updated: " + vote.getTopiaId()); + } + + return true; + } catch (Exception e) { + throw new PollenTechnicalException("Could not update vote", e); } - FeedUtil.feedFeed(feedFile, title, voteURL, content); + } - */ -// public boolean canVote(Poll poll, PollAccount participant) { -// try { -// if (log.isDebugEnabled()) { -// log.debug("Poll started : " + poll.isStarted()); -// log.debug("Poll finished : " + poll.isFinished()); -// log.debug("Poll closed : " + poll.isClosed()); -// } -// boolean result = true; -// if (!poll.isRunning()) { -// if (log.isInfoEnabled()) { -// log.info("Poll is no longer running... Participant can't vote"); -// } -// result = false; -// // Creator can't vote -// } else if (poll.getCreator().equals(participant)) { -// return false; -// } else if (poll.getType().isRestrictedOrGroup()) { -// if (log.isInfoEnabled()) { -// log.info("Poll is restricted... Participant need to be identified"); -// } -// -// PollAccountDAO dao = -// PollenDAOHelper.getPollAccountDAO(getTransaction()); -// -// TopiaQuery query = dao.createQueryFindAllParticipantsByPoll(poll); -// -// String participantAlias = PollenQueryHelper.ALIAS_POLL_ACCOUNT_CHILD; -// -// String pollCreatorProperty = -// TopiaQuery.getProperty(PollenQueryHelper.ALIAS_POLL, -// Poll.PROPERTY_CREATOR); -// -// query.addEquals(participantAlias, participant). -// addWhere(pollCreatorProperty, TopiaQuery.Op.NEQ, participant); -// -// if (log.isDebugEnabled()) { -// log.debug("Query : " + query); -// } -// -// result = dao.existByQuery(query); -// } -// return result; -// } catch (TopiaException e) { -// throw new PollenTechnicalException(e); -// } -// } -// -// public PollAccount getNewPollAccount(UserAccount user) { -// PollAccount result = new PollAccountImpl(); -// if (user != null) { -// result.setName(user.getDisplayName()); -// result.setEmail(user.getEmail()); -// result.setUserAccount(user); -// } -// return result; -// } -// -// public PollAccount getPollAccount(String accountUid) { -// try { -// PollenQueryHelper.PollAccountProperty accountProperty = -// PollenQueryHelper.newPollAccountProperty(); -// -// TopiaQuery query = PollenQueryHelper.createQuery(accountProperty). -// addEquals(accountProperty.uid(), accountUid). -// // Need fetching account votes to edit them -// addFetch(accountProperty.choiceVote()); -// -// PollAccount result = -// PollenDAOHelper.getPollAccountDAO(getTransaction()). -// findByQuery(query); -// -// return result; -// } catch (TopiaException e) { -// throw new PollenTechnicalException(e); -// } -// } -// -// public List<PollAccount> getVotes(EntityFilter filter) { -// try { -// PollAccountDAO dao = PollenDAOHelper.getPollAccountDAO(getTransaction()); -// -//// String participantChoiceProperty = -//// TopiaQuery.getProperty(PollenQueryHelper.ALIAS_POLL_ACCOUNT, -//// PollAccount.PROPERTY_CHOICE_VOTE, -//// Vote.PROPERTY_CHOICE); -//// -//// TopiaQuery query = dao.createQueryFindAllByPoll(filter). -//// addDistinct(). -//// addFetch(participantChoiceProperty); -// -// PollenQueryHelper.PollAccountProperty accountProperty = -// PollenQueryHelper.newPollAccountProperty(); -// -// TopiaQuery query = dao.createQueryFindAllByPoll(filter). -// addDistinct(). -// addFetch(accountProperty.choiceVoteProperty().choice()); -// -// List<PollAccount> results = dao.findAllByQuery(query); -// -// if (log.isDebugEnabled()) { -// log.debug("Query : " + query); -// } -// -// return results; -// } catch (TopiaException e) { -// throw new PollenTechnicalException(e); -// } -// } -// -// public void deleteVote(Poll poll, PollAccount participant) { -// try { -// // For free poll, delete the participant -// // For other types, delete all votes and set voteDate to null -// -// PollAccountDAO dao = -// PollenDAOHelper.getPollAccountDAO(getTransaction()); -// -// if (poll.getType().isFree()) { -// -// PollAccount participantToDelete = -// dao.findByTopiaId(participant.getTopiaId()); -// -// dao.delete(participantToDelete); -// -// } else if (poll.getType().isRestrictedOrGroup()) { -// -// PollAccount participantToUpdate = -// dao.findByTopiaId(participant.getTopiaId()); -// -// // Remove all votes deleted by cascade -// participantToUpdate.clearChoiceVote(); -// // Reset date, the participant is considered like he has not alerady -// // voted -// participantToUpdate.setVoteDate(null); -// -// dao.update(participantToUpdate); -// } -// } catch (TopiaException e) { -// throw new PollenTechnicalException(e); -// } -// } -// -// public int getNbVotes(Poll poll) { -// try { -// PollAccountDAO dao = -// PollenDAOHelper.getPollAccountDAO(getTransaction()); -// -// EntityFilter filter = new TopiaFilter(); -// filter.setReference(poll); -// TopiaQuery query = dao.createQueryFindAllByPoll(filter); -// -// int result = dao.countByQuery(query); -// return result; -// } catch (TopiaException e) { -// throw new PollenTechnicalException(e); -// } -// } -// -// public Vote getNewVote(Choice choice) { -// Vote result = new VoteImpl(); -// result.setChoice(choice); -// return result; -// } -// -// public PollAccount saveVote(Poll poll, PollAccount participant) throws VoteNotAllowedException, VoteDoubloonException { -// try { -// // 1- executeCanVote(transaction, poll, participant); -// // 2- generate uid if needed -// // 3- update or create participant -// // 4- add participant in the poll if needed -// // 5- save only votes with voteValue != 0 : update/create/delete -// // 6- execute calcul for continuousResults poll -// -// // NEED test : -// // need existing poll with choices -// // getNewPollAccount -// // set some votes for poll choices -// // saveVote -// -// PollAccount result; -// if (canVote(poll, participant)) { -// -// PollAccountDAO dao = PollenDAOHelper.getPollAccountDAO(getTransaction()); -// -// checkParticipantDoubloons(dao, poll, participant); -// -// PollAccount participantToSave = -// dao.findByTopiaId(participant.getTopiaId()); -// -// // canVote method check the existence of participant for a -// // restricted poll, so the create case is only for FREE poll. -// if (participantToSave == null) { -// if (log.isInfoEnabled()) { -// log.info("Create new participant for FREE poll"); -// } -// participantToSave = createNewParticipant(poll); -// } -// -// // Copy data (care about anonymous case) -// copyPollAccount(participant, participantToSave); -// -// // Update vote date -// participantToSave.setVoteDate(serviceContext.getCurrentTime()); -// -// if (log.isDebugEnabled()) { -// log.debug("Vote " + participantToSave.getName() + -// " updated : " + participantToSave.getVoteDate()); -// } -// -// // Save votes -// saveParticipantVotes(participant, participantToSave); -// -// dao.update(participantToSave); -// -// result = participantToSave; -// -// // TODO-fdesbois-2010-05-27 : execute vote counting if poll.isContinuousResults() -// -// } else { -// throw new VoteNotAllowedException(); -// } -// -// return result; -// } catch (TopiaException e) { -// throw new PollenTechnicalException(e); -// } -// } -// -// //FIXME tchemit-20120123 : what is the need ? :( -// public PollAccount findPollAccount(Poll poll, UserAccount user) { -// throw new UnsupportedOperationException("unsupported operation findPollAccount"); -// } -// -// protected void checkParticipantDoubloons(PollAccountDAO dao, -// Poll poll, -// PollAccount participant) -// throws TopiaException, VoteDoubloonException { -// -// TopiaQuery query = dao.createQueryFindAllParticipantsByPoll(poll); -// -// String participantAlias = PollenQueryHelper.ALIAS_POLL_ACCOUNT_CHILD; -// -// String participantNameProperty = -// TopiaQuery.getProperty(participantAlias, -// PollAccount.PROPERTY_NAME); -// -// query.addEquals(participantNameProperty, participant.getName()); -// -// if (StringUtils.isNotEmpty(participant.getTopiaId())) { -// query.addWhere(participantAlias, TopiaQuery.Op.NEQ, participant); -// } -// -// if (dao.existByQuery(query)) { -// throw new VoteDoubloonException(); -// } -// } -// -// /** -// * Create a new participant for a running {@code poll}. The participant uid -// * will be generated and the new instance of pollAccount will be added to -// * the {@code poll}. -// * -// * @param poll Poll where the new participant will be added -// * @return a new PollAccount added to the poll -// * @throws TopiaException for Topia errors -// */ -// protected PollAccount createNewParticipant(Poll poll) -// throws TopiaException { -// -// PollAccountDAO dao = -// PollenDAOHelper.getPollAccountDAO(getTransaction()); -// -// // Generate new uid -// String newUid = serviceContext.createPollenUrlId(); -// PollAccount newParticipant = dao.create(newUid); -// -// // Add the new participant in poll -// PollDAO pollDAO = PollenDAOHelper.getPollDAO(getTransaction()); -// -// Poll pollFound = pollDAO.findByTopiaId(poll.getTopiaId()); -// pollFound.addPollAccount(newParticipant); -// -// pollDAO.update(pollFound); -// return newParticipant; -// } -// -// /** -// * Save all votes from a participant. Votes will be added/updated or deleted -// * depends on voteValue and existence in database. -// * -// * @param source PollAccount source that contains vote to save -// * @param destination PollAccount destination where votes will be added to -// * be persisted -// * @throws TopiaException for Topia errors -// */ -// protected void saveParticipantVotes(PollAccount source, -// PollAccount destination) -// throws TopiaException { -// -// VoteDAO voteDAO = PollenDAOHelper.getVoteDAO(getTransaction()); -// for (Vote vote : source.getChoiceVote()) { -// -// Vote voteToSave = voteDAO.findByTopiaId(vote.getTopiaId()); -// -// if (vote.getVoteValue() != 0) { -// // Create vote -// if (voteToSave == null) { -// if (log.isDebugEnabled()) { -// log.debug("Create vote for " + destination.getUid()); -// } -// voteToSave = voteDAO.create(); -// voteToSave.setChoice(vote.getChoice()); -// // Add new vote to destination participant -// destination.addChoiceVote(voteToSave); -// } -// -// // Update vote -// voteToSave.setVoteValue(vote.getVoteValue()); -// voteDAO.update(voteToSave); -// -// // Delete vote if exist and new value is 0 -// } else if (voteToSave != null) { -// if (log.isDebugEnabled()) { -// log.debug("Delete vote for " + destination.getUid()); -// } -// // Normally the vote will be deleted -// destination.removeChoiceVote(voteToSave); -// } -// } -// } -// -// /** -// * Copy from account {@code source} to {@code destination}. The {@code -// * destination} will be persisted. The anonymous case is manage, properties -// * that identify the person are excluded from the copy in this case. -// * -// * @param source PollAccount to save -// * @param destination PollAccount with up to date data to persist -// * @see TopiaEntityBinder#copyExcluding(Object, Object, String...) -// */ -// protected void copyPollAccount(PollAccount source, PollAccount destination) { -// // Uid is the naturalId, don't copy it -// String[] excludes = new String[]{PollAccount.PROPERTY_UID}; -// -// // Exclude properties for anonymous account -// if (source.isAnonymous()) { -// if (log.isInfoEnabled()) { -// log.info("Anonymous vote, doesn't copy participant's data : " + -// "name, email, userAccount"); -// } -// -// excludes = new String[]{ -// PollAccount.PROPERTY_UID, -// PollAccount.PROPERTY_NAME, -// PollAccount.PROPERTY_EMAIL, -// PollAccount.PROPERTY_USER_ACCOUNT}; -// } -// -// TopiaEntityBinder<PollAccount> binder = -// PollenBinderHelper.getSimpleTopiaBinder(PollAccount.class); -// -// binder.copyExcluding(source, destination, excludes); -// } -} //ServiceVote + public boolean deleteVote(String voteId) { + TopiaContext transaction = getTransaction(); + try { + + VoteDAO voteDAO = PollenDAOHelper.getVoteDAO(transaction); + Vote voteEntity = voteDAO.findByTopiaId(voteId); + + if (voteEntity == null) { + throw new VoteNotFoundException(); + } + + voteDAO.delete(voteEntity); + + if (log.isDebugEnabled()) { + log.debug("Entity deleted: " + voteId); + } + + return true; + } catch (Exception e) { + throw new PollenTechnicalException("Could not obtain votes", e); + } + } + + public List<Vote> getVotesByPoll(Poll poll, + int startIndex, int endIndex) { + TopiaContext transaction = getTransaction(); + List<Vote> results = new ArrayList<Vote>(); + try { + // No need to load votes for an anonymous poll + if (poll.getAnonymous()) { + return results; + } + + // FIXME-FD20100309 : change model to suppress link from poll to vote + // It's not necessary to have votes when retrieving the poll, this + // method do this job only when it's needed + + if (log.isDebugEnabled()) { + log.debug("Load votes for poll with uid = " + poll.getPollId()); + log.debug("LIMIT : startIndex = " + startIndex + " _ " + + "endIndex = " + endIndex); + } + + // FIXME : refactor this to use TopiaQuery from ToPIA 2.3 + // Order the results by creation date (the last vote done will be + // at the end of the list) + List<Vote> votes = transaction.find( + "FROM " + Vote.class.getName() + + " WHERE poll.pollId = :pollUId" + + " ORDER BY topiaCreateDate", + startIndex, endIndex, + new Object[]{"pollUId", poll.getPollId()}); + + if (log.isDebugEnabled()) { + log.debug("Nb votes found : " + votes.size()); + } + + results = Lists.newArrayList(results); + + } catch (TopiaException e) { + throw new PollenTechnicalException("Could not obtain votes", e); + } + return results; + } + + public boolean hasAlreadyVoted(String votingId, Poll poll) { + TopiaContext transaction = getTransaction(); + boolean result = false; + try { + + // Test using a count(*) on votes + List<Long> tmp = transaction.find( + "SELECT COUNT(*)" + + " FROM " + Vote.class.getName() + + " WHERE poll.pollId = :pollUId" + + " AND pollAccount.votingId = :votingId", + "pollUId", poll.getPollId(), + "votingId", votingId); + + int count = tmp.get(0).intValue(); + + // If the count is greater than 0, there is an existing votingId + // who has already voted for the poll + result = count > 0; + + } catch (Exception e) { + throw new PollenTechnicalException( + "Unable test vote existing for account with votingId = " + + votingId + " and poll with uid = " + poll.getPollId(), e); + } + return result; + } + + public List<Vote> selectVotes(Map<String, Object> properties) { + TopiaContext transaction = getTransaction(); + try { + VoteDAO voteDAO = PollenDAOHelper.getVoteDAO(transaction); + + List<Vote> voteEntities; + + if (properties == null) { + voteEntities = voteDAO.findAll(); + if (log.isWarnEnabled()) { + log.warn("Attention : tous les votes ont été sélectionnés !"); + } + } else { + voteEntities = voteDAO.findAllByProperties(properties); + } + + List<Vote> results = Lists.newArrayList(voteEntities); + + if (log.isDebugEnabled()) { + log.debug("Entities found: " + + ((results == null) ? "null" : results.size())); + } + + return results; + } catch (Exception e) { + throw new PollenTechnicalException("Could not obtain votes", e); + } + } + +} //voteservice Modified: branches/pollen-1.2.6-struts2/pollen-services/src/test/java/org/chorem/pollen/services/AbstractPollenServiceTest.java =================================================================== --- branches/pollen-1.2.6-struts2/pollen-services/src/test/java/org/chorem/pollen/services/AbstractPollenServiceTest.java 2012-02-26 17:19:03 UTC (rev 3146) +++ branches/pollen-1.2.6-struts2/pollen-services/src/test/java/org/chorem/pollen/services/AbstractPollenServiceTest.java 2012-02-26 17:19:55 UTC (rev 3147) @@ -38,12 +38,15 @@ public abstract class AbstractPollenServiceTest extends PollenServiceSupport { @Rule - public FakeServiceContext serviceContext = new FakeServiceContext(); + public FakeServiceContext fakeContext = new FakeServiceContext(); @Before public void setUp() throws Exception { - serviceContext.setServiceFactory(new PollenServiceFactory()); - setServiceContext(serviceContext); + getServiceContext().setServiceFactory(new PollenServiceFactory()); + setServiceContext(getServiceContext()); } + public FakeServiceContext getServiceContext() { + return fakeContext; + } } Modified: branches/pollen-1.2.6-struts2/pollen-services/src/test/java/org/chorem/pollen/services/FakeServiceContext.java =================================================================== --- branches/pollen-1.2.6-struts2/pollen-services/src/test/java/org/chorem/pollen/services/FakeServiceContext.java 2012-02-26 17:19:03 UTC (rev 3146) +++ branches/pollen-1.2.6-struts2/pollen-services/src/test/java/org/chorem/pollen/services/FakeServiceContext.java 2012-02-26 17:19:55 UTC (rev 3147) @@ -24,9 +24,9 @@ package org.chorem.pollen.services; -import com.google.common.base.Supplier; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.chorem.pollen.PolleIOUtil; import org.chorem.pollen.PollenConfiguration; import org.chorem.pollen.PollenTopiaRootContextFactory; import org.junit.rules.TestWatcher; @@ -39,7 +39,10 @@ import java.io.File; import java.net.URL; import java.util.Date; +import java.util.HashMap; import java.util.Locale; +import java.util.Map; +import java.util.Properties; import java.util.UUID; /** @@ -50,12 +53,10 @@ public static final String CONFIGURATION_PATH = "pollen-fake.properties"; - protected Supplier<TopiaContext> rootContextSupplier; - private static final Log log = LogFactory.getLog(FakeServiceContext.class); /** A time-stamp, allow to make multiple build and keep the tests data. */ - protected static final String TIMESTAMP = String.valueOf(System.nanoTime()); + protected static final long TIMESTAMP = System.nanoTime(); protected PollenServiceFactory serviceFactory; @@ -65,39 +66,21 @@ protected Date fakeCurrentTime; - protected File getTestSpecificDirectory(Description description) { - // Trying to look for the temporary folder to store data for the test - String tempDirPath = System.getProperty("java.io.tmpdir"); - if (tempDirPath == null) { - // can this really occur ? - tempDirPath = ""; - if (log.isWarnEnabled()) { - log.warn("'\"java.io.tmpdir\" not defined"); - } - } - File tempDirFile = new File(tempDirPath); + protected File testDir; - // create the directory to store database data - String dataBasePath = description.getClassName() - + File.separator // a directory with the test class name - + description.getMethodName()// a sub-directory with the method name - + '_' - + TIMESTAMP; // and a timestamp - File databaseFile = new File(tempDirFile, dataBasePath); - return databaseFile; + public TopiaContext getRootContext() { + return this.rootContext; } - public Supplier<TopiaContext> getRootContextSupplier() { - return rootContextSupplier; - } - @Override protected void starting(Description description) { super.starting(description); - description.getMethodName(); PollenTopiaRootContextFactory factory = new PollenTopiaRootContextFactory(); - File testDir = getTestSpecificDirectory(description); + testDir = PolleIOUtil.getTestSpecificDirectory( + description.getTestClass(), + description.getMethodName(), + TIMESTAMP); if (log.isInfoEnabled()) { log.info("Test dir = " + testDir); } @@ -138,8 +121,8 @@ public Date getCurrentTime() { if (fakeCurrentTime == null) { throw new IllegalStateException("le service testé a besoin de " + - "connaître la date, il faudrait en préciser une de test via les" - + " méthodes serviceContext#setCurrentTime(...)"); + "connaître la date, il faudrait en préciser une de test via les" + + " méthodes serviceContext#setCurrentTime(...)"); } else { log.trace("injecting fake date in service : " + fakeCurrentTime); } @@ -181,8 +164,11 @@ @Override public PollenConfiguration getConfiguration() { + + Properties props = new Properties(); + props.put("testDirectory", testDir); PollenConfiguration configuration - = new PollenConfiguration(CONFIGURATION_PATH); + = new PollenConfiguration(CONFIGURATION_PATH, props); return configuration; } Modified: branches/pollen-1.2.6-struts2/pollen-services/src/test/java/org/chorem/pollen/services/impl/EmailServiceTest.java =================================================================== --- branches/pollen-1.2.6-struts2/pollen-services/src/test/java/org/chorem/pollen/services/impl/EmailServiceTest.java 2012-02-26 17:19:03 UTC (rev 3146) +++ branches/pollen-1.2.6-struts2/pollen-services/src/test/java/org/chorem/pollen/services/impl/EmailServiceTest.java 2012-02-26 17:19:55 UTC (rev 3147) @@ -24,7 +24,6 @@ package org.chorem.pollen.services.impl; import org.chorem.pollen.services.AbstractPollenServiceTest; -import org.chorem.pollen.services.impl.EmailService; import org.junit.Before; import org.junit.Test; @@ -40,7 +39,7 @@ @Before public void setUp() throws Exception { - serviceContext.setCurrentTime(5, 10, 2011); + getServiceContext().setCurrentTime(5, 10, 2011); super.setUp(); } Modified: branches/pollen-1.2.6-struts2/pollen-services/src/test/java/org/chorem/pollen/services/impl/FavoriteServiceTest.java =================================================================== --- branches/pollen-1.2.6-struts2/pollen-services/src/test/java/org/chorem/pollen/services/impl/FavoriteServiceTest.java 2012-02-26 17:19:03 UTC (rev 3146) +++ branches/pollen-1.2.6-struts2/pollen-services/src/test/java/org/chorem/pollen/services/impl/FavoriteServiceTest.java 2012-02-26 17:19:55 UTC (rev 3147) @@ -46,7 +46,7 @@ @Before public void setUp() throws Exception { - serviceContext.setCurrentTime(5, 10, 2011); + getServiceContext().setCurrentTime(5, 10, 2011); super.setUp(); } Modified: branches/pollen-1.2.6-struts2/pollen-services/src/test/java/org/chorem/pollen/services/impl/PollServiceTest.java =================================================================== --- branches/pollen-1.2.6-struts2/pollen-services/src/test/java/org/chorem/pollen/services/impl/PollServiceTest.java 2012-02-26 17:19:03 UTC (rev 3146) +++ branches/pollen-1.2.6-struts2/pollen-services/src/test/java/org/chorem/pollen/services/impl/PollServiceTest.java 2012-02-26 17:19:55 UTC (rev 3147) @@ -24,7 +24,6 @@ package org.chorem.pollen.services.impl; import org.chorem.pollen.services.AbstractPollenServiceTest; -import org.chorem.pollen.services.impl.PollService; import org.junit.Before; import org.junit.Ignore; import org.junit.Test; @@ -43,7 +42,7 @@ @Before public void setUp() throws Exception { - serviceContext.setCurrentTime(5, 10, 2011); + getServiceContext().setCurrentTime(5, 10, 2011); super.setUp(); Modified: branches/pollen-1.2.6-struts2/pollen-services/src/test/java/org/chorem/pollen/services/impl/UserServiceTest.java =================================================================== --- branches/pollen-1.2.6-struts2/pollen-services/src/test/java/org/chorem/pollen/services/impl/UserServiceTest.java 2012-02-26 17:19:03 UTC (rev 3146) +++ branches/pollen-1.2.6-struts2/pollen-services/src/test/java/org/chorem/pollen/services/impl/UserServiceTest.java 2012-02-26 17:19:55 UTC (rev 3147) @@ -24,7 +24,6 @@ package org.chorem.pollen.services.impl; import org.chorem.pollen.services.AbstractPollenServiceTest; -import org.chorem.pollen.services.impl.UserService; import org.junit.Before; import org.junit.Ignore; import org.junit.Test; @@ -42,7 +41,7 @@ @Before public void setUp() throws Exception { - serviceContext.setCurrentTime(5, 10, 2011); + getServiceContext().setCurrentTime(5, 10, 2011); super.setUp(); } Modified: branches/pollen-1.2.6-struts2/pollen-services/src/test/java/org/chorem/pollen/services/impl/VoteServiceTest.java =================================================================== --- branches/pollen-1.2.6-struts2/pollen-services/src/test/java/org/chorem/pollen/services/impl/VoteServiceTest.java 2012-02-26 17:19:03 UTC (rev 3146) +++ branches/pollen-1.2.6-struts2/pollen-services/src/test/java/org/chorem/pollen/services/impl/VoteServiceTest.java 2012-02-26 17:19:55 UTC (rev 3147) @@ -24,7 +24,6 @@ package org.chorem.pollen.services.impl; import org.chorem.pollen.services.AbstractPollenServiceTest; -import org.chorem.pollen.services.impl.VoteService; import org.junit.Before; import org.junit.Test; @@ -40,7 +39,7 @@ @Before public void setUp() throws Exception { - serviceContext.setCurrentTime(5, 10, 2011); + getServiceContext().setCurrentTime(5, 10, 2011); super.setUp(); }