From 686be32987f66524de17f04c747bb91cbcdaa7f0 Mon Sep 17 00:00:00 2001 From: duyenthaind Date: Thu, 5 Sep 2024 21:47:15 +0700 Subject: [PATCH] feat(export) - VTD-export - export data, fix parse number strategy --- .gitignore | 1 + person.xlsx | Bin 7475 -> 7475 bytes .../processor/WriteCellProcessor.java | 2 +- .../service/WriteExportFileServiceImpl.java | 2 +- .../importexport/utils/WorkbookUtil.java | 4 +++ src/test/java/example/Configuration.java | 19 ++++++++++ src/test/java/example/Main.java | 16 ++++++++- src/test/java/example/common/Person.java | 28 +++++++++++++++ .../configuration/PersonColumnMetaData.java | 18 ++++++++-- .../java/example/dto/FetchPersonRequest.java | 7 ++++ .../example/service/FetchPersonService.java | 34 ++++++++++++++++++ 11 files changed, 125 insertions(+), 6 deletions(-) create mode 100644 src/test/java/example/dto/FetchPersonRequest.java create mode 100644 src/test/java/example/service/FetchPersonService.java diff --git a/.gitignore b/.gitignore index 2499f4c..c7eeac1 100644 --- a/.gitignore +++ b/.gitignore @@ -43,3 +43,4 @@ bin/ .idea log/ +output/ diff --git a/person.xlsx b/person.xlsx index 061a7983db9c5570fc2f942a339cad076d6f735e..17f99f5e99ffcab36df17e72adae55b6022639a6 100644 GIT binary patch delta 199 zcmdmNwb_a{z?+#xgn@&DgQ0z`>PFrPjLblK^8!Y72;&sfJ_w_mB@4p%!TJu&nEaCc z7g+5xjvq{5#zSs3b})lWcpWpCv0F?7qUo7<6IjjUY$-dC%;YsvW?=fMlr@-EmbL-Y hNzxGc#nSc=d1(tUtsnytPn59&^XJL9g6U^6z5x2hMri;5 delta 199 zcmdmNwb_a{z?+#xgn@&DgMn*3<3`>IjLblK^8!Y72;&sfJ_w_mB@4p%!TJu&nEaCc z7g+5xjvq{5#zSs3b})lWcpWpCv0F?7qUo7<6IjjUY$-dC%;YsvW?=fMlr@-EmbL-Y hNzxGc#nSc=d1(tUtsnytPn59&^XJL9g6U^6z5oDwIKKb@ diff --git a/src/main/java/com/vitadairy/libraries/importexport/processor/WriteCellProcessor.java b/src/main/java/com/vitadairy/libraries/importexport/processor/WriteCellProcessor.java index 58ca349..0839598 100644 --- a/src/main/java/com/vitadairy/libraries/importexport/processor/WriteCellProcessor.java +++ b/src/main/java/com/vitadairy/libraries/importexport/processor/WriteCellProcessor.java @@ -26,7 +26,7 @@ public void write(Cell cell, Object input) throws Exception { cell.setCellValue((String) cellData); break; case NUMBER: - cell.setCellValue((double) cellData); + cell.setCellValue(((Number) cellData).doubleValue()); break; case DATE: cell.setCellValue((Date) cellData); diff --git a/src/main/java/com/vitadairy/libraries/importexport/service/WriteExportFileServiceImpl.java b/src/main/java/com/vitadairy/libraries/importexport/service/WriteExportFileServiceImpl.java index deef8c9..360ba19 100644 --- a/src/main/java/com/vitadairy/libraries/importexport/service/WriteExportFileServiceImpl.java +++ b/src/main/java/com/vitadairy/libraries/importexport/service/WriteExportFileServiceImpl.java @@ -91,7 +91,7 @@ protected void writeExportFileHeader(Sheet sheet, Map cel cellMetadata.forEach((key, value) -> { // write header Cell cell = header.createCell(key); - cell.setCellValue(value.getFieldName()); + cell.setCellValue(value.getName()); }); } diff --git a/src/main/java/com/vitadairy/libraries/importexport/utils/WorkbookUtil.java b/src/main/java/com/vitadairy/libraries/importexport/utils/WorkbookUtil.java index 33a21fa..ae41eb6 100644 --- a/src/main/java/com/vitadairy/libraries/importexport/utils/WorkbookUtil.java +++ b/src/main/java/com/vitadairy/libraries/importexport/utils/WorkbookUtil.java @@ -28,6 +28,10 @@ public static void write(Workbook workbook, String filePath, boolean isOverride, if (file.exists() && !isOverride) { return; } + if (!file.exists()) { + file.getParentFile().mkdirs(); + file.createNewFile(); + } outputStream = new FileOutputStream(file); workbook.write(outputStream); diff --git a/src/test/java/example/Configuration.java b/src/test/java/example/Configuration.java index 46899cd..6a84485 100644 --- a/src/test/java/example/Configuration.java +++ b/src/test/java/example/Configuration.java @@ -3,9 +3,12 @@ import com.vitadairy.libraries.importexport.config.Log4j2Configurator; import com.vitadairy.libraries.importexport.helper.Log4j2Logger; import com.vitadairy.libraries.importexport.processor.ReadRowProcessor; +import com.vitadairy.libraries.importexport.processor.WriteRowProcessor; import com.vitadairy.libraries.importexport.service.*; import example.common.Person; import example.configuration.PersonColumnMetaData; +import example.dto.FetchPersonRequest; +import example.service.FetchPersonService; import example.service.ParsePersonService; /** @@ -33,6 +36,22 @@ public ReadImportFileService getReadImportFileService() { ); } + public WriteExportFileService getWriteExportFileService() { + return new WriteExportFileServiceImpl<>( + new FetchPersonService(), + new WriteRowProcessor( + new WriteDataServiceStrategy( + new WriteDataDefaultService(), + new WriteDataNumberService(), + new WriteDataDateService("dd/MM/yyyy", logger) + ), + PersonColumnMetaData.getMetaDataMap(), + logger + ), + logger + ); + } + public static Configuration getInstance() { return Singleton.INSTANCE; } diff --git a/src/test/java/example/Main.java b/src/test/java/example/Main.java index b834ff8..faa2169 100644 --- a/src/test/java/example/Main.java +++ b/src/test/java/example/Main.java @@ -1,9 +1,14 @@ package example; import com.fasterxml.jackson.databind.ObjectMapper; +import com.vitadairy.libraries.importexport.common.ExportResponse; import com.vitadairy.libraries.importexport.common.ReadResponse; -import example.common.Person; +import com.vitadairy.libraries.importexport.dto.FetchRequest; +import com.vitadairy.libraries.importexport.dto.Page; import com.vitadairy.libraries.importexport.service.ReadImportFileService; +import com.vitadairy.libraries.importexport.service.WriteExportFileService; +import example.common.Person; +import example.dto.FetchPersonRequest; import java.io.File; @@ -22,5 +27,14 @@ public static void main(String[] args) throws Exception { ReadResponse readResponse = readService.importFile(fileImport); Configuration.logger.info(objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(readResponse)); + + WriteExportFileService writeService = Configuration.getInstance().getWriteExportFileService(); + String folderPath = System.getProperty("user.dir") + File.separator + "output"; + String fileName = "person_export"; + FetchPersonRequest fetchPersonRequest = new FetchPersonRequest(); + FetchRequest fetchRequest = new FetchRequest<>(fetchPersonRequest, new Page(1, 10), false); + + ExportResponse exportRes = writeService.exportFile(folderPath, fileName, fetchRequest); + Configuration.logger.info(objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(exportRes)); } } \ No newline at end of file diff --git a/src/test/java/example/common/Person.java b/src/test/java/example/common/Person.java index b89e039..f8b8a8c 100644 --- a/src/test/java/example/common/Person.java +++ b/src/test/java/example/common/Person.java @@ -40,4 +40,32 @@ public String getAddress() { public void setAddress(String address) { this.address = address; } + + public static class Builder { + private String name; + private Integer age; + private String address; + + public Builder() { + } + + public Builder name(String name) { + this.name = name; + return this; + } + + public Builder age(Integer age) { + this.age = age; + return this; + } + + public Builder address(String address) { + this.address = address; + return this; + } + + public Person build() { + return new Person(name, age, address); + } + } } diff --git a/src/test/java/example/configuration/PersonColumnMetaData.java b/src/test/java/example/configuration/PersonColumnMetaData.java index 85d17ee..90ae174 100644 --- a/src/test/java/example/configuration/PersonColumnMetaData.java +++ b/src/test/java/example/configuration/PersonColumnMetaData.java @@ -16,9 +16,21 @@ public class PersonColumnMetaData { public static final List META_DATA_LIST = new ArrayList<>(); static { - META_DATA_LIST.add(CellMetaData.builder().name("Name").dataType(DataType.STRING).build()); - META_DATA_LIST.add(CellMetaData.builder().name("Age").dataType(DataType.NUMBER).build()); - META_DATA_LIST.add(CellMetaData.builder().name("Address").dataType(DataType.STRING).build()); + META_DATA_LIST.add(CellMetaData.builder() + .name("Name") + .dataType(DataType.STRING) + .fieldName("name") + .build()); + META_DATA_LIST.add(CellMetaData.builder() + .name("Age") + .dataType(DataType.NUMBER) + .fieldName("age") + .build()); + META_DATA_LIST.add(CellMetaData.builder() + .name("Address") + .dataType(DataType.STRING) + .fieldName("address") + .build()); } public static Map getMetaDataMap() { diff --git a/src/test/java/example/dto/FetchPersonRequest.java b/src/test/java/example/dto/FetchPersonRequest.java new file mode 100644 index 0000000..58f478e --- /dev/null +++ b/src/test/java/example/dto/FetchPersonRequest.java @@ -0,0 +1,7 @@ +package example.dto; + +/** + * @author duyenthai + */ +public class FetchPersonRequest { +} diff --git a/src/test/java/example/service/FetchPersonService.java b/src/test/java/example/service/FetchPersonService.java new file mode 100644 index 0000000..7efe71d --- /dev/null +++ b/src/test/java/example/service/FetchPersonService.java @@ -0,0 +1,34 @@ +package example.service; + +import com.vitadairy.libraries.importexport.dto.FetchRequest; +import com.vitadairy.libraries.importexport.service.FetchDataService; +import example.common.Person; +import example.dto.FetchPersonRequest; + +import java.util.List; + +/** + * @author duyenthai + */ +public class FetchPersonService implements FetchDataService { + @Override + public List fetch(FetchRequest request) { + return List.of( + new Person.Builder() + .name("John") + .age(20) + .address("USA") + .build(), + new Person.Builder() + .name("Alice") + .age(25) + .address("UK") + .build(), + new Person.Builder() + .name("Bob") + .age(30) + .address("Canada") + .build() + ); + } +}