/*
|
* 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<String, Object> entity;
|
|
public JsonApiTypeFichierInsertData(String tableName, Map<String, Object> 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<String> 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<JsonTypeFichierInsertMetaData> children() throws Exception {
|
Map<String, Object> mapOfObjectElts = new HashMap<>();
|
|
for (Map.Entry<String, Object> 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<String, Object> entity) {
|
Map<String, Object> mapOfPrimitiveElts = new HashMap<>();
|
|
for (Map.Entry<String, Object> 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<JsonTypeFichierInsertMetaData> children(Map<String, Object> mapOfObjectElts) {
|
List<JsonTypeFichierInsertMetaData> children = new ArrayList<>();
|
|
for (Map.Entry<String, Object> 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<String, Object>) entry.getValue());
|
children.add(metaData);
|
}
|
}
|
return children;
|
}
|
|
private List<JsonTypeFichierInsertMetaData> listValues(String key, Object value) {
|
List<JsonTypeFichierInsertMetaData> 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<String, Object>) o);
|
children.add(metaData);
|
}
|
} else {
|
//Liste de primitifs
|
for (Object o : valueList) {
|
Map<String, Object> map = new HashMap<>();
|
map.put(key.toLowerCase(), o);
|
JsonTypeFichierInsertMetaData metaData = new JsonApiTypeFichierInsertData(tableName + "_" + key.toLowerCase(), map);
|
children.add(metaData);
|
}
|
}
|
}
|
return children;
|
}
|
}
|