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
/*
 * 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.model.enumeration.TypeDonnee;
import com.megatim.fdxcommons.model.integration.ColumnDefinition;
import com.megatim.fdxcommons.tools.database.exceptions.BadDataValueException;
import com.megatim.fdxcommons.tools.database.exceptions.ColumnNotFoundException;
import com.megatim.fdxcommons.tools.database.exceptions.LocalDateTimeValueParseError;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.validator.GenericValidator;
 
/**
 *
 * @author ASUS
 */
public class JsonDataRowToUpdate {
 
    private final Map<String, Object> dataToSet;
    private final String parentTableName;
    private final List<ColumnDefinition> columnsDefinition;
 
    public JsonDataRowToUpdate(Map<String, Object> data, String parentTableName, List<ColumnDefinition> columnsDefinition) {
        this.dataToSet = data;
        this.parentTableName = parentTableName;
        this.columnsDefinition = columnsDefinition;
    }
 
    public Map<String, Map<String, Object>> dataRow() throws ColumnNotFoundException, SQLException, BadDataValueException, LocalDateTimeValueParseError {
        Map<String, Map<String, Object>> tableToDataRow = new HashMap<>();
 
        for (Map.Entry<String, Object> entry : dataToSet.entrySet()) {
 
            if (nomColonneIsValid(entry.getKey())) {
                String tableName = tableName(parentTableName, entry.getKey());
                Map<String, Object> dataToSetToTable = tableToDataRow.get(tableName);
 
                if (dataToSetToTable == null) {
                    dataToSetToTable = new HashMap<>();
                }
                ColumnDefinition columnDefinition = columnsDefinition.stream().filter(c -> c.getName().equalsIgnoreCase(entry.getKey())).findFirst().get();
 
                Object data = parse(columnDefinition, entry.getValue());
                dataToSetToTable.put(cleanFieldName(entry.getKey()), data);
                tableToDataRow.put(tableName, dataToSetToTable);
            } else {
                throw new ColumnNotFoundException("La colonne " + entry.getKey() + " n'est pas prĂ©sente pour cette strcuture JSON");
            }
        }
        return tableToDataRow;
    }
 
    private String cleanFieldName(String fieldName) {
        int index = fieldName.lastIndexOf(".");
        if (index > 0) {
            String cleanFieldName = fieldName.substring(index + 1);
            return cleanFieldName;
        } else {
            return fieldName;
        }
 
    }
 
    private String tableName(String rootTable, String fieldName) {
        int index = fieldName.lastIndexOf(".");
        ColumnDefinition columnDefinition = columnsDefinition.stream().filter(c -> c.getName().equalsIgnoreCase(fieldName)).findFirst().get();
 
        if (columnDefinition.isCollection()) {
            if (index <= 0) {
                return (rootTable + "_" + fieldName).toLowerCase();
            } else {
                String name = fieldName.substring(index);
                return (rootTable + "_" + name).toLowerCase();
            }
        } else {
            if (index <= 0) {
                return rootTable;
            } else {
                String name = fieldName.subSequence(0, index).toString().replaceAll("\\.", "_");
                return (rootTable + "_" + name).toLowerCase();
            }
        }
 
    }
 
    private boolean nomColonneIsValid(String nomColonne) {
        return nomColonne != null
                && columnsDefinition
                        .stream()
                        .filter(c -> c.getName().equalsIgnoreCase(nomColonne))
                        .findFirst()
                        .isPresent();
    }
 
    private Object parse(ColumnDefinition columnDefinition, Object value) throws BadDataValueException, LocalDateTimeValueParseError {
 
        String typeDonnee = columnDefinition.getTypeDonnee();
 
        if (typeDonnee.equals(TypeDonnee.ALPHANUMERIQUE.toString())
                && (value == null || (value instanceof String && lengthLowerOrEquals((String) value, columnDefinition.getTaille())))) {
 
            return value != null ? value.toString().trim() : value;
 
        } else if (typeDonnee.equals(TypeDonnee.NUMERIQUE.toString())) {
 
            if (value == null) {
                return 0L;
            }
 
            String stringValue = value.toString();
            if (lengthLowerOrEquals(stringValue, columnDefinition.getTaille()) && GenericValidator.isLong(stringValue)) {
                return Long.valueOf(stringValue);
            }
 
        } else if (typeDonnee.equals(TypeDonnee.DECIMAL.toString())) {
 
            if (value == null) {
                return 0.0;
            }
 
            String stringValue = value.toString();
            if (lengthLowerOrEquals(stringValue, columnDefinition.getTaille()) && GenericValidator.isDouble(stringValue)) {
                return Double.valueOf(stringValue);
            }
 
        } else if (typeDonnee.equals(TypeDonnee.DATE.toString())) {
 
            if (value == null) {
                return null;
            }
            String stringValue = value.toString();
 
            if (lengthEquals(stringValue, columnDefinition.getTaille()) && GenericValidator.isDate(stringValue, columnDefinition.getFormatDate(), true)) {
                return new InsertionLocalDateTimeValue(columnDefinition.getFormatDate(), value).value();
            }
        }
 
        throw new BadDataValueException("La valeur " + value + " ne correspond pas au format attendu pour la colonne " + columnDefinition.getName());
    }
 
    private boolean lengthEquals(String value, int columnMaxLength) {
        return value.trim().length() == columnMaxLength;
    }
 
    private boolean lengthLowerOrEquals(String value, int columnMaxLength) {
        return value.trim().length() <= columnMaxLength;
    }
}