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
/*
 * 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.util;
 
import com.megatim.typefichier.validator.utilities.ParserUtils;
import static com.megatim.typefichier.validator.utilities.Utilities.getCharset;
import com.megatim.fdxconvert.pojo.FileToValidateDescription;
import com.megatim.fdxconvert.pojo.ColumnDefinition;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVParser;
import org.apache.commons.csv.CSVRecord;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
 
/**
 *
 * @author ASUS
 */
public class ExportToJson {
 
    public static void parseFileByColumnLength(FileToValidateDescription fileDesc, File outputFile, List<ColumnDefinition> colDefs) throws Exception {
        final int nbElts = Count.countEltsOfTextFile(fileDesc, false);
        int index = 1;
        Collections.sort(colDefs, (c1, c2) -> Integer.valueOf(c1.getPosition()).compareTo(c2.getPosition()));
 
        if (fileDesc != null) {
 
            Charset charset = getCharset(fileDesc.getFile());
 
            //Lecture des lignes du fichier et chargement dans une liste
            try ( FileInputStream fis = new FileInputStream(fileDesc.getFile());  InputStreamReader isr = new InputStreamReader(fis, charset);  BufferedReader reader = new BufferedReader(isr)) {
                if (outputFile.exists()) {
                    outputFile.delete();
                }
                //Initialisation du writer pour ecrire chaque objet json
                try ( BufferedWriter bufferWriter = Files.newBufferedWriter(outputFile.toPath(), charset, StandardOpenOption.APPEND, StandardOpenOption.CREATE, StandardOpenOption.SYNC);) {
                    bufferWriter.write("[");
                    String line;
 
                    while ((line = reader.readLine()) != null) {
                        String json = createJson(colDefs, line);
                        bufferWriter.write(json);
 
                        if (index < nbElts) {
                            bufferWriter.write(",");
                            index++;
                        }
                    }
                    bufferWriter.write("]");
                }
            }
        }
    }
 
    public static void parseCsvFile(FileToValidateDescription fileDesc, File outputFile, List<ColumnDefinition> colDefs, boolean headerPresent) throws Exception {
        final int nbElts = Count.countEltsOfCsvFile(fileDesc, headerPresent);
        int index = 0;
 
        if (fileDesc != null) {
            Charset charset = getCharset(fileDesc.getFile());
            AtomicBoolean withHeader = new AtomicBoolean(headerPresent);
 
            if (outputFile.exists()) {
                outputFile.delete();
            }
 
            try ( FileInputStream fis = new FileInputStream(fileDesc.getFile());  InputStreamReader isr = new InputStreamReader(fis, charset);  BufferedReader reader = new BufferedReader(isr)) {
                //Initialisation du writer pour ecrire chaque objet json
                try ( BufferedWriter bufferWriter = Files.newBufferedWriter(outputFile.toPath(), charset, StandardOpenOption.APPEND, StandardOpenOption.CREATE, StandardOpenOption.SYNC)) {
                    CSVParser parser = new CSVParser(reader, CSVFormat.DEFAULT.builder()
                            .setSkipHeaderRecord(headerPresent)
                            .setIgnoreSurroundingSpaces(true)
                            .setTrim(true)
                            .setDelimiter(fileDesc.getColumnDelimiter())
                            .setRecordSeparator(fileDesc.getRowDelimiter())
                            .build());
 
                    bufferWriter.write("[");
 
                    for (CSVRecord record : parser) {
                        String[] columnsTableLine = new String[colDefs.size()];
                        AtomicInteger i = new AtomicInteger(0);
 
                        if (!withHeader.get()) {
                            record.forEach(column -> {
                                columnsTableLine[i.getAndIncrement()] = column != null ? column.replaceAll(" {2,}", " ").replaceAll("\r", "").replaceAll("\n", "").replaceAll("\t", "") : "";
                            });
                            String json = createJson(colDefs, columnsTableLine);
                            bufferWriter.write(json);
 
                            if (index < nbElts) {
                                bufferWriter.write(",");
                                index++;
                            }
                        } else {
                            withHeader.set(false);
                        }
                    }
                    bufferWriter.write("]");
                }
            } catch (Exception e) {
                Logger.getLogger(ImportData.class.getName()).log(Level.SEVERE, e.getMessage(), e);
            }
        }
    }
 
    public static void parseXlsFile(FileToValidateDescription fileDesc, File outputFile, List<ColumnDefinition> colDefs, boolean headerPresent) throws Exception {
        try ( InputStream is = new FileInputStream(fileDesc.getFile());  Workbook wb = new HSSFWorkbook(is)) {
            parseExcelFile(fileDesc, wb, outputFile, colDefs, headerPresent);
        }
    }
 
    public static void parseXlsxFile(FileToValidateDescription fileDesc, File outputFile, List<ColumnDefinition> colDefs, boolean headerPresent) throws Exception {
        try ( InputStream is = new FileInputStream(fileDesc.getFile());  Workbook wb = new XSSFWorkbook(is)) {
            parseExcelFile(fileDesc, wb, outputFile, colDefs, headerPresent);
        }
    }
 
    private static Path filePath(File file, int sheetNumber) {
        int extensionIndex = file.getName().lastIndexOf(".");
        return Paths.get(file.getParent(),
                file.getName().substring(0, extensionIndex) + sheetNumber + file.getName().substring(extensionIndex + 1));
    }
 
    private static void parseExcelFile(FileToValidateDescription fileDesc, Workbook wb, File outputFile, List<ColumnDefinition> colDefs, boolean headerPresent) throws IOException, Exception {
        Charset charset = getCharset(fileDesc.getFile());
        AtomicBoolean withHeader = new AtomicBoolean(headerPresent);
 
        for (int i = 0; i < wb.getNumberOfSheets(); i++) {
            Sheet sheet = wb.getSheetAt(i);
            final int nbElts = Count.countEltsOfXlsFile(fileDesc.getFile(), i, headerPresent);
            AtomicInteger position = new AtomicInteger(1);
 
            if (outputFile.exists()) {
                outputFile.delete();
            }
 
            //Initialisation du writer pour ecrire chaque objet json
            try ( BufferedWriter bufferWriter = Files.newBufferedWriter(filePath(outputFile, i), charset, StandardOpenOption.APPEND, StandardOpenOption.CREATE, StandardOpenOption.SYNC);) {
                bufferWriter.write("[");
                for (org.apache.poi.ss.usermodel.Row r : sheet) {
                    if (!withHeader.get()) {
                        //Représente les colonnes extraites de chaque ligne du  fichier
                        String[] columnsTableLine = new String[colDefs.size()];
                        AtomicInteger index = new AtomicInteger(0);
 
                        r.forEach(c -> {
                            columnsTableLine[index.get()] = ParserUtils.getXlxsCellValue(c,
                                    wb,
                                    colDefs.get(index.get()).getTypeDonnee(),
                                    colDefs.get(index.get()).getFormatDate(),
                                    colDefs.get(index.get()).getSeparateurDate(),
                                    colDefs.get(index.get()).getName()
                            );
                            index.incrementAndGet();
                        });
                        String json = createJson(colDefs, columnsTableLine);
                        bufferWriter.write(json);
 
                        if (position.get() < nbElts) {
                            bufferWriter.write(",");
                            position.incrementAndGet();
                        }
                    } else {
                        withHeader.set(false);
                    }
                }
                bufferWriter.write("]");
            }
 
        }
    }
 
    private static String createJson(List<ColumnDefinition> colDefs, String[] columnsTableLine) throws Exception {
        StringBuilder json = new StringBuilder();
        System.out.println("----------------- colDefs.size() = " + colDefs.size());
        System.out.println("---------------- columnsTableLine.length = " + columnsTableLine.length);
        if (colDefs.size() != columnsTableLine.length) {
            throw new Exception("Existence des lignes mal formattées");
        }
 
        json.append("\t{\n");
        int i = 0;
 
        for (ColumnDefinition c : colDefs) {
            String str = "\"" + c.getName() + "\" : " + "\"" + columnsTableLine[i].trim() + "\"";
 
            if (i < colDefs.size() - 1) {
                str += ",";
            } else {
                str += "\n";
            }
            json.append(str);
            i++;
        }
        json.append("\t}\n");
        return json.toString();
    }
 
    private static String createJson(List<ColumnDefinition> colDefs, String line) {
        StringBuilder json = new StringBuilder();
        json.append("\t{");
        int i = 0;
        int count = 0;
 
        for (ColumnDefinition c : colDefs) {
            String str = "\"" + c.getName() + "\" : " + "\"" + line.substring(i, i + c.getTaille()).trim() + "\"";
 
            if (count < colDefs.size() - 1) {
                str += ",";
            }
            json.append(str);
            i += c.getTaille();
            count++;
        }
        json.append("\t}\n");
        return json.toString();
    }
 
}