Skip to content

Commit

Permalink
Merge pull request #27 from systelab/24-being-able-to-get-the-report-…
Browse files Browse the repository at this point in the history
…as-a-stream

Adds extra methods to get reports as an output stream.
  • Loading branch information
gmartinez3werfen authored Oct 21, 2022
2 parents 7b9b0e7 + e199f95 commit b2551c4
Show file tree
Hide file tree
Showing 10 changed files with 221 additions and 76 deletions.
5 changes: 1 addition & 4 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,4 @@ allure-results/

# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*
/form_report.pdf
/grid_report.pdf
/grid_report.xlsx
/grid_report_null_values.pdf
test_reports/
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

<groupId>com.systelab</groupId>
<artifactId>java-report-generator</artifactId>
<version>1.8.2</version>
<version>1.9.0</version>
<packaging>jar</packaging>
<name>java-report-generator</name>
<url>https://github.com/systelab/java-report-generator</url>
Expand Down Expand Up @@ -50,7 +50,7 @@
<poi.version>5.1.0</poi.version>
<java.version>1.8</java.version>
<maven-compiler-plugin.version>3.8.1</maven-compiler-plugin.version>
<java-report-test-utilities.version>v.1.0.0</java-report-test-utilities.version>
<java-report-test-utilities.version>v.1.2.0</java-report-test-utilities.version>
<junit.version>5.3.2</junit.version>
</properties>

Expand Down
14 changes: 12 additions & 2 deletions src/main/java/com/werfen/report/service/FormReportService.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,30 @@
package com.werfen.report.service;

import com.werfen.report.exception.ReportException;
import com.werfen.report.model.FormReportConfiguration;
import com.werfen.report.model.FormReportData;
import com.werfen.report.model.PageFormat;
import com.werfen.report.exception.ReportException;
import com.werfen.report.service.pdf.PdfFormReportService;
import net.sf.jasperreports.engine.JRException;

import java.io.ByteArrayOutputStream;
import java.io.File;

public class FormReportService {

public File build(FormReportConfiguration formReportConfiguration, FormReportData formReportData, PageFormat pageFormat) throws ReportException {
try {
PdfFormReportService pdfFormReportService = new PdfFormReportService();
return pdfFormReportService.export(formReportConfiguration, formReportData, pageFormat);
return pdfFormReportService.exportToFile(formReportConfiguration, formReportData, pageFormat);
} catch (JRException ex) {
throw new ReportException(ex);
}
}

public ByteArrayOutputStream buildToStream(FormReportConfiguration formReportConfiguration, FormReportData formReportData, PageFormat pageFormat) throws ReportException {
try {
PdfFormReportService pdfFormReportService = new PdfFormReportService();
return pdfFormReportService.exportToStream(formReportConfiguration, formReportData, pageFormat);
} catch (JRException ex) {
throw new ReportException(ex);
}
Expand Down
39 changes: 33 additions & 6 deletions src/main/java/com/werfen/report/service/GridReportService.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,29 @@
import com.werfen.report.service.pdf.PdfGridReportService;
import net.sf.jasperreports.engine.JRException;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;

public class GridReportService {

public File build(GridReportConfiguration gridReportConfiguration, GridPageDataSource gridPageDataSource, ReportFormat reportFormat, PageFormat pageFormat) throws ReportException, ReportFormatException {
public File buildFile(GridReportConfiguration gridReportConfiguration, GridPageDataSource gridPageDataSource, ReportFormat reportFormat, PageFormat pageFormat) throws ReportException, ReportFormatException {
if (reportFormat == ReportFormat.PDF) {
return buildPDF(gridReportConfiguration, gridPageDataSource, pageFormat);
return buildPDFFile(gridReportConfiguration, gridPageDataSource, pageFormat);
} else {
return buildExcel(gridReportConfiguration, gridPageDataSource);
return buildExcelFile(gridReportConfiguration, gridPageDataSource);
}
}

private File buildPDF(GridReportConfiguration gridReportConfiguration, GridPageDataSource gridPageDataSource, PageFormat pageFormat) throws ReportException {
public ByteArrayOutputStream buildStream(GridReportConfiguration gridReportConfiguration, GridPageDataSource gridPageDataSource, ReportFormat reportFormat, PageFormat pageFormat) throws ReportException, ReportFormatException {
if (reportFormat == ReportFormat.PDF) {
return buildPDFStream(gridReportConfiguration, gridPageDataSource, pageFormat);
} else {
return buildExcelStream(gridReportConfiguration, gridPageDataSource);
}
}

private File buildPDFFile(GridReportConfiguration gridReportConfiguration, GridPageDataSource gridPageDataSource, PageFormat pageFormat) throws ReportException {
try {
String filePath = gridReportConfiguration.getOutputFilePath();
PdfGridReportService pdfGridReportService = new PdfGridReportService();
Expand All @@ -34,7 +43,16 @@ private File buildPDF(GridReportConfiguration gridReportConfiguration, GridPageD
}
}

private File buildExcel(GridReportConfiguration gridReportConfiguration, GridPageDataSource gridPageDataSource) throws ReportException {
private ByteArrayOutputStream buildPDFStream(GridReportConfiguration gridReportConfiguration, GridPageDataSource gridPageDataSource, PageFormat pageFormat) throws ReportException {
try {
PdfGridReportService pdfGridReportService = new PdfGridReportService();
return pdfGridReportService.exportToStream(gridReportConfiguration, gridPageDataSource, pageFormat);
} catch (JRException ex) {
throw new ReportException(ex);
}
}

private File buildExcelFile(GridReportConfiguration gridReportConfiguration, GridPageDataSource gridPageDataSource) throws ReportException {
try {
String filePath = gridReportConfiguration.getOutputFilePath();
ExcelGridReportService excelGridReportService = new ExcelGridReportService();
Expand All @@ -44,4 +62,13 @@ private File buildExcel(GridReportConfiguration gridReportConfiguration, GridPag
throw new ReportException(ex);
}
}
}

private ByteArrayOutputStream buildExcelStream(GridReportConfiguration gridReportConfiguration, GridPageDataSource gridPageDataSource) throws ReportException {
try {
ExcelGridReportService excelGridReportService = new ExcelGridReportService();
return excelGridReportService.exportToStream(gridReportConfiguration, gridPageDataSource);
} catch (IOException ex) {
throw new ReportException(ex);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package com.werfen.report.service.excel;

import com.werfen.report.model.*;
import com.werfen.report.model.GridPageDataSource;
import com.werfen.report.model.GridReportConfiguration;
import com.werfen.report.service.excel.template.DataSheetTemplate;
import com.werfen.report.service.excel.template.SummarySheetTemplate;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.logging.Logger;
Expand All @@ -15,19 +17,36 @@ public class ExcelGridReportService {
Logger log = Logger.getLogger(ExcelGridReportService.class.getName());

public void export(String filePath, GridReportConfiguration gridReportConfiguration, GridPageDataSource dataSource) throws IOException {
Workbook workbook = new XSSFWorkbook();

DataSheetTemplate dataTemplate=new DataSheetTemplate();
SummarySheetTemplate summaryTemplate=new SummarySheetTemplate();
try (FileOutputStream fileOut = new FileOutputStream(filePath);
Workbook workbook = generateWorkbook(gridReportConfiguration, dataSource)) {
workbook.write(fileOut);
} catch (IOException ex) {
log.info("Error exporting to Excel: " + ex.getMessage());
}
}

dataTemplate.generate(workbook, dataSource, gridReportConfiguration.getGridColumnConfigurations());
summaryTemplate.generate(workbook, gridReportConfiguration);
public ByteArrayOutputStream exportToStream(GridReportConfiguration gridReportConfiguration, GridPageDataSource dataSource) throws IOException {

try (FileOutputStream fileOut = new FileOutputStream(filePath)) {
workbook.write(fileOut);
try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
Workbook workbook = generateWorkbook(gridReportConfiguration, dataSource)) {
workbook.write(outputStream);
return outputStream;
} catch (IOException ex) {
log.info("Error exporting to Excel: " + ex.getMessage());
}

return null;
}

private Workbook generateWorkbook(GridReportConfiguration gridReportConfiguration, GridPageDataSource dataSource) throws IOException {
Workbook workbook = new XSSFWorkbook();

DataSheetTemplate dataTemplate = new DataSheetTemplate();
SummarySheetTemplate summaryTemplate = new SummarySheetTemplate();

dataTemplate.generate(workbook, dataSource, gridReportConfiguration.getGridColumnConfigurations());
summaryTemplate.generate(workbook, gridReportConfiguration);
return workbook;
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,32 @@
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.design.JasperDesign;

import java.io.ByteArrayOutputStream;
import java.io.File;

public class PdfFormReportService {

public File export(FormReportConfiguration formReportConfiguration, FormReportData formReportData, PageFormat pageFormat) throws JRException {
public File exportToFile(FormReportConfiguration formReportConfiguration, FormReportData formReportData, PageFormat pageFormat) throws JRException {

JasperPrint jasperPrint = buildJasperPrint(formReportConfiguration, formReportData, pageFormat);

return new PdfExportService().exportToFile(jasperPrint, formReportConfiguration.getOutputFilePath());
}

public ByteArrayOutputStream exportToStream(FormReportConfiguration formReportConfiguration, FormReportData formReportData, PageFormat pageFormat) throws JRException {

JasperPrint jasperPrint = buildJasperPrint(formReportConfiguration, formReportData, pageFormat);

return new PdfExportService().exportToStream(jasperPrint);
}

private JasperPrint buildJasperPrint(FormReportConfiguration formReportConfiguration, FormReportData formReportData, PageFormat pageFormat) throws JRException {
JasperDesign jasperDesign = new JasperDesignFactory().build("formReport", pageFormat);

new HeaderReportTemplateBuilder().addHeader(jasperDesign, formReportConfiguration.getHeaderConfiguration());
new FormReportTemplateBuilder().addForm(jasperDesign, formReportData);
new FooterReportTemplateBuilder().addFooter(jasperDesign, formReportConfiguration.getFooterConfiguration());

JasperPrint jasperPrint= new PdfFillService().fill(jasperDesign, new JREmptyDataSource(), formReportConfiguration.getHeaderConfiguration());

return new PdfExportService().export(jasperPrint, formReportConfiguration.getOutputFilePath());
return new PdfFillService().fill(jasperDesign, new JREmptyDataSource(), formReportConfiguration.getHeaderConfiguration());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,29 @@
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.design.JasperDesign;

import java.io.ByteArrayOutputStream;

public class PdfGridReportService {

public void export(String filePath, GridReportConfiguration gridReportConfiguration, GridPageDataSource gridPageDataSource, PageFormat pageFormat) throws JRException {
JasperDesign jasperDesign = new JasperDesignFactory().build("gridReport", pageFormat);
JasperPrint jasperPrint = generateJasperPrint(gridReportConfiguration, gridPageDataSource, pageFormat);

new HeaderReportTemplateBuilder().addHeader(jasperDesign, gridReportConfiguration.getHeaderConfiguration());
new GridReportTemplateBuilder().addGrid(jasperDesign, gridReportConfiguration.getGridColumnConfigurations());
new FooterReportTemplateBuilder().addFooter(jasperDesign, gridReportConfiguration.getFooterConfiguration());

JasperPrint jasperPrint= new PdfFillService().fill(jasperDesign, new GridReportDataSource(gridPageDataSource), gridReportConfiguration.getHeaderConfiguration());
new PdfExportService().exportToFile(jasperPrint, filePath);
}

new PdfExportService().export(jasperPrint, filePath);
public ByteArrayOutputStream exportToStream(GridReportConfiguration gridReportConfiguration, GridPageDataSource gridPageDataSource, PageFormat pageFormat) throws JRException {
JasperPrint jasperPrint = generateJasperPrint(gridReportConfiguration, gridPageDataSource, pageFormat);

return new PdfExportService().exportToStream(jasperPrint);
}

private JasperPrint generateJasperPrint(GridReportConfiguration gridReportConfiguration, GridPageDataSource gridPageDataSource, PageFormat pageFormat) throws JRException {
JasperDesign jasperDesign = new JasperDesignFactory().build("gridReport", pageFormat);

new HeaderReportTemplateBuilder().addHeader(jasperDesign, gridReportConfiguration.getHeaderConfiguration());
new GridReportTemplateBuilder().addGrid(jasperDesign, gridReportConfiguration.getGridColumnConfigurations());
new FooterReportTemplateBuilder().addFooter(jasperDesign, gridReportConfiguration.getFooterConfiguration());


return new PdfFillService().fill(jasperDesign, new GridReportDataSource(gridPageDataSource), gridReportConfiguration.getHeaderConfiguration());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,34 @@
import net.sf.jasperreports.export.SimplePdfExporterConfiguration;
import net.sf.jasperreports.export.SimplePdfReportConfiguration;

import java.io.ByteArrayOutputStream;
import java.io.File;

public class PdfExportService {

public File export(JasperPrint jasperPrint, String filePath) throws JRException {
public File exportToFile(JasperPrint jasperPrint, String filePath) throws JRException {
JRPdfExporter exporter = exportDocument(jasperPrint, new SimpleOutputStreamExporterOutput(filePath));

exporter.exportReport();
return new File(filePath);
}

public ByteArrayOutputStream exportToStream(JasperPrint jasperPrint) throws JRException {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
SimpleOutputStreamExporterOutput simpleOutputStreamExporterOutput = new SimpleOutputStreamExporterOutput(outputStream);
JRPdfExporter exporter = exportDocument(jasperPrint, simpleOutputStreamExporterOutput);

exporter.exportReport();
return outputStream;
}

private JRPdfExporter exportDocument(JasperPrint jasperPrint, SimpleOutputStreamExporterOutput simpleOutputStreamExporterOutput) {
JRPdfExporter exporter = new JRPdfExporter();
exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(filePath));
exporter.setExporterOutput(simpleOutputStreamExporterOutput);
exporter.setConfiguration(getPdfReportConfiguration());
exporter.setConfiguration(getPdfExporterConfiguration());

exporter.exportReport();
return new File(filePath);
return exporter;
}

private SimplePdfReportConfiguration getPdfReportConfiguration() {
Expand Down
38 changes: 30 additions & 8 deletions src/test/java/com/werfen/report/FormReportTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,42 +5,64 @@
import com.werfen.report.service.FormReportService;
import com.werfen.report.test.utils.assertions.ComparisonResultAssertions;
import com.werfen.report.test.utils.pdf.PDFComparator;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.io.File;
import java.io.IOException;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;

public class FormReportTest {
private static final String GOLDEN_PATH = "src/test/resources/golden/";
private static final String TEST_PATH = "test_reports/";
private static final String GOLDEN_SUFFIX = "_golden";
public static String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";

@BeforeEach
void beforeEach() {
File directory = new File(TEST_PATH);
if(!directory.exists()){
directory.mkdirs();
}
}
@Test
public void generateFormReport() throws IOException, ReportException {
public void generateFileFormReport() throws IOException, ReportException {
String fileName = "form_report";

FormReportService formReportService = new FormReportService();
File file = formReportService.build(this.getConfiguration(fileName + ReportFormat.PDF.getFileExtension()), this.getData(), PageFormat.A4);
File file = formReportService.build(this.getConfiguration(TEST_PATH + fileName + ReportFormat.PDF.getFileExtension()), this.getData(), PageFormat.A4);
file.createNewFile();

File expectedFile = new File(GOLDEN_PATH + fileName + GOLDEN_SUFFIX + ReportFormat.PDF.getFileExtension());
File actualFile = new File(fileName + ReportFormat.PDF.getFileExtension());
File actualFile = new File(TEST_PATH + fileName + ReportFormat.PDF.getFileExtension());
ComparisonResultAssertions.assertEquals(PDFComparator.compareFiles(expectedFile, actualFile));
}

@Test
public void generateFormReportWithLessFields() throws IOException, ReportException {
public void generateStreamFormReport() throws IOException, ReportException {
String fileName = "form_report";

FormReportService formReportService = new FormReportService();
ByteArrayOutputStream report = formReportService.buildToStream(this.getConfiguration(null), this.getData(), PageFormat.A4);

InputStream actualStream = new ByteArrayInputStream(report.toByteArray());
InputStream expectedStream = Files.newInputStream(Paths.get(GOLDEN_PATH + fileName + GOLDEN_SUFFIX + ReportFormat.PDF.getFileExtension()));
ComparisonResultAssertions.assertEquals(PDFComparator.compareStreams(expectedStream, actualStream));
}

@Test
public void generateFileFormReportWithLessFields() throws IOException, ReportException {
String fileName = "form_report_less_fields";

FormReportService formReportService = new FormReportService();
File file = formReportService.build(this.getLessFieldsConfiguration(fileName + ReportFormat.PDF.getFileExtension()), this.getData(), PageFormat.A4);
File file = formReportService.build(this.getLessFieldsConfiguration(TEST_PATH + fileName + ReportFormat.PDF.getFileExtension()), this.getData(), PageFormat.A4);
file.createNewFile();

File expectedFile = new File(GOLDEN_PATH + fileName + GOLDEN_SUFFIX + ReportFormat.PDF.getFileExtension());
File actualFile = new File(fileName + ReportFormat.PDF.getFileExtension());
File actualFile = new File(TEST_PATH + fileName + ReportFormat.PDF.getFileExtension());
ComparisonResultAssertions.assertEquals(PDFComparator.compareFiles(expectedFile, actualFile));
}

Expand Down
Loading

0 comments on commit b2551c4

Please sign in to comment.