r2886 - in trunk/pollen-business/src: main/java/org/chorem/pollen/business main/java/org/chorem/pollen/business/services test/java/org/chorem/pollen/business test/java/org/chorem/pollen/business/services
Author: fdesbois Date: 2010-02-25 16:17:00 +0100 (Thu, 25 Feb 2010) New Revision: 2886 Modified: trunk/pollen-business/src/main/java/org/chorem/pollen/business/PollenBusinessException.java trunk/pollen-business/src/main/java/org/chorem/pollen/business/PollenContext.java trunk/pollen-business/src/main/java/org/chorem/pollen/business/services/ServicePollImpl.java trunk/pollen-business/src/test/java/org/chorem/pollen/business/TestManager.java trunk/pollen-business/src/test/java/org/chorem/pollen/business/services/ServicePollImplTest.java Log: - Add currentDate in PollenContext for tests - Refactor old query to use TopiaQuery :D Modified: trunk/pollen-business/src/main/java/org/chorem/pollen/business/PollenBusinessException.java =================================================================== --- trunk/pollen-business/src/main/java/org/chorem/pollen/business/PollenBusinessException.java 2010-02-25 13:13:26 UTC (rev 2885) +++ trunk/pollen-business/src/main/java/org/chorem/pollen/business/PollenBusinessException.java 2010-02-25 15:17:00 UTC (rev 2886) @@ -20,21 +20,20 @@ protected PollenExceptionType type; public PollenBusinessException(PollenExceptionType type) { - super(type.toString()); + super(type.getMessage()); } public enum PollenExceptionType { LOAD_CONFIGURATION(_("pollen.exception.load_configuration")); - private String name; + private String message; - PollenExceptionType(String name) { - this.name = name; + PollenExceptionType(String message) { + this.message = message; } - @Override - public String toString() { - return name; + public String getMessage() { + return message; } } Modified: trunk/pollen-business/src/main/java/org/chorem/pollen/business/PollenContext.java =================================================================== --- trunk/pollen-business/src/main/java/org/chorem/pollen/business/PollenContext.java 2010-02-25 13:13:26 UTC (rev 2885) +++ trunk/pollen-business/src/main/java/org/chorem/pollen/business/PollenContext.java 2010-02-25 15:17:00 UTC (rev 2886) @@ -1,11 +1,11 @@ package org.chorem.pollen.business; +import java.util.Date; import java.util.Properties; import java.util.UUID; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.chorem.pollen.business.PollenBusinessException.PollenExceptionType; import org.chorem.pollen.business.persistence.PollenModelDAOHelper; import org.chorem.pollen.business.services.ServiceUserImpl; import org.nuiton.i18n.I18n; @@ -37,6 +37,8 @@ private static ApplicationConfig configuration; + private static Date currentDate; + /** * Default configuration file will be loaded using * {@link org.nuiton.util.ApplicationConfig }. @@ -254,4 +256,15 @@ return UUID.randomUUID().toString().replaceAll("-", ""); } + public static void setCurrentDate(Date currentDate) { + PollenContext.currentDate = currentDate; + } + + public static Date getCurrentDate() { + if (currentDate == null) { + currentDate = new Date(); + } + return currentDate; + } + } Modified: trunk/pollen-business/src/main/java/org/chorem/pollen/business/services/ServicePollImpl.java =================================================================== --- trunk/pollen-business/src/main/java/org/chorem/pollen/business/services/ServicePollImpl.java 2010-02-25 13:13:26 UTC (rev 2885) +++ trunk/pollen-business/src/main/java/org/chorem/pollen/business/services/ServicePollImpl.java 2010-02-25 15:17:00 UTC (rev 2886) @@ -17,6 +17,7 @@ package org.chorem.pollen.business.services; import java.util.ArrayList; +import java.util.Date; import java.util.HashMap; import java.util.HashSet; import java.util.List; @@ -25,8 +26,6 @@ import java.util.Set; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.chorem.pollen.business.PollenBusinessException; -import org.chorem.pollen.business.PollenContext; import org.chorem.pollen.business.PollenConverter; import org.chorem.pollen.business.converters.DataPollConverter; import org.chorem.pollen.business.converters.DataVotingListConverter; @@ -58,6 +57,8 @@ import org.chorem.pollen.business.PollenContext; import org.nuiton.topia.TopiaContext; import org.nuiton.topia.TopiaException; +import org.nuiton.topia.framework.TopiaQuery; +import org.nuiton.topia.framework.TopiaQuery.Op; import static org.nuiton.i18n.I18n._; /** @@ -576,27 +577,43 @@ public List<PollDTO> findRunningPolls(boolean withEndDate) { TopiaContext transaction = null; List<PollDTO> results = null; - List<Poll> pollEntities = null; try { transaction = rootContext.beginTransaction(); - // requête de sélection des sondages + PollDAO dao = PollenModelDAOHelper.getPollDAO(transaction); + + String beginDate = Poll.BEGIN_DATE; + String endDate = Poll.END_DATE; + Date currentDate = PollenContext.getCurrentDate(); + + TopiaQuery query = dao.createQuery(). + addNullOr(beginDate, Op.LT, currentDate); + if (withEndDate) { - pollEntities = transaction - .find("from " - + Poll.class.getName() - + " as poll where (poll.endDate is not null and poll.endDate > current_timestamp())" - + " and (poll.beginDate is null or poll.beginDate < current_timestamp())"); + query.add(endDate, Op.GT, currentDate); } else { - pollEntities = transaction - .find("from " - + Poll.class.getName() - + " as poll where (poll.endDate is null or poll.endDate > current_timestamp())" - + " and (poll.beginDate is null or poll.beginDate < current_timestamp())"); + query.addNullOr(endDate, Op.GT, currentDate); } + List<Poll> entities = dao.findAllByQuery(query); + + // requête de sélection des sondages +// if (withEndDate) { +// pollEntities = transaction +// .find("from " +// + Poll.class.getName() +// + " as poll where (poll.endDate is not null and poll.endDate > current_timestamp())" +// + " and (poll.beginDate is null or poll.beginDate < current_timestamp())"); +// } else { +// pollEntities = transaction +// .find("from " +// + Poll.class.getName() +// + " as poll where (poll.endDate is null or poll.endDate > current_timestamp())" +// + " and (poll.beginDate is null or poll.beginDate < current_timestamp())"); +// } + converter.setTransaction(transaction); - results = converter.createPollDTOs(pollEntities); + results = converter.createPollDTOs(entities); transaction.commitTransaction(); Modified: trunk/pollen-business/src/test/java/org/chorem/pollen/business/TestManager.java =================================================================== --- trunk/pollen-business/src/test/java/org/chorem/pollen/business/TestManager.java 2010-02-25 13:13:26 UTC (rev 2885) +++ trunk/pollen-business/src/test/java/org/chorem/pollen/business/TestManager.java 2010-02-25 15:17:00 UTC (rev 2886) @@ -3,6 +3,8 @@ import java.io.IOException; import java.io.InputStream; +import java.util.Calendar; +import java.util.GregorianCalendar; import java.util.Properties; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -43,6 +45,10 @@ PollenContext.loadConfiguration(config); PollenContext.start(); + + // Set currentDate to 23/02/2010 for tests + Calendar calendar = new GregorianCalendar(2010, 1, 23, 0, 0, 0); + PollenContext.setCurrentDate(calendar.getTime()); } public static void stop() throws IOException { Modified: trunk/pollen-business/src/test/java/org/chorem/pollen/business/services/ServicePollImplTest.java =================================================================== --- trunk/pollen-business/src/test/java/org/chorem/pollen/business/services/ServicePollImplTest.java 2010-02-25 13:13:26 UTC (rev 2885) +++ trunk/pollen-business/src/test/java/org/chorem/pollen/business/services/ServicePollImplTest.java 2010-02-25 15:17:00 UTC (rev 2886) @@ -17,7 +17,6 @@ import java.io.IOException; import java.util.ArrayList; -import org.chorem.pollen.business.PollenBusinessException; import org.chorem.pollen.business.dto.CommentDTO; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; @@ -27,6 +26,7 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.chorem.pollen.business.PollenContext; import org.chorem.pollen.business.TestData; import org.chorem.pollen.business.TestManager; import org.chorem.pollen.business.dto.ChoiceDTO; @@ -38,7 +38,6 @@ import org.chorem.pollen.business.persistence.CommentDAO; import org.chorem.pollen.business.persistence.Poll; import org.chorem.pollen.business.persistence.PollenModelDAOHelper; -import org.chorem.pollen.business.utils.ContextUtil; import org.chorem.pollen.common.ChoiceType; import org.chorem.pollen.common.PollType; import org.chorem.pollen.common.VoteCountingType; @@ -48,7 +47,6 @@ import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; -import org.nuiton.topia.TopiaContext; /** * Tests du service de gestion des sondages. @@ -331,7 +329,7 @@ TestManager.start("testFindRunningPolls"); instance = new ServicePollImpl(); - Date now = new Date(); + Date now = PollenContext.getCurrentDate(); // sondage en cours sans date de fin PollDTO poll1 = new PollDTO();
participants (1)
-
fdesbois@users.chorem.org