Skip to content

Commit

Permalink
Merge pull request #233 from fiterlatam/FBR-339-pentaho-build-fix
Browse files Browse the repository at this point in the history
Feature/FBR-339: Fix Failing Pentaho Report on Fineract
  • Loading branch information
BrianMuhimbura authored Oct 23, 2023
2 parents ffaf41c + cd72b23 commit 42cb542
Show file tree
Hide file tree
Showing 46 changed files with 47 additions and 37 deletions.
6 changes: 3 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ RUN mv /fineract/fineract-provider/build/libs/*.jar /fineract/fineract-provider/

# https://issues.apache.org/jira/browse/LEGAL-462
# https://issues.apache.org/jira/browse/FINERACT-762
# We include an alternative JDBC driver (which is faster, but not allowed to be default in Apache distribution)
# We include an alternative JDBC driver (which is faster, but not allowed to be default in Apache distribution)
# allowing implementations to switch the driver used by changing start-up parameters (for both tenants and each tenant DB)
# The commented out lines in the docker-compose.yml illustrate how to do this.
WORKDIR /app/libs
Expand All @@ -36,8 +36,8 @@ RUN wget -q https://repo1.maven.org/maven2/mysql/mysql-connector-java/8.0.23/mys
# =========================================

FROM azul/zulu-openjdk:17 as fineract
COPY --from=builder /fineract/fineract-provider/pentahoReports/*.properties /root/.mifos/pentahoReports/
COPY --from=builder /fineract/fineract-provider/pentahoReports/*.prpt /root/.mifos/pentahoReports/
COPY --from=builder /fineract/fineract-provider/pentahoReports/*.properties /root/.mifosx/pentahoReports/
COPY --from=builder /fineract/fineract-provider/pentahoReports/*.prpt /root/.mifosx/pentahoReports/
COPY --from=builder /fineract/fineract-provider/build/libs/ /app
COPY --from=builder /app/libs /app/libs

Expand Down
Binary file modified fineract-provider/pentahoReports/Client Listing(Pentaho).prpt
Binary file not shown.
Binary file modified fineract-provider/pentahoReports/Loan Transaction Receipt.prpt
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@
*/
package org.apache.fineract.infrastructure.report.service;


import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.sql.Date;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.TimeZone;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.Response;
import org.apache.commons.lang3.StringUtils;
Expand All @@ -35,14 +36,19 @@
import org.apache.fineract.infrastructure.report.annotation.ReportService;
import org.apache.fineract.infrastructure.security.service.PlatformSecurityContext;
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.MasterReport;
import org.pentaho.reporting.engine.classic.core.ReportProcessingException;
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;
import org.pentaho.reporting.engine.classic.core.modules.output.table.csv.CSVReportUtil;
import org.pentaho.reporting.engine.classic.core.modules.output.table.html.HtmlReportUtil;
import org.pentaho.reporting.engine.classic.core.modules.output.table.xls.ExcelReportUtil;
import org.pentaho.reporting.engine.classic.core.parameters.ParameterDefinitionEntry;
import org.pentaho.reporting.engine.classic.core.util.ReportParameterValues;
import org.pentaho.reporting.libraries.resourceloader.Resource;
import org.pentaho.reporting.libraries.resourceloader.ResourceException;
import org.pentaho.reporting.libraries.resourceloader.ResourceManager;
Expand All @@ -57,7 +63,7 @@
public class PentahoReportingProcessServiceImpl implements ReportingProcessService {

private static final Logger LOGGER = LoggerFactory.getLogger(PentahoReportingProcessServiceImpl.class);
private static final String MIFOS_BASE_DIR = File.separator + System.getProperty("user.home") + File.separator + ".mifosx";
private static final String MIFOS_BASE_DIR = System.getProperty("user.home") + File.separator + ".mifosx";

private final PlatformSecurityContext context;
private final FineractProperties fineractProperties;
Expand All @@ -67,7 +73,6 @@ public class PentahoReportingProcessServiceImpl implements ReportingProcessServi
@Autowired
public PentahoReportingProcessServiceImpl(final PlatformSecurityContext context, final FineractProperties fineractProperties) {
ClassicEngineBoot.getInstance().start();

this.context = context;
this.fineractProperties = fineractProperties;
}
Expand All @@ -77,37 +82,59 @@ public Response processRequest(final String reportName, final MultivaluedMap<Str
final var outputTypeParam = queryParams.getFirst("output-type");
final var reportParams = getReportParams(queryParams);
final var locale = ApiParameterHelper.extractLocale(queryParams);

var outputType = "HTML";
if (StringUtils.isNotBlank(outputTypeParam)) {
outputType = outputTypeParam;
}

if ((!outputType.equalsIgnoreCase("HTML") && !outputType.equalsIgnoreCase("PDF") && !outputType.equalsIgnoreCase("XLS")
&& !outputType.equalsIgnoreCase("XLSX") && !outputType.equalsIgnoreCase("CSV"))) {
throw new PlatformDataIntegrityException("error.msg.invalid.outputType", "No matching Output Type: " + outputType);
}

final var reportPath = MIFOS_BASE_DIR + File.separator + pentahoFolderName + File.separator + reportName + ".prpt";
var outPutInfo = "Report path: " + reportPath;
LOGGER.info("Report path: {}", outPutInfo);

// load report definition
final var manager = new ResourceManager();
manager.registerDefaults();
Resource res;

try {
res = manager.createDirectly(reportPath, MasterReport.class);
final var masterReport = (MasterReport) res.getResource();
final ReportParameterValues parameterValues = masterReport.getParameterValues();
addParametersToReport(masterReport, reportParams);
String tenantUrl = (String) parameterValues.get("tenantUrl");
String username = (String) parameterValues.get("username");
String password = (String) parameterValues.get("password");
DataFactory dataFactory = masterReport.getDataFactory();
CompoundDataFactory compoundDataFactory = (CompoundDataFactory) dataFactory;
SQLReportDataFactory sqlReportDataFactory = (SQLReportDataFactory) compoundDataFactory.get(0);
DriverConnectionProvider connectionProvider = (DriverConnectionProvider) sqlReportDataFactory.getConnectionProvider();
final var tenant = ThreadLocalContextUtil.getTenant();
final var tenantConnection = tenant.getConnection();
connectionProvider.setUrl(tenantUrl);
connectionProvider.setDriver("org.mariadb.jdbc.Driver");
connectionProvider.setProperty("password", password);
connectionProvider.setProperty("user", username);
connectionProvider.setProperty("::pentaho-reporting::hostname", tenantConnection.getSchemaServer());
connectionProvider.setProperty("::pentaho-reporting::database-type", "MariaDb");
connectionProvider.setProperty("::pentaho-reporting::name", "fineract");
connectionProvider.setProperty("::pentaho-reporting::database-name", tenantConnection.getReadOnlySchemaName());
connectionProvider.setProperty("defaultFetchSize", "500");
connectionProvider.setProperty("::pentaho-reporting-other-attribute::STREAM_RESULTS", "Y");
connectionProvider.setProperty("::pentaho-reporting-other-attribute::PORT_NUMBER", tenantConnection.getSchemaServerPort());
connectionProvider.setProperty("::pentaho-reporting::port", tenantConnection.getSchemaServerPort());
connectionProvider.setProperty("socketFactory", null);
connectionProvider.setProperty("cloudSqlInstance", null);
final var reportEnvironment = (DefaultReportEnvironment) masterReport.getReportEnvironment();
if (locale != null) {
reportEnvironment.setLocale(locale);
}
addParametersToReport(masterReport, reportParams);

TimeZone timeZone = TimeZone.getDefault();
if (Arrays.asList(TimeZone.getAvailableIDs()).contains(tenant.getTimezoneId())) {
timeZone = TimeZone.getTimeZone(tenant.getTimezoneId());
}
reportEnvironment.setTimeZone(timeZone);
final var baos = new ByteArrayOutputStream();

if ("PDF".equalsIgnoreCase(outputType)) {
PdfReportUtil.createPDF(masterReport, baos);
return Response.ok().entity(baos.toByteArray()).type("application/pdf").build();
Expand Down Expand Up @@ -146,16 +173,12 @@ private void addParametersToReport(final MasterReport report, final Map<String,
try {
final var rptParamValues = report.getParameterValues();
final var paramsDefinition = report.getParameterDefinition();

// only allow integer, long, date and string parameter types and assume all mandatory - could go more
// detailed like Pawel did in Mifos later and could match incoming and Pentaho parameters better...
// currently assuming they come in ok... and if not an error
for (final ParameterDefinitionEntry paramDefEntry : paramsDefinition.getParameterDefinitions()) {
final String paramName = paramDefEntry.getName();
final String pValue = queryParams.get(paramName);
if (!(paramName.equals("tenantUrl")
|| (paramName.equals("userhierarchy") || paramName.equals("username")
|| (paramName.equals("password") || paramName.equals("userid")))
|| (paramName.equals("password") || paramName.equals("userid")))
|| (StringUtils.isBlank(pValue) && (paramName.equals("startDate") || paramName.equals("endDate"))))) {
LOGGER.info("paramName:" + paramName);
if (StringUtils.isBlank(pValue)) {
Expand All @@ -177,32 +200,19 @@ private void addParametersToReport(final MasterReport report, final Map<String,
}
}

// Tenant database name and current user's office hierarchy
// passed as parameters to allow multitenant Pentaho reporting
// and data scoping
final var tenant = ThreadLocalContextUtil.getTenant();
final var tenantConnection = tenant.getConnection();
// final var tenantUrl = toJdbcUrl(
// fineractProperties.getTenant().getProtocol() + ":" + fineractProperties.getTenant().getSubprotocol(),
// tenantConnection.getSchemaServer(), tenantConnection.getSchemaServerPort(),
// tenantConnection.getSchemaName(),
// tenantConnection.getSchemaConnectionParameters());
//
final String schemaParameters = tenantConnection.getSchemaConnectionParameters();
var tenantUrl = "jdbc:mariadb://" + tenantConnection.getSchemaServer() + ":" + tenantConnection.getSchemaServerPort() + "/"
+ tenantConnection.getSchemaName() + "?useSSL=false";

final var userhierarchy = currentUser.getOffice().getHierarchy();
var outPutInfo4 = "db URL:" + tenantUrl + " userhierarchy:" + userhierarchy;
+ tenantConnection.getSchemaName() + "?useSSL=false&" + schemaParameters;
final var userHierarchy = currentUser.getOffice().getHierarchy();
var outPutInfo4 = "db URL:" + tenantUrl + " userhierarchy:" + userHierarchy;
LOGGER.info(outPutInfo4);

rptParamValues.put("userhierarchy", userhierarchy);

rptParamValues.put("userhierarchy", userHierarchy);
final var userid = currentUser.getId();
var outPutInfo5 = "db URL:" + tenantUrl + " userid:" + userid;
LOGGER.info(outPutInfo5);

rptParamValues.put("userid", userid);

rptParamValues.put("tenantUrl", tenantUrl);
rptParamValues.put("username", tenantConnection.getSchemaUsername());
rptParamValues.put("password", tenantConnection.getSchemaPassword());
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added fineract-provider/thirdparty/jcommon-1.0.16.jar
Binary file not shown.
Binary file removed fineract-provider/thirdparty/jcommon-1.0.23.jar
Binary file not shown.
Binary file not shown.
Binary file removed fineract-provider/thirdparty/jfreechart-1.0.19.jar
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file removed fineract-provider/thirdparty/postgresql-42.2.12.jar
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

0 comments on commit 42cb542

Please sign in to comment.