From 7b3853f0c976eb0e84926dbfee0eb0bd51ee0126 Mon Sep 17 00:00:00 2001 From: willymwai Date: Mon, 16 Sep 2024 11:55:44 +0300 Subject: [PATCH] Refactor `PentahoReportingProcessServiceImpl` to recursively collect sub-reports within sub-reports --- .../PentahoReportingProcessServiceImpl.java | 42 ++++++++++++++----- 1 file changed, 31 insertions(+), 11 deletions(-) diff --git a/src/main/java/org/apache/fineract/infrastructure/report/service/PentahoReportingProcessServiceImpl.java b/src/main/java/org/apache/fineract/infrastructure/report/service/PentahoReportingProcessServiceImpl.java index b9b7657..a84c56a 100644 --- a/src/main/java/org/apache/fineract/infrastructure/report/service/PentahoReportingProcessServiceImpl.java +++ b/src/main/java/org/apache/fineract/infrastructure/report/service/PentahoReportingProcessServiceImpl.java @@ -24,7 +24,12 @@ import java.sql.DriverManager; import java.sql.SQLException; import java.text.SimpleDateFormat; -import java.util.*; +import java.util.ArrayList; +import java.util.Date; +import java.util.HashMap; +import java.util.List; +import java.util.Locale; +import java.util.Map; import javax.sql.DataSource; @@ -42,7 +47,14 @@ import org.apache.fineract.infrastructure.report.annotation.ReportService; import org.apache.fineract.infrastructure.security.constants.TenantConstants; import org.apache.fineract.infrastructure.security.service.PlatformSecurityContext; -import org.pentaho.reporting.engine.classic.core.*; +import org.pentaho.reporting.engine.classic.core.ClassicEngineBoot; +import org.pentaho.reporting.engine.classic.core.CompoundDataFactory; +import org.pentaho.reporting.engine.classic.core.DataFactory; +import org.pentaho.reporting.engine.classic.core.DefaultReportEnvironment; +import org.pentaho.reporting.engine.classic.core.Element; +import org.pentaho.reporting.engine.classic.core.MasterReport; +import org.pentaho.reporting.engine.classic.core.Section; +import org.pentaho.reporting.engine.classic.core.SubReport; import org.pentaho.reporting.engine.classic.core.modules.misc.datafactory.sql.DriverConnectionProvider; import org.pentaho.reporting.engine.classic.core.modules.misc.datafactory.sql.SQLReportDataFactory; import org.pentaho.reporting.engine.classic.core.modules.output.pageable.pdf.PdfReportUtil; @@ -104,22 +116,28 @@ public List getSubReports(MasterReport masterReport) { collectSubReports(masterReport.getPageFooter(), subReports); collectSubReports(masterReport.getItemBand(), subReports); return subReports; - } + } - private void collectSubReports(Section section, List subReports) { + private void collectSubReports(Section section, List subReports) { if (section == null) { return; } for (int i = 0; i < section.getElementCount(); i++) { Element element = section.getElement(i); - if (element instanceof SubReport) { - subReports.add((SubReport) element); + if (element instanceof SubReport subReport) { + subReports.add(subReport); + // Recursively collect subreports within subreports + for (int j = 0; j < subReport.getElementCount(); j++) { + if (subReport.getElement(j) instanceof Section subSection) { + collectSubReports(subSection, subReports); + } + } } } - } + } - @Override - public Response processRequest(final String reportName, final MultivaluedMap queryParams) { + @Override + public Response processRequest(final String reportName, final MultivaluedMap queryParams) { final var outputTypeParam = queryParams.getFirst("output-type"); final var reportParams = getReportParams(queryParams); final var locale = ApiParameterHelper.extractLocale(queryParams); @@ -170,7 +188,8 @@ public Response processRequest(final String reportName, final MultivaluedMap subReports = getSubReports(masterReport); for (SubReport subReport : subReports) { - setConnectionDetail(subReport.getDataFactory()); + CompoundDataFactory subReportCompoundDataFactory = (CompoundDataFactory) subReport.getDataFactory(); + setConnectionDetail(subReportCompoundDataFactory.get(0)); } final var baos = new ByteArrayOutputStream(); @@ -202,7 +221,8 @@ public Response processRequest(final String reportName, final MultivaluedMap