Skip to content

Commit

Permalink
Refactor PentahoReportingProcessServiceImpl to recursively collect …
Browse files Browse the repository at this point in the history
…sub-reports within sub-reports
  • Loading branch information
willymwai committed Sep 16, 2024
1 parent 01d40f4 commit 7b3853f
Showing 1 changed file with 31 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
Expand Down Expand Up @@ -104,22 +116,28 @@ public List<SubReport> getSubReports(MasterReport masterReport) {
collectSubReports(masterReport.getPageFooter(), subReports);
collectSubReports(masterReport.getItemBand(), subReports);
return subReports;
}
}

private void collectSubReports(Section section, List<SubReport> subReports) {
private void collectSubReports(Section section, List<SubReport> 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<String, String> queryParams) {
@Override
public Response processRequest(final String reportName, final MultivaluedMap<String, String> queryParams) {
final var outputTypeParam = queryParams.getFirst("output-type");
final var reportParams = getReportParams(queryParams);
final var locale = ApiParameterHelper.extractLocale(queryParams);
Expand Down Expand Up @@ -170,7 +188,8 @@ public Response processRequest(final String reportName, final MultivaluedMap<Str

List<SubReport> subReports = getSubReports(masterReport);
for (SubReport subReport : subReports) {
setConnectionDetail(subReport.getDataFactory());
CompoundDataFactory subReportCompoundDataFactory = (CompoundDataFactory) subReport.getDataFactory();
setConnectionDetail(subReportCompoundDataFactory.get(0));
}

final var baos = new ByteArrayOutputStream();
Expand Down Expand Up @@ -202,7 +221,8 @@ public Response processRequest(final String reportName, final MultivaluedMap<Str
throw new PlatformDataIntegrityException("error.msg.invalid.outputType", "No matching Output Type: " + outputType);

}
} catch (Throwable t) {
}
catch (Throwable t) {
logger.error("Pentaho failed", t);
throw new PlatformDataIntegrityException("error.msg.reporting.error", "Pentaho failed: " + t.getMessage());
}
Expand Down

0 comments on commit 7b3853f

Please sign in to comment.