Kenmegne
6 days ago 908e81dd173f380879d5fabeb5794d5b7a76df0e
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/*
 * 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.fdxconvert;
 
import com.megatim.dynamicjsonparser.enums.TypeDonnee;
import com.megatim.dynamicjsonparser.extender.JsonDataValidator;
import com.megatim.dynamicjsonparser.pojo.JsonField;
import static com.megatim.dynamicjsonparser.utils.Utility.writeToFile;
import com.megatim.typefichier.validator.Validator;
import com.megatim.typefichier.validator.model.ConfigJsonValidator;
import com.megatim.typefichier.validator.utilities.Utilities;
import com.megatim.fdxconvert.dao.JsonStructureDAO;
import com.megatim.fdxconvert.dao.TypeFichierJsonDAO;
import com.megatim.fdxconvert.model.JsonStructure;
import com.megatim.fdxconvert.model.TypeFichier;
import com.megatim.fdxconvert.model.TypeFichierJson;
import com.megatim.fdxconvert.model.jaxb.TypeFichierJsonsList;
import com.megatim.fdxconvert.service.JsonStructureService;
import com.megatim.fdxconvert.service.TypeFichierJsonService;
import com.megatim.fdxconvert.util.TypeFichierJsonMarshaller;
import com.megatim.fdxconvert.util.TypeFichierJsonConverter;
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.bind.JAXBException;
import org.apache.poi.ss.usermodel.DateUtil;
 
/**
 *
 * @author STEPHANIE
 */
public class Test {
 
    public static void main(String[] args) throws Exception {
        Date date = DateUtil.getJavaDate(Double.parseDouble("45112   "));
        SimpleDateFormat sdf
                = new SimpleDateFormat("dd/MM/yyyy");
        System.out.println("" + sdf.format(date));
    }
 
    private static void unMarshall() {
        String filePath = "C:\\Users\\ASUS\\Documents\\DATAS\\Export\\typefichierJsons.xml";
        try {
            TypeFichierJsonsList typeFichierJsonsList = TypeFichierJsonMarshaller.unMarshall(filePath);
            TypeFichierJsonService service = TypeFichierJsonService.getInstance();
 
            for (TypeFichierJson t : typeFichierJsonsList.getTypeFichierJson()) {
                service.add(t);
            }
        } catch (JAXBException ex) {
            Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception ex) {
            Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
 
    private static void unMarshallOne(String filePath) {
        try {
            TypeFichierJson t = TypeFichierJsonMarshaller.unMarshallOne(filePath);
            TypeFichierJsonService.getInstance().add(t);
 
        } catch (JAXBException ex) {
            Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception ex) {
            Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
 
    private static void marshallOne(String codeTypeFichier) {
        try {
            TypeFichierJson typeFichierJson = TypeFichierJsonDAO.getByTypeFichier(codeTypeFichier);
            TypeFichierJsonMarshaller.marshall("C:\\Users\\ASUS\\Documents\\DATAS\\Export", typeFichierJson);
        } catch (Exception ex) {
            Logger.getLogger(Test.class.getName()).log(Level.SEVERE, ex.getMessage(), ex);
        }
    }
 
    private static void marshall() {
        try {
            List<TypeFichierJson> typefichierJsons = TypeFichierJsonService.getInstance().getAll();
            for (TypeFichierJson t : typefichierJsons) {
                JsonStructure jsonStructure = JsonStructureDAO.loadSubJsonStructure(t.getJsonStructure());
                t.setJsonStructure(jsonStructure);
            }
            TypeFichierJsonMarshaller.marshall(typefichierJsons, "C:\\Users\\ASUS\\Documents\\DATAS\\Export");
        } catch (Exception ex) {
            Logger.getLogger(Test.class.getName()).log(Level.SEVERE, ex.getMessage(), ex);
        }
    }
 
    private static void validateWithValidator() {
        File file = new File("C:\\Users\\ASUS\\Documents\\DATAS\\DGBACT.json");
        String validatorPath = "C:\\Users\\ASUS\\Documents\\DATAS\\Export\\DGBACT20250417151916.xml";
        Validator validator = new Validator();
        ConfigJsonValidator configJson = new ConfigJsonValidator(
                "C:\\Users\\ASUS\\Documents\\ERROR",
                "C:\\Users\\ASUS\\Documents\\OUT",
                file
        );
        try {
            boolean result = validator.validate(validatorPath, configJson, System.getProperty("user.dir") + "\\src\\main\\java", true, true, 1);
            System.out.println("result = " + result);
        } catch (IOException ex) {
            Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
        } catch (JAXBException ex) {
            Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
 
    private static void validate() {
        try {
            final String packageName = "com.megatim.dynamicjsonparser.generated";
            File file = new File("C:\\Users\\ASUS\\Documents\\DATAS\\DGBACT.json");
 
            List<JsonField> fields = new TypeFichierJsonConverter(dgbact()).convert();
            JsonDataValidator jsonDataValidator = new JsonDataValidator();
 
            boolean result = jsonDataValidator.validateDatas(fields, System.getProperty("user.dir") + "\\src\\main\\java", packageName, file, 1);
            System.out.println("result = " + result + ", " + LocalDateTime.now());
 
            if (!result) {
                Path errorPath = Paths.get("C:\\Users\\ASUS\\Documents\\JSON_TEST\\DGBACT.txt.err");
                writeToFile(jsonDataValidator.getParsedData().getErrors(), errorPath);
            }
            System.out.println("NbElts = " + jsonDataValidator.getParsedData().getVirtuals().size() + ", " + LocalDateTime.now());
//            jsonDataValidator.getParsedData().getVirtuals().forEach(v -> System.out.println(v));
        } catch (IOException ex) {
            Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
//    private static String print(List<JsonDataType> jsonDataTypes) {
//        if (jsonDataTypes != null && !jsonDataTypes.isEmpty()) {
//            jsonDataTypes.stream().forEach(j -> {
//                if (j.getTypeDonnee().equals(TypeDonnee.OBJET)) {
//                    System.out.println(j);
//                    System.out.println("    " + print(j.getJsonDataTypes()));
//                } else {
//                    System.out.println(j);
//                }
//            });
//        }
//        return "";
//    }
 
    public static void save() {
        TypeFichierJson t = typeFichierJson();
        JsonStructure jsonStruct = t.getJsonStructure();
        jsonStruct.setTypeFichierJson(null);
 
        try {
            TypeFichierJsonService tjService = TypeFichierJsonService.getInstance();
            JsonStructureService jService = JsonStructureService.getInstance();
 
            JsonStructure newJsonStruct = jService.add(t.getJsonStructure());
            t.setJsonStructure(newJsonStruct);
            TypeFichierJson newEntity = tjService.add(t);
 
        } catch (Exception ex) {
            Logger.getLogger(Test.class.getName()).log(Level.SEVERE, ex.getMessage(), ex);
        }
    }
 
    public static TypeFichierJson typeFichierJson() {
        TypeFichierJson typeFichierJson = new TypeFichierJson();
        typeFichierJson.setTypeFichier(typeFichier());
 
        JsonStructure parent = jsonStructure(null, typeFichierJson.getTypeFichier().getCode(), true, true, TypeDonnee.OBJET, 10);
        parent.setFields(jsonStructures(parent));
        parent.setTypeFichierJson(typeFichierJson);
 
        typeFichierJson.setJsonStructure(parent);
 
        return typeFichierJson;
    }
 
    private static TypeFichier typeFichier() {
        TypeFichier typeFichier = new TypeFichier();
        typeFichier.setCode("DGBACT");
 
        return typeFichier;
    }
 
    private static TypeFichierJson dgbact() {
        TypeFichierJson typeFichierJson = new TypeFichierJson();
        typeFichierJson.setTypeFichier(typeFichier());
 
        JsonStructure parent = jsonStructure(null, typeFichierJson.getTypeFichier().getCode(), true, true, TypeDonnee.OBJET, 10);
        parent.setFields(jsonStructures(parent));
        parent.setTypeFichierJson(typeFichierJson);
 
        typeFichierJson.setJsonStructure(parent);
 
        List<JsonStructure> jsonStructures = new ArrayList<>();
        jsonStructures.add(jsonStructure(parent, "EXERCICE", true, false, TypeDonnee.ALPHANUMERIQUE, 2));
        jsonStructures.add(jsonStructure(parent, "CHAPITRE", true, false, TypeDonnee.ALPHANUMERIQUE, 2));
        jsonStructures.add(jsonStructure(parent, "CODEPROGRAMME", true, false, TypeDonnee.ALPHANUMERIQUE, 3));
        jsonStructures.add(jsonStructure(parent, "CODEACTION", true, false, TypeDonnee.ALPHANUMERIQUE, 2));
        jsonStructures.add(jsonStructure(parent, "LIBELLEF", true, false, TypeDonnee.ALPHANUMERIQUE, 255));
        jsonStructures.add(jsonStructure(parent, "LIBELLEA", true, false, TypeDonnee.ALPHANUMERIQUE, 255));
 
        parent.setFields(jsonStructures);
 
        return typeFichierJson;
    }
 
    private static List<JsonStructure> jsonStructures(JsonStructure parent) {
        List<JsonStructure> jsonStructures = new ArrayList<>();
        jsonStructures.add(jsonStructure(parent, "numeros", true, true, TypeDonnee.NUMERIQUE, 8));
        jsonStructures.add(jsonStructure(parent, "noms", true, false, TypeDonnee.ALPHANUMERIQUE, 100));
        jsonStructures.add(jsonStructure(parent, "nbreEnfants", false, false, TypeDonnee.NUMERIQUE, 2));
        jsonStructures.add(jsonStructure(parent, "salaire", true, false, TypeDonnee.DECIMAL, 14));
        jsonStructures.add(jsonStructure(parent, "dateNaissance", true, false, "yyyyMMdd", "-", 10));
 
        JsonStructure jsonStructure = jsonStructure(parent, "cptBanque", true, true, TypeDonnee.OBJET, 10);
        jsonStructure.setFields(Arrays.asList(
                jsonStructure(jsonStructure, "rib", true, false, TypeDonnee.NUMERIQUE, 23),
                jsonStructure(jsonStructure, "solde", true, false, TypeDonnee.DECIMAL, 14),
                jsonStructure(jsonStructure, "dateCreation", true, false, "yyyyMMdd", "-", 10)
        ));
        jsonStructures.add(jsonStructure);
 
        return jsonStructures;
    }
 
    private static JsonStructure jsonStructure(JsonStructure parent, String name, boolean required, boolean collection, TypeDonnee typeDonnee, int taille) {
        JsonStructure jsonStructure = new JsonStructure();
        jsonStructure.setName(name);
        jsonStructure.setCollection(collection);
        jsonStructure.setTypeDonnee(typeDonnee);
        jsonStructure.setRequired(required);
        jsonStructure.setParent(parent);
        jsonStructure.setLengthh(taille);
 
        return jsonStructure;
    }
 
    private static JsonStructure jsonStructure(JsonStructure parent, String name, boolean required, boolean collection, String formatDate, String delimiteurDate, int taille) {
        JsonStructure jsonStructure = new JsonStructure();
        jsonStructure.setName(name);
        jsonStructure.setCollection(collection);
        jsonStructure.setTypeDonnee(TypeDonnee.DATE);
        jsonStructure.setRequired(required);
        jsonStructure.setFormatDate(formatDate);
        jsonStructure.setCodeDelimiteurDate(delimiteurDate);
        jsonStructure.setParent(parent);
        jsonStructure.setLengthh(taille);
 
        return jsonStructure;
    }
 
}