Kenmegne
7 days ago 494d349fb67be74da49caae2794fda702f595fb4
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
package com.megatim.fdxgenerator.controller;
 
import com.megatim.module.compression.impl.JavaZipZip4Impl;
import com.megatim.module.encryption.ifaces.EncryptionFace;
import com.megatim.module.encryption.impl.AESImpl;
import com.megatimfx.common.abstracts.AbstractEditDialogController;
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.fdxgenerator.enums.DataType;
import com.megatim.fdxgenerator.enums.TypeDonnee;
import com.megatim.fdxgenerator.exceptions.ConfigException;
import com.megatim.fdxgenerator.forms.FichierValidationEditFormController;
import com.megatim.fdxgenerator.model.Configuration;
import com.megatim.fdxgenerator.model.FichierValidation;
import com.megatim.fdxgenerator.model.StructureLigne;
import com.megatim.fdxgenerator.model.ValidateurFichier;
import com.megatim.fdxgenerator.model.dao.StructureLigneDAO;
import com.megatim.fdxgenerator.model.dao.ValidateurFichierDAO;
import com.megatim.fdxgenerator.service.ConfigurationService;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.ResourceBundle;
import javafx.concurrent.Task;
import javafx.event.ActionEvent;
import javafx.scene.Node;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
import javax.activation.UnsupportedDataTypeException;
import org.apache.commons.io.FileUtils;
 
/**
 *
 * @author STEPHANIE
 */
public class FichierValidationEditDialogController extends AbstractEditDialogController<FichierValidation> {
 
    private FichierValidationEditFormController fichierValidationEditFormController;
 
    private int index;
 
    Boolean result = true;
 
    private final String ENCRYPT_DIR = "ENCRYPT_DIR";
 
    @Override
    public void initialize(URL url, ResourceBundle rb) {
        fichierValidationEditFormController = new FichierValidationEditFormController();
        super.initialize(url, rb);
    }
 
    @Override
    public String getTitle() {
        getEditButton().setText("Générer");
        return "Génération du fichier de validation d'un type fichier";
    }
 
    @Override
    public Pane getContentFormPane() throws IOException {
        return ViewLoaderUtil.getPaneFromFxmlFile(fichierValidationEditFormController.getClass().getResource("FichierValidationEditForm.fxml"), fichierValidationEditFormController);
 
    }
 
    @Override
    public Object getContentFormController() {
        return fichierValidationEditFormController;
    }
 
    @Override
    public boolean beforeSave(ActionEvent event) {
        boolean bool = super.beforeSave(event);
 
        if (fichierValidationEditFormController.getIsProtegeCheckBox().isSelected()) {
            return bool && fichierValidationEditFormController.getKeyConfirmationField() != null && fichierValidationEditFormController.getKeyToEncryptField() != null;
        } else {
            return bool;
        }
    }
 
    private Boolean generateFichierValidation(FichierValidation f, Configuration config) throws Exception {
        final String charset = "UTF-8";
        String fileExtension = ".xml";
        String fileNamePrefix = "Fdx-Validator";
        index = 0;
        result = true;
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyyMMdd-HHmmss");
 
        if (config == null) {
            throw new ConfigException("Génération impossible, le répertoire de dépôt des fichiers de validation n'est pas configuré");
        }
        try {
            Optional<ValidateurFichier> validateurFichier = ValidateurFichierDAO.findByTypeFichier(f.getTypeFichier().getCode());
            File file = new File(config.getOutputDir(), fileNamePrefix + f.getTypeFichier().getCode() + "-" + dateTimeFormatter.format(LocalDateTime.now()) + fileExtension);
 
            if (validateurFichier.isPresent()) {
                ValidateurFichier vf = validateurFichier.get();
 
                openTags(file, charset);
                FileUtils.writeStringToFile(file, "\t\t<entity name=\"com.megatim.typefichier.validator.model.GenericTypeFichier\" error-report-field=\"codeErreur\" activate=\"true\"> \n\n", charset, true);
                fileNameValidation(f, file, charset);
 
                //vérifie si le fichier est vide
                emptyFileValidation(file, charset);
                FileUtils.writeStringToFile(file, "\t\t</entity>\n\n", charset, true);
 
                //validation des lignes de données
                String csvClass = "ExcelFileLine";
                String txtClass = "FileLine";
                String entityTagPart1 = "\t\t<entity name=\"com.megatim.typefichier.validator.model.";
                String entityTagPart2 = " error-report-field=\"codeErreur\" activate=\"true\"";
                String newLine = ">  \n \n";
 
                String extension = "\" extension=\"" + vf.getDataType().name().toLowerCase() + "\" ";
                String headerPresent = " header-present=\"" + vf.isHeaderPresent() + "\"";
 
                switch (vf.getDataType()) {
                    case CSV:
                        String lineDelimiter = " line-delimiter=\"" + vf.getCodeDelimiteurLigne() + "\"";
                        String columnDelimiter = " column-delimiter=\"" + vf.getCodeDelimiteurColonne() + "\"";
                        FileUtils.writeStringToFile(file, entityTagPart1 + csvClass + extension + entityTagPart2 + headerPresent + lineDelimiter + columnDelimiter + newLine, charset, true);
                        break;
                    case XLS:
                    case XLSX:
                        FileUtils.writeStringToFile(file, entityTagPart1 + csvClass + extension + entityTagPart2 + headerPresent + newLine, charset, true);
                        break;
                    case TXT:
                        FileUtils.writeStringToFile(file, entityTagPart1 + txtClass + extension + entityTagPart2 + newLine, charset, true);
                        break;
                    default:
                        throw new UnsupportedDataTypeException("'" + vf.getDataType() + "' : format inconnu");
                }
                columnsValidation(vf, file, charset);
 
                if (vf.getDataType().equals(DataType.TXT)) {
                    lineLengthValidation(f, file, charset);
                }
                closeTags(file, charset);
 
            } else {
                FileUtils.writeStringToFile(file, "Le type fichier " + f.getTypeFichier().getCode() + " n'a pas de validateur enregistré ", "UTF-8", false);
            }
            if (fichierValidationEditFormController.getIsProtegeCheckBox().isSelected()) {
                result = encryptAndZipFile(file, config, f.getKeyToEncrypt());
            }
        } catch (Exception ex) {
            result = false;
        }
        return result;
    }
 
    private void openTags(File file, final String charset) throws IOException {
        FileUtils.writeStringToFile(file, "<?xml version=\"1.0\" encoding=\"UTF-8\"?> \n", charset, false);
        FileUtils.writeStringToFile(file, "<predicate-config xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" \n", charset, true);
        FileUtils.writeStringToFile(file, "xsi:schemaLocation=\"http://www.leadware.net/predicatelogic-config ../xsd/predicatelogic-config.xsd\" \n", charset, true);
        FileUtils.writeStringToFile(file, "xmlns=\"http://www.leadware.net/predicatelogic-config\"> \n \n", charset, true);
        FileUtils.writeStringToFile(file, "\t<entities> \n", charset, true);
    }
 
    private void fileNameValidation(FichierValidation f, File file, final String charset) throws IOException {
        int fileNameIndex = 0;
 
        if (f.isValidateFileName()) {
 
            //validation du code participant dans le nom du fichier
            FileUtils.writeStringToFile(file, "\t\t\t<validation field-name=\"CodeParticipant\" activate=\"true\">\n", charset, true);
            FileUtils.writeStringToFile(file, "\t\t\t\t<assertion assert-name=\"ifCodeAgentOrCodeParticipant\" error-code=\"CodeParticipant\" activate=\"true\">\n", charset, true);
            FileUtils.writeStringToFile(file, "\t\t\t\t\t<param-value name=\"code\" value=\"" + f.getTypeFichier().getCodeParticipant() + "\" />\n", charset, true);
            FileUtils.writeStringToFile(file, "\t\t\t\t\t<param-value name=\"fileName\" field=\"fileName\" />\n", charset, true);
            FileUtils.writeStringToFile(file, "\t\t\t\t\t<param-value name=\"index\" value=\"" + fileNameIndex + "\" />\n", charset, true);
            FileUtils.writeStringToFile(file, "\t\t\t\t\t<param-value name=\"taille\" value=\"" + f.getTypeFichier().getCodeParticipant().length() + "\" />\n", charset, true);
            FileUtils.writeStringToFile(file, "\t\t\t\t</assertion>\n", charset, true);
            FileUtils.writeStringToFile(file, "\t\t\t</validation>\n\n", charset, true);
 
            fileNameIndex += f.getTypeFichier().getCode().length();
 
            //validation de la date dans le nom du fichier
            FileUtils.writeStringToFile(file, "\t\t\t<validation field-name=\"Date\" activate=\"true\">\n", charset, true);
            FileUtils.writeStringToFile(file, "\t\t\t\t<assertion assert-name=\"ifDate\" error-code=\"Date\" activate=\"true\">\n", charset, true);
            FileUtils.writeStringToFile(file, "\t\t\t\t\t<param-value name=\"format\" value=\"" + f.getFormatDate() + "\" />\n", charset, true);
            FileUtils.writeStringToFile(file, "\t\t\t\t\t<param-value name=\"fileName\" field=\"fileName\" />\n", charset, true);
            FileUtils.writeStringToFile(file, "\t\t\t\t\t<param-value name=\"index\" value=\"" + fileNameIndex + "\" />\n", charset, true);
            FileUtils.writeStringToFile(file, "\t\t\t\t</assertion>\n", charset, true);
            FileUtils.writeStringToFile(file, "\t\t\t</validation>\n\n", charset, true);
            fileNameIndex += f.getFormatDate().length();
 
            //validation des tierces
            if (f.getMaxValueTierce() > 0) {
                FileUtils.writeStringToFile(file, "\t\t\t<validation field-name=\"Tierce\" activate=\"true\">\n", charset, true);
                FileUtils.writeStringToFile(file, "\t\t\t\t<assertion assert-name=\"ifTierce\" error-code=\"Tierce\" activate=\"true\">\n", charset, true);
                FileUtils.writeStringToFile(file, "\t\t\t\t\t<param-value name=\"minValue\" value=\"0\" />\n", charset, true);
                FileUtils.writeStringToFile(file, "\t\t\t\t\t<param-value name=\"maxValue\" value=\"" + f.getMaxValueTierce() + "\" />\n", charset, true);
                FileUtils.writeStringToFile(file, "\t\t\t\t\t<param-value name=\"index\" value=\"" + fileNameIndex + "\" />\n", charset, true);
                FileUtils.writeStringToFile(file, "\t\t\t\t\t<param-value name=\"fileName\" field=\"fileName\" />\n", charset, true);
                FileUtils.writeStringToFile(file, "\t\t\t\t</assertion>\n", charset, true);
                FileUtils.writeStringToFile(file, "\t\t\t</validation>\n\n", charset, true);
            }
 
            //validation de l'extension du fichier
            FileUtils.writeStringToFile(file, "\t\t\t<validation field-name=\"Extension\" activate=\"true\">\n", charset, true);
            FileUtils.writeStringToFile(file, "\t\t\t\t<assertion assert-name=\"equal\" error-code=\"Extension\" activate=\"true\">\n", charset, true);
            FileUtils.writeStringToFile(file, "\t\t\t\t\t<param-value name=\"equalValue\" value=\"" + f.getExtension() + "\" />\n", charset, true);
            FileUtils.writeStringToFile(file, "\t\t\t\t\t<param-value name=\"equalParam\" field=\"extension\" />\n", charset, true);
            FileUtils.writeStringToFile(file, "\t\t\t\t</assertion>\n", charset, true);
            FileUtils.writeStringToFile(file, "\t\t\t</validation>\n\n", charset, true);
        }
    }
 
    private void emptyFileValidation(File file, final String charset) throws IOException {
        FileUtils.writeStringToFile(file, "\t\t\t<validation field-name=\"FichierVide\" activate=\"true\">\n", charset, true);
        FileUtils.writeStringToFile(file, "\t\t\t\t\t<assertion assert-name=\"notEqual\" error-code=\"FichierVide\" activate=\"true\">\n", charset, true);
        FileUtils.writeStringToFile(file, "\t\t\t\t\t<param-value name=\"notEqualValue\" value=\"0\" />\n", charset, true);
        FileUtils.writeStringToFile(file, "\t\t\t\t\t<param-value name=\"notEqualParam\" field=\"nbOfLines\" />\n", charset, true);
        FileUtils.writeStringToFile(file, "\t\t\t\t</assertion>\n", charset, true);
        FileUtils.writeStringToFile(file, "\t\t\t</validation>\n\n", charset, true);
    }
 
    private void lineLengthValidation(FichierValidation f, File file, final String charset) throws IOException {
        FileUtils.writeStringToFile(file, "\t\t\t<validation field-name=\"validation" + f.getTypeFichier().getCode() + "Taille\" activate=\"true\"> \n \n", charset, true);
        FileUtils.writeStringToFile(file, "\t\t\t\t<assertion assert-name=\"equal\" error-code=\"" + f.getTypeFichier().getCode() + "Taille\" activate=\"true\"> \n", charset, true);
        FileUtils.writeStringToFile(file, "\t\t\t\t\t<param-value name=\"equalValue\" value=\"" + index + "\" /> \n", charset, true);
        FileUtils.writeStringToFile(file, "\t\t\t\t\t<param-value name=\"equalParam\" field=\"taille\" /> \n", charset, true);
        FileUtils.writeStringToFile(file, "\t\t\t\t</assertion> \n \n", charset, true);
        FileUtils.writeStringToFile(file, "\t\t\t</validation> \n", charset, true);
    }
 
    private void closeTags(File file, final String charset) throws IOException {
        FileUtils.writeStringToFile(file, "\t\t</entity> \n\n", charset, true);
        FileUtils.writeStringToFile(file, "\t</entities> \n", charset, true);
        FileUtils.writeStringToFile(file, "</predicate-config>", charset, true);
    }
 
    private void columnsValidation(ValidateurFichier vf, File file, final String charset) throws IOException {
        List<StructureLigne> structureLignes = StructureLigneDAO.findByValidateurFichierId(vf.getId());
        Collections.sort(structureLignes, (StructureLigne s1, StructureLigne s2) -> new Integer(s1.getPosition()).compareTo(s2.getPosition()));
 
        switch (vf.getDataType()) {
            case TXT:
                txtColumns(structureLignes, file, charset);
                break;
            case CSV:
            case XLS:
            case XLSX:
                csvColumns(structureLignes, file, charset);
                break;
            default:
                throw new UnsupportedDataTypeException("Format de données inconnu");
        }
    }
 
    private void txtColumns(List<StructureLigne> structureLignes, File file, final String charset) throws IOException {
 
        for (StructureLigne s : structureLignes) {
            FileUtils.writeStringToFile(file, "\t\t\t<validation field-name=\"" + s.getCode() + "\" activate=\"true\"> \n \n", charset, true);
            FileUtils.writeStringToFile(file, "\t\t\t\t<assertion assert-name=\"ifTypeFichier\" error-code=\"" + s.getCode() + "\" activate=\"true\"> \n", charset, true);
            FileUtils.writeStringToFile(file, "\t\t\t\t\t<param-value name=\"position\" value=\"" + s.getPosition() + "\" /> \n", charset, true);
            FileUtils.writeStringToFile(file, "\t\t\t\t\t<param-value name=\"index\" value=\"" + index + "\" /> \n", charset, true);
 
            if (s.getTypeDonnee().getValue().equals(TypeDonnee.DATE.getValue()) && s.getSeparateurDate() != null && !s.getSeparateurDate().isEmpty()) {
                structureLigneNewLength(s);
            }
 
            FileUtils.writeStringToFile(file, "\t\t\t\t\t<param-value name=\"taille\" value=\"" + s.getTaille() + "\" /> \n", charset, true);
            FileUtils.writeStringToFile(file, "\t\t\t\t\t<param-value name=\"typeDonnee\" value=\"" + s.getTypeDonnee() + "\" /> \n", charset, true);
 
            if (s.getTypeDonnee().getValue().equals(TypeDonnee.DATE.getValue())) {
 
                FileUtils.writeStringToFile(file, "\t\t\t\t\t<param-value name=\"formatDate\" value=\"" + s.getFormatDate() + "\" /> \n", charset, true);
 
                if (s.getSeparateurDate() != null && !s.getSeparateurDate().isEmpty()) {
                    FileUtils.writeStringToFile(file, "\t\t\t\t\t<param-value name=\"separateurDate\" value=\"" + s.getSeparateurDate() + "\" /> \n", charset, true);
                }
            }
 
            if (s.getTypeDonnee().equals(TypeDonnee.DECIMAL)) {
                index += s.getTaillePartieDecimal() + s.getSeparateurDecimal().length();
                FileUtils.writeStringToFile(file, "\t\t\t\t\t<param-value name=\"taillePartieDecimale\" value=\"" + s.getTaillePartieDecimal() + "\" /> \n", charset, true);
                FileUtils.writeStringToFile(file, "\t\t\t\t\t<param-value name=\"separateurDecimal\" value=\"" + s.getSeparateurDecimal() + "\" /> \n", charset, true);
            }
            FileUtils.writeStringToFile(file, "\t\t\t\t\t<param-value name=\"ligne\" field=\"ligne\" /> \n", charset, true);
            FileUtils.writeStringToFile(file, "\t\t\t\t</assertion> \n \n", charset, true);
            FileUtils.writeStringToFile(file, "\t\t\t</validation> \n \n", charset, true);
 
            index += s.getTaille();
        }
    }
 
    //SI on suppose que les dates sont au format yyyy-MM-dd où le tiret (-) est le séparateur dans cet exemple 
    //on aura donc 2 séparateurs par date et dans dans ce cas, on ajoute 2, à l'index de fin de la date
    private void structureLigneNewLength(StructureLigne s) {
        //Si la date contient le mois, ajouter 1 à la taille pour le séparateur(-)
        if (s.getFormatDate().contains("MM")) {
            s.setTaille(s.getTaille() + 1);
        }
 
        //Si la date contient le jour, ajouter 1 à la taille pour le séparateur(-)
        if (s.getFormatDate().contains("dd")) {
            s.setTaille(s.getTaille() + 1);
        }
 
        //Si la date contient l'heure, ajouter 1 à la taille pour le séparateur (L'espace ou T) 
        if (s.getFormatDate().contains("HH")) {
            s.setTaille(s.getTaille() + 1);
        }
 
        //Si la date contient la minute, ajouter 1 à la taille pour le séparateur (:) 
        if (s.getFormatDate().contains("mm")) {
            s.setTaille(s.getTaille() + 1);
        }
 
        //Si la date contient la seconde, ajouter 1 à la taille pour le séparateur (:) 
        if (s.getFormatDate().contains("ss")) {
            s.setTaille(s.getTaille() + 1);
        }
    }
 
    private void csvColumns(List<StructureLigne> structureLignes, File file, final String charset) throws IOException {
        for (StructureLigne s : structureLignes) {
            FileUtils.writeStringToFile(file, "\t\t\t<validation field-name=\"" + s.getCode() + "\" activate=\"true\"> \n \n", charset, true);
            FileUtils.writeStringToFile(file, "\t\t\t\t<assertion assert-name=\"ifTypeFichierCsv\" error-code=\"" + s.getCode() + "\" activate=\"true\"> \n", charset, true);
            FileUtils.writeStringToFile(file, "\t\t\t\t\t<param-value name=\"position\" value=\"" + s.getPosition() + "\" /> \n", charset, true);
            FileUtils.writeStringToFile(file, "\t\t\t\t\t<param-value name=\"typeDonnee\" value=\"" + s.getTypeDonnee() + "\" /> \n", charset, true);
 
            if (s.getTypeDonnee().getValue().equals(TypeDonnee.DATE.getValue())) {
 
                FileUtils.writeStringToFile(file, "\t\t\t\t\t<param-value name=\"formatDate\" value=\"" + s.getFormatDate() + "\" /> \n", charset, true);
 
                if (s.getSeparateurDate() != null && !s.getSeparateurDate().isEmpty()) {
                    structureLigneNewLength(s);
                    FileUtils.writeStringToFile(file, "\t\t\t\t\t<param-value name=\"separateurDate\" value=\"" + s.getSeparateurDate() + "\" /> \n", charset, true);
                }
            } else if (s.getTypeDonnee().equals(TypeDonnee.DECIMAL)) {
                FileUtils.writeStringToFile(file, "\t\t\t\t\t<param-value name=\"taillePartieDecimale\" value=\"" + s.getTaillePartieDecimal() + "\" /> \n", charset, true);
                FileUtils.writeStringToFile(file, "\t\t\t\t\t<param-value name=\"separateurDecimal\" value=\"" + s.getSeparateurDecimal() + "\" /> \n", charset, true);
 
            }
            FileUtils.writeStringToFile(file, "\t\t\t\t\t<param-value name=\"taille\" value=\"" + s.getTaille() + "\" /> \n", charset, true);
            FileUtils.writeStringToFile(file, "\t\t\t\t\t<param-value name=\"ligne\" field=\"columns\" /> \n", charset, true);
            FileUtils.writeStringToFile(file, "\t\t\t\t</assertion> \n \n", charset, true);
            FileUtils.writeStringToFile(file, "\t\t\t</validation> \n \n", charset, true);
        }
        FileUtils.writeStringToFile(file, "\t\t\t<validation field-name=\"NBRE_COLONNES\" activate=\"true\"> \n \n", charset, true);
        FileUtils.writeStringToFile(file, "\t\t\t\t<assertion assert-name=\"equal\" error-code=\"NBRE_COLONNES\" activate=\"true\"> \n", charset, true);
        FileUtils.writeStringToFile(file, "\t\t\t\t\t<param-value name=\"equalValue\" value=\"" + structureLignes.size() + "\" /> \n", charset, true);
        FileUtils.writeStringToFile(file, "\t\t\t\t\t<param-value name=\"equalParam\" field=\"nbreColonnes\" /> \n", charset, true);
        FileUtils.writeStringToFile(file, "\t\t\t\t</assertion> \n \n", charset, true);
        FileUtils.writeStringToFile(file, "\t\t\t</validation> \n \n", charset, true);
 
    }
 
    private Boolean encryptAndZipFile(File file, Configuration config, String keyToEncrypt) throws Exception {
        Boolean res = false;
        Boolean bool = encryptFile(file, config, keyToEncrypt);
 
        if (bool) {
            JavaZipZip4Impl zip4Impl = new JavaZipZip4Impl();
            String destinationPath = config.getOutputDir();
            File encryptDir = new File(config.getOutputDir(), ENCRYPT_DIR);
            File encryptFile = new File(encryptDir.getAbsolutePath(), file.getName());
 
            zip4Impl.zipFile(encryptFile, new File(destinationPath));
            res = true;
 
            file.delete();
            encryptFile.delete();
        }
 
        return res;
    }
 
    private Boolean encryptFile(File file, Configuration config, String keyToEncrypt) throws Exception {
        File encryptDir = new File(config.getOutputDir(), ENCRYPT_DIR);
 
        if (!encryptDir.exists()) {
            encryptDir.mkdir();
        }
 
        EncryptionFace aes = new AESImpl();
        aes.encryptFile(keyToEncrypt, 128, file.getAbsolutePath(), new File(encryptDir.getAbsolutePath(), file.getName()).getAbsolutePath());
 
        return true;
    }
 
    @Override
    public void afterSave(ActionEvent event) {
        Configuration config = ConfigurationService.getInstance().getCurrentConfig();
        Task<Boolean> task = new Task<Boolean>() {
            @Override
            protected Boolean call() throws Exception {
                FichierValidation f = getCurrentObject();
                return generateFichierValidation(f, config);
            }
        };
        Node source = (Node) event.getSource();
        Stage parentStage = (Stage) source.getScene().getWindow();
 
        task.setOnRunning(e -> LoadinIndicatorDialogUtil.getLoadingIndicatorDialog().show());
        task.setOnFailed(e -> {
            LoadinIndicatorDialogUtil.getLoadingIndicatorDialog().hide();
            Throwable th = task.getException();
 
            if (th instanceof ConfigException) {
                NotificationDialog notificationDialog = new NotificationDialog(
                        th.getMessage(), NotificationType.WARNING, parentStage);
                notificationDialog.showNotification();
            } else {
                AlertMessageUtil.showAlertException(th, "Une exception s'est produite pendant la génération du fichier de validation", "Erreur");
            }
        });
        task.setOnSucceeded(e -> {
            LoadinIndicatorDialogUtil.getLoadingIndicatorDialog().hide();
            Boolean taskResult = task.getValue();
 
            if (taskResult) {
                NotificationDialog notificationDialog = new NotificationDialog(
                        "Génération du fichier de validation réussie",
                        NotificationType.SUCCESS,
                        parentStage
                );
                notificationDialog.showNotification();
            } else {
                NotificationDialog notificationDialog = new NotificationDialog(
                        "Echec Génération du fichier de validation. Une erreur est survenue lors du traitement",
                        NotificationType.ERROR,
                        parentStage
                );
                notificationDialog.showNotification();
            }
        });
        new Thread(task).start();
    }
}