|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* Jasper report integration for PHP |
|
7
|
|
|
* |
|
8
|
|
|
* @link https://github.com/belgattitude/soluble-jasper |
|
9
|
|
|
* @author Vanvelthem Sébastien |
|
10
|
|
|
* @copyright Copyright (c) 2017-2019 Vanvelthem Sébastien |
|
11
|
|
|
* @license MIT |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace Soluble\Jasper\Runner; |
|
15
|
|
|
|
|
16
|
|
|
use Psr\Log\LoggerInterface; |
|
17
|
|
|
use Psr\Log\NullLogger; |
|
18
|
|
|
use Soluble\Japha\Bridge\Adapter as BridgeAdapter; |
|
19
|
|
|
use Soluble\Jasper\Context\DefaultClassLoader; |
|
20
|
|
|
use Soluble\Jasper\Context\DefaultFileResolver; |
|
21
|
|
|
use Soluble\Jasper\DataSource\Contract\DataSourceInterface; |
|
22
|
|
|
use Soluble\Jasper\DataSource\Contract\JavaSqlConnectionInterface; |
|
23
|
|
|
use Soluble\Jasper\DataSource\Contract\JRDataSourceFromDataSourceInterface; |
|
24
|
|
|
use Soluble\Jasper\DataSource\Contract\JRDataSourceFromReportParamsInterface; |
|
25
|
|
|
use Soluble\Jasper\DataSource\EmptyDataSource; |
|
26
|
|
|
use Soluble\Jasper\Exception; |
|
27
|
|
|
use Soluble\Jasper\Exporter\BridgedExportManager; |
|
28
|
|
|
use Soluble\Jasper\Exporter\ExportManagerInterface; |
|
29
|
|
|
use Soluble\Jasper\JRParameter; |
|
30
|
|
|
use Soluble\Jasper\Proxy\Engine\JasperCompileManager; |
|
31
|
|
|
use Soluble\Jasper\Proxy\Engine\JasperFillManager; |
|
32
|
|
|
use Soluble\Jasper\Proxy\Engine\JasperPrint; |
|
33
|
|
|
use Soluble\Jasper\Proxy\Engine\JasperReport; |
|
34
|
|
|
use Soluble\Jasper\Report; |
|
35
|
|
|
use Soluble\Jasper\ReportParams; |
|
36
|
|
|
|
|
37
|
|
|
class BridgedReportRunner implements ReportRunnerInterface |
|
38
|
|
|
{ |
|
39
|
|
|
/** |
|
40
|
|
|
* @var BridgeAdapter |
|
41
|
|
|
*/ |
|
42
|
|
|
private $ba; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @var JasperCompileManager |
|
46
|
|
|
*/ |
|
47
|
|
|
private $compileManager; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @var LoggerInterface |
|
51
|
|
|
*/ |
|
52
|
|
|
private $logger; |
|
53
|
|
|
|
|
54
|
19 |
|
public function __construct(BridgeAdapter $bridgeAdapter, LoggerInterface $logger = null) |
|
55
|
|
|
{ |
|
56
|
19 |
|
$this->ba = $bridgeAdapter; |
|
57
|
19 |
|
if ($logger === null) { |
|
58
|
17 |
|
$logger = new NullLogger(); |
|
59
|
|
|
} |
|
60
|
19 |
|
$this->logger = $logger; |
|
61
|
19 |
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @param Report $report |
|
65
|
|
|
* |
|
66
|
|
|
* @throws Exception\BrokenXMLReportFileException when cannot parse the xml content or invalid xml file |
|
67
|
|
|
* @throws Exception\ReportFileNotFoundException when the report file cannot be located (both php and java sides) |
|
68
|
|
|
* @throws Exception\ReportCompileException when there's an error compiling/evaluating the report |
|
69
|
|
|
* @throws Exception\JavaProxiedException when the compileReport has encountered a Java error |
|
70
|
|
|
* @throws Exception\RuntimeException when an unexpected problem have been encountered |
|
71
|
|
|
* |
|
72
|
|
|
* @return JasperReport |
|
73
|
|
|
*/ |
|
74
|
19 |
|
public function compileReport(Report $report): JasperReport |
|
75
|
|
|
{ |
|
76
|
|
|
try { |
|
77
|
19 |
|
if ($this->compileManager === null) { |
|
78
|
19 |
|
$this->compileManager = new JasperCompileManager($this->ba); |
|
79
|
|
|
} |
|
80
|
19 |
|
$jasperReport = $this->compileManager->compileReport($report->getReportFile()); |
|
81
|
2 |
|
} catch (\Throwable $e) { |
|
82
|
2 |
|
$this->logger->error( |
|
83
|
2 |
|
sprintf( |
|
84
|
2 |
|
"Compilation of report '%s' failed with '%s' (%s)", |
|
85
|
2 |
|
basename($report->getReportFile()), |
|
86
|
2 |
|
(new \ReflectionClass($e))->getShortName(), |
|
87
|
2 |
|
$e->getMessage() |
|
88
|
|
|
) |
|
89
|
|
|
); |
|
90
|
|
|
|
|
91
|
2 |
|
throw $e; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
17 |
|
return new JasperReport($this->ba, $jasperReport, $report); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* @param JasperReport $jasperReport The compiled version of the jasper report |
|
99
|
|
|
* @param ReportParams|null $reportParams if set will override/add to the Report->getReportParams() |
|
100
|
|
|
* @param DataSourceInterface|null $dataSource if ste, will overrive report datasource |
|
101
|
|
|
* |
|
102
|
|
|
* @return JasperPrint |
|
103
|
|
|
* |
|
104
|
|
|
* @throws Exception\JavaProxiedException |
|
105
|
|
|
* @throws Exception\BrokenJsonDataSourceException |
|
106
|
|
|
*/ |
|
107
|
15 |
|
public function fillReport( |
|
108
|
|
|
JasperReport $jasperReport, |
|
109
|
|
|
ReportParams $reportParams = null, |
|
110
|
|
|
DataSourceInterface $dataSource = null |
|
111
|
|
|
): JasperPrint { |
|
112
|
|
|
try { |
|
113
|
|
|
// Step 1: Get the fill manager |
|
114
|
15 |
|
$fillManager = new JasperFillManager($this->ba); |
|
115
|
|
|
|
|
116
|
|
|
// Step 2: get the datasource |
|
117
|
15 |
|
if ($dataSource === null) { |
|
118
|
10 |
|
$dataSource = $jasperReport->getReport()->getDataSource() ?? new EmptyDataSource(); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
// Step 2: Assigning reportParams |
|
122
|
|
|
|
|
123
|
15 |
|
$originalReportParams = $jasperReport->getReport()->getReportParams() ?? new ReportParams(); |
|
124
|
15 |
|
$reportParams = $originalReportParams->withMergedParams($reportParams ?? new ReportParams()); |
|
125
|
|
|
|
|
126
|
|
|
// Step 3: Getting some defaults |
|
127
|
|
|
|
|
128
|
15 |
|
$reportPath = $jasperReport->getReport()->getReportPath(); |
|
129
|
15 |
|
$fileResolver = (new DefaultFileResolver($this->ba))->getFileResolver([$reportPath]); |
|
130
|
15 |
|
$classLoader = (new DefaultClassLoader($this->ba))->getClassLoader([$reportPath]); |
|
131
|
|
|
//$resourceBundle = (new DefaultResourceBundle($this->>ba))->getResourceBundle(); |
|
132
|
15 |
|
$reportParams->addParams([ |
|
133
|
15 |
|
JRParameter::REPORT_FILE_RESOLVER => $fileResolver, |
|
134
|
15 |
|
JRParameter::REPORT_CLASS_LOADER => $classLoader |
|
135
|
|
|
]); |
|
136
|
|
|
|
|
137
|
|
|
// Step 4: Assign parameters from datasource or set the JrDatasource |
|
138
|
|
|
|
|
139
|
15 |
|
$javaDataSource = null; |
|
140
|
15 |
|
if ($dataSource instanceof JRDataSourceFromReportParamsInterface) { |
|
141
|
6 |
|
$dataSource->assignDataSourceReportParams($reportParams); |
|
142
|
9 |
|
} elseif ($dataSource instanceof JRDataSourceFromDataSourceInterface) { |
|
143
|
8 |
|
$javaDataSource = $dataSource->getJRDataSource($this->ba); |
|
144
|
1 |
|
} elseif ($dataSource instanceof JavaSqlConnectionInterface) { |
|
145
|
1 |
|
$javaDataSource = $dataSource->getJasperReportSqlConnection($this->ba); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
15 |
|
$paramsHashMap = $this->ba->java('java.util.HashMap', $reportParams->toArray()); |
|
149
|
|
|
|
|
150
|
15 |
|
$jasperPrint = $fillManager->fillReport( |
|
151
|
15 |
|
$jasperReport->getJavaProxiedObject(), |
|
152
|
|
|
$paramsHashMap, |
|
153
|
|
|
$javaDataSource, |
|
154
|
15 |
|
$jasperReport->getReport()->getReportFile() |
|
155
|
|
|
); |
|
156
|
|
|
|
|
157
|
13 |
|
return new JasperPrint($jasperPrint, $jasperReport->getReport()); |
|
158
|
2 |
|
} catch (\Throwable $e) { |
|
159
|
2 |
|
$this->logger->error( |
|
160
|
2 |
|
sprintf( |
|
161
|
2 |
|
"Filling report '%s' failed with '%s' (%s)", |
|
162
|
2 |
|
basename($jasperReport->getReport()->getReportFile()), |
|
163
|
2 |
|
(new \ReflectionClass($e))->getShortName(), |
|
164
|
2 |
|
$e->getMessage() |
|
165
|
|
|
) |
|
166
|
|
|
); |
|
167
|
|
|
|
|
168
|
2 |
|
throw $e; |
|
169
|
|
|
} |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
9 |
|
public function getExportManager(Report $report): ExportManagerInterface |
|
173
|
|
|
{ |
|
174
|
9 |
|
return new BridgedExportManager($this, $report); |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
public function getLogger(): LoggerInterface |
|
178
|
|
|
{ |
|
179
|
|
|
return $this->logger; |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
12 |
|
public function getBridgeAdapter(): BridgeAdapter |
|
183
|
|
|
{ |
|
184
|
12 |
|
return $this->ba; |
|
185
|
|
|
} |
|
186
|
|
|
} |
|
187
|
|
|
|