/* * 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 data, JsonStructure jsonStructure, boolean withCustomColumns) { List> finalData = mergeAll(data, jsonStructure, withCustomColumns); return new JsonSelectQueryResult(lastIndexRead(data), mergeSimpleColumns(finalData, jsonStructure, withCustomColumns)); } public Map> tableToMatchingIndexes(Map data, String rootTableName) { Map> tableToMatchingIndexes = new HashMap<>(); data.forEach((k, v) -> { Set 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> mergeAll(Map data, JsonStructure jsonStructure, boolean withCustomColumns) { List> finalData = new ArrayList<>(); for (Object value : data.values()) { Map map = (Map) value; finalData.add(mergeData(map, jsonStructure, withCustomColumns)); } return finalData; } private List> mergeSimpleColumns(List> data, JsonStructure jsonStructure, boolean withCustomColumns) { List> finalData = new ArrayList<>(); for (Map datum : data) { finalData.add(mergeSimpleColumns(datum, jsonStructure, withCustomColumns)); } return finalData; } private Map mergeSimpleColumns(Map dataMap, JsonStructure jsonStructure, boolean withCustomColumns) { Map finalData = new HashMap<>(); List fields = jsonStructure.getFields(); for (Map.Entry entry : dataMap.entrySet()) { Optional optField = fields.stream().filter(p -> p.getName().equalsIgnoreCase(entry.getKey())).findFirst(); if (optField.isPresent()) { JsonStructure field = optField.get(); if (field.isCollection()) { List> liste = (List) entry.getValue(); List finalValues = new ArrayList<>(); if (field.getTypeDonnee().equals(TypeDonnee.OBJET)) { for (Map map : liste) { finalValues.add(mergeSimpleColumns(map, field, withCustomColumns)); } } else { for (Map map : liste) { for (Map.Entry e : map.entrySet()) { finalValues.add(e.getValue()); } } } finalData.put(entry.getKey(), finalValues); } else if (field.getTypeDonnee().equals(TypeDonnee.OBJET)) { Map map = null; if (entry.getValue() != null) { if (entry.getValue() instanceof List) { map = (Map) ((List) entry.getValue()).get(0); } else { map = (Map) 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> tableToMatchingIndexes) { if (data instanceof Map) { Map dataMap = (Map) data; for (Map.Entry entry : dataMap.entrySet()) { if (entry.getKey() instanceof Long) { Long index = (Long) entry.getKey(); Set 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 mergeData(Map dataMap, JsonStructure jsonStructure, boolean withCustomColumns) { Map finalMap = new HashMap<>(); for (Map.Entry 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 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 data) { Long max = 0L; for (Long l : data.keySet()) { if (l > max) { max = l; } } return Integer.parseInt(max + ""); } }