diff --git a/www-server/src/main/java/fr/agrometinfo/www/server/rs/IndicatorResource.java b/www-server/src/main/java/fr/agrometinfo/www/server/rs/IndicatorResource.java index 6ca575c9873f2dba0524db738480c19a19c7f2d2..b4a21373ff70abed70a6fdf89063b60b74fe6564 100644 --- a/www-server/src/main/java/fr/agrometinfo/www/server/rs/IndicatorResource.java +++ b/www-server/src/main/java/fr/agrometinfo/www/server/rs/IndicatorResource.java @@ -86,6 +86,15 @@ public class IndicatorResource implements IndicatorService { return "no translation !"; } + private static void throwWebApplicationException(final Response.Status status, final String message) + throws WebApplicationException { + throw new WebApplicationException(Response.status(status) // + .entity(ErrorResponseDTO.of(status.getStatusCode(), // + status.getReasonPhrase(), // + message)) // + .build()); + } + private static IndicatorDTO toDTO(final Indicator indicator, final Locale locale) { final var dto = new IndicatorDTO(); dto.setCode(indicator.getCode()); @@ -315,17 +324,16 @@ public class IndicatorResource implements IndicatorService { // final var indicator = indicatorDao.findByCodeAndPeriod(indicatorUid, periodCode); if (indicator == null) { - final var status = Response.Status.BAD_REQUEST; - throw new WebApplicationException(Response.status(status) // - .entity(ErrorResponseDTO.of(status.getStatusCode(), // - status.getReasonPhrase(), // - indicatorUid + " is unknown for " + periodCode)) // - .build()); + throwWebApplicationException(Response.Status.BAD_REQUEST, indicatorUid + " is unknown for " + periodCode); } final var firstDay = getDate(year, indicator.getPeriod().getFirstDay()); final var lastDay = getDate(year, indicator.getPeriod().getLastDay()); final var date = praDailyValueDao.findLastDate(indicator, year); + if (date == null) { + throwWebApplicationException(Response.Status.NOT_FOUND, + "No value found for indicator " + indicatorUid + " in " + year); + } final Double averageValue; final Double comparedValue; final Map<LocalDate, Float[]> tmpDailyValues; @@ -431,8 +439,15 @@ public class IndicatorResource implements IndicatorService { // final FeatureCollection collection = new FeatureCollection(); final Indicator indicator = indicatorDao.findByCodeAndPeriod(indicatorUid, periodCode); + if (indicator == null) { + throwWebApplicationException(Response.Status.BAD_REQUEST, indicatorUid + " is unknown for " + periodCode); + } addMetadata(collection, indicator, year, locale); final LocalDate date = praDailyValueDao.findLastDate(indicator, year); + if (date == null) { + throwWebApplicationException(Response.Status.NOT_FOUND, + "No value found for indicator " + indicatorUid + " in " + year); + } final Region region; if (regionId == null) { region = null; @@ -477,6 +492,20 @@ public class IndicatorResource implements IndicatorService { } // final List<Integer> result = praDailyValueDao.findYears(); + if (!result.isEmpty()) { + // ensure the first year of data match the end year of the indicator. + final Integer firstYear = result.get(0); + // all indicators with value for this year + final List<Indicator> indicators = praDailyValueDao.findIndicators(firstYear); + if (indicators.isEmpty()) { + result.remove(firstYear); + } else { + final boolean found = indicators.stream().anyMatch(i -> i.getPeriod().getFirstDay().startsWith("YYYY")); + if (!found) { + result.remove(firstYear); + } + } + } cacheService.setCache(result); return result; }