Kenmegne
7 days ago 23a46b4be35277e06ec89f48730eeb694e686be8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
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 + "");
    }
}