From d66352bd78c349339f92b4bf2727df3417f70d01 Mon Sep 17 00:00:00 2001
From: Olivier Maury <Olivier.Maury@inrae.fr>
Date: Tue, 5 Mar 2024 18:23:24 +0100
Subject: [PATCH] Ajouter la date de calcul dans le cartouche. fixes #61

---
 .../agrometinfo/www/client/i18n/AppMessages.java  |  7 +++++++
 .../www/client/presenter/MapPresenter.java        | 15 +++++++++++++++
 .../www/client/ui/map/CanvasTitle.java            | 10 +++++-----
 .../www/client/i18n/AppMessages_fr.properties     |  1 +
 .../www/server/rs/IndicatorResource.java          |  7 +++++++
 .../www/server/service/CacheService.java          |  2 ++
 .../www/shared/service/IndicatorService.java      | 12 ++++++++++++
 7 files changed, 49 insertions(+), 5 deletions(-)

diff --git a/www-client/src/main/java/fr/agrometinfo/www/client/i18n/AppMessages.java b/www-client/src/main/java/fr/agrometinfo/www/client/i18n/AppMessages.java
index f6cbd88..0eb4dd5 100644
--- a/www-client/src/main/java/fr/agrometinfo/www/client/i18n/AppMessages.java
+++ b/www-client/src/main/java/fr/agrometinfo/www/client/i18n/AppMessages.java
@@ -55,6 +55,13 @@ public interface AppMessages extends Messages {
             + "{0} ({1}) and the normal value at {2} in {3}")
     String comparedValue(String indicator, String period, String region, Integer year);
 
+    /**
+     * @param date date of last modification
+     * @return translation
+     */
+    @DefaultMessage("Computed on {0,date,long}")
+    String computedOn(Date date);
+
     /**
      * @param details failure details
      * @return translation
diff --git a/www-client/src/main/java/fr/agrometinfo/www/client/presenter/MapPresenter.java b/www-client/src/main/java/fr/agrometinfo/www/client/presenter/MapPresenter.java
index aa6c13d..0a74e37 100644
--- a/www-client/src/main/java/fr/agrometinfo/www/client/presenter/MapPresenter.java
+++ b/www-client/src/main/java/fr/agrometinfo/www/client/presenter/MapPresenter.java
@@ -14,6 +14,7 @@ import elemental2.dom.HTMLDivElement;
 import fr.agrometinfo.www.client.App;
 import fr.agrometinfo.www.client.event.FeatureSelectEvent;
 import fr.agrometinfo.www.client.i18n.AppConstants;
+import fr.agrometinfo.www.client.i18n.AppMessages;
 import fr.agrometinfo.www.client.util.ApplicationUtils;
 import fr.agrometinfo.www.client.view.BaseView;
 import fr.agrometinfo.www.client.view.MapView;
@@ -21,6 +22,7 @@ import fr.agrometinfo.www.shared.dto.ChoiceDTO;
 import fr.agrometinfo.www.shared.dto.FeatureLevel;
 import fr.agrometinfo.www.shared.dto.IndicatorDTO;
 import fr.agrometinfo.www.shared.service.IndicatorService;
+import fr.agrometinfo.www.shared.service.IndicatorServiceFactory;
 
 /**
  * Presenter for the embedded map with indicator results.
@@ -60,6 +62,11 @@ public final class MapPresenter implements Presenter {
     private static final String VALUES_URL = ApplicationUtils.getApplicationUrl() + "/rs/"
             + IndicatorService.PATH + "/" + IndicatorService.PATH_VALUES;
 
+    /**
+     * I18N messages.
+     */
+    private static final AppMessages MSGS = GWT.create(AppMessages.class);
+
     /**
      * Container for the map.
      */
@@ -70,6 +77,11 @@ public final class MapPresenter implements Presenter {
      */
     private View view;
 
+    /**
+     * Date of last modification of indicators values in database.
+     */
+    private String lastModification = "";
+
     /**
      * Load indicator values on the map.
      *
@@ -88,6 +100,7 @@ public final class MapPresenter implements Presenter {
         titleLines.add(indicator.getDescription() + "(" + indicator.getUnit() + ")");
         titleLines.add(regionName);
         titleLines.add(choice.getYear().toString());
+        titleLines.add(lastModification);
         final JsRestfulRequestFactory factory = new JsRestfulRequestFactory();
         final RestfulRequest request = factory.get(VALUES_URL);
         request.addQueryParam("indicator", choice.getIndicator());
@@ -124,6 +137,8 @@ public final class MapPresenter implements Presenter {
     @Override
     public void start() {
         GWT.log("MapPresenter.start()");
+        IndicatorServiceFactory.INSTANCE.getLastModification()
+                .onSuccess(date -> lastModification = MSGS.computedOn(date)).send();
         view = new MapView(container);
         view.setPresenter(this);
         view.init();
diff --git a/www-client/src/main/java/fr/agrometinfo/www/client/ui/map/CanvasTitle.java b/www-client/src/main/java/fr/agrometinfo/www/client/ui/map/CanvasTitle.java
index b8bb994..54ee830 100644
--- a/www-client/src/main/java/fr/agrometinfo/www/client/ui/map/CanvasTitle.java
+++ b/www-client/src/main/java/fr/agrometinfo/www/client/ui/map/CanvasTitle.java
@@ -62,7 +62,7 @@ public final class CanvasTitle extends CanvasWidget {
     /**
      * Keep space around logo, scale and text.
      */
-    private static final int PADDING = 3;
+    private static final int PADDING = 5;
 
     /**
      * @return total width of title box, PADDING included, limited to screen width.
@@ -114,7 +114,7 @@ public final class CanvasTitle extends CanvasWidget {
         int i = 0;
         final int x = PADDING + LOGO_WIDTH + (getWidth() - LOGO_WIDTH) / 2;
         for (final String line : lines) {
-            ctx.fillText(line, x, (double) LINE_HEIGHT + LINE_HEIGHT * i);
+            ctx.fillText(line, x, (double) PADDING + LINE_HEIGHT + LINE_HEIGHT * i);
             i++;
         }
         this.drawScale();
@@ -130,7 +130,7 @@ public final class CanvasTitle extends CanvasWidget {
         final int totalScaleHeight = 3 * LINE_HEIGHT;
         final double height = Math.max(//
                 LINE_HEIGHT * nbOfLines + 2d * BORDER_SIZE, //
-                LOGO_WIDTH / LOGO_SIZE_RATIO + BORDER_SIZE + totalScaleHeight);
+                BORDER_SIZE + PADDING + LOGO_WIDTH / LOGO_SIZE_RATIO + PADDING + totalScaleHeight + BORDER_SIZE);
         ctx.setFillStyle("#818181");
         ctx.fillRect(0, 0, getWidth(), height + 2 * PADDING);
         ctx.setFillStyle("#ffffff");
@@ -158,9 +158,9 @@ public final class CanvasTitle extends CanvasWidget {
     private void drawScale() {
         final int nbOfIntervals = colorIntervals.size();
         final double logoHeight = LOGO_WIDTH / LOGO_SIZE_RATIO;
-        final int cellWidth = getWidth() / (nbOfIntervals + 1);
+        final int cellWidth = (int) (getWidth() / (nbOfIntervals + 0.5));
         final int cellHeight = 2 * LINE_HEIGHT;
-        final double cellTop = logoHeight + BORDER_SIZE + PADDING;
+        final double cellTop = BORDER_SIZE + PADDING + logoHeight + PADDING;
         final double cellBottom = cellTop + cellHeight;
         final double labelTop = cellBottom + LINE_HEIGHT;
         final Context2d ctx = getCanvas().getContext2d();
diff --git a/www-client/src/main/resources/fr/agrometinfo/www/client/i18n/AppMessages_fr.properties b/www-client/src/main/resources/fr/agrometinfo/www/client/i18n/AppMessages_fr.properties
index 2505c78..9fdfc3c 100644
--- a/www-client/src/main/resources/fr/agrometinfo/www/client/i18n/AppMessages_fr.properties
+++ b/www-client/src/main/resources/fr/agrometinfo/www/client/i18n/AppMessages_fr.properties
@@ -4,6 +4,7 @@ averageValue = Valeur moyenne de l’indicateur {0} ({1}) en {2} sur {3}
 cell = Maille n°{0}
 chartSubtitle = {0,date,medium}. Unité : {1}
 comparedValue = Écart moyen de la valeur de l’indicateur {0} ({1}) en {2} sur {3}
+computedOn = Calculé le {0,date,long}
 failureResponse = La communication avec le serveur a échoué : {0}
 failureStatusCode = Code d’état HTTP : {0}
 nbOfIndicatorPeriods[\=0] = Aucune période.
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 ef39990..386c66a 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
@@ -200,6 +200,13 @@ public class IndicatorResource implements IndicatorService {
         }
     }
 
+    @GET
+    @Path(IndicatorService.PATH_LAST_MODIFICATION)
+    @Override
+    public Date getLastModification() {
+        return DateUtils.toDate(cacheService.getLastModification().toLocalDate());
+    }
+
     /**
      * @return indicator categories with their indicators
      */
diff --git a/www-server/src/main/java/fr/agrometinfo/www/server/service/CacheService.java b/www-server/src/main/java/fr/agrometinfo/www/server/service/CacheService.java
index 8c038d9..f34521e 100644
--- a/www-server/src/main/java/fr/agrometinfo/www/server/service/CacheService.java
+++ b/www-server/src/main/java/fr/agrometinfo/www/server/service/CacheService.java
@@ -22,6 +22,7 @@ import jakarta.ws.rs.core.EntityTag;
 import jakarta.ws.rs.core.HttpHeaders;
 import jakarta.ws.rs.core.Request;
 import jakarta.ws.rs.ext.RuntimeDelegate;
+import lombok.Getter;
 import lombok.Setter;
 import lombok.extern.log4j.Log4j2;
 
@@ -62,6 +63,7 @@ public class CacheService {
     /**
      * Date of last modification of indicators values in database.
      */
+    @Getter
     @Setter
     @Inject
     @Named("lastModification")
diff --git a/www-shared/src/main/java/fr/agrometinfo/www/shared/service/IndicatorService.java b/www-shared/src/main/java/fr/agrometinfo/www/shared/service/IndicatorService.java
index cd3d633..711b516 100644
--- a/www-shared/src/main/java/fr/agrometinfo/www/shared/service/IndicatorService.java
+++ b/www-shared/src/main/java/fr/agrometinfo/www/shared/service/IndicatorService.java
@@ -1,5 +1,6 @@
 package fr.agrometinfo.www.shared.service;
 
+import java.util.Date;
 import java.util.List;
 import java.util.Map;
 
@@ -48,6 +49,17 @@ public interface IndicatorService {
      * Path for {@link IndicatorService#getYears()}.
      */
     String PATH_YEARS = "years";
+    /**
+     * Path for {@link IndicatorService#getLastModification()}.
+     */
+    String PATH_LAST_MODIFICATION = "last_modification";
+
+    /**
+     * @return Date of last modification of indicators values in database.
+     */
+    @GET
+    @Path(PATH_LAST_MODIFICATION)
+    Date getLastModification();
 
     /**
      * @return list of years of computed indicators.
-- 
GitLab