Skip to content

Commit

Permalink
feat(export) - VTD-export - export data, fix parse number strategy
Browse files Browse the repository at this point in the history
  • Loading branch information
duyenthaind committed Sep 5, 2024
1 parent 54bbd6a commit 686be32
Show file tree
Hide file tree
Showing 11 changed files with 125 additions and 6 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,4 @@ bin/

.idea
log/
output/
Binary file modified person.xlsx
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ protected void writeExportFileHeader(Sheet sheet, Map<Integer, CellMetaData> cel
cellMetadata.forEach((key, value) -> {
// write header
Cell cell = header.createCell(key);
cell.setCellValue(value.getFieldName());
cell.setCellValue(value.getName());
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
19 changes: 19 additions & 0 deletions src/test/java/example/Configuration.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -33,6 +36,22 @@ public ReadImportFileService<Person> getReadImportFileService() {
);
}

public WriteExportFileService<Person, FetchPersonRequest> 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;
}
Expand Down
16 changes: 15 additions & 1 deletion src/test/java/example/Main.java
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -22,5 +27,14 @@ public static void main(String[] args) throws Exception {

ReadResponse<Person> readResponse = readService.importFile(fileImport);
Configuration.logger.info(objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(readResponse));

WriteExportFileService<Person, FetchPersonRequest> writeService = Configuration.getInstance().getWriteExportFileService();
String folderPath = System.getProperty("user.dir") + File.separator + "output";
String fileName = "person_export";
FetchPersonRequest fetchPersonRequest = new FetchPersonRequest();
FetchRequest<FetchPersonRequest> fetchRequest = new FetchRequest<>(fetchPersonRequest, new Page(1, 10), false);

ExportResponse exportRes = writeService.exportFile(folderPath, fileName, fetchRequest);
Configuration.logger.info(objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(exportRes));
}
}
28 changes: 28 additions & 0 deletions src/test/java/example/common/Person.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
18 changes: 15 additions & 3 deletions src/test/java/example/configuration/PersonColumnMetaData.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,21 @@ public class PersonColumnMetaData {
public static final List<CellMetaData> 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<String, CellMetaData> getMetaDataMap() {
Expand Down
7 changes: 7 additions & 0 deletions src/test/java/example/dto/FetchPersonRequest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package example.dto;

/**
* @author duyenthai
*/
public class FetchPersonRequest {
}
34 changes: 34 additions & 0 deletions src/test/java/example/service/FetchPersonService.java
Original file line number Diff line number Diff line change
@@ -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<Person, FetchPersonRequest> {
@Override
public List<Person> fetch(FetchRequest<FetchPersonRequest> 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()
);
}
}

0 comments on commit 686be32

Please sign in to comment.