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
/*
 * 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.megatimfx.common.abstracts.AbstractEditDialogController;
import com.megatimfx.common.enums.Operateur;
import com.megatimfx.common.pojo.SearchCriteria;
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.enums.DataType;
import com.megatim.fdxconvert.exceptions.ConfigException;
import com.megatim.fdxconvert.exceptions.ValidatorException;
import com.megatim.fdxconvert.forms.DataFileToJsonFormController;
import com.megatim.fdxconvert.model.Configuration;
import com.megatim.fdxconvert.model.Validateur;
import com.megatim.fdxconvert.pojo.ColumnDefinition;
import com.megatim.fdxconvert.pojo.DataFileToJson;
import com.megatim.fdxconvert.service.ConfigurationService;
import com.megatim.fdxconvert.util.ExportToJson;
import com.megatim.fdxconvert.pojo.FileToValidateDescription;
import com.megatim.fdxconvert.service.ValidateurService;
import com.megatim.typefichier.validator.utilities.ParserUtils;
import com.megatim.fdxconvert.util.ValidateurParser;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
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;
 
/**
 *
 * @author ASUS
 */
public class DataFileToJsonEditDialogController extends AbstractEditDialogController<DataFileToJson> {
 
    org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(DataFileToJsonEditDialogController.class);
    private DataFileToJsonFormController txtFileToJsonFormController;
 
    @Override
    public void initialize(URL url, ResourceBundle rb) {
        txtFileToJsonFormController = new DataFileToJsonFormController();
        super.initialize(url, rb);
 
    }
 
    @Override
    public String getTitle() {
        getEditButton().setText("Convertir");
        return "Conversion d'un fichier de données en JSON";
    }
 
    @Override
    public Pane getContentFormPane() throws IOException {
        return ViewLoaderUtil.getPaneFromFxmlFile(txtFileToJsonFormController.getClass().getResource("DataFileToJsonEditForm.fxml"), txtFileToJsonFormController);
 
    }
 
    @Override
    public Object getContentFormController() {
        return txtFileToJsonFormController;
    }
 
    @Override
    public void afterSave(ActionEvent event) {
        List<Validateur> validateurs = ValidateurService.getInstance().getAll(Arrays.asList(validateurCriterion(getCurrentObject().getTypeFichier().getCode())));
        boolean proceed = !validateurs.isEmpty() || getCurrentObject().getValidateurPath() != null;
        Task<Void> task = new Task<Void>() {
 
            @Override
            protected Void call() throws Exception {
                parseFile();
                return null;
            }
        };
        task.setOnRunning(e -> LoadinIndicatorDialogUtil.getLoadingIndicatorDialog().show());
        task.setOnSucceeded(e -> {
 
            LoadinIndicatorDialogUtil.getLoadingIndicatorDialog().hide();
            Node source = (Node) event.getSource();
            Stage parentStage = (Stage) source.getScene().getWindow();
 
            NotificationDialog notificationDialog = new NotificationDialog("Conversion en JSON reussie", NotificationType.SUCCESS, 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);
                logger.warn(th.getMessage());
                notificationDialog.showNotification();
 
            } else {
                logger.error(th);
                AlertMessageUtil.showAlertException(th, "Une exception s'est produite pendant la validation du fichier", "Erreur");
            }
        });
        if (proceed) {
            new Thread(task).start();
        } else {
            AlertMessageUtil.showAlertException(new Exception(""), "Validateur de " + getCurrentObject().getTypeFichier().getCode() + " introuvable", "Erreur");
        }
    }
 
    private SearchCriteria validateurCriterion(String codeTypeFichier) {
        SearchCriteria criterion = new SearchCriteria();
        criterion.setFieldName("codeTypeFichier");
        criterion.setFieldType(String.class);
        criterion.setFieldValue(codeTypeFichier);
        criterion.setOperateur(Operateur.EQUALS);
 
        return criterion;
    }
 
    private void parseFile() throws Exception {
        Configuration config = ConfigurationService.getInstance().getCurrentConfig();
        List<ColumnDefinition> colDefs = new ArrayList<>();
 
        if (getCurrentObject().getValidateurPath()!= null) {
            colDefs = ValidateurParser.retrieveFieldsFromValidator(new File(getCurrentObject().getValidateurPath()), getCurrentObject().getTypeFichier().getCode());
        } else {
            ParserUtils.getValidateurElements(getCurrentObject().getValidateurBytes());
        }
        parseFile(getCurrentObject(), config, colDefs);
    }
 
    private void parseFile(DataFileToJson txtFileToJson, Configuration config, List<ColumnDefinition> colDefs) throws Exception {
        String fileName = (new File(txtFileToJson.getFileToConvertPath())).getName();
        int index = fileName.lastIndexOf(".");
        fileName = fileName.substring(0, index);
        File outputFile = new File(config.getDatasDir(), fileName + ".json");
        FileToValidateDescription fileDesc = null;
 
        if (!txtFileToJson.getDataType().equals(DataType.TXT)) {
            fileDesc = new FileToValidateDescription(new File(txtFileToJson.getFileToConvertPath()),
                    txtFileToJson.getDelimiteurLigne().getCode(),
                    txtFileToJson.getDelimiteurColonne().getCode());
        }
 
        switch (txtFileToJson.getDataType()) {
            case CSV:
                ExportToJson.parseCsvFile(fileDesc, outputFile, colDefs, txtFileToJson.isHeaderPresent());
                break;
            case TXT:
                ExportToJson.parseFileByColumnLength(
                        new FileToValidateDescription(new File(txtFileToJson.getFileToConvertPath()), "", ""),
                        outputFile, colDefs);
                break;
            case XLS:
                ExportToJson.parseXlsFile(fileDesc, outputFile, colDefs, txtFileToJson.isHeaderPresent());
                break;
            case XLSX:
                ExportToJson.parseXlsxFile(fileDesc, outputFile, colDefs, txtFileToJson.isHeaderPresent());
                break;
            default:
                throw new Exception("Type de données non supporté");
        }
    }
 
}