From 23a46b4be35277e06ec89f48730eeb694e686be8 Mon Sep 17 00:00:00 2001
From: Kenmegne <stephanie.kenmegne@gmail.com>
Date: Thu, 18 Jun 2026 15:40:06 +0000
Subject: [PATCH] add fdx-commons and fdx-consultation

---
 fdx-commons/fdxcommons-tools/src/main/java/com/megatim/fdxcommons/tools/database/queries/metadata/JsonDataRowToUpdate.java |  153 +++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 153 insertions(+), 0 deletions(-)

diff --git a/fdx-commons/fdxcommons-tools/src/main/java/com/megatim/fdxcommons/tools/database/queries/metadata/JsonDataRowToUpdate.java b/fdx-commons/fdxcommons-tools/src/main/java/com/megatim/fdxcommons/tools/database/queries/metadata/JsonDataRowToUpdate.java
new file mode 100644
index 0000000..02043e4
--- /dev/null
+++ b/fdx-commons/fdxcommons-tools/src/main/java/com/megatim/fdxcommons/tools/database/queries/metadata/JsonDataRowToUpdate.java
@@ -0,0 +1,153 @@
+/*
+ * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
+ * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
+ */
+package com.megatim.fdxcommons.tools.database.queries.metadata;
+
+import com.megatim.fdxcommons.model.enumeration.TypeDonnee;
+import com.megatim.fdxcommons.model.integration.ColumnDefinition;
+import com.megatim.fdxcommons.tools.database.exceptions.BadDataValueException;
+import com.megatim.fdxcommons.tools.database.exceptions.ColumnNotFoundException;
+import com.megatim.fdxcommons.tools.database.exceptions.LocalDateTimeValueParseError;
+import java.sql.SQLException;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import org.apache.commons.validator.GenericValidator;
+
+/**
+ *
+ * @author ASUS
+ */
+public class JsonDataRowToUpdate {
+
+    private final Map<String, Object> dataToSet;
+    private final String parentTableName;
+    private final List<ColumnDefinition> columnsDefinition;
+
+    public JsonDataRowToUpdate(Map<String, Object> data, String parentTableName, List<ColumnDefinition> columnsDefinition) {
+        this.dataToSet = data;
+        this.parentTableName = parentTableName;
+        this.columnsDefinition = columnsDefinition;
+    }
+
+    public Map<String, Map<String, Object>> dataRow() throws ColumnNotFoundException, SQLException, BadDataValueException, LocalDateTimeValueParseError {
+        Map<String, Map<String, Object>> tableToDataRow = new HashMap<>();
+
+        for (Map.Entry<String, Object> entry : dataToSet.entrySet()) {
+
+            if (nomColonneIsValid(entry.getKey())) {
+                String tableName = tableName(parentTableName, entry.getKey());
+                Map<String, Object> dataToSetToTable = tableToDataRow.get(tableName);
+
+                if (dataToSetToTable == null) {
+                    dataToSetToTable = new HashMap<>();
+                }
+                ColumnDefinition columnDefinition = columnsDefinition.stream().filter(c -> c.getName().equalsIgnoreCase(entry.getKey())).findFirst().get();
+
+                Object data = parse(columnDefinition, entry.getValue());
+                dataToSetToTable.put(cleanFieldName(entry.getKey()), data);
+                tableToDataRow.put(tableName, dataToSetToTable);
+            } else {
+                throw new ColumnNotFoundException("La colonne " + entry.getKey() + " n'est pas présente pour cette strcuture JSON");
+            }
+        }
+        return tableToDataRow;
+    }
+
+    private String cleanFieldName(String fieldName) {
+        int index = fieldName.lastIndexOf(".");
+        if (index > 0) {
+            String cleanFieldName = fieldName.substring(index + 1);
+            return cleanFieldName;
+        } else {
+            return fieldName;
+        }
+
+    }
+
+    private String tableName(String rootTable, String fieldName) {
+        int index = fieldName.lastIndexOf(".");
+        ColumnDefinition columnDefinition = columnsDefinition.stream().filter(c -> c.getName().equalsIgnoreCase(fieldName)).findFirst().get();
+
+        if (columnDefinition.isCollection()) {
+            if (index <= 0) {
+                return (rootTable + "_" + fieldName).toLowerCase();
+            } else {
+                String name = fieldName.substring(index);
+                return (rootTable + "_" + name).toLowerCase();
+            }
+        } else {
+            if (index <= 0) {
+                return rootTable;
+            } else {
+                String name = fieldName.subSequence(0, index).toString().replaceAll("\\.", "_");
+                return (rootTable + "_" + name).toLowerCase();
+            }
+        }
+
+    }
+
+    private boolean nomColonneIsValid(String nomColonne) {
+        return nomColonne != null
+                && columnsDefinition
+                        .stream()
+                        .filter(c -> c.getName().equalsIgnoreCase(nomColonne))
+                        .findFirst()
+                        .isPresent();
+    }
+
+    private Object parse(ColumnDefinition columnDefinition, Object value) throws BadDataValueException, LocalDateTimeValueParseError {
+
+        String typeDonnee = columnDefinition.getTypeDonnee();
+
+        if (typeDonnee.equals(TypeDonnee.ALPHANUMERIQUE.toString())
+                && (value == null || (value instanceof String && lengthLowerOrEquals((String) value, columnDefinition.getTaille())))) {
+
+            return value != null ? value.toString().trim() : value;
+
+        } else if (typeDonnee.equals(TypeDonnee.NUMERIQUE.toString())) {
+
+            if (value == null) {
+                return 0L;
+            }
+
+            String stringValue = value.toString();
+            if (lengthLowerOrEquals(stringValue, columnDefinition.getTaille()) && GenericValidator.isLong(stringValue)) {
+                return Long.valueOf(stringValue);
+            }
+
+        } else if (typeDonnee.equals(TypeDonnee.DECIMAL.toString())) {
+
+            if (value == null) {
+                return 0.0;
+            }
+
+            String stringValue = value.toString();
+            if (lengthLowerOrEquals(stringValue, columnDefinition.getTaille()) && GenericValidator.isDouble(stringValue)) {
+                return Double.valueOf(stringValue);
+            }
+
+        } else if (typeDonnee.equals(TypeDonnee.DATE.toString())) {
+
+            if (value == null) {
+                return null;
+            }
+            String stringValue = value.toString();
+
+            if (lengthEquals(stringValue, columnDefinition.getTaille()) && GenericValidator.isDate(stringValue, columnDefinition.getFormatDate(), true)) {
+                return new InsertionLocalDateTimeValue(columnDefinition.getFormatDate(), value).value();
+            }
+        }
+
+        throw new BadDataValueException("La valeur " + value + " ne correspond pas au format attendu pour la colonne " + columnDefinition.getName());
+    }
+
+    private boolean lengthEquals(String value, int columnMaxLength) {
+        return value.trim().length() == columnMaxLength;
+    }
+
+    private boolean lengthLowerOrEquals(String value, int columnMaxLength) {
+        return value.trim().length() <= columnMaxLength;
+    }
+}

--
Gitblit v1.10.0