|
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\Proxy\Engine; |
|
15
|
|
|
|
|
16
|
|
|
use Soluble\Japha\Bridge\Adapter as BridgeAdapter; |
|
17
|
|
|
use Soluble\Japha\Bridge\Exception\JavaException; |
|
18
|
|
|
use Soluble\Japha\Interfaces\JavaObject; |
|
19
|
|
|
use Soluble\Jasper\DataSource\JsonDataSource; |
|
20
|
|
|
use Soluble\Jasper\Exception; |
|
21
|
|
|
use Soluble\Jasper\Proxy\RemoteJavaObjectProxyInterface; |
|
22
|
|
|
|
|
23
|
|
|
class JasperFillManager implements RemoteJavaObjectProxyInterface |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* @var BridgeAdapter |
|
27
|
|
|
*/ |
|
28
|
|
|
private $ba; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var JavaObject Java('net.sf.jasperreports.engine.JasperFillManager') |
|
32
|
|
|
*/ |
|
33
|
|
|
private $jasperFillManager; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var JavaObject|null |
|
37
|
|
|
*/ |
|
38
|
|
|
private $jasperReportsContext; |
|
39
|
|
|
|
|
40
|
17 |
|
public function __construct(BridgeAdapter $bridgeAdapter, JavaObject $jasperReportsContext = null) |
|
41
|
|
|
{ |
|
42
|
17 |
|
$this->ba = $bridgeAdapter; |
|
43
|
17 |
|
$this->jasperReportsContext = $jasperReportsContext; |
|
44
|
17 |
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @param JavaObject $jasperReport Java('net.sf.jasperreports.engine.JasperReport') |
|
48
|
|
|
* @param JavaObject $params Java('java.util.HashMap') |
|
49
|
|
|
* @param JavaObject|null $dataSource Java('net.sf.jasperreports.engine.JRDataSource') |
|
50
|
|
|
* |
|
51
|
|
|
* @return JavaObject Java('net.sf.jasperreports.engine.JasperPrint') |
|
52
|
|
|
* |
|
53
|
|
|
* @throws Exception\BrokenJsonDataSourceException When the json datasource cannot be parsed |
|
54
|
|
|
* @throws Exception\JavaProxiedException When filling the report has encountered a Java error |
|
55
|
|
|
* @throws Exception\RuntimeException An unexpected error happened |
|
56
|
|
|
*/ |
|
57
|
15 |
|
public function fillReport( |
|
58
|
|
|
JavaObject $jasperReport, |
|
59
|
|
|
JavaObject $params, |
|
60
|
|
|
JavaObject $dataSource = null, |
|
61
|
|
|
string $reportFile = null |
|
62
|
|
|
): JavaObject { |
|
63
|
|
|
try { |
|
64
|
15 |
|
return ($dataSource === null) ? |
|
65
|
6 |
|
$this->getJavaProxiedObject()->fillReport($jasperReport, $params) |
|
66
|
13 |
|
: $this->getJavaProxiedObject()->fillReport($jasperReport, $params, $dataSource); |
|
67
|
2 |
|
} catch (JavaException $e) { |
|
68
|
2 |
|
throw $this->getFillManagerJavaException($e, $jasperReport, $params, $reportFile); |
|
69
|
|
|
} catch (\Throwable $e) { |
|
70
|
|
|
throw new Exception\RuntimeException($e->getMessage(), $e->getCode(), $e); |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Return jasperFillManager mapped exception:. |
|
76
|
|
|
* |
|
77
|
|
|
* Exception\BrokenJsonDataSourceException when the json datasource cannot be parsed |
|
78
|
|
|
* Exception\JavaProxiedException when filling the report has encountered a Java error |
|
79
|
|
|
*/ |
|
80
|
2 |
|
private function getFillManagerJavaException( |
|
81
|
|
|
JavaException $e, |
|
82
|
|
|
JavaObject $jasperReport, |
|
83
|
|
|
JavaObject $params, |
|
84
|
|
|
?string $reportFile = null |
|
85
|
|
|
): Exception\ExceptionInterface { |
|
86
|
2 |
|
$exception = null; |
|
87
|
2 |
|
$className = $e->getJavaClassName(); |
|
88
|
2 |
|
if ($className === 'net.sf.jasperreports.engine.JRException') { |
|
89
|
2 |
|
$cause = $e->getCause(); |
|
90
|
2 |
|
$jsonParseMatch = preg_match('/Json([A-Z])+Exception/i', $cause); |
|
91
|
2 |
|
if ($jsonParseMatch > 0) { |
|
92
|
2 |
|
$exception = new Exception\BrokenJsonDataSourceException($e, sprintf( |
|
93
|
2 |
|
'Fill error, json datasource cannot be parsed "%s" in %s', |
|
94
|
2 |
|
(string) $params[JsonDataSource::PARAM_JSON_SOURCE], |
|
95
|
2 |
|
$reportFile ?? $jasperReport->getName() |
|
96
|
|
|
)); |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
2 |
|
return $exception ?? new Exception\JavaProxiedException( |
|
101
|
|
|
$e, |
|
102
|
|
|
sprintf( |
|
103
|
|
|
'Error filling report "%s"', |
|
104
|
2 |
|
$reportFile ?? $jasperReport->getName() |
|
105
|
|
|
) |
|
106
|
|
|
); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* @return JavaObject Java('net.sf.jasperreports.engine.JasperFillManager') |
|
111
|
|
|
*/ |
|
112
|
17 |
|
public function getJavaProxiedObject(): JavaObject |
|
113
|
|
|
{ |
|
114
|
17 |
|
if ($this->jasperFillManager === null) { |
|
115
|
17 |
|
if ($this->jasperReportsContext === null) { |
|
116
|
16 |
|
$this->jasperFillManager = $this->ba->javaClass('net.sf.jasperreports.engine.JasperFillManager'); |
|
117
|
|
|
} else { |
|
118
|
1 |
|
$cls = $this->ba->javaClass('net.sf.jasperreports.engine.JasperFillManager'); |
|
119
|
|
|
|
|
120
|
1 |
|
$this->jasperFillManager = $cls->getInstance($this->jasperReportsContext); |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
17 |
|
return $this->jasperFillManager; |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
|