Skip to content

Commit

Permalink
WW-5455 Extracts logic to create connection or dataSource
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaszlenart committed Dec 19, 2024
1 parent 0f9323e commit e89a357
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -156,21 +156,13 @@ public void setNotExcludedAcceptedPatterns(NotExcludedAcceptedPatternsChecker no
protected void doExecute(String finalLocation, ActionInvocation invocation) throws Exception {
initializeProperties(invocation);

LOG.debug("Creating JasperReport for dataSource = {}, format = {}", dataSource, format);
LOG.debug("Creating JasperReport for dataSource: {} and format: {}", dataSource, format);
// Construct the data source for the report.
ValueStack stack = invocation.getStack();
ValueStackDataSource stackDataSource = null;

Connection conn = (Connection) stack.findValue(connection);
if (conn == null) {
boolean evaluated = parsedDataSource != null && !parsedDataSource.equals(dataSource);
boolean reevaluate = !evaluated || isAcceptableExpression(parsedDataSource);
if (reevaluate) {
stackDataSource = new ValueStackDataSource(stack, parsedDataSource, wrapField);
} else {
throw new ServletException(String.format("Error building dataSource for excluded or not accepted [%s]",
parsedDataSource));
}
Connection reportConnection = (Connection) stack.findValue(connection);
ValueStackDataSource reportDataSource = null;
if (reportConnection == null) {
reportDataSource = prepareDataSource(stack);
}

if (invocation.getAction() instanceof JasperReport7Aware action) {
Expand All @@ -186,25 +178,17 @@ protected void doExecute(String finalLocation, ActionInvocation invocation) thro

applyLocale(invocation, parameters);
applyTimeZone(invocation, parameters);

// Add any report parameters from action to param map.
boolean evaluated = parsedReportParameters != null && !parsedReportParameters.equals(reportParameters);
boolean reevaluate = !evaluated || isAcceptableExpression(parsedReportParameters);
Map<String, Object> reportParams = reevaluate ? (Map<String, Object>) stack.findValue(parsedReportParameters) : null;
if (reportParams != null) {
LOG.debug("Found report parameters; adding to parameters...");
parameters.putAll(reportParams);
}
applyCustomParameters(stack, parameters);

JasperPrint jasperPrint;

// Fill the report and produce a print object
try {
JasperReport jasperReport = (JasperReport) JRLoader.loadObject(new File(systemId));
if (conn == null) {
jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, stackDataSource);
if (reportConnection == null) {
jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, reportDataSource);
} else {
jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, conn);
jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, reportConnection);
}

if (invocation.getAction() instanceof JasperReport7Aware action) {
Expand All @@ -213,7 +197,7 @@ protected void doExecute(String finalLocation, ActionInvocation invocation) thro
action.afterReportGeneration(invocation, jasperReport);
}
} catch (JRException e) {
LOG.error("Error building report for uri {}", systemId, e);
LOG.error("Error building report for uri: {}", systemId, e);
throw new ServletException(e.getMessage(), e);
}

Expand All @@ -229,16 +213,26 @@ protected void doExecute(String finalLocation, ActionInvocation invocation) thro
throw new ServletException(e.getMessage(), e);
} finally {
try {
if (conn != null) {
// avoid NPE if connection was not used for the report
conn.close();
if (reportConnection != null) {
reportConnection.close();
}
} catch (Exception e) {
LOG.warn("Could not close db connection properly", e);
}
}
}

protected ValueStackDataSource prepareDataSource(ValueStack stack) throws ServletException {
boolean evaluated = parsedDataSource != null && !parsedDataSource.equals(dataSource);
boolean reevaluate = !evaluated || isAcceptableExpression(parsedDataSource);
if (reevaluate) {
return new ValueStackDataSource(stack, parsedDataSource, wrapField);
} else {
throw new ServletException(String.format("Error building dataSource for excluded or not accepted [%s]",
parsedDataSource));
}
}

protected void applyLocale(ActionInvocation invocation, Map<String, Object> parameters) {
Locale locale = null;
if (invocation.getAction() instanceof JasperReport7Aware action) {
Expand All @@ -263,6 +257,16 @@ protected void applyTimeZone(ActionInvocation invocation, Map<String, Object> pa
}
}

protected void applyCustomParameters(ValueStack stack, Map<String, Object> parameters) {
boolean evaluated = parsedReportParameters != null && !parsedReportParameters.equals(reportParameters);
boolean reevaluate = !evaluated || isAcceptableExpression(parsedReportParameters);
Map<String, Object> reportParams = reevaluate ? (Map<String, Object>) stack.findValue(parsedReportParameters) : null;
if (reportParams != null) {
LOG.debug("Found report parameters; adding to parameters...");
parameters.putAll(reportParams);
}
}

protected void exportReport(ActionInvocation invocation, JasperPrint jasperPrint, JasperReport7ExporterProvider<?> exporterProvider) throws StrutsException {
HttpServletResponse response = prepapreHttpServletResponse(invocation);
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,10 @@ public JasperReport7CsvExporterProvider(
@Override
public JRCsvExporter createExporter(ActionInvocation invocation, JasperPrint jasperPrint) throws StrutsException {
LOG.debug("Creating: {} exporter", this.getClass().getSimpleName());
HttpServletResponse response = invocation.getInvocationContext().getServletResponse();

HttpServletResponse response = invocation.getInvocationContext().getServletResponse();
response.setContentType("text/csv");

JRCsvExporter exporter = new JRCsvExporter();

String reportDelimiter = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public class JasperReport7PdfExporterProvider implements JasperReport7ExporterPr
@Override
public JRPdfExporter createExporter(ActionInvocation invocation, JasperPrint jasperPrint) throws StrutsException {
LOG.debug("Creating: {} exporter", this.getClass().getSimpleName());

HttpServletResponse response = invocation.getInvocationContext().getServletResponse();
response.setContentType("application/pdf");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ public class JasperReport7XlsxExporterProvider implements JasperReport7ExporterP
@Override
public JRXlsxExporter createExporter(ActionInvocation invocation, JasperPrint jasperPrint) throws StrutsException {
LOG.debug("Creating: {} exporter", this.getClass().getSimpleName());
HttpServletResponse response = invocation.getInvocationContext().getServletResponse();

HttpServletResponse response = invocation.getInvocationContext().getServletResponse();
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");

JRXlsxExporter exporter = new JRXlsxExporter();
Expand Down

0 comments on commit e89a357

Please sign in to comment.