/* * 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.tools.database.contrat.JsonTypeFichierInsertMetaData; import com.megatim.fdxcommons.tools.database.contrat.TypeFichierDataRow; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.stream.Collectors; /** * * @author ASUS */ public class JsonApiTypeFichierInsertData implements JsonTypeFichierInsertMetaData { private final String tableName; private final Map entity; public JsonApiTypeFichierInsertData(String tableName, Map entity) { this.tableName = tableName; this.entity = entity; } @Override public String tableName() { return tableName; } /** * Dans cette méthode on ne s'intéresse qu'aux champs de type primitif * * @return * @throws Exception */ @Override public TypeFichierDataRow row() throws Exception { return primitiveValues(entity); } @Override public List columnNames() throws Exception { return entity.entrySet() .stream() .filter(p -> !(p.getValue() instanceof Map) && !(p.getValue() instanceof List)) .map(e -> e.getKey()) .sorted((e1, e2) -> e1.compareTo(e2)) //On se rassure de classer le résultat .collect(Collectors.toList()); } /** * Dans cette méthode on ne s'intéresse qu'aux champs de type Map ou List * * @return * @throws Exception */ @Override public List children() throws Exception { Map mapOfObjectElts = new HashMap<>(); for (Map.Entry e : entity.entrySet()) { if ((e.getValue() instanceof Map) || (e.getValue() instanceof List)) { mapOfObjectElts.put(e.getKey(), e.getValue()); } } return children(mapOfObjectElts); } private TypeFichierDataRow primitiveValues(Map entity) { Map mapOfPrimitiveElts = new HashMap<>(); for (Map.Entry e : entity.entrySet()) { if (!(e.getValue() instanceof Map) && !(e.getValue() instanceof List)) { mapOfPrimitiveElts.put(e.getKey(), e.getValue()); } } if (!mapOfPrimitiveElts.isEmpty()) { return new DefaultTypeFichierDataRow(mapOfPrimitiveElts); } return null; } private List children(Map mapOfObjectElts) { List children = new ArrayList<>(); for (Map.Entry entry : mapOfObjectElts.entrySet()) { if (entry.getValue() instanceof List) { children.addAll(listValues(entry.getKey(), entry.getValue())); } else if (entry.getValue() instanceof Map) { JsonTypeFichierInsertMetaData metaData = new JsonApiTypeFichierInsertData(tableName + "_" + entry.getKey().toLowerCase(), (Map) entry.getValue()); children.add(metaData); } } return children; } private List listValues(String key, Object value) { List children = new ArrayList<>(); List valueList = (List) value; if (!valueList.isEmpty()) { Object obj = valueList.get(0); //Liste d'objets if (obj instanceof Map) { for (Object o : valueList) { JsonTypeFichierInsertMetaData metaData = new JsonApiTypeFichierInsertData(tableName + "_" + key.toLowerCase(), (Map) o); children.add(metaData); } } else { //Liste de primitifs for (Object o : valueList) { Map map = new HashMap<>(); map.put(key.toLowerCase(), o); JsonTypeFichierInsertMetaData metaData = new JsonApiTypeFichierInsertData(tableName + "_" + key.toLowerCase(), map); children.add(metaData); } } } return children; } }