diff --git a/common-library/src/main/java/com/yas/commonlibrary/csv/CsvExporter.java b/common-library/src/main/java/com/yas/commonlibrary/csv/CsvExporter.java index ca4ef2a2dd..fceb5f5f60 100644 --- a/common-library/src/main/java/com/yas/commonlibrary/csv/CsvExporter.java +++ b/common-library/src/main/java/com/yas/commonlibrary/csv/CsvExporter.java @@ -1,6 +1,7 @@ package com.yas.commonlibrary.csv; import com.opencsv.CSVWriter; +import com.opencsv.ICSVWriter; import com.yas.commonlibrary.csv.anotation.CsvColumn; import com.yas.commonlibrary.csv.anotation.CsvName; import com.yas.commonlibrary.utils.DateTimeUtils; @@ -8,6 +9,8 @@ import java.io.IOException; import java.io.OutputStreamWriter; import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; import java.nio.charset.StandardCharsets; import java.time.LocalDateTime; import java.util.List; @@ -20,15 +23,18 @@ @Slf4j public class CsvExporter { - public static final String COMMA = ","; + private CsvExporter() { + } + + private static final String GET_PREFIX = "get"; public static byte[] exportToCsv(List dataList, Class clazz) throws IOException { try (ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); OutputStreamWriter outputStreamWriter = new OutputStreamWriter(byteArrayOutputStream, StandardCharsets.UTF_8); - CSVWriter csvWriter = new CSVWriter(outputStreamWriter, CSVWriter.DEFAULT_SEPARATOR, - CSVWriter.NO_QUOTE_CHARACTER, - CSVWriter.DEFAULT_ESCAPE_CHARACTER, CSVWriter.DEFAULT_LINE_END)) { + CSVWriter csvWriter = new CSVWriter(outputStreamWriter, ICSVWriter.DEFAULT_SEPARATOR, + ICSVWriter.NO_QUOTE_CHARACTER, + ICSVWriter.DEFAULT_ESCAPE_CHARACTER, ICSVWriter.DEFAULT_LINE_END)) { // Write CSV header writeCsvHeader(csvWriter, clazz); @@ -77,16 +83,20 @@ private static String[] getFieldValues(Field[] fields, Object data) { private static String getFieldValueAsString(Field field, Object data) { try { - field.setAccessible(true); - Object value = field.get(data); + String getterName = GET_PREFIX + StringUtils.capitalize(field.getName()); + Method getter = data.getClass().getMethod(getterName); + Object value = getter.invoke(data); + if (!Objects.isNull(value) && value instanceof List) { return ("[" + String.join("|", (List) value) + "]"); } return value != null ? value.toString() : StringUtils.EMPTY; } catch (IllegalAccessException e) { - log.warn("Get value field err "); - e.printStackTrace(); + log.warn("Get value field err {}", e.getMessage()); + return StringUtils.EMPTY; + } catch (InvocationTargetException | NoSuchMethodException e) { + log.warn("Get value err {}", e.getMessage()); return StringUtils.EMPTY; } } diff --git a/common-library/src/main/java/com/yas/commonlibrary/utils/DateTimeUtils.java b/common-library/src/main/java/com/yas/commonlibrary/utils/DateTimeUtils.java index c45c7548dc..aee455363e 100644 --- a/common-library/src/main/java/com/yas/commonlibrary/utils/DateTimeUtils.java +++ b/common-library/src/main/java/com/yas/commonlibrary/utils/DateTimeUtils.java @@ -5,6 +5,9 @@ public class DateTimeUtils { + private DateTimeUtils() { + } + private static final String DEFAULT_PATTERN = "dd-MM-yyyy_HH-mm-ss"; public static String format(LocalDateTime dateTime) { diff --git a/common-library/src/test/java/com/yas/commonlibrary/csv/CsvExporterTest.java b/common-library/src/test/java/com/yas/commonlibrary/csv/CsvExporterTest.java index d9d766c5c8..be71c2412b 100644 --- a/common-library/src/test/java/com/yas/commonlibrary/csv/CsvExporterTest.java +++ b/common-library/src/test/java/com/yas/commonlibrary/csv/CsvExporterTest.java @@ -2,6 +2,8 @@ import com.yas.commonlibrary.csv.anotation.CsvColumn; import com.yas.commonlibrary.csv.anotation.CsvName; +import lombok.Getter; +import lombok.Setter; import lombok.experimental.SuperBuilder; import org.junit.jupiter.api.Test; @@ -16,6 +18,8 @@ class CsvExporterTest { @SuperBuilder @CsvName(fileName = "TestFile") + @Getter + @Setter static class TestData extends BaseCsv { @CsvColumn(columnName = "Name") @@ -27,29 +31,31 @@ static class TestData extends BaseCsv { @Test void testExportToCsv_withValidData_shouldReturnCorrectCsvContent() throws IOException { - // Given - List dataList = Arrays.asList( - TestData.builder() - .id(1L) - .name("Alice") - .tags(Arrays.asList("tag1", "tag2")) - .build(), - TestData.builder() - .id(2L) - .name("Bob") - .tags(Arrays.asList("tag3", "tag4")) - .build() - ); - // When - byte[] csvBytes = CsvExporter.exportToCsv(dataList, TestData.class); - String csvContent = new String(csvBytes); - - // Then - String expectedCsv = "Id,Name,Tags\n" + - "1,Alice,[tag1|tag2]\n" + - "2,Bob,[tag3|tag4]\n"; - - assertEquals(expectedCsv, csvContent); + // Given + List dataList = Arrays.asList( + TestData.builder() + .id(1L) + .name("Alice") + .tags(Arrays.asList("tag1", "tag2")) + .build(), + TestData.builder() + .id(2L) + .name("Bob") + .tags(Arrays.asList("tag3", "tag4")) + .build() + ); + // When + byte[] csvBytes = CsvExporter.exportToCsv(dataList, TestData.class); + String csvContent = new String(csvBytes); + + // Then + String expectedCsv = """ + Id,Name,Tags + 1,Alice,[tag1|tag2] + 2,Bob,[tag3|tag4] + """; + + assertEquals(expectedCsv, csvContent); } @Test diff --git a/order/src/main/java/com/yas/order/model/csv/OrderItemCsv.java b/order/src/main/java/com/yas/order/model/csv/OrderItemCsv.java index efff167e4a..bc2dc9a4f3 100644 --- a/order/src/main/java/com/yas/order/model/csv/OrderItemCsv.java +++ b/order/src/main/java/com/yas/order/model/csv/OrderItemCsv.java @@ -8,10 +8,14 @@ import com.yas.order.model.enumeration.PaymentStatus; import java.math.BigDecimal; import java.time.ZonedDateTime; +import lombok.Getter; +import lombok.Setter; import lombok.experimental.SuperBuilder; @CsvName(fileName = "Orders") @SuperBuilder +@Getter +@Setter public class OrderItemCsv extends BaseCsv { @CsvColumn(columnName = "Order status")