From bc687c11196dc8e6931a069f9dc732fe8592dccf Mon Sep 17 00:00:00 2001
From: Olivier Maury <Olivier.Maury@inrae.fr>
Date: Mon, 2 Dec 2024 17:09:18 +0100
Subject: [PATCH] =?UTF-8?q?feat:=20Pr=C3=A9ciser=20la=20comparaison=20?=
 =?UTF-8?q?=C3=A0=20la=20normale=20dans=20l'infobulle.=20fixes=20#108?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 .../agrometinfo/www/client/view/MapView.java  | 23 +++++++++++++++++--
 .../www/server/rs/IndicatorResource.java      |  6 +++--
 .../www/shared/dto/FeatureProperty.java       | 12 ++++++----
 3 files changed, 33 insertions(+), 8 deletions(-)

diff --git a/www-client/src/main/java/fr/agrometinfo/www/client/view/MapView.java b/www-client/src/main/java/fr/agrometinfo/www/client/view/MapView.java
index 9bb1d36..99ad507 100644
--- a/www-client/src/main/java/fr/agrometinfo/www/client/view/MapView.java
+++ b/www-client/src/main/java/fr/agrometinfo/www/client/view/MapView.java
@@ -22,6 +22,7 @@ import fr.agrometinfo.www.client.App;
 import fr.agrometinfo.www.client.event.FeatureSelectEvent;
 import fr.agrometinfo.www.client.event.FeatureSelectHandler;
 import fr.agrometinfo.www.client.event.MapClickEvent;
+import fr.agrometinfo.www.client.i18n.AppConstants;
 import fr.agrometinfo.www.client.i18n.MapConstants;
 import fr.agrometinfo.www.client.i18n.MapMessages;
 import fr.agrometinfo.www.client.presenter.MapPresenter;
@@ -121,6 +122,11 @@ public final class MapView extends HtmlContentBuilder<HTMLElement> implements Fe
      */
     private static final String TITLE = "title";
 
+    /**
+     * I18N constants.
+     */
+    private static final AppConstants APP_CSTS = GWT.create(AppConstants.class);
+
     /**
      * Add base layers (OSM, IGN, None).
      *
@@ -295,6 +301,11 @@ public final class MapView extends HtmlContentBuilder<HTMLElement> implements Fe
      */
     private List<ColorInterval> colorIntervals;
 
+    /**
+     * If indicator values are comparison to normal.
+     */
+    private boolean isComparisonToNormal;
+
     /**
      * Name of selected indicator.
      */
@@ -387,12 +398,17 @@ public final class MapView extends HtmlContentBuilder<HTMLElement> implements Fe
                     && !featureSelect.getFeatures().isEmpty() //
                     && featureSelect.getFeatures().item(0) != null) {
 
+                final String title;
+                if (isComparisonToNormal) {
+                    title = APP_CSTS.normalComparison() + "<br/>" + indicatorName;
+                } else {
+                    title = indicatorName;
+                }
                 final Feature feature = featureSelect.getFeatures().item(0);
                 final Date date = getPropertyAsDate(feature, FeatureProperty.DATE);
                 final Double value = getPropertyAsDouble(feature, FeatureProperty.VALUE);
                 final String praName = praNames.getOrDefault(feature.getId(), feature.getId() + "/" + praNames.size());
-                final String content = MSGS.popupContent(indicatorName, value, indicatorUnit, praName, periodStartDate,
-                        date);
+                final String content = MSGS.popupContent(title, value, indicatorUnit, praName, periodStartDate, date);
 
                 final Extent extent = feature.getGeometry().getExtent();
                 tooltip.setPosition(extent);
@@ -537,6 +553,8 @@ public final class MapView extends HtmlContentBuilder<HTMLElement> implements Fe
         if (propertiesFeature.getId() == null) {
             indicatorName = getProperty(propertiesFeature, FeatureProperty.INDICATOR_NAME);
             indicatorUnit = getProperty(propertiesFeature, FeatureProperty.INDICATOR_UNIT);
+            isComparisonToNormal = "true"
+                    .equalsIgnoreCase(getProperty(propertiesFeature, FeatureProperty.COMPARISON_TO_NORMAL));
             periodStartDate = getPropertyAsDate(propertiesFeature, FeatureProperty.PERIOD_FIRST_DAY);
 
             final Feature[] filtered = new Feature[allFeatures.length - 1];
@@ -563,6 +581,7 @@ public final class MapView extends HtmlContentBuilder<HTMLElement> implements Fe
         final org.geojson.Feature fakeFeature = list.remove(0);
         indicatorName = getProperty(fakeFeature, FeatureProperty.INDICATOR_NAME);
         indicatorUnit = getProperty(fakeFeature, FeatureProperty.INDICATOR_UNIT);
+        isComparisonToNormal = "true".equalsIgnoreCase(getProperty(fakeFeature, FeatureProperty.COMPARISON_TO_NORMAL));
         periodStartDate = getPropertyAsDate(fakeFeature, FeatureProperty.PERIOD_FIRST_DAY);
         final Feature[] features = list.toArray(new Feature[list.size()]);
         setFeatures(features);
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 b4a2137..14a3f8c 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
@@ -187,13 +187,15 @@ public class IndicatorResource implements IndicatorService {
      * @param indicator  indicator with metadata to add
      * @param year       to get the first day of indicator period
      * @param locale     locale of descriptions
+     * @param comparison if returned values are comparison to normal
      */
     private void addMetadata(final FeatureCollection collection, final Indicator indicator,
-            final Integer year, final Locale locale) {
+            final Integer year, final Locale locale, final boolean comparison) {
         final var periodFirstDay = getDate(year, indicator.getPeriod().getFirstDay());
         final Feature feature = new Feature();
         final MultiPolygon multiPolygon = new MultiPolygon();
         feature.setGeometry(multiPolygon);
+        feature.setProperty(FeatureProperty.COMPARISON_TO_NORMAL.toCamelCase(), String.valueOf(comparison));
         feature.setProperty(FeatureProperty.INDICATOR_NAME.toCamelCase(),
                 getTranslation(indicator.getDescriptions(), locale));
         feature.setProperty(FeatureProperty.INDICATOR_UNIT.toCamelCase(), indicator.getUnit());
@@ -442,7 +444,7 @@ public class IndicatorResource implements IndicatorService {
         if (indicator == null) {
             throwWebApplicationException(Response.Status.BAD_REQUEST, indicatorUid + " is unknown for " + periodCode);
         }
-        addMetadata(collection, indicator, year, locale);
+        addMetadata(collection, indicator, year, locale, Boolean.TRUE.equals(comparison));
         final LocalDate date = praDailyValueDao.findLastDate(indicator, year);
         if (date == null) {
             throwWebApplicationException(Response.Status.NOT_FOUND,
diff --git a/www-shared/src/main/java/fr/agrometinfo/www/shared/dto/FeatureProperty.java b/www-shared/src/main/java/fr/agrometinfo/www/shared/dto/FeatureProperty.java
index 04ea6ca..9a7ab74 100644
--- a/www-shared/src/main/java/fr/agrometinfo/www/shared/dto/FeatureProperty.java
+++ b/www-shared/src/main/java/fr/agrometinfo/www/shared/dto/FeatureProperty.java
@@ -8,13 +8,13 @@ package fr.agrometinfo.www.shared.dto;
  */
 public enum FeatureProperty {
     /**
-     * Feature property.
+     * If indicator values are comparison to normal.
      */
-    DATE,
+    COMPARISON_TO_NORMAL,
     /**
      * Feature property.
      */
-    VALUE,
+    DATE,
     /**
      * Collection property.
      */
@@ -26,7 +26,11 @@ public enum FeatureProperty {
     /**
      * Collection property.
      */
-    PERIOD_FIRST_DAY;
+    PERIOD_FIRST_DAY,
+    /**
+     * Feature property.
+     */
+    VALUE;
 
     /**
      * @return enum name as camel case format
-- 
GitLab