From eb70c59e4169342b1f356f07d4fcb86c059ed438 Mon Sep 17 00:00:00 2001
From: Olivier Maury <Olivier.Maury@inrae.fr>
Date: Tue, 4 Jun 2024 10:49:45 +0200
Subject: [PATCH] Mise en forme par Eclipse

---
 .../seasonhandler/MainConfiguration.java      | 22 ++++++-------
 .../seasonhandler/cmd/MainCommand.java        |  1 +
 .../dao/DailyvalueDaoHibernate.java           |  2 +-
 .../seasonhandler/di/DiHelper.java            | 26 ++++++++++-----
 .../error/AgroMetInfoException.java           |  5 +--
 .../jms/IntegrationDoneProducer.java          |  4 ++-
 .../jms/IntegrationDoneReceiver.java          | 33 +++++++++++--------
 .../jms/SimulationDoneReceiver.java           |  1 +
 .../error/AgroMetInfoErrorTypeTest.java       |  2 +-
 9 files changed, 59 insertions(+), 37 deletions(-)

diff --git a/src/main/java/fr/agrometinfo/seasonhandler/MainConfiguration.java b/src/main/java/fr/agrometinfo/seasonhandler/MainConfiguration.java
index 5a78bbe..ee523f1 100644
--- a/src/main/java/fr/agrometinfo/seasonhandler/MainConfiguration.java
+++ b/src/main/java/fr/agrometinfo/seasonhandler/MainConfiguration.java
@@ -207,16 +207,6 @@ public class MainConfiguration {
         return properties.getProperty(key.getKey());
     }
 
-    /**
-     * Searches for the property with the specified key in the config.
-     *
-     * @param key the property key.
-     * @return the value in this property list with the specified key value.
-     */
-    public int getInt(final Property key) {
-        return Integer.parseInt(get(key));
-    }
-
     /**
      * Searches for the property with the specified key and suffix in the config.
      *
@@ -255,6 +245,16 @@ public class MainConfiguration {
         return map;
     }
 
+    /**
+     * Searches for the property with the specified key in the config.
+     *
+     * @param key the property key.
+     * @return the value in this property list with the specified key value.
+     */
+    public int getInt(final Property key) {
+        return Integer.parseInt(get(key));
+    }
+
     /**
      * @param property a property
      * @return all suffixes of configured keys
@@ -350,7 +350,7 @@ public class MainConfiguration {
      */
     private void initEmptyCacheRestUrls() {
         getKeySuffixes(Property.EMPTY_CACHE_REST_URL).stream() //
-                .map(suffix -> get(Property.SIMULATION_PATH, suffix)) //
+                .map(suffix -> get(Property.EMPTY_CACHE_REST_URL, suffix)) //
                 .filter(v -> v != null && !v.isBlank()) //
                 .forEach(emptyCacheRestUrls::add);
     }
diff --git a/src/main/java/fr/agrometinfo/seasonhandler/cmd/MainCommand.java b/src/main/java/fr/agrometinfo/seasonhandler/cmd/MainCommand.java
index a1da118..abf42d1 100644
--- a/src/main/java/fr/agrometinfo/seasonhandler/cmd/MainCommand.java
+++ b/src/main/java/fr/agrometinfo/seasonhandler/cmd/MainCommand.java
@@ -142,6 +142,7 @@ public final class MainCommand implements Callable<Integer> {
 
             final IntegrationDoneReceiver integrationDoneReceiver = new IntegrationDoneReceiver(jmsContext,
                     integrationDoneQueue);
+            diHelper.inject(integrationDoneReceiver);
             integrationDoneReceiver.run();
 
             jmsContext.start();
diff --git a/src/main/java/fr/agrometinfo/seasonhandler/dao/DailyvalueDaoHibernate.java b/src/main/java/fr/agrometinfo/seasonhandler/dao/DailyvalueDaoHibernate.java
index 0b91307..cf6b234 100644
--- a/src/main/java/fr/agrometinfo/seasonhandler/dao/DailyvalueDaoHibernate.java
+++ b/src/main/java/fr/agrometinfo/seasonhandler/dao/DailyvalueDaoHibernate.java
@@ -23,7 +23,7 @@ public class DailyvalueDaoHibernate extends DaoHibernate implements DailyvalueDa
     @Override
     public final void insertFromTable(@NonNull final String tableName) {
         doInJpaTransaction(em -> {
-            var sql = String.format("SELECT 1 FROM insert_indicators('%s')", tableName);
+            final var sql = String.format("SELECT 1 FROM insert_indicators('%s')", tableName);
             em.createNativeQuery(sql).getSingleResult();
         });
     }
diff --git a/src/main/java/fr/agrometinfo/seasonhandler/di/DiHelper.java b/src/main/java/fr/agrometinfo/seasonhandler/di/DiHelper.java
index 8ddd7b0..83de89f 100644
--- a/src/main/java/fr/agrometinfo/seasonhandler/di/DiHelper.java
+++ b/src/main/java/fr/agrometinfo/seasonhandler/di/DiHelper.java
@@ -23,6 +23,7 @@ import fr.agrometinfo.seasonhandler.dao.AmiSimulationDao;
 import fr.agrometinfo.seasonhandler.dao.AmiSimulationDaoHibernate;
 import fr.agrometinfo.seasonhandler.dao.DailyvalueDao;
 import fr.agrometinfo.seasonhandler.dao.DailyvalueDaoHibernate;
+import fr.agrometinfo.seasonhandler.jms.IntegrationDoneReceiver;
 import fr.agrometinfo.seasonhandler.jms.SafranReceiver;
 import fr.agrometinfo.seasonhandler.jms.SimulationDoneReceiver;
 import fr.inrae.agroclim.indicators.resources.I18n;
@@ -68,6 +69,15 @@ public final class DiHelper {
      */
     private final MainConfiguration config;
 
+    /**
+     * Inject dependencies in receiver.
+     *
+     * @param receiver receiver.
+     */
+    public void inject(final IntegrationDoneReceiver receiver) {
+        receiver.setConfig(config);
+    }
+
     /**
      * Inject dependencies in receiver.
      *
@@ -100,6 +110,14 @@ public final class DiHelper {
         receiver.setSimulationSoilDao(proviceSimulationSoilDao(pm));
     }
 
+    /**
+     * @param pm JPA persistence manager
+     * @return DAO for {@link SimulationSoilDao}.
+     */
+    private SimulationSoilDao proviceSimulationSoilDao(final PersistenceManager pm) {
+        return new SimulationSoilDaoHibernate(pm, config.getDatabaseRoles());
+    }
+
     /**
      * @param pm JPA persistence manager
      * @return DAO for AmiSimulationDao.
@@ -159,14 +177,6 @@ public final class DiHelper {
         return new SimulationResultDaoHibernate(pm, config.getDatabaseRoles());
     }
 
-    /**
-     * @param pm JPA persistence manager
-     * @return DAO for {@link SimulationSoilDao}.
-     */
-    private SimulationSoilDao proviceSimulationSoilDao(final PersistenceManager pm) {
-        return new SimulationSoilDaoHibernate(pm, config.getDatabaseRoles());
-    }
-
     /**
      * @param pm JPA persistence manager
      * @return DAO for varietal parameters.
diff --git a/src/main/java/fr/agrometinfo/seasonhandler/error/AgroMetInfoException.java b/src/main/java/fr/agrometinfo/seasonhandler/error/AgroMetInfoException.java
index 0e40509..7d561f8 100644
--- a/src/main/java/fr/agrometinfo/seasonhandler/error/AgroMetInfoException.java
+++ b/src/main/java/fr/agrometinfo/seasonhandler/error/AgroMetInfoException.java
@@ -1,11 +1,12 @@
 package fr.agrometinfo.seasonhandler.error;
 
+import java.io.Serializable;
+import java.util.List;
+
 import fr.agrometinfo.seasonhandler.di.DiHelper;
 import fr.inrae.agroclim.indicators.exception.ErrorMessage;
 import fr.inrae.agroclim.indicators.exception.ErrorMessageException;
 import fr.inrae.agroclim.indicators.exception.ErrorType;
-import java.io.Serializable;
-import java.util.List;
 
 /**
  * An exception with {@link ErrorMessage} to describe the error in the handler to the user.
diff --git a/src/main/java/fr/agrometinfo/seasonhandler/jms/IntegrationDoneProducer.java b/src/main/java/fr/agrometinfo/seasonhandler/jms/IntegrationDoneProducer.java
index c0a4373..6fbb1fc 100644
--- a/src/main/java/fr/agrometinfo/seasonhandler/jms/IntegrationDoneProducer.java
+++ b/src/main/java/fr/agrometinfo/seasonhandler/jms/IntegrationDoneProducer.java
@@ -2,9 +2,11 @@ package fr.agrometinfo.seasonhandler.jms;
 
 import java.time.LocalDateTime;
 import java.time.format.DateTimeFormatter;
+
 import javax.jms.Destination;
 import javax.jms.JMSContext;
 import javax.jms.JMSProducer;
+
 import lombok.extern.log4j.Log4j2;
 
 /**
@@ -37,7 +39,7 @@ public final class IntegrationDoneProducer {
     }
 
     public void send() {
-        var body = LocalDateTime.now().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME);
+        final var body = LocalDateTime.now().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME);
         producer.send(destination, body);
     }
 }
diff --git a/src/main/java/fr/agrometinfo/seasonhandler/jms/IntegrationDoneReceiver.java b/src/main/java/fr/agrometinfo/seasonhandler/jms/IntegrationDoneReceiver.java
index 2a3f07c..f86b4f9 100644
--- a/src/main/java/fr/agrometinfo/seasonhandler/jms/IntegrationDoneReceiver.java
+++ b/src/main/java/fr/agrometinfo/seasonhandler/jms/IntegrationDoneReceiver.java
@@ -1,12 +1,12 @@
 package fr.agrometinfo.seasonhandler.jms;
 
-import fr.agrometinfo.seasonhandler.MainConfiguration;
 import java.io.IOException;
 import java.net.URI;
 import java.net.http.HttpClient;
 import java.net.http.HttpRequest;
 import java.net.http.HttpResponse;
 import java.time.LocalDateTime;
+
 import javax.jms.Destination;
 import javax.jms.JMSConsumer;
 import javax.jms.JMSContext;
@@ -14,9 +14,12 @@ import javax.jms.JMSException;
 import javax.jms.Message;
 import javax.jms.MessageListener;
 import javax.ws.rs.core.Response;
+
+import org.apache.activemq.artemis.jms.client.ActiveMQMessage;
+
+import fr.agrometinfo.seasonhandler.MainConfiguration;
 import lombok.Setter;
 import lombok.extern.log4j.Log4j2;
-import org.apache.activemq.artemis.jms.client.ActiveMQMessage;
 
 /**
  * JMS message receiver to empty caches in the web apps.
@@ -58,20 +61,24 @@ public final class IntegrationDoneReceiver implements MessageListener, Runnable
             try {
                 final String body = msg.getBody(String.class);
                 LOGGER.info("Body from received message: {}", body);
-                var parsed = LocalDateTime.parse(body);
-                if (lastDateTime == null || (parsed != null && parsed.isAfter(lastDateTime))) {
+                final var parsed = LocalDateTime.parse(body);
+                if (lastDateTime == null || parsed != null && parsed.isAfter(lastDateTime)) {
                     // empty cache
-                    HttpClient client = HttpClient.newHttpClient();
+                    final HttpClient client = HttpClient.newHttpClient();
                     config.getEmptyCacheRestUrls().forEach(url -> {
-                    HttpRequest request = HttpRequest.newBuilder().uri(URI.create(url)).build();
-                    try {
-                        HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
-                        if (response.statusCode() != Response.Status.OK.getStatusCode()) {
-                            LOGGER.error("AgroMetInfo did not response OK at {}: {}", url, response);
+                        LOGGER.trace("calling {}", url);
+                        try {
+                            final var request = HttpRequest.newBuilder().uri(URI.create(url)).build();
+                            final var response = client.send(request, HttpResponse.BodyHandlers.ofString());
+                            if (response.statusCode() != Response.Status.OK.getStatusCode()
+                                    && response.statusCode() != Response.Status.NO_CONTENT.getStatusCode()) {
+                                LOGGER.error("AgroMetInfo did not response OK at {}: {}", url, response);
+                            }
+                        } catch (final IllegalArgumentException ex) {
+                            LOGGER.error("Wrong AgroMetInfo REST end point {}!", url, ex);
+                        } catch (IOException | InterruptedException ex) {
+                            LOGGER.error("Something went wrong when calling AgroMetInfo REST end point {}!", url, ex);
                         }
-                    } catch (IOException | InterruptedException ex) {
-                        LOGGER.error("Something went wrong when calling AgroMetInfo REST end point {}!", url, ex);
-                    }
                     });
                     lastDateTime = LocalDateTime.now();
                 }
diff --git a/src/main/java/fr/agrometinfo/seasonhandler/jms/SimulationDoneReceiver.java b/src/main/java/fr/agrometinfo/seasonhandler/jms/SimulationDoneReceiver.java
index 71dd4af..34a2d47 100644
--- a/src/main/java/fr/agrometinfo/seasonhandler/jms/SimulationDoneReceiver.java
+++ b/src/main/java/fr/agrometinfo/seasonhandler/jms/SimulationDoneReceiver.java
@@ -19,6 +19,7 @@ import fr.inrae.agroclim.season.core.dao.SimulationResultDao;
 import fr.inrae.agroclim.season.core.dao.SimulationSoilDao;
 import fr.inrae.agroclim.season.core.model.Simulation;
 import fr.inrae.agroclim.season.core.model.SimulationError;
+import fr.inrae.agroclim.season.core.model.SimulationSoil;
 import lombok.Setter;
 import lombok.extern.log4j.Log4j2;
 
diff --git a/src/test/java/fr/agrometinfo/seasonhandler/error/AgroMetInfoErrorTypeTest.java b/src/test/java/fr/agrometinfo/seasonhandler/error/AgroMetInfoErrorTypeTest.java
index 15696f0..4a16b32 100644
--- a/src/test/java/fr/agrometinfo/seasonhandler/error/AgroMetInfoErrorTypeTest.java
+++ b/src/test/java/fr/agrometinfo/seasonhandler/error/AgroMetInfoErrorTypeTest.java
@@ -1,6 +1,5 @@
 package fr.agrometinfo.seasonhandler.error;
 
-import fr.agrometinfo.seasonhandler.di.DiHelper;
 import static org.junit.jupiter.api.Assertions.assertTrue;
 
 import java.util.List;
@@ -11,6 +10,7 @@ import java.util.Set;
 import org.junit.jupiter.params.ParameterizedTest;
 import org.junit.jupiter.params.provider.MethodSource;
 
+import fr.agrometinfo.seasonhandler.di.DiHelper;
 import fr.inrae.agroclim.indicators.exception.ErrorType;
 import fr.inrae.agroclim.indicators.util.ErrorTypeUtils;
 
-- 
GitLab