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/utils/JsonDataUtil.java | 206 +++++++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 206 insertions(+), 0 deletions(-)
diff --git a/fdx-commons/fdxcommons-tools/src/main/java/com/megatim/fdxcommons/tools/utils/JsonDataUtil.java b/fdx-commons/fdxcommons-tools/src/main/java/com/megatim/fdxcommons/tools/utils/JsonDataUtil.java
new file mode 100644
index 0000000..fa31dd5
--- /dev/null
+++ b/fdx-commons/fdxcommons-tools/src/main/java/com/megatim/fdxcommons/tools/utils/JsonDataUtil.java
@@ -0,0 +1,206 @@
+/*
+ * 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.utils;
+
+import com.megatim.fdxcommons.model.enumeration.TypeDonnee;
+import com.megatim.fdxcommons.model.integration.json.JsonStructure;
+import com.megatim.fdxcommons.tools.database.queries.metadata.JsonSelectQueryResult;
+import com.megatim.fdxcommons.tools.database.tables.appcolumns.FdxApiColumnDefinitions;
+import com.megatim.fdxcommons.tools.database.tables.appcolumns.FdxConsultationColumnDefinitions;
+import com.megatim.fdxcommons.tools.database.tables.appcolumns.TokenColumnDefinition;
+import java.sql.Timestamp;
+import java.time.LocalDateTime;
+import java.time.format.DateTimeFormatter;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+import java.util.Set;
+
+/**
+ *
+ * @author ASUS
+ */
+public class JsonDataUtil {
+
+ private final String tokenColumn = new TokenColumnDefinition().name();
+ private final FdxApiColumnDefinitions apiColumnDefinitions = new FdxApiColumnDefinitions();
+ private final FdxConsultationColumnDefinitions fdxConsColumnDefinitions = new FdxConsultationColumnDefinitions();
+
+ public JsonSelectQueryResult merge(Map<Long, Object> data, JsonStructure jsonStructure, boolean withCustomColumns) {
+ List<Map<String, Object>> finalData = mergeAll(data, jsonStructure, withCustomColumns);
+ return new JsonSelectQueryResult(lastIndexRead(data), mergeSimpleColumns(finalData, jsonStructure, withCustomColumns));
+ }
+
+ public Map<String, Set<Long>> tableToMatchingIndexes(Map<Long, Object> data, String rootTableName) {
+ Map<String, Set<Long>> tableToMatchingIndexes = new HashMap<>();
+
+ data.forEach((k, v) -> {
+ Set<Long> indexes = tableToMatchingIndexes.get(rootTableName);
+ if (indexes == null) {
+ indexes = new HashSet<>();
+ }
+ indexes.add(k);
+ tableToMatchingIndexes.put(rootTableName, indexes);
+ tableToMatchingIndexes(v, rootTableName, tableToMatchingIndexes);
+ });
+ return tableToMatchingIndexes;
+ }
+
+ private List<Map<String, Object>> mergeAll(Map<Long, Object> data, JsonStructure jsonStructure, boolean withCustomColumns) {
+ List<Map<String, Object>> finalData = new ArrayList<>();
+ for (Object value : data.values()) {
+ Map map = (Map) value;
+ finalData.add(mergeData(map, jsonStructure, withCustomColumns));
+ }
+ return finalData;
+ }
+
+ private List<Map<String, Object>> mergeSimpleColumns(List<Map<String, Object>> data, JsonStructure jsonStructure, boolean withCustomColumns) {
+ List<Map<String, Object>> finalData = new ArrayList<>();
+
+ for (Map<String, Object> datum : data) {
+ finalData.add(mergeSimpleColumns(datum, jsonStructure, withCustomColumns));
+ }
+ return finalData;
+ }
+
+ private Map<String, Object> mergeSimpleColumns(Map<String, Object> dataMap, JsonStructure jsonStructure, boolean withCustomColumns) {
+ Map<String, Object> finalData = new HashMap<>();
+ List<JsonStructure> fields = jsonStructure.getFields();
+
+ for (Map.Entry<String, Object> entry : dataMap.entrySet()) {
+ Optional<JsonStructure> optField = fields.stream().filter(p -> p.getName().equalsIgnoreCase(entry.getKey())).findFirst();
+
+ if (optField.isPresent()) {
+ JsonStructure field = optField.get();
+
+ if (field.isCollection()) {
+ List<Map<String, Object>> liste = (List) entry.getValue();
+ List<Object> finalValues = new ArrayList<>();
+
+ if (field.getTypeDonnee().equals(TypeDonnee.OBJET)) {
+ for (Map<String, Object> map : liste) {
+ finalValues.add(mergeSimpleColumns(map, field, withCustomColumns));
+ }
+ } else {
+ for (Map<String, Object> map : liste) {
+ for (Map.Entry<String, Object> e : map.entrySet()) {
+ finalValues.add(e.getValue());
+ }
+ }
+ }
+ finalData.put(entry.getKey(), finalValues);
+ } else if (field.getTypeDonnee().equals(TypeDonnee.OBJET)) {
+ Map<String, Object> map = null;
+ if (entry.getValue() != null) {
+ if (entry.getValue() instanceof List) {
+ map = (Map<String, Object>) ((List) entry.getValue()).get(0);
+ } else {
+ map = (Map<String, Object>) entry.getValue();
+ }
+ finalData.put(entry.getKey(), mergeSimpleColumns(map, field, withCustomColumns));
+ }
+ } else {
+ switch (field.getTypeDonnee()) {
+ case DATE:
+ Timestamp timestamp = Timestamp.valueOf(entry.getValue().toString());
+ LocalDateTime date = timestamp.toLocalDateTime();
+ DateTimeFormatter formatter = DateTimeFormatter.ofPattern(field.getFormatDate());
+ finalData.put(entry.getKey(), formatter.format(date));
+ break;
+
+ default:
+ finalData.put(entry.getKey(), entry.getValue());
+ break;
+ }
+ }
+ } else {
+ if (!entry.getKey().equalsIgnoreCase(tokenColumn) && withCustomColumns && (apiColumnDefinitions.isAppColumnDefinition(entry.getKey()) || fdxConsColumnDefinitions.isAppColumnDefinition(entry.getKey()))) {
+ finalData.put(entry.getKey(), entry.getValue());
+ }
+ }
+ }
+ return finalData;
+ }
+
+ private void tableToMatchingIndexes(Object data, String tableName, Map<String, Set<Long>> tableToMatchingIndexes) {
+ if (data instanceof Map) {
+ Map<Object, Object> dataMap = (Map) data;
+
+ for (Map.Entry<Object, Object> entry : dataMap.entrySet()) {
+ if (entry.getKey() instanceof Long) {
+ Long index = (Long) entry.getKey();
+ Set<Long> indexes = tableToMatchingIndexes.get(tableName);
+
+ if (indexes == null) {
+ indexes = new HashSet<>();
+ }
+ if (!indexes.contains(index)) {
+ indexes.add(index);
+ }
+ tableToMatchingIndexes.put(tableName, indexes);
+ tableToMatchingIndexes(entry.getValue(), tableName, tableToMatchingIndexes);
+
+ } else if (entry.getValue() instanceof Map) {
+ String newTableName = tableName + "_" + tableName(entry.getKey().toString());
+ tableToMatchingIndexes(entry.getValue(), newTableName, tableToMatchingIndexes);
+
+ }
+ }
+ }
+ }
+
+ private String tableName(String key) {
+ String[] array = key.split("\\.");
+ return (array[array.length - 1]).toLowerCase();
+ }
+
+ private Map<String, Object> mergeData(Map<Object, Object> dataMap, JsonStructure jsonStructure, boolean withCustomColumns) {
+ Map<String, Object> finalMap = new HashMap<>();
+
+ for (Map.Entry<Object, Object> entry : dataMap.entrySet()) {
+
+ if (entry.getKey() instanceof Long) {
+ finalMap = mergeData((Map) entry.getValue(), jsonStructure, withCustomColumns);
+ } else {
+ String key = cleanKey(entry.getKey().toString());
+
+ if (entry.getValue() instanceof Map) {
+ Map<Long, Object> map = (Map) entry.getValue();
+ finalMap.put(key, mergeAll(map, jsonStructure, withCustomColumns));
+ } else {
+ if (!key.equalsIgnoreCase(tokenColumn)) {
+ if (withCustomColumns) {
+ finalMap.put(key, entry.getValue());
+ } else {
+ if (!apiColumnDefinitions.isAppColumnDefinition(key) && !fdxConsColumnDefinitions.isAppColumnDefinition(key)) {
+ finalMap.put(key, entry.getValue());
+ }
+ }
+ }
+ }
+ }
+ }
+ return finalMap;
+ }
+
+ private String cleanKey(String key) {
+ String[] array = key.split("\\.");
+ return array[array.length - 1];
+ }
+
+ private int lastIndexRead(Map<Long, Object> data) {
+ Long max = 0L;
+ for (Long l : data.keySet()) {
+ if (l > max) {
+ max = l;
+ }
+ }
+ return Integer.parseInt(max + "");
+ }
+}
--
Gitblit v1.10.0