Skip to content

Commit

Permalink
OLMIS-8075: Add support for XLSX format in report generation
Browse files Browse the repository at this point in the history
  • Loading branch information
mgrochalskisoldevelo committed Jan 31, 2025
1 parent 221f74d commit 4d8c807
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 5 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
1.4.0 / wip
=================
Improvements:
* [OLMIS-8075](https://openlmis.atlassian.net/browse/OLMIS-8075): Support XLSX report generation

1.3.0 / 2025.01.15
=================
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ private JasperExporter getJasperExporter(String format, JasperPrint jasperPrint)
return new JasperCsvExporter(jasperPrint);
case "xls":
return new JasperXlsExporter(jasperPrint);
case "xlsx":
return new JasperXlsxExporter(jasperPrint);
case "html":
return new JasperHtmlExporter(jasperPrint);
default:
Expand Down
43 changes: 43 additions & 0 deletions src/main/java/org/openlmis/report/service/JasperXlsxExporter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* This program is part of the OpenLMIS logistics management information system platform software.
* Copyright © 2020 VillageReach
*
* This program is free software: you can redistribute it and/or modify it under the terms
* of the GNU Affero General Public License as published by the Free Software Foundation, either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Affero General Public License for more details. You should have received a copy of
* the GNU Affero General Public License along with this program. If not, see
* http://www.gnu.org/licenses.  For additional information contact [email protected].
*/

package org.openlmis.report.service;

import java.io.ByteArrayOutputStream;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.export.ooxml.JRXlsxExporter;
import net.sf.jasperreports.export.SimpleExporterInput;
import net.sf.jasperreports.export.SimpleOutputStreamExporterOutput;

public class JasperXlsxExporter implements JasperExporter {

private final JasperPrint jasperPrint;

JasperXlsxExporter(JasperPrint jasperPrint) {
this.jasperPrint = jasperPrint;
}

@Override
public byte[] exportReport() throws JRException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
JRXlsxExporter exporter = new JRXlsxExporter();

exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(baos));
exporter.exportReport();
return baos.toByteArray();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,7 @@ public ReportCategoryDto getReportCategoryById(UUID categoryId) {
* Get page of all report categories.
*/
public Page<ReportCategoryDto> getAllReportCategories(Pageable pageable) {
permissionService.canManageReportCategories();

Page<ReportCategory> reportCategories = reportCategoryRepository.findAll(pageable);

return reportCategories.map(ReportCategoryDto::newInstance);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,9 @@ public ResponseEntity<byte[]> generateReport(
MediaType mediaType;
if ("csv".equals(format)) {
mediaType = new MediaType("text", "csv", StandardCharsets.UTF_8);
} else if ("xls".equals(format)) {
mediaType = new MediaType("application", "vnd.ms-excel", StandardCharsets.UTF_8);
} else if ("xls".equals(format) || "xlsx".equals(format)) {
mediaType = new MediaType("application",
"vnd.openxmlformats-officedocument.spreadsheetml.sheet", StandardCharsets.UTF_8);
} else if ("html".equals(format)) {
mediaType = MediaType.TEXT_HTML;
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ public class JasperReportsViewServiceTest {
@Mock
private JasperXlsExporter jasperXlsExporter;

@Mock
private JasperXlsxExporter jasperXlsxExporter;

@Mock
private JasperHtmlExporter jasperHtmlExporter;

Expand Down Expand Up @@ -111,6 +114,12 @@ public void shouldSelectHtmlExporterForHtmlFormat() throws Exception {
verify(jasperHtmlExporter, times(1)).exportReport();
}

@Test
public void shouldSelectXlsxExporterForXlsxFormat() throws Exception {
viewService.getJasperReportsView(jasperTemplate, getParamsWithFormat("xlsx"));
verify(jasperXlsxExporter, times(1)).exportReport();
}

@Test(expected = JasperReportViewException.class)
public void shouldThrowJasperReportViewExceptionWhenConnectionCantBeOpen() throws Exception {
when(replicationDataSource.getConnection()).thenThrow(new SQLException());
Expand All @@ -128,5 +137,6 @@ private void initializeExporterMocks() throws Exception {
whenNew(JasperXlsExporter.class).withAnyArguments().thenReturn(jasperXlsExporter);
whenNew(JasperHtmlExporter.class).withAnyArguments().thenReturn(jasperHtmlExporter);
whenNew(JasperPdfExporter.class).withAnyArguments().thenReturn(jasperPdfExporter);
whenNew(JasperXlsxExporter.class).withAnyArguments().thenReturn(jasperXlsxExporter);
}
}

0 comments on commit 4d8c807

Please sign in to comment.