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
/*
 * 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.forms;
 
import com.megatimfx.common.abstracts.AbstractEditDialogController;
import com.megatimfx.common.abstracts.AbstractMainDialogController;
import com.megatimfx.common.pojo.SearchCriteria;
import com.megatimfx.common.service.GenericCrudService;
import com.megatimfx.common.utils.ViewLoaderUtil;
import com.megatimfx.components.customdialogs.LoadinIndicatorDialogUtil;
import com.megatimfx.components.dialogs.NotificationDialog;
import com.megatimfx.components.dialogs.NotificationType;
import com.megatim.fdxconvert.controller.ValidateurEditDialogController;
import com.megatim.fdxconvert.controller.search.ValidateurSearchFormController;
import com.megatim.fdxconvert.controller.table.ValidateurTable;
import com.megatim.fdxconvert.exceptions.ConfigException;
import com.megatim.fdxconvert.model.Configuration;
import com.megatim.fdxconvert.model.Validateur;
import com.megatim.fdxconvert.service.ConfigurationService;
import com.megatim.fdxconvert.service.ValidateurService;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.concurrent.Task;
import javafx.event.Event;
import javafx.fxml.FXML;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
import org.apache.commons.io.FileUtils;
 
/**
 *
 * @author STEPHANIE
 */
public class ValidateurListFormController extends AbstractMainDialogController<Validateur, Validateur> {
 
    private ValidateurSearchFormController validateurSearchFormController;
 
    private double dx, dy;
 
    @FXML
    private Button btnPrintValidator;
 
    @FXML
    private Label btnPrintValidatorLabel;
 
    @Override
    public void initialize(URL url, ResourceBundle rb) {
        validateurSearchFormController = new ValidateurSearchFormController();
        setPrintButtonRemoved(true);
        setEditButtonRemoved(true);
 
        btnPrintValidator.setOnAction(e -> {
            exportValidatorFile(e);
        });
        btnPrintValidatorLabel.setOnMouseClicked(e -> {
            exportValidatorFile(e);
        });
        super.initialize(url, rb);
    }
 
    @Override
    public GenericCrudService<Validateur> getGenericCrudService() {
        return ValidateurService.getInstance();
    }
 
    @Override
    public String getTitle() {
        return "Importations des formats de validation";
    }
 
    @Override
    public Class<Validateur> getElementClazz() {
        return Validateur.class;
    }
 
    @Override
    public List<TableColumn> getElementTableColumns() {
        return Arrays.asList(ValidateurTable.typeFichierColumn(), ValidateurTable.dataConversionColumn(), ValidateurTable.dateCreationColumn(), ValidateurTable.keyLengthColumn());
    }
 
    @Override
    public AbstractEditDialogController<Validateur> getAbstractEditDialogController() {
        return new ValidateurEditDialogController();
    }
 
    @Override
    public List<SearchCriteria> getPermanentSearchCriterias() {
        List<SearchCriteria> liste = new ArrayList<>();
 
        return liste;
    }
 
    @Override
    public Object getSearchFormController() {
        return validateurSearchFormController;
    }
 
    @Override
    public void resetFormFields(Object searchController, Object advancedSearchController) {
 
        if (searchController instanceof ValidateurSearchFormController) {
            ValidateurSearchFormController controller = (ValidateurSearchFormController) searchController;
            controller.getTypeFichierAbstractSelectionItem().setOldElement(null);
        }
    }
 
    @Override
    public Pane getSearchFormPane() throws IOException {
        return ViewLoaderUtil.getPaneFromFxmlFile(validateurSearchFormController.getClass().getResource("ValidateurSearchForm.fxml"), validateurSearchFormController);
    }
 
    @FXML
    public void titleBarMousePressed(MouseEvent event) {
        Scene scene = ((Node) event.getSource()).getScene();
        Stage stage = (Stage) scene.getWindow();
 
        dx = stage.getX() - event.getScreenX();
        dy = stage.getY() - event.getScreenY();
    }
 
    @FXML
    public void titleBarMouseDragged(MouseEvent event) {
        Scene scene = ((Node) event.getSource()).getScene();
        Stage stage = (Stage) scene.getWindow();
 
        stage.setX(event.getScreenX() + dx);
        stage.setY(event.getScreenY() + dy);
    }
 
    private void exportValidatorFile(Event event) {
        Node node = (Node) event.getSource();
        Stage parentStage = (Stage) node.getScene().getWindow();
        Validateur validateur = getElementTable().getSelectionModel().getSelectedItem();
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyyMMdd-HHmmss");
 
        try {
            Configuration config = ConfigurationService.getInstance().getCurrentConfig();
 
            if (config == null) {
                throw new ConfigException("Répertoires non configurés");
            }
 
            if (validateur != null) {
 
                exportOneFile(validateur.getCodeTypeFichier(), config.getValidatorsDir(), dtf, validateur.getContent());
 
                NotificationDialog notificationDialog = new NotificationDialog(
                        "Succès de l'exportation, consulter le répertoire '" + config.getValidatorsDir() + "' pour voir le fichier validateur du typefichier " + validateur.getCodeTypeFichier(),
                        NotificationType.SUCCESS,
                        parentStage
                );
                notificationDialog.showNotification();
 
            } else {
                List<Validateur> validateurs = ValidateurService.getInstance().getAll();
                Task<Void> task = new Task<Void>() {
                    @Override
                    protected Void call() throws Exception {
 
                        for (Validateur v : validateurs) {
                            exportOneFile(v.getCodeTypeFichier(), config.getValidatorsDir(), dtf, v.getContent());
                        }
                        return null;
                    }
 
                };
                task.setOnRunning(e -> LoadinIndicatorDialogUtil.getLoadingIndicatorDialog().show());
                task.setOnSucceeded(e -> {
                    String message = "";
 
                    if (validateurs.isEmpty()) {
                        message = "Aucun validateur à exporter";
                    } else {
                        message = "Tous les validateurs ont été exporté dans le répertoire " + config.getValidatorsDir();
                    }
                    LoadinIndicatorDialogUtil.getLoadingIndicatorDialog().hide();
                    NotificationDialog notificationDialog = new NotificationDialog(
                            message, NotificationType.INFO, parentStage
                    );
                    notificationDialog.showNotification();
                });
                task.setOnFailed(e -> {
                    LoadinIndicatorDialogUtil.getLoadingIndicatorDialog().hide();
                    NotificationDialog notificationDialog = new NotificationDialog(
                            "Echec de l'exportation, une erreur est survenue ",
                            NotificationType.ERROR,
                            parentStage
                    );
                    notificationDialog.showNotification();
                });
 
                Thread thread1 = new Thread(task);
                thread1.setDaemon(true);
                thread1.start();
            }
        } catch (ConfigException ex) {
            NotificationDialog notificationDialog = new NotificationDialog(
                    "Echec de l'exportation, Veuillez configurer les répertoires avant d'exécuter cette opération",
                    NotificationType.WARNING,
                    parentStage
            );
            notificationDialog.showNotification();
 
        } catch (Exception ex) {
 
            NotificationDialog notificationDialog = new NotificationDialog(
                    "Echec de l'exportation, une erreur est survenue ",
                    NotificationType.ERROR,
                    parentStage
            );
            notificationDialog.showNotification();
            Logger.getLogger(ValidateurListFormController.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
 
    private void exportOneFile(String codeTypeFichier, String validatorsDir, DateTimeFormatter dtf, byte[] content) throws IOException {
        File file = new File(validatorsDir, "Fdx-Validateur" + codeTypeFichier + "-" + dtf.format(LocalDateTime.now()) + ".xml");
        FileUtils.writeByteArrayToFile(file, content, false);
    }
}