diff --git a/cli/src/main/java/fr/inrae/agroclim/season/cli/output/DbOutput.java b/cli/src/main/java/fr/inrae/agroclim/season/cli/output/DbOutput.java index 831491fe056ea907935e666b114afb5ff4c54ab0..c5a08f9d01d9d1ff4904fdc0253dc26706b801f0 100644 --- a/cli/src/main/java/fr/inrae/agroclim/season/cli/output/DbOutput.java +++ b/cli/src/main/java/fr/inrae/agroclim/season/cli/output/DbOutput.java @@ -1,15 +1,16 @@ package fr.inrae.agroclim.season.cli.output; -import fr.inrae.agroclim.indicators.exception.ErrorMessageException; import java.time.LocalDate; import java.util.ArrayList; import java.util.HashMap; +import java.util.HashSet; import java.util.List; import java.util.Locale; import java.util.Map; import java.util.Objects; -import java.util.stream.Collectors; +import java.util.Set; +import fr.inrae.agroclim.indicators.exception.ErrorMessageException; import fr.inrae.agroclim.indicators.model.AnnualPhase; import fr.inrae.agroclim.indicators.model.Evaluation; import fr.inrae.agroclim.indicators.model.data.Variable; @@ -35,8 +36,6 @@ import fr.inrae.agroclim.season.core.model.SimulationTreatment; import fr.inrae.agroclim.season.core.model.Treatment; import fr.inrae.agroclim.season.core.model.TreatmentPhase; import jakarta.persistence.PersistenceException; -import java.util.HashSet; -import java.util.Set; import lombok.RequiredArgsConstructor; import lombok.extern.log4j.Log4j2; @@ -267,9 +266,7 @@ public final class DbOutput extends AbstractOutput { tttPhase.setStart(LocalDate.ofYearDay(year, 1)); tttPhase.setTreatment(ttt); tttPhase.setYear(year); - if (!treatmentPhases.containsKey(year)) { - treatmentPhases.put(year, new HashMap<>()); - } + treatmentPhases.computeIfAbsent(year, key -> new HashMap<>()); treatmentPhases.get(year).put(phase, tttPhase); } @@ -292,8 +289,7 @@ public final class DbOutput extends AbstractOutput { } final Integer iYear = Integer.valueOf(year); if (!treatmentPhases.containsKey(iYear)) { - throw new RuntimeException("Strange, no phase for the year " - + year); + throw new RuntimeException("Strange, no phase for the year " + year); } if (!treatmentPhases.get(iYear).containsKey(currentPhase)) { LOGGER.warn("{} Strange, no treatmentPhase for the year {}" @@ -302,36 +298,42 @@ public final class DbOutput extends AbstractOutput { createTreatmentPhase(year, currentPhase); } - TreatmentPhase tttPhase = treatmentPhases.get(iYear).get(currentPhase); + final TreatmentPhase tttPhase = treatmentPhases.get(iYear).get(currentPhase); + + final TreatmentPhase phase = new TreatmentPhase(); + phase.setPhase(tttPhase.getPhase()); + phase.setStart(tttPhase.getStart()); + phase.setTreatment(tttPhase.getTreatment()); + phase.setYear(tttPhase.getYear()); // change date when Evaluation#computeEachDate() is used. - if (this.getCurrentDate() != null // - && this.getCurrentDate().isAfter(tttPhase.getStart()) // - && this.getCurrentDate().isBefore(tttPhase.getEnd())) { - final TreatmentPhase phase = new TreatmentPhase(); - phase.setEnd(tttPhase.getEnd()); - phase.setPhase(tttPhase.getPhase()); - phase.setStart(tttPhase.getStart()); - phase.setTreatment(tttPhase.getTreatment()); - phase.setYear(tttPhase.getYear()); + if (this.getCurrentDate() != null) { + if (this.getCurrentDate().isBefore(tttPhase.getStart()) // + || this.getCurrentDate().isAfter(tttPhase.getEnd())) { + // result must be given only for the phase + return 0; + } phase.setEnd(this.getCurrentDate()); - tttPhase = phase; - final String key = phase.getYear() + " − " + phase.getPhase() + " − " + phase.getEnd(); + final String key = phase.getYear() + " − " + phase.getPhase() + " − " + phase.getEnd() + " - " + + indicator.getIndicatorId(); if (resultKey.contains(key)) { + LOGGER.warn("Strange, same indicator: {}", key); return 0; } resultKey.add(key); + } else { + phase.setEnd(tttPhase.getEnd()); } final Double norm = indicator.getNormalizedValue(); final Double raw = indicator.getRawValue(); final SimulationResult result = new SimulationResult( indicator.getIndicatorCategory(), indicator.getIndicatorId(), - tttPhase, norm, raw); + phase, norm, raw); results.add(result); int nbOfResults = 1; if (!indicator.getIndicatorResults().isEmpty()) { nbOfResults += indicator.getIndicatorResults() .stream() - .map((res) -> handleIndicator(year, res)) + .map(res -> handleIndicator(year, res)) .reduce(nbOfResults, Integer::sum); } return nbOfResults; @@ -441,7 +443,7 @@ public final class DbOutput extends AbstractOutput { final String category; final String code; final String json; - if (exception instanceof ErrorMessageException iex) { + if (exception instanceof final ErrorMessageException iex) { category = iex.getErrorMessage().getType().getCategory().getCode(); code = iex.getErrorMessage().getType().getFullCode(); json = iex.getErrorMessage().toJSON(); @@ -466,13 +468,14 @@ public final class DbOutput extends AbstractOutput { private void storeSoilDataFor(final String userName, final int simulationId) { final String soilTableName = simulationSoilDao.getTableName(userName, simulationId); final List<SimulationSoil> soilResults = getTreatment().getEvaluation().getClimaticResource().getData() // - .stream().map(data -> - new SimulationSoil( + .stream() // + .map(data -> new SimulationSoil( getTreatment().getCell(), data.getDate(), data.getRawValue(Variable.SOILWATERCONTENT), data.getRawValue(Variable.WATER_RESERVE)) - ).collect(Collectors.toList()); + ) // + .toList(); simulationSoilDao.add(soilTableName, soilResults); }