From a4d2e74e8baaad6608b451490aa96a1aa5ca4b59 Mon Sep 17 00:00:00 2001
From: philippe tcheriatinsky <philippe.tcherniatinsky@inrae.fr>
Date: Mon, 17 Jun 2024 10:08:28 +0200
Subject: [PATCH] correction #227

---
 .../data/read/query/ComponentOrderBy.java     | 48 ++++++++++++++++
 .../fr/inra/oresing/rest/DataCsvBuilder.java  | 56 ++++++++++---------
 .../fr/inra/oresing/rest/OreSiService.java    |  1 +
 ui/package-lock.json                          |  2 +-
 ui/package.json                               |  2 +-
 5 files changed, 80 insertions(+), 29 deletions(-)

diff --git a/src/main/java/fr/inra/oresing/domain/data/read/query/ComponentOrderBy.java b/src/main/java/fr/inra/oresing/domain/data/read/query/ComponentOrderBy.java
index 51e9e95bf..ee347c209 100644
--- a/src/main/java/fr/inra/oresing/domain/data/read/query/ComponentOrderBy.java
+++ b/src/main/java/fr/inra/oresing/domain/data/read/query/ComponentOrderBy.java
@@ -1,7 +1,21 @@
 package fr.inra.oresing.domain.data.read.query;
 
+import fr.inra.oresing.domain.application.configuration.ComponentDescription;
+import fr.inra.oresing.domain.application.configuration.StandardDataDescription;
+import fr.inra.oresing.domain.application.configuration.checker.ReferenceChecker;
+import fr.inra.oresing.domain.checker.type.DateType;
+import fr.inra.oresing.domain.checker.type.FieldType;
+import fr.inra.oresing.domain.checker.type.StringType;
 import fr.inra.oresing.domain.exceptions.data.data.BadDownloadDatasetQuery;
 import fr.inra.oresing.persistence.DataRepository;
+import fr.inra.oresing.persistence.DataRow;
+import org.springframework.context.i18n.LocaleContextHolder;
+
+import java.time.LocalDateTime;
+import java.time.format.DateTimeFormatter;
+import java.util.Optional;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
 
 import static fr.inra.oresing.domain.exceptions.data.data.BadDownloadDatasetQuery.MISSING_COMPONENT_KEY_FOR_SEARCH;
 
@@ -16,4 +30,38 @@ public record ComponentOrderBy(String componentKey, DataRepository.Order order,
         this.componentKey = componentKey;
         this.order = order == null ? DataRepository.Order.ASC : order;
     }
+
+
+    public String toValue(DataRepository dataRepository, DataRow dataRow, StandardDataDescription dataDescription) {
+        String componentKey = componentKey();
+        FieldType fieldType = dataRow.getValues().get(componentKey);
+        final String value = fieldType.toString();
+        return switch (sqlType()) {
+            case null -> "";
+            case ComponentDateType componentDateType -> {
+                Matcher matcher = Pattern.compile(DateType.PATTERN_DATE_REGEXP_FIND_DATE).matcher(((StringType) fieldType).getValue());
+                if (matcher.matches()) {
+                    yield DateTimeFormatter.ofPattern(componentDateType.format()).format(LocalDateTime.parse(matcher.group(1)));
+                }
+                yield "";
+            }
+            case ComponentReferenceType componentReferenceType -> {
+                yield Optional.ofNullable(dataDescription)
+                        .map(StandardDataDescription::componentDescriptions)
+                        .map(components -> components.get(componentKey()))
+                        .map(ComponentDescription::checker)
+                        .filter(ReferenceChecker.class::isInstance)
+                        .map(ReferenceChecker.class::cast)
+                        .map(ReferenceChecker::refType)
+                        .map(referencetype -> Optional.of(dataRepository)
+                                .map(repository -> repository.findDisplayByNaturalKey(referencetype))
+                                .map(map -> map.get(value))
+                                .map(map -> map.get(LocaleContextHolder.getLocale().getLanguage()))
+                                .orElse(null)
+                        )
+                        .orElse(value);
+            }
+            default -> value;
+        };
+    }
 }
diff --git a/src/main/java/fr/inra/oresing/rest/DataCsvBuilder.java b/src/main/java/fr/inra/oresing/rest/DataCsvBuilder.java
index 5a2aba093..9ec199f88 100644
--- a/src/main/java/fr/inra/oresing/rest/DataCsvBuilder.java
+++ b/src/main/java/fr/inra/oresing/rest/DataCsvBuilder.java
@@ -1,10 +1,10 @@
 package fr.inra.oresing.rest;
 
-import com.google.common.collect.ImmutableList;
 import com.google.common.collect.ImmutableMap;
 import com.google.common.collect.ImmutableSet;
 import com.opencsv.CSVWriter;
 import fr.inra.oresing.domain.application.configuration.ComponentDescription;
+import fr.inra.oresing.domain.application.configuration.Internationalizations;
 import fr.inra.oresing.domain.application.configuration.StandardDataDescription;
 import fr.inra.oresing.domain.checker.type.*;
 import fr.inra.oresing.domain.data.DataValue;
@@ -40,6 +40,8 @@ public class DataCsvBuilder {
     private DataService referenceService;
     private Flux<DataRow> datas;
     private ZipOutputStream zipOutputStream;
+    private Locale locale;
+    Function<String, Internationalizations.InternationalizationData> internationalizationData;
 
     public DataCsvBuilder(final BiFunction<String, String, DataImporterContext> buildReferenceImporterContext) {
         super();
@@ -50,7 +52,8 @@ public class DataCsvBuilder {
         return new DataCsvBuilder(referenceImporterContextBuilder);
     }
 
-    private static void addDatasToZip(StandardDataDescription dataDescription, final DataRow dataRow,
+    private static void addDatasToZip(DataRepository dataRepository, StandardDataDescription dataDescription,
+                                      final DataRow dataRow,
                                       final AtomicLong counter,
                                       final CSVWriter writer,
                                       final UUIDsfromData uuidsfromData,
@@ -72,29 +75,21 @@ public class DataCsvBuilder {
                 .flatMap(List::stream)
                 .collect(Collectors.toSet()));
         final Map<String, FieldType> record = dataRow.getValues();
+
+        Function<ComponentOrderBy, String> toValue = componentOrderBy -> componentOrderBy.toValue(dataRepository, dataRow, dataDescription);
         List<String> rowAsRecord = columns.values().stream()
-                .map(ComponentOrderBy::componentKey)
-                .map(dataRow.getValues()::get)
-                .map(DataCsvBuilder::getValue)
-                /*.map(column->{
-                    FieldType fieldType = dataRow.getValues().get(column.componentKey());
-                    ComponentOrderBy componentOrderBy = columns.get(column.componentKey());
-                    return getValue(fieldType);
-                })*/
+                .map(toValue)
                 .toList();
-        /*final ImmutableList<String> rowAsRecord = columns.values().stream()
-                .map(component -> dataDescription.getTypeForComponentKey(component.componentKey()))
-                .map(fieldType -> DateType.sorteableDateToFormattedDate(fieldType.toString()))
-                .collect(ImmutableList.toImmutableList());*/
         try {
             writer.writeNext(rowAsRecord.toArray(new String[]{}));
             writer.flush();
-        }catch (Exception e){
+        } catch (Exception e) {
             System.out.println(e.getMessage());
         }
     }
-    public static String getValue(FieldType fieldType){
-        return switch(fieldType){
+
+    public static String getValue(FieldType fieldType) {
+        return switch (fieldType) {
             case DateType dateType -> DateType.sortableDateToFormattedDate(dateType.toString());
             default -> fieldType.toString();
         };
@@ -104,6 +99,7 @@ public class DataCsvBuilder {
         this.downloadDatasetQuery = downloadDatasetQuery;
         return this;
     }
+
     public DataCsvBuilder onRepositories(DataRepository dataRepository, AdditionalFileRepository additionalFileRepository) {
         this.dataRepository = dataRepository;
         this.additionalFileRepository = additionalFileRepository;
@@ -135,18 +131,18 @@ public class DataCsvBuilder {
         final UUIDsfromData uuiDsfromData;
         final AtomicLong counter = new AtomicLong();
         String language = LocaleContextHolder.getLocale().getLanguage();
-       
-       Function<String, String> componentNameToExportHeader = componentName -> data
-               .map(StandardDataDescription::componentDescriptions)
-               .map(descriptions-> descriptions.get(componentName))
-               .map(ComponentDescription::getExportHeaderName)
-               .orElse(null);
+
+        Function<String, String> componentNameToExportHeader = componentName -> data
+                .map(StandardDataDescription::componentDescriptions)
+                .map(descriptions -> descriptions.get(componentName))
+                .map(ComponentDescription::getExportHeaderName)
+                .orElse(null);
         try {
             Set<String> componentSelects = Optional.ofNullable(downloadDatasetQuery)
                     .map(DownloadDatasetQuery::componentSelects)
                     .orElseGet(ImmutableSet::of);
-            if(componentSelects.isEmpty()){
-                componentSelects= data
+            if (componentSelects.isEmpty()) {
+                componentSelects = data
                         .map(StandardDataDescription::componentDescriptions)
                         .map(Map::keySet)
                         .orElseGet(Set::of);
@@ -171,7 +167,14 @@ public class DataCsvBuilder {
             datas
                     .subscribe(dataRow -> {
                         try {
-                            addDatasToZip(dataDescription, dataRow, counter, writer, uuiDsfromData, columns);
+                            addDatasToZip(
+                                    dataRepository,
+                                    dataDescription,
+                                    dataRow,
+                                    counter,
+                                    writer,
+                                    uuiDsfromData,
+                                    columns);
                         } catch (final IOException e) {
                             throw new RuntimeException("can't flush", e);
                         }
@@ -261,5 +264,4 @@ public class DataCsvBuilder {
         this.zipOutputStream = zipOutputStream;
         return this;
     }
-
 }
\ No newline at end of file
diff --git a/src/main/java/fr/inra/oresing/rest/OreSiService.java b/src/main/java/fr/inra/oresing/rest/OreSiService.java
index 74ffab825..03557a811 100644
--- a/src/main/java/fr/inra/oresing/rest/OreSiService.java
+++ b/src/main/java/fr/inra/oresing/rest/OreSiService.java
@@ -60,6 +60,7 @@ import org.jetbrains.annotations.NotNull;
 import org.springframework.beans.factory.BeanFactory;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
+import org.springframework.context.i18n.LocaleContextHolder;
 import org.springframework.core.io.Resource;
 import org.springframework.dao.DataIntegrityViolationException;
 import org.springframework.jdbc.BadSqlGrammarException;
diff --git a/ui/package-lock.json b/ui/package-lock.json
index 9d637de66..1e9d5fc9e 100644
--- a/ui/package-lock.json
+++ b/ui/package-lock.json
@@ -25,7 +25,7 @@
         "sass-loader": "^14.2.1",
         "sortablejs": "^1.14.0",
         "vee-validate": "^3.4.15",
-        "vue": "^2.7.14",
+        "vue": "^2.7.16",
         "vue-chartjs": "^3.5.1",
         "vue-class-component": "^7.2.6",
         "vue-i18n": "^8.28.2",
diff --git a/ui/package.json b/ui/package.json
index 9687d5033..5b496b48d 100644
--- a/ui/package.json
+++ b/ui/package.json
@@ -28,7 +28,7 @@
     "sass-loader": "^14.2.1",
     "sortablejs": "^1.14.0",
     "vee-validate": "^3.4.15",
-    "vue": "^2.7.14",
+    "vue": "^2.7.16",
     "vue-chartjs": "^3.5.1",
     "vue-class-component": "^7.2.6",
     "vue-i18n": "^8.28.2",
-- 
GitLab