Skip to content

Commit 9f41b72

Browse files
committed
Add support for single_report=true
1 parent aa5b0b8 commit 9f41b72

File tree

2 files changed

+25
-6
lines changed

2 files changed

+25
-6
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ You can generically pass the generic `feature` query parameter, which will be re
4343

4444
If multiple feature ids are specified, an aggregated report for all specified features will be returned.
4545

46+
Alternatively, provided `FEATURE_ID` is defined as a list in your report, you can pass `single_report=true`, and the entire list of feature ids will be passed to the report, which can i.e. render them in a table.
47+
4648
To map `feature` to the report feature parameter, and to resolve `feature=*`, the table name, primary key column and report feature parameter will be extracted, if possible, from the report query string, which is expected to be of the form
4749

4850
SELECT <...> FROM <table_name> WHERE <pk_column> = $P{<FEATURE_PARAM_NAME>}

src/report_compiler.py

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -140,14 +140,15 @@ def resolve_datasource(self, datasource, report_filename, open_conns):
140140
open_conns.append(conn)
141141
return conn
142142

143-
def compile_report(self, report_filename, fill_params, tmpdir, resources, permitted_resources, compile_subreport=False):
143+
def compile_report(self, report_filename, fill_params, tmpdir, resources, permitted_resources, single_report, compile_subreport=False):
144144
""" Compile a report (or subreport), resolving the datasource, mapping parameter values, and processing permitted subreports.
145145
146146
:param report_filename str: The filename of the jrxml report source
147147
:param fill_params dict: Report fill parameters
148148
:param tmpdir str: Tempdir in which to write processed .jrxml sources and compiled .jasper reports
149149
:param resources dict: Resource configuration
150150
:param permitted_resources list: List of permitted resources
151+
:param single_report bool: Whether to produce single report, passing the array of feature IDs, instead of producing one report per feature
151152
:param compile_subreport bool: Whether a subreport is being compiled
152153
"""
153154

@@ -227,11 +228,14 @@ def compile_report(self, report_filename, fill_params, tmpdir, resources, permit
227228
fill_params[data_param] = [fill_params[data_param]]
228229

229230
# Iterate over parameters, try to map parameters
231+
data_param_nested_class = None
230232
parameters = root.findall(".//jasper:parameter", namespace)
231233
for parameter in parameters:
232234
parameterName = parameter.get("name")
233235
if parameterName in fill_params:
234236
parameterClass = parameter.get("class")
237+
if parameterName == data_param:
238+
data_param_nested_class = parameter.get("nestedType")
235239
if isinstance(fill_params[parameterName], list):
236240
try:
237241
fill_params[parameterName] = [jpype.JClass(parameterClass)(value) for value in fill_params[parameterName]]
@@ -257,7 +261,7 @@ def compile_report(self, report_filename, fill_params, tmpdir, resources, permit
257261
self.logger.info("Subreport template %s" % subreport_template)
258262
if os.path.exists(subreport_filename):
259263
if subreport_template in permitted_resources:
260-
subreport_result = self.compile_report(subreport_filename, fill_params, tmpdir, resources, permitted_resources, True)
264+
subreport_result = self.compile_report(subreport_filename, fill_params, tmpdir, resources, permitted_resources, single_report, True)
261265
if not subreport_result:
262266
self.logger.info("Failed to compile subreport %s" % subreport_filename)
263267
subreportExpression.text = ""
@@ -298,9 +302,16 @@ def compile_report(self, report_filename, fill_params, tmpdir, resources, permit
298302
jasperReport = self.JasperCompileManager.getInstance(self.jContext).compile(temp_report_filename)
299303
jasperPrints = self.ArrayList()
300304
if data_param is not None and data_param in fill_params:
301-
feature_ids = fill_params[data_param]
302-
for feature_id in feature_ids:
303-
fill_params[data_param] = feature_id
305+
if not single_report:
306+
feature_ids = fill_params[data_param]
307+
for feature_id in feature_ids:
308+
fill_params[data_param] = feature_id
309+
jasperPrints.add(self.SimpleExporterInputItem(self.JasperFillManager.getInstance(self.jContext).fill(jasperReport, fill_params, conn)))
310+
else:
311+
data_param_list = self.ArrayList()
312+
for value in fill_params[data_param]:
313+
data_param_list.add(jpype.JClass(data_param_nested_class)(value))
314+
fill_params[data_param] = data_param_list
304315
jasperPrints.add(self.SimpleExporterInputItem(self.JasperFillManager.getInstance(self.jContext).fill(jasperReport, fill_params, conn)))
305316
else:
306317
jasperPrints.add(self.SimpleExporterInputItem(self.JasperFillManager.getInstance(self.jContext).fill(jasperReport, fill_params, conn)))
@@ -384,6 +395,12 @@ def get_document(self, config, permitted_resources, tenant, template, fill_param
384395
# Set the tenant in fill_params
385396
fill_params["TENANT"] = tenant
386397

398+
# Read/clear single_report param
399+
if "single_report" in fill_params:
400+
single_report = fill_params.get("single_report", "").lower() in ["1","true"]
401+
del fill_params["single_report"]
402+
403+
387404
tmpdir = tempfile.mkdtemp()
388405

389406
# Set virtualizer in fill_params
@@ -399,7 +416,7 @@ def get_document(self, config, permitted_resources, tenant, template, fill_param
399416
fill_params[self.JRParameter.REPORT_VIRTUALIZER] = virtualizer
400417

401418
# Compile report
402-
jasperPrints = self.compile_report(report_filename, fill_params, tmpdir, resources, permitted_resources)
419+
jasperPrints = self.compile_report(report_filename, fill_params, tmpdir, resources, permitted_resources, single_report)
403420
shutil.rmtree(tmpdir)
404421

405422
if jasperPrints is None:

0 commit comments

Comments
 (0)