From 23a46b4be35277e06ec89f48730eeb694e686be8 Mon Sep 17 00:00:00 2001
From: Kenmegne <stephanie.kenmegne@gmail.com>
Date: Thu, 18 Jun 2026 15:40:06 +0000
Subject: [PATCH] add fdx-commons and fdx-consultation
---
apifdxweb/api/apifdxweb-core-impl/src/main/java/com/megatim/apifdxweb/core/impl/helper/ApiRequestDataExporterImpl.java | 191 +++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 191 insertions(+), 0 deletions(-)
diff --git a/apifdxweb/api/apifdxweb-core-impl/src/main/java/com/megatim/apifdxweb/core/impl/helper/ApiRequestDataExporterImpl.java b/apifdxweb/api/apifdxweb-core-impl/src/main/java/com/megatim/apifdxweb/core/impl/helper/ApiRequestDataExporterImpl.java
new file mode 100644
index 0000000..788e371
--- /dev/null
+++ b/apifdxweb/api/apifdxweb-core-impl/src/main/java/com/megatim/apifdxweb/core/impl/helper/ApiRequestDataExporterImpl.java
@@ -0,0 +1,191 @@
+package com.megatim.apifdxweb.core.impl.helper;
+
+import com.megatim.apifdxweb.core.ifaces.helper.ApiRequestDataExporter;
+import com.megatim.apifdxweb.tools.exceptions.ApplicationServerException;
+import com.megatim.fdxcommons.core.ifaces.helper.DataInMemoryHandler;
+import com.megatim.fdxcommons.tools.integration.FdxConsultationIntegrationData;
+import com.megatim.fdxcommons.model.enumeration.TypeDonnee;
+import com.megatim.fdxcommons.model.integration.ColumnDefinition;
+import java.io.BufferedWriter;
+import java.io.File;
+import java.io.IOException;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.nio.file.StandardOpenOption;
+import java.sql.Timestamp;
+import java.time.LocalDateTime;
+import java.time.format.DateTimeFormatter;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+import javax.enterprise.context.Dependent;
+import javax.inject.Inject;
+import com.megatim.fdxcommons.tools.database.queries.metadata.SelectQueryResult;
+import com.megatim.fdxcommons.tools.database.tables.FdxApiTable;
+import com.megatim.fdxcommons.tools.database.tables.dto.FdxTableRowDto;
+import com.megatim.fdxcommons.tools.mappers.FdxTableMapper;
+import java.util.Collections;
+
+/**
+ *
+ * @author ASUS
+ */
+@Dependent
+public class ApiRequestDataExporterImpl implements ApiRequestDataExporter {
+
+ @Inject
+ private DataInMemoryHandler dataInMemoryHandler;
+
+ @Inject
+ private FdxTableMapper fdxTableMapper;
+
+ @Override
+ public File export(FdxConsultationIntegrationData fileIntegrationData, String exportDirectory) {
+ Path path = constructFileName(fileIntegrationData.getDataProduction().getCodeTypeFichier(),
+ exportDirectory);
+
+ try {
+ List<ColumnDefinition> columnsDefinition = new ArrayList(dataInMemoryHandler
+ .getDataInMemory()
+ .getTypeFichierToColumnDefinitions()
+ .get(fileIntegrationData.getDataProduction()
+ .getCodeTypeFichier())
+ .values());
+
+ saveToFile(fileIntegrationData.getRows(), path, columnsDefinition);
+ } catch (IOException ex) {
+ Logger.getLogger(ApiRequestDataExporterImpl.class.getName()).log(Level.SEVERE, ex.getMessage(), ex);
+ throw new ApplicationServerException();
+ }
+ return path.toFile();
+ }
+
+ @Override
+ public File exportAll(String codeTypeFichier, String exportDirectory) {
+ Path path = constructFileName(codeTypeFichier, exportDirectory);
+
+ try {
+ SelectQueryResult result = new FdxApiTable(
+ codeTypeFichier,
+ dataInMemoryHandler.getDataInMemory().getReferentielEnCours().getVersion(),
+ null
+ ).selectResult(null);
+
+ List<ColumnDefinition> columnsDefinition = new ArrayList(dataInMemoryHandler.getDataInMemory().getTypeFichierToColumnDefinitions().get(codeTypeFichier).values());
+
+ saveToFile(fdxTableMapper.fdxTableRowListToFdxTableRowDtoList(result.rows()), path, columnsDefinition);
+
+ } catch (Exception ex) {
+ Logger.getLogger(ApiRequestDataExporterImpl.class.getName()).log(Level.SEVERE, ex.getMessage(), ex);
+ throw new ApplicationServerException();
+ }
+
+ return path.toFile();
+ }
+
+ private void saveToFile(List<FdxTableRowDto> rows, Path path, List<ColumnDefinition> columnsDefinition) throws IOException {
+ Collections.sort(columnsDefinition, (ColumnDefinition c1, ColumnDefinition c2) -> Integer.valueOf(c1.getPosition()).compareTo(c2.getPosition()));
+
+ try ( BufferedWriter bufferWriter = Files.newBufferedWriter(path, StandardCharsets.UTF_8, StandardOpenOption.APPEND, StandardOpenOption.CREATE, StandardOpenOption.SYNC)) {
+
+ for (FdxTableRowDto fdxTableRow : rows) {
+ String line = getLineFromColumnData(fdxTableRow, columnsDefinition);
+ bufferWriter.write(line + System.lineSeparator());
+ }
+ }
+ }
+
+ private String getLineFromColumnData(FdxTableRowDto fdxTableRow, List<ColumnDefinition> columnsDefinition) {
+ StringBuilder line = new StringBuilder("");
+
+ for (ColumnDefinition columnDefinition : columnsDefinition) {
+ fdxTableRow.getDataColumns().stream().filter(p -> p.getName().trim().equalsIgnoreCase(columnDefinition.getName().trim())).findFirst().ifPresent(c -> {
+ line.append(normalizeValue(c.getValue(), columnDefinition));
+ });
+ }
+ return line.toString();
+
+ }
+
+ private String normalizeValue(Object value, ColumnDefinition columnDefinition) {
+ String stringValue = value != null ? value.toString() : "";
+
+ if (columnDefinition.getTypeDonnee().equals(TypeDonnee.DATE.getValue())) {
+ if (stringValue.isEmpty()) {
+ stringValue = generateUnknown(columnDefinition.getTaille(), " ");
+ } else {
+ Timestamp timestamp = null;
+ if (value instanceof Timestamp) {
+ timestamp = (Timestamp) value;
+ } else {
+ timestamp = new Timestamp((long) value);
+ }
+ stringValue = formatDateToString(timestamp, columnDefinition);
+ }
+ }
+ if (stringValue.length() < columnDefinition.getTaille()) {
+ stringValue = formatData(TypeDonnee.fromValeur(columnDefinition.getTypeDonnee()), stringValue, columnDefinition.getTaille());
+ }
+ return stringValue;
+ }
+
+ private String generateUnknown(int nbOfCharacter, String character) {
+ StringBuilder str = new StringBuilder("");
+
+ for (int i = 0; i < nbOfCharacter; i++) {
+ str.append(character);
+ }
+ return str.toString();
+ }
+
+ private String formatData(TypeDonnee typeDonnee, String data, int columnSize) {
+
+ if (typeDonnee != null) {
+
+ switch (typeDonnee) {
+
+ case NUMERIQUE:
+
+ case DECIMAL:
+ return generateUnknown(columnSize - data.length(), "0") + data;
+
+ case ALPHANUMERIQUE:
+
+ case DATE:
+
+ default:
+ return data + generateUnknown(columnSize - data.length(), " ");
+ }
+ } else {
+ throw new IllegalArgumentException();
+ }
+ }
+
+ private Path constructFileName(String codeTypeFichier, String exportDirectory) {
+ LocalDateTime date = LocalDateTime.now();
+ DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
+ Path path = Paths.get(exportDirectory, codeTypeFichier.toUpperCase() + date.format(formatter) + ".txt");
+
+ return path;
+ }
+
+ private String formatDateToString(Object obj, ColumnDefinition columnDefinition) {
+ if (obj == null) {
+ return generateUnknown(columnDefinition.getTaille(), " ");
+ }
+ Timestamp data = (Timestamp) obj;
+ LocalDateTime localDateTime = data.toLocalDateTime();
+ DateTimeFormatter formatter = DateTimeFormatter.ofPattern(columnDefinition.getFormatDate());
+
+ if (formatter == null) {
+ return localDateTime.toString().substring(0, columnDefinition.getTaille());
+ } else {
+ return localDateTime.format(formatter);
+ }
+
+ }
+
+}
--
Gitblit v1.10.0