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
/*
 * 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.controller;
 
import com.megatim.typefichier.validator.Validator;
import com.megatim.typefichier.validator.model.ConfigStreamValidator;
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.fdxconvert.App;
import com.megatim.fdxconvert.dao.ValidateurDAO;
import com.megatim.fdxconvert.exceptions.ConfigException;
import com.megatim.fdxconvert.exceptions.ValidatorException;
import com.megatim.fdxconvert.forms.TxtFileToValidateEditFormController;
import com.megatim.fdxconvert.model.Configuration;
import com.megatim.fdxconvert.pojo.TxtFileToValidate;
import com.megatim.fdxconvert.model.Validateur;
import com.megatim.fdxconvert.service.ConfigurationService;
import com.megatim.fdxconvert.util.Utilities;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URL;
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 org.apache.commons.io.IOUtils;
 
/**
 *
 * @author STEPHANIE
 */
public class TxtFileToValidateEditDialogController extends AbstractEditDialogController<TxtFileToValidate> {
 
    private TxtFileToValidateEditFormController txtFileToValidateEditFormController;
 
    @Override
    public String getTitle() {
        getEditButton().setText("Vérifier");
        return "Vérification format de fichier Fdx";
    }
 
    @Override
    public Pane getContentFormPane() throws IOException {
        return ViewLoaderUtil.getPaneFromFxmlFile(txtFileToValidateEditFormController.getClass().getResource("TxtFileToValidateEditForm.fxml"), txtFileToValidateEditFormController);
 
    }
 
    @Override
    public Object getContentFormController() {
        return txtFileToValidateEditFormController;
    }
 
    @Override
    public void initialize(URL url, ResourceBundle rb) {
        txtFileToValidateEditFormController = new TxtFileToValidateEditFormController();
        super.initialize(url, rb);
    }
 
    @Override
    public void afterSave(ActionEvent event) {
 
        Configuration config = ConfigurationService.getInstance().getCurrentConfig();
 
        Task<Boolean> task = new Task<Boolean>() {
 
            @Override
            protected Boolean call() throws Exception {
 
                TxtFileToValidate val = getCurrentObject();
 
                return validate(val, 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();
 
            if (valid) {
 
                 NotificationDialog notificationDialog = new NotificationDialog(
                        "Vérification réussie. Fichier correcte",
                        NotificationType.SUCCESS,
                        parentStage
                );
 
                notificationDialog.showNotification();
                
            } else {
                 NotificationDialog notificationDialog = new NotificationDialog(
                        "Echec de la vérification. Le fichier contient des erreurs, veuillez consulter le fichier d'erreur pour en savoir plus",
                        NotificationType.ERROR,
                        parentStage
                );
 
                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);
 
                notificationDialog.showNotification();
 
            } else {
 
                AlertMessageUtil.showAlertException(th, "Une exception s'est produite pendant la validation du fichier", "Erreur");
 
            }
 
        });
 
        new Thread(task).start();
 
    }
 
    private boolean validate(TxtFileToValidate val, Configuration config) throws Exception {
 
        if (config == null) {
            throw new ConfigException("Validation impossible, les repertoires ne sont pas configurés. Bien vouloir les configurer avant avant d'effectuer cette action");
        }
 
        String codeTypeFichier = val.getTypeFichier().getCode();
 
        Optional<Validateur> validateur = ValidateurDAO.findByCodeTypeFichier(codeTypeFichier);
 
        if (!validateur.isPresent()) {
            throw new ValidatorException("Validation impossible. Le type fichier " + codeTypeFichier + " n'a pas de validateur enregistré");
        }
 
        File file = new File(val.getTxtFilePath());
 
        if (!file.exists()) {
            throw new FileNotFoundException("Le fichier " + file.getAbsolutePath() + " est introuvable");
        }
 
        byte[] targetArray = IOUtils.toByteArray(App.class.getClass().getResourceAsStream("/predicatelogic-engine.xml"));
 
        ConfigStreamValidator configValidator = new ConfigStreamValidator(targetArray, validateur.get().getContent(), config.getErrorDir(), config.getOutputDir(), file,file);
 
        return new Validator().validate(configValidator, Boolean.FALSE, Boolean.FALSE,Utilities.getNbThreads());
    }
}