Skip to content

Commit

Permalink
Merge pull request #62 from croz-ltd/feature_easierInitializationOfFl…
Browse files Browse the repository at this point in the history
…atDataForExcelReport

Feature easier initialisation of flat data for excel report
  • Loading branch information
jsajlovic authored Mar 28, 2022
2 parents bfe6c31 + c330555 commit b3be676
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,27 @@ public class CreateExcelReportRequest {
*/
private final MultiRowDataProvider multiRowDataProvider;

/**
* Creates {@link CreateExcelReportRequest} Builder instance from flat data.
*
* @param data Flat data to be written
* @return A {@link CreateExcelReportRequest} builder instance
*/
public static CreateExcelReportRequest.CreateExcelReportRequestBuilder fromFlatData(Object[][] data) {
return CreateExcelReportRequest.builder().multiRowDataProvider((start, limit) -> start == 0 ? data : null);
}

/**
* Creates {@link CreateExcelReportRequest} Builder instance from {@link MultiRowDataProvider} instance.
*
* @param multiRowDataProvider Row provider for data to be written
* @return A {@link CreateExcelReportRequest} builder instance
*/
public static CreateExcelReportRequest.CreateExcelReportRequestBuilder fromRowDataProvider(MultiRowDataProvider multiRowDataProvider) {
return CreateExcelReportRequest.builder().multiRowDataProvider(multiRowDataProvider);
}

private static CreateExcelReportRequest.CreateExcelReportRequestBuilder builder() {
return new CreateExcelReportRequest.CreateExcelReportRequestBuilder();
}
}
4 changes: 1 addition & 3 deletions nrich-excel/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,6 @@ Example usage of `ExcelReportService` is:
File file = new File("directory/excel-report.xlsx");
// rows in excel
Object[][] rowData = new Object[][] { { 1.1, "value 1", new Date(), new Date() }, { 2.2, "value 2", new Date(), new Date() };
// no need for batching since we have only two records
MultiRowDataProvider multiRowDataProvider = (start, limit) -> start == 0 ? rowData : null;
// template variable defined in template with value ${templateVariable} will be replaced with resolvedValue
List<TemplateVariable> templateVariableList = Collections.singletonList(new TemplateVariable("templateVariable", "resolvedValue"));
Expand All @@ -105,7 +103,7 @@ Example usage of `ExcelReportService` is:
try (FileOutputStream outputStream = new FileOutputStream(file)) {
// first row index is 3 since first two rows contain column headers
CreateExcelReportRequest request = CreateExcelReportRequest.builder().templateVariableList(templateVariableList).columnDataFormatList(columnDataFormatList).multiRowDataProvider(multiRowDataProvider).batchSize(10).outputStream(outputStream).templatePath("classpath:excel/template.xlsx").firstRowIndex(3).build();
CreateExcelReportRequest request = CreateExcelReportRequest.fromFlatData(rowData).templateVariableList(templateVariableList).columnDataFormatList(columnDataFormatList).batchSize(10).outputStream(outputStream).templatePath("classpath:excel/template.xlsx").firstRowIndex(3).build();
excelReportService.createExcelReport(request);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ void shouldThrowExceptionOnInvalidBatchSize() {
void shouldThrowExceptionOnMissingRowDataProvider() {
// given
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
CreateExcelReportRequest request = createExcelReportRequest((Object[][]) null, 10, outputStream, TEMPLATE_DATA_FIRST_ROW_INDEX);
CreateExcelReportRequest request = createExcelReportRequest((MultiRowDataProvider) null, 10, outputStream, TEMPLATE_DATA_FIRST_ROW_INDEX);

// when
Throwable thrown = catchThrowable(() -> excelReportService.createExcelReport(request));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,29 +1,30 @@
package net.croz.nrich.excel.testutil;

import lombok.SneakyThrows;
import net.croz.nrich.excel.api.model.MultiRowDataProvider;
import net.croz.nrich.excel.api.request.CreateExcelReportRequest;

import java.io.OutputStream;

public final class ExcelReportRequestGeneratingUtil {

private static final String TEMPLATE_PATH = "classpath:excel/template.xlsx";

private ExcelReportRequestGeneratingUtil() {
}

public static CreateExcelReportRequest createExcelReportRequest(Object[][] rowData, int batchSize, OutputStream outputStream, int firstRowIndex) {
MultiRowDataProvider multiRowDataProvider = (start, limit) -> start == 0 ? rowData : null;

return createExcelReportRequest(rowData == null ? null : multiRowDataProvider, batchSize, outputStream, firstRowIndex);
return CreateExcelReportRequest.fromFlatData(rowData)
.batchSize(batchSize)
.outputStream(outputStream)
.templatePath(TEMPLATE_PATH)
.firstRowIndex(firstRowIndex).build();
}

@SneakyThrows
public static CreateExcelReportRequest createExcelReportRequest(MultiRowDataProvider multiRowDataProvider, int batchSize, OutputStream outputStream, int firstRowIndex) {
return CreateExcelReportRequest.builder()
.multiRowDataProvider(multiRowDataProvider)
return CreateExcelReportRequest.fromRowDataProvider(multiRowDataProvider)
.batchSize(batchSize)
.outputStream(outputStream)
.templatePath("classpath:excel/template.xlsx")
.templatePath(TEMPLATE_PATH)
.firstRowIndex(firstRowIndex).build();
}
}

0 comments on commit b3be676

Please sign in to comment.