Skip to content

Commit

Permalink
Adjustments when compiling sub reports and java typing of parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
jadsonbr committed Apr 10, 2024
1 parent 3082ff5 commit 0adc912
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 14 deletions.
4 changes: 2 additions & 2 deletions docs/source/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Class PyReportJasper
Class responsible for facilitating the management and export of reports

.. py:classmethod:: config(input_file, output_file=False, output_formats=['pdf'], parameters={}, db_connection={},locale='pt_BR', resource=None)
.. py:classmethod:: config(input_file, output_file=False, output_formats=['pdf'], parameters={}, db_connection={},locale='en_US', resource=None)
Method responsible for preparing and validating the settings and parameters

Expand Down Expand Up @@ -38,7 +38,7 @@ Class PyReportJasper
:return: Returns a parameter list
:rtype: ``list(str)``

.. py:classmethod:: process(input_file, output_file=False, format_list=['pdf'], parameters={}, db_connection={}, locale='pt_BR', resource="")
.. py:classmethod:: process(input_file, output_file=False, format_list=['pdf'], parameters={}, db_connection={}, locale='en_US', resource="")
.. warning:: This method still works more in the next versions will be removed.

Expand Down
4 changes: 2 additions & 2 deletions pyreportjasper/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
from .pyreportjasper import PyReportJasper

__title__ = 'PyReportJasper'
__version__ = '2.1.3'
__version__ = '2.1.4'
__author__ = 'Jadson Bonfim Ribeiro'
__license__ = 'GNU GENERAL PUBLIC LICENSE'
__copyright__ = 'Copyright 2023 J BONFIM RIBEIRO'
__copyright__ = 'Copyright 2024 J BONFIM RIBEIRO'
__name__ = 'pyreportjasper'

__all__ = [
Expand Down
2 changes: 1 addition & 1 deletion pyreportjasper/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class Config:
jsonQuery = None
jsonQLQuery = None
jsonLocale = None
locale = 'pt_BR'
locale = 'en_US'
output = None
outputFormats = None
params = {}
Expand Down
30 changes: 27 additions & 3 deletions pyreportjasper/pyreportjasper.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
from pyreportjasper.report import Report


TypeJava = Report.TypeJava


class PyReportJasper:
config = None
DSTYPES = (
Expand Down Expand Up @@ -43,7 +46,7 @@ class PyReportJasper:
METHODS = ('GET', 'POST', 'PUT')

def config(self, input_file, output_file=False, output_formats=['pdf'], parameters={}, db_connection={},
locale='pt_BR', resource=None, subreports=None):
locale='en_US', resource=None, subreports=None):
if not input_file:
raise NameError('No input file!')
if isinstance(output_formats, list):
Expand Down Expand Up @@ -103,7 +106,7 @@ def compile(self, write_jasper=False):
except Exception as ex:
error = NameError('Error compile file: {}'.format(str(ex)))
elif os.path.isdir(self.config.input):
list_files_dir = [arq for arq in self.config.input if os.path.isfile(arq)]
list_files_dir = [arq for arq in os.listdir(str(self.config.input)) if os.path.isfile(arq)]
list_jrxml = [arq for arq in list_files_dir if arq.lower().endswith(".jrxml")]
for file in list_jrxml:
try:
Expand All @@ -119,6 +122,27 @@ def compile(self, write_jasper=False):
raise error
else:
return True

def compile_all_jrxml_dir(self, dir):
if os.path.isdir(dir):
list_files_dir = [arq for arq in os.listdir(str(dir)) if os.path.isfile(arq)]
list_jrxml = [arq for arq in list_files_dir if arq.lower().endswith(".jrxml")]
for file in list_jrxml:
try:
file_imput = os.path.join(dir, file)
with open(file_imput, 'rb') as file_bytes:
self.config.writeJasper = True
print("Compiling: {}".format(file_imput))
report = Report(self.config, file_imput)
report.input_file = file_imput
report.jasper_design = report.JRXmlLoader.load(report.ByteArrayInputStream(file_bytes.read()))
report.initial_input_type = 'JASPER_DESIGN'
report.compile_to_file()
except Exception as ex:
error = NameError('Error compile file: {}'.format(str(ex)))
print(error)
else:
print("Value entered as a parameter is not a directory")

def instantiate_report(self):
report = Report(self.config, self.config.input)
Expand Down Expand Up @@ -181,7 +205,7 @@ def list_report_params(self):
return list_param

def process(self, input_file, output_file=False, format_list=['pdf'],
parameters={}, db_connection={}, locale='pt_BR', resource=""):
parameters={}, db_connection={}, locale='en_US', resource=""):
warnings.warn("process is deprecated - use config and then process_report instead. See the documentation "
"https://pyreportjasper.readthedocs.io",
DeprecationWarning)
Expand Down
38 changes: 32 additions & 6 deletions pyreportjasper/report.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
import pathlib
from pyreportjasper.config import Config
from pyreportjasper.db import Db
from jpype.types import *
from enum import Enum




class Report:
Expand All @@ -16,6 +20,13 @@ class Report:
defaultLocale = None
initial_input_type = None
output = None

TypeJava = Enum('TypeJava', [('Array', 'JArray'), ('Class', 'JClass'),
('Boolean', 'JBoolean'), ('Byte', 'JByte'),
('Char', 'JChar'), ('Short', 'JShort'),
('Int', 'JInt'), ('Long', 'JLong'),
('Float', 'JFloat'), ('Double', 'JDouble'),
('String', 'JString'), ('Object', 'JObject')])

def __init__(self, config: Config, input_file):
self.SCRIPT_DIR = os.path.abspath(os.path.dirname(__file__))
Expand Down Expand Up @@ -154,9 +165,9 @@ def __init__(self, config: Config, input_file):
self.jasper_subreports = {}
for subreport_name, subreport_file in self.config.subreports.items():
try:
subreport_jasper_design = self.JRXmlLoader.load(self.ByteArrayInputStream(subreport_file))
self.jasper_subreports[subreport_name] = self.jvJasperCompileManager.compileReport(
subreport_jasper_design)
with open(subreport_file, 'rb') as subreport_file_bytes:
subreport_jasper_design = self.JRXmlLoader.load(self.ByteArrayInputStream(subreport_file_bytes.read()))
self.jasper_subreports[subreport_name] = self.jvJasperCompileManager.compileReport(subreport_jasper_design)
except Exception:
raise NameError('input file: {0} is not a valid jrxml file'.format(subreport_name))

Expand Down Expand Up @@ -190,11 +201,26 @@ def compile_to_file(self):

def fill(self):
self.fill_internal()

def get_type_instance_java(self, value):
for type_instance in TypeJava:
if type_instance.name == value:
return getattr(jpype.types, type_instance.value)

def fill_internal(self):
parameters = self.HashMap()
for key in self.config.params:
parameters.put(key, self.config.params[key])
if isinstance(self.config.params[key], dict):
param_dict = self.config.params[key]
type_var = param_dict.get('type')
if isinstance(type_var, TypeJava):
type_java_class = self.get_type_instance_java(type_var)
value_java = type_java_class(param_dict.get('value'))
parameters.put(key, value_java)
else:
print('{} parameter does not have an TypeJava type'.format(key))
else:
parameters.put(key, self.config.params[key])

# /!\ NOTE: Sub-reports are loaded after params to avoid them to be override
for subreport_key, subreport in self.jasper_subreports.items():
Expand Down Expand Up @@ -267,8 +293,8 @@ def get_output_stream_pdf(self):

def fetch_pdf_report(self):
output_stream_pdf = self.get_output_stream_pdf()
res = self.String(output_stream_pdf.toByteArray(), 'ISO-8859-1')
return bytes(str(res), 'ISO-8859-1')
res = self.String(output_stream_pdf.toByteArray(), 'UTF-8')
return bytes(str(res), 'UTF-8')

def export_pdf(self):
output_stream = self.get_output_stream('.pdf')
Expand Down

0 comments on commit 0adc912

Please sign in to comment.