Kenmegne
7 days ago b3d0580439b9a00c7eb918085de1694151066004
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
package com.megatim.fdxconvert.controller;
 
import com.megatim.typefichier.validator.Validator;
import com.megatim.typefichier.validator.model.ConfigJsonValidator;
import com.megatim.typefichier.validator.model.ConfigStreamValidator;
import com.megatimfx.common.abstracts.AbstractEditDialogController;
import com.megatimfx.common.enums.TypeOperation;
import com.megatimfx.common.utils.ViewLoaderUtil;
import com.megatimfx.components.customdialogs.AlertMessageUtil;
import com.megatimfx.components.customdialogs.LoadinIndicatorDialogUtil;
import com.megatimfx.components.dialogs.NotificationDialog;
import com.megatimfx.components.dialogs.NotificationType;
import com.megatim.fdxconvert.dao.TypeFichierJsonDAO;
import com.megatim.fdxconvert.dao.ValidateurDAO;
import com.megatim.fdxconvert.enums.DataType;
import com.megatim.fdxconvert.exceptions.ConfigException;
import com.megatim.fdxconvert.exceptions.ValidatorException;
import com.megatim.fdxconvert.forms.MetaAlphaNumeriqueFieldEditFormController;
import com.megatim.fdxconvert.forms.FileToValidateEditFormController;
import com.megatim.fdxconvert.model.AlphaNumeriqueField;
import com.megatim.fdxconvert.model.Configuration;
import com.megatim.fdxconvert.model.TypeFichierJson;
import com.megatim.fdxconvert.pojo.FileToValidate;
import com.megatim.fdxconvert.model.Validateur;
import com.megatim.fdxconvert.service.ConfigurationService;
import com.megatim.fdxconvert.util.ImportData;
import com.megatim.fdxconvert.pojo.FileToValidateDescription;
import com.megatim.fdxconvert.util.TypeFichierJsonConverter;
import com.megatim.fdxconvert.util.Utilities;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URL;
import java.nio.file.Paths;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashSet;
import java.util.Optional;
import java.util.Random;
import java.util.ResourceBundle;
import java.util.Set;
import javafx.concurrent.Task;
import javafx.event.ActionEvent;
import javafx.scene.Node;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
import org.apache.commons.io.IOUtils;
 
/**
 *
 * @author STEPHANIE
 */
public class FileToValidateEditDialogController extends AbstractEditDialogController<FileToValidate> {
 
    org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(FileToValidateEditDialogController.class);
 
    private FileToValidateEditFormController fileToValidateEditFormController;
    private MetaAlphaNumeriqueFieldEditDialogController metaAlphaNumeriqueFieldEditDialogController;
 
    @Override
    public String getTitle() {
 
        getEditButton().setText("Valider");
        return "Conversion manuelle des fichiers";
 
    }
 
    @Override
    public Pane getContentFormPane() throws IOException {
        return ViewLoaderUtil.getPaneFromFxmlFile(fileToValidateEditFormController.getClass().getResource("FileToValidateEditForm.fxml"), fileToValidateEditFormController);
    }
 
    @Override
    public Object getContentFormController() {
        return fileToValidateEditFormController;
    }
 
    @Override
    public void initialize(URL url, ResourceBundle rb) {
        fileToValidateEditFormController = new FileToValidateEditFormController();
        metaAlphaNumeriqueFieldEditDialogController = new MetaAlphaNumeriqueFieldEditDialogController();
 
        metaAlphaNumeriqueFieldEditDialogController.setAlphaNumeriqueFieldEditFormController(new MetaAlphaNumeriqueFieldEditFormController());
 
        super.initialize(url, rb);
 
    }
 
    @Override
    protected void afterBindCurrentObjectWithFormField(FileToValidate bindObject, Object formController, TypeOperation typeOperation) {
        FileToValidateEditFormController controller = (FileToValidateEditFormController) formController;
        controller.getMetaAlphaNumeriqueFieldAbstractTable().setController(metaAlphaNumeriqueFieldEditDialogController);
    }
 
    @Override
    protected void afterBindFormFieldWithCurrentObject(FileToValidate bindObject, Object formController, TypeOperation typeOperation) {
 
        if (formController instanceof FileToValidateEditFormController) {
            FileToValidateEditFormController controller = (FileToValidateEditFormController) formController;
            controller.getMetaAlphaNumeriqueFieldAbstractTable().setController(metaAlphaNumeriqueFieldEditDialogController);
 
            switch (typeOperation) {
 
                case ADD:
                    controller.getTypeFichierAbstractSelectItem().selectedElementProperty().addListener((observable, oldValue, newValue) -> {
 
                        if (newValue != null) {
                            metaAlphaNumeriqueFieldEditDialogController.getAlphaNumeriqueFieldEditFormController().setTypeFichier(newValue);
 
                            if (controller.getDataTypeComboBox().getSelectionModel().getSelectedItem() != null && controller.getDataTypeComboBox().getSelectionModel().getSelectedItem().equals(DataType.JSON)) {
                                controller.getSaisieAlphaNumeriqueFieldChecBox().setDisable(true);
                            }
                            controller.getSaisieAlphaNumeriqueFieldChecBox().setDisable(false);
                        }
                    });
                    fileToValidateEditFormController.getSaisieAlphaNumeriqueFieldChecBox().selectedProperty().addListener(listener -> {
 
                        if (fileToValidateEditFormController.getSaisieAlphaNumeriqueFieldChecBox().isSelected()) {
                            fileToValidateEditFormController.getMetaAlphaNumeriqueFieldAbstractTable().setDisable(false);
                        }
                    });
                    fileToValidateEditFormController.getStrictValidationCheckbox().selectedProperty().addListener(l -> {
                        if (fileToValidateEditFormController.getStrictValidationCheckbox().isSelected()) {
                            fileToValidateEditFormController.getMetaAlphaNumeriqueFieldAbstractTable().setDisable(true);
                        }
                    });
                    break;
                default:
                    controller.getSaisieAlphaNumeriqueFieldChecBox().setSelected(true);
                    controller.getSaisieAlphaNumeriqueFieldChecBox().setDisable(true);
                    break;
            }
        }
    }
 
    @Override
    public void afterSave(ActionEvent event) {
        Configuration config = ConfigurationService.getInstance().getCurrentConfig();
 
        if (getCurrentObject().getDataType().equals(DataType.JSON)) {
            validateJsonFile(config, event);
        } else {
            Optional<Validateur> validateur = ValidateurDAO.findByCodeTypeFichier(getCurrentObject().getTypeFichier().getCode());
 
            if (!validateur.isPresent()) {
                Node source = (Node) event.getSource();
                Stage parentStage = (Stage) source.getScene().getWindow();
                NotificationDialog notificationDialog = new NotificationDialog("Validation impossible. Le type fichier " + getCurrentObject().getTypeFichier().getCode() + " n'a pas de validateur enregistré!",
                        NotificationType.ERROR,
                        parentStage);
                notificationDialog.showNotification();
            }
            validateTexteFile(config, validateur.get(), event);
        }
 
    }
 
    private void validateTexteFile(Configuration config, Validateur validateur, ActionEvent event) {
        Task<Void> task = new Task<Void>() {
 
            @Override
            protected Void call() throws Exception {
 
                if (validateur.isConvertDataBeforeValidation()) {
 
                    //On converti le fichier à valider en fichier
                    generateAndParseTxtFile(getCurrentObject(), config, validateur);
                }
                return null;
            }
        };
 
        task.setOnRunning(e -> LoadinIndicatorDialogUtil.getLoadingIndicatorDialog().show());
        task.setOnFailed(e -> {
            LoadinIndicatorDialogUtil.getLoadingIndicatorDialog().hide();
            Throwable th = task.getException();
 
            if ((th instanceof ConfigException) || (th instanceof ValidatorException)) {
 
                Node source = (Node) event.getSource();
                Stage parentStage = (Stage) source.getScene().getWindow();
                NotificationDialog notificationDialog = new NotificationDialog(th.getMessage(), NotificationType.WARNING, parentStage);
                logger.warn(th.getMessage());
                notificationDialog.showNotification();
 
            } else {
                logger.error(task.getException());
                AlertMessageUtil.showAlertException(task.getException(), "Une exception s'est produite pendant l'importation des données", "Erreur");
            }
        });
        task.setOnSucceeded(e -> {
            LoadinIndicatorDialogUtil.getLoadingIndicatorDialog().hide();
            validateGeneratedFile(config, validateur, event);
        });
        new Thread(task).start();
    }
 
    private void validateGeneratedFile(Configuration config, Validateur validateur, ActionEvent event) {
        Task<Boolean> task = new Task<Boolean>() {
 
            @Override
            protected Boolean call() throws Exception {
                return validate(config, validateur);
            }
        };
        task.setOnRunning(e -> LoadinIndicatorDialogUtil.getLoadingIndicatorDialog().show());
        task.setOnSucceeded(e -> {
            LoadinIndicatorDialogUtil.getLoadingIndicatorDialog().hide();
            Node source = (Node) event.getSource();
            Stage parentStage = (Stage) source.getScene().getWindow();
 
            boolean valid = task.getValue();
            String message;
            NotificationDialog notificationDialog;
            String errorFileName = getCurrentObject().getTxtFilePath() != null
                    ? new File(getCurrentObject().getTxtFilePath()).getName()
                    : new File(getCurrentObject().getFilePath()).getName();
 
            if (!valid) {
                message = "Echec de la validation. Le fichier contient des erreurs, veuillez consulter le répertoire " + config.getErrorDir() + ", puis et ouvrir le fichier d'erreur "
                        + errorFileName + ".err pour en savoir plus";
                notificationDialog = new NotificationDialog(message, NotificationType.ERROR, parentStage);
 
            } else {
                message = "Validation réussie. Fichier correcte";
                notificationDialog = new NotificationDialog(message, NotificationType.SUCCESS, parentStage);
            }
            logger.error(message);
            notificationDialog.showNotification();
 
        });
 
        task.setOnFailed(e -> {
            LoadinIndicatorDialogUtil.getLoadingIndicatorDialog().hide();
            Throwable th = task.getException();
 
            if (th instanceof ConfigException || th instanceof ValidatorException) {
                Node source = (Node) event.getSource();
                Stage parentStage = (Stage) source.getScene().getWindow();
                NotificationDialog notificationDialog = new NotificationDialog(th.getMessage(), NotificationType.WARNING, parentStage);
                logger.warn(th.getMessage());
                notificationDialog.showNotification();
 
            } else {
                logger.error(th);
                AlertMessageUtil.showAlertException(th, "Une exception s'est produite pendant la validation du fichier", "Erreur");
            }
        });
 
        new Thread(task).start();
 
    }
 
    /**
     * Méthode qui appelle la méthode de validation
     *
     * @param val :
     * @param config
     * @return
     * @throws Exception
     */
    private boolean validate(Configuration config, Validateur validateur) throws Exception {
 
        if (config == null) {
            throw new ConfigException("Validation impossible, les répertoires ne sont pas configurés. Bien vouloir les configurer avant avant d'effectuer cette action");
        }
        File originalFile = new File(getCurrentObject().getFilePath());
        File file = validateur.isConvertDataBeforeValidation() ? new File(getCurrentObject().getTxtFilePath()) : originalFile; //Fichier à valider
 
        if (!file.exists()) {
            throw new FileNotFoundException("Le fichier " + file.getAbsolutePath() + " est introuvable");
        }
        byte[] targetArray = IOUtils.toByteArray(FileToValidateEditDialogController.class.getClassLoader().getResourceAsStream("predicatelogic-engine.xml"));
        ConfigStreamValidator configValidator = new ConfigStreamValidator(targetArray, validateur.getContent(), config.getErrorDir(), config.getOutputDir(), file, originalFile);
        Validator validator = new Validator();
        return validator.validate(configValidator, Boolean.TRUE, Boolean.TRUE, Utilities.getNbThreads());
    }
 
    /**
     * Méthode qui génère et parse fichier txt à partir d'un fichier de données
     * csv, txt, ou excel
     *
     * @param val : objet contenant
     * @param config : objet contenant l'ensemeble des repertoires nécessaire à
     * l'extraction des données
     * @return : renvoie le chemin vers le fichier crée
     * @throws Exception
     */
    private void generateAndParseTxtFile(FileToValidate fileToValidate, Configuration config, Validateur validateur) throws Exception {
        if (config == null) {
            throw new ConfigException("Validation impossible, veuillez configurer les repertoires avant d'effectuer cette action");
        }
        String fileName = fileToValidate.getTypeFichier().getCode() + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss")) + generateTierce() + ".txt";
        File outputFile = new File(config.getDatasDir(), fileName);
 
        if (!outputFile.exists()) {
 
            //Creation du fichier
            outputFile.createNewFile();
        }
        getCurrentObject().setTxtFilePath(outputFile.getAbsolutePath());
        Set<AlphaNumeriqueField> alphaNumeriqueToBeTruncated = new HashSet<>();
 
        fileToValidate.getMetaAlphaNumeriqueFields().forEach(m -> {
            alphaNumeriqueToBeTruncated.add(m.getAlphaNumeriqueField());
        });
 
        switch (fileToValidate.getDataType()) {
            case TXT:
                FileToValidateDescription raw = new FileToValidateDescription(new File(fileToValidate.getFilePath()), fileToValidate.getDelimiteurLigne().getCode(), fileToValidate.getDelimiteurColonne().getCode());
 
                if (fileToValidate.isStrictValidation()) {
                    ImportData.parseFileByFieldLength(raw, validateur, outputFile, fileToValidate.isHeaderPresent());
 
                } else {
                    ImportData.parseFileCharacterByCharacter(raw, validateur, outputFile, fileToValidate.isHeaderPresent(), alphaNumeriqueToBeTruncated);
                }
                break;
            case CSV:
                raw = new FileToValidateDescription(new File(fileToValidate.getFilePath()), fileToValidate.getDelimiteurLigne().getCode(), fileToValidate.getDelimiteurColonne().getCode());
                ImportData.parseCsvFile(raw, validateur, outputFile, fileToValidate.isHeaderPresent(), alphaNumeriqueToBeTruncated);
                break;
 
            case XLS:
                ImportData.parseXlsFile(fileToValidate.getFilePath(), fileToValidate.isHeaderPresent(), outputFile, validateur, alphaNumeriqueToBeTruncated);
                break;
            case XLSX:
                ImportData.parseXlsxFile(fileToValidate.getFilePath(), fileToValidate.isHeaderPresent(), outputFile, validateur, alphaNumeriqueToBeTruncated);
                break;
            default:
                break;
        }
    }
 
    private String generateTierce() {
 
        // Instance of random class
        Random rand = new Random();
        int upperbound = 60;
        int random = rand.nextInt(upperbound);
 
        return String.format("%02d", random);
 
    }
 
    private boolean validateJson(FileToValidate fileToValidate, Configuration config) throws IOException {
 
        if (config == null) {
            throw new ConfigException("Validation impossible, les répertoires ne sont pas configurés. Bien vouloir les configurer avant avant d'effectuer cette action");
        }
        TypeFichierJson typefichierJson = TypeFichierJsonDAO.getByTypeFichier(fileToValidate.getTypeFichier().getCode());
 
        if (typefichierJson == null) {
            throw new ValidatorException("Validation impossible. Le type fichier " + fileToValidate.getTypeFichier().getCode() + " n'a pas de structure JSON enregistré");
        }
        Validator validator = new Validator();
        ConfigJsonValidator configJson = new ConfigJsonValidator(
                fileToValidate.getErrorFilePath(),
                config.getOutputDir(),
                new File(fileToValidate.getFilePath()),
                new TypeFichierJsonConverter(typefichierJson).convert()
        );
 
        return validator.validate(configJson, System.getProperty("user.dir") + "\\src\\main\\java", true, true, Utilities.getNbThreads());
    }
 
    private void validateJsonFile(Configuration config, ActionEvent event) {
        getCurrentObject().setErrorFilePath(Paths.get(config.getErrorDir(),
                getCurrentObject().getTypeFichier().getCode() + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss"))
                + generateTierce() + "_JSON.txt").toString());
 
        Task<Boolean> task = new Task<Boolean>() {
 
            @Override
            protected Boolean call() throws Exception {
 
                return validateJson(getCurrentObject(), config);
 
            }
 
        };
 
        task.setOnRunning(e -> LoadinIndicatorDialogUtil.getLoadingIndicatorDialog().show());
 
        task.setOnSucceeded(e -> {
 
            LoadinIndicatorDialogUtil.getLoadingIndicatorDialog().hide();
            Node source = (Node) event.getSource();
            Stage parentStage = (Stage) source.getScene().getWindow();
 
            boolean valid = task.getValue();
 
            String message;
            NotificationDialog notificationDialog;
 
            if (!valid) {
 
                message = "Echec de la validation. Le fichier contient des erreurs, veuillez consulter le répertoire " + config.getErrorDir() + ", puis ouvrir le fichier d'erreur "
                        + new File(getCurrentObject().getErrorFilePath()).getName() + ".err pour en savoir plus";
 
                notificationDialog = new NotificationDialog(message, NotificationType.ERROR, parentStage);
 
            } else {
 
                message = "Validation réussie. Fichier correcte";
 
                notificationDialog = new NotificationDialog(message, NotificationType.SUCCESS, parentStage);
            }
 
            logger.error(message);
 
            notificationDialog.showNotification();
 
        });
 
        task.setOnFailed(e -> {
 
            LoadinIndicatorDialogUtil.getLoadingIndicatorDialog().hide();
 
            Throwable th = task.getException();
 
            if (th instanceof ConfigException || th instanceof ValidatorException) {
 
                Node source = (Node) event.getSource();
 
                Stage parentStage = (Stage) source.getScene().getWindow();
 
                NotificationDialog notificationDialog = new NotificationDialog(th.getMessage(), NotificationType.WARNING, parentStage);
 
                logger.warn(th.getMessage());
 
                notificationDialog.showNotification();
 
            } else {
 
                logger.error(th);
 
                AlertMessageUtil.showAlertException(th, "Une exception s'est produite pendant la validation du fichier", "Erreur");
 
            }
 
        });
 
        new Thread(task).start();
 
    }
}