[Suiviobsmer-commits] r1244 - in trunk/wao-business/src: main/java/fr/ifremer/wao/service test/java/fr/ifremer/wao/service
Author: bleny Date: 2011-04-29 14:29:07 +0000 (Fri, 29 Apr 2011) New Revision: 1244 Log: add all hours of the days and all monthes of the year in obsdeb synthesis Modified: trunk/wao-business/src/main/java/fr/ifremer/wao/service/ServiceSynthesisImpl.java trunk/wao-business/src/test/java/fr/ifremer/wao/service/ObsDebTest.java trunk/wao-business/src/test/java/fr/ifremer/wao/service/ServiceSamplingImplTest.java Modified: trunk/wao-business/src/main/java/fr/ifremer/wao/service/ServiceSynthesisImpl.java =================================================================== --- trunk/wao-business/src/main/java/fr/ifremer/wao/service/ServiceSynthesisImpl.java 2011-04-29 13:03:57 UTC (rev 1243) +++ trunk/wao-business/src/main/java/fr/ifremer/wao/service/ServiceSynthesisImpl.java 2011-04-29 14:29:07 UTC (rev 1244) @@ -65,10 +65,12 @@ import fr.ifremer.wao.entity.IndicatorLogImpl; import fr.ifremer.wao.entity.SampleRow; import fr.ifremer.wao.entity.WaoUser; +import org.apache.commons.collections.CollectionUtils; import org.nuiton.topia.TopiaContext; import org.nuiton.topia.TopiaException; import org.nuiton.topia.framework.TopiaQuery; import org.nuiton.topia.framework.TopiaQuery.Op; +import org.nuiton.util.CollectionUtil; import org.nuiton.util.DateUtil; import org.nuiton.util.PeriodDates; import org.slf4j.Logger; @@ -79,6 +81,7 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; +import java.util.Collections; import java.util.Date; import java.util.HashMap; import java.util.HashSet; @@ -1259,7 +1262,7 @@ log.debug(contacts.size() + " found for observation hours synthesis"); } SimpleDateFormat yearFormat = new SimpleDateFormat("yyyy"); - SimpleDateFormat hourFormat = new SimpleDateFormat("HH:00"); + SimpleDateFormat hourFormat = new SimpleDateFormat("HH"); Map<String, Map<String, Integer>> result = new TreeMap<String, Map<String, Integer>>(); for (Contact contact : contacts) { @@ -1267,17 +1270,22 @@ if (observationDate != null) { String year = yearFormat.format(observationDate); - String hour = hourFormat.format(observationDate); Map<String, Integer> hourToObservationCount = result.get(year); if (hourToObservationCount == null) { + // init a map of size 24, with all different hour of the day + // as key and 0 as values. hourToObservationCount = new TreeMap<String, Integer>(); + for (int hourOfTheDay = 0 ; hourOfTheDay < 24 ; ++hourOfTheDay) { + String hour = String.format("%1$02d", hourOfTheDay); + hourToObservationCount.put(hour, 0); + } result.put(year, hourToObservationCount); } + + // for the hour of this observation, add 1 to the count + String hour = hourFormat.format(observationDate); Integer count = hourToObservationCount.get(hour); - if (count == null) { - count = 0; - } count += 1; hourToObservationCount.put(hour, count); } @@ -1300,7 +1308,7 @@ protected Map<String, Map<String, Integer>> computeBoatsCounts(List<Contact> contacts) { SimpleDateFormat yearFormat = new SimpleDateFormat("yyyy"); - SimpleDateFormat monthFormat = new SimpleDateFormat("M"); + SimpleDateFormat monthFormat = new SimpleDateFormat("MM"); // For each year of the contact found, for each month of this year, the // boats observed this month of this year. We use a set, so we ensure @@ -1333,15 +1341,15 @@ Map<String, Map<String, Integer>> result = new TreeMap<String, Map<String, Integer>>(); for (Map.Entry<String, Map<String, Set<Boat>>> entry : temp.entrySet()) { String year = entry.getKey(); - for (Map.Entry<String, Set<Boat>> entry2 : entry.getValue().entrySet()) { - String month = entry2.getKey(); - Map<String, Integer> monthToCount = result.get(year); - if (monthToCount == null) { - monthToCount = new TreeMap<String, Integer>(); - result.put(year, monthToCount); - } - monthToCount.put(month, temp.get(year).get(month).size()); + Map<String, Integer> monthToCount = new TreeMap<String, Integer>(); + for (int monthNumber = 1 ; monthNumber <= 12 ; ++monthNumber) { + // format april as '04' in order to make it before december '12' and not after + String month = String.format("%1$02d", monthNumber); + Set<Boat> boats = entry.getValue().get(month); + Integer count = CollectionUtils.isEmpty(boats) ? 0 : boats.size(); + monthToCount.put(month, count); } + result.put(year, monthToCount); } return result; Modified: trunk/wao-business/src/test/java/fr/ifremer/wao/service/ObsDebTest.java =================================================================== --- trunk/wao-business/src/test/java/fr/ifremer/wao/service/ObsDebTest.java 2011-04-29 13:03:57 UTC (rev 1243) +++ trunk/wao-business/src/test/java/fr/ifremer/wao/service/ObsDebTest.java 2011-04-29 14:29:07 UTC (rev 1244) @@ -41,7 +41,6 @@ import org.apache.commons.lang.StringUtils; import org.junit.Assert; import org.junit.Before; -import org.junit.Ignore; import org.junit.Test; import org.nuiton.util.DateUtil; import org.nuiton.util.PeriodDates; @@ -428,11 +427,12 @@ SamplingFilter samplingFilter = serviceSampling.newSamplingFilter(fixtures.joshAsCoordinator()); Map<String, Map<String, Integer>> observationHours = serviceSynthesis.getObservationHours(samplingFilter); - Assert.assertEquals(2, observationHours.get("2010").get("09:00").intValue()); + Assert.assertEquals(2, observationHours.get("2010").get("09").intValue()); + Assert.assertEquals("24 values associated with '2010', one for each hour of the day", 24, observationHours.get("2010").size()); samplingFilter = serviceSampling.newSamplingFilter(fixtures.joshAsCoordinator()); Map<String, Map<String, Integer>> boatsCounts = serviceSynthesis.getDistinctBoatsCounts(samplingFilter); - // Two distinct boats observed on February 2010 - Assert.assertEquals(2, boatsCounts.get("2010").get("2").intValue()); + Assert.assertEquals("Two distinct boats observed on February 2010", 2, boatsCounts.get("2010").get("02").intValue()); + Assert.assertEquals("12 values for must be associated to '2010', one for each month", 12, boatsCounts.get("2010").size()); } } Modified: trunk/wao-business/src/test/java/fr/ifremer/wao/service/ServiceSamplingImplTest.java =================================================================== --- trunk/wao-business/src/test/java/fr/ifremer/wao/service/ServiceSamplingImplTest.java 2011-04-29 13:03:57 UTC (rev 1243) +++ trunk/wao-business/src/test/java/fr/ifremer/wao/service/ServiceSamplingImplTest.java 2011-04-29 14:29:07 UTC (rev 1244) @@ -139,7 +139,7 @@ months.add(month); } - SampleRow row = serviceSampling.newSampleRow(); + SampleRow row = serviceSampling.newSampleRow(getConnectedAdmin()); row.setCode("2010_03"); row.setNbObservants(3); row.setAverageTideTime(2.5);
participants (1)
-
bleny@users.labs.libre-entreprise.org