Skip to content

Commit a28cba8

Browse files
committed
Use jsonrpc to call report for v>=11
1 parent 1ef4ff8 commit a28cba8

File tree

4 files changed

+314
-160
lines changed

4 files changed

+314
-160
lines changed

pom.xml

+65-49
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,70 @@
1-
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2-
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3-
<modelVersion>4.0.0</modelVersion>
1+
<project xmlns="http://maven.apache.org/POM/4.0.0"
2+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
45

5-
<groupId>com.odoojava</groupId>
6-
<artifactId>odoo-java-api</artifactId>
7-
<version>3.1.2</version>
8-
<packaging>jar</packaging>
6+
<groupId>com.odoojava</groupId>
7+
<artifactId>odoo-java-api</artifactId>
8+
<version>3.1.2</version>
9+
<packaging>jar</packaging>
910

10-
<name>odoo-java-api</name>
11-
<url>http://maven.apache.org</url>
11+
<name>odoo-java-api</name>
12+
<url>http://maven.apache.org</url>
1213

13-
<properties>
14-
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15-
<maven.compiler.source>1.8</maven.compiler.source>
16-
<maven.compiler.target>1.8</maven.compiler.target>
17-
</properties>
14+
<properties>
15+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
16+
<maven.compiler.source>1.8</maven.compiler.source>
17+
<maven.compiler.target>1.8</maven.compiler.target>
18+
</properties>
1819

19-
<dependencies>
20-
<dependency>
21-
<groupId>junit</groupId>
22-
<artifactId>junit</artifactId>
23-
<version>4.12</version>
24-
<scope>test</scope>
25-
</dependency>
26-
<dependency>
27-
<groupId>org.apache.xmlrpc</groupId>
28-
<artifactId>xmlrpc-client</artifactId>
29-
<version>3.1.3</version>
30-
</dependency>
31-
<dependency>
32-
<groupId>org.assertj</groupId>
33-
<artifactId>assertj-core</artifactId>
34-
<version>3.3.0</version>
35-
<scope>test</scope>
36-
</dependency>
37-
<dependency>
38-
<groupId>org.mock-server</groupId>
39-
<artifactId>mockserver-netty</artifactId>
40-
<version>3.10.2</version>
41-
<scope>test</scope>
42-
</dependency>
43-
</dependencies>
44-
<organization>
45-
<name>odoo-java</name>
46-
<url>http://odoo-java.com</url>
47-
</organization>
48-
<scm>
49-
<url>https://github.com/odoo-java/odoo-java-api</url>
50-
</scm>
51-
<issueManagement>
52-
<url>https://github.com/odoo-java/odoo-java-api/issues</url>
53-
</issueManagement>
20+
<dependencies>
21+
<dependency>
22+
<groupId>junit</groupId>
23+
<artifactId>junit</artifactId>
24+
<version>4.12</version>
25+
<scope>test</scope>
26+
</dependency>
27+
<dependency>
28+
<groupId>org.apache.xmlrpc</groupId>
29+
<artifactId>xmlrpc-client</artifactId>
30+
<version>3.1.3</version>
31+
</dependency>
32+
<dependency>
33+
<groupId>org.assertj</groupId>
34+
<artifactId>assertj-core</artifactId>
35+
<version>3.3.0</version>
36+
<scope>test</scope>
37+
</dependency>
38+
<dependency>
39+
<groupId>org.mock-server</groupId>
40+
<artifactId>mockserver-netty</artifactId>
41+
<version>3.10.2</version>
42+
<scope>test</scope>
43+
</dependency>
44+
<dependency>
45+
<groupId>javax.xml.bind</groupId>
46+
<artifactId>jaxb-api</artifactId>
47+
<version>2.4.0-b180830.0359</version>
48+
</dependency>
49+
<dependency>
50+
<groupId>org.eclipse.lsp4j</groupId>
51+
<artifactId>org.eclipse.lsp4j.jsonrpc</artifactId>
52+
<version>0.9.0</version>
53+
</dependency>
54+
<dependency>
55+
<groupId>com.github.briandilley.jsonrpc4j</groupId>
56+
<artifactId>jsonrpc4j</artifactId>
57+
<version>1.5.1</version>
58+
</dependency>
59+
</dependencies>
60+
<organization>
61+
<name>odoo-java</name>
62+
<url>http://odoo-java.com</url>
63+
</organization>
64+
<scm>
65+
<url>https://github.com/odoo-java/odoo-java-api</url>
66+
</scm>
67+
<issueManagement>
68+
<url>https://github.com/odoo-java/odoo-java-api/issues</url>
69+
</issueManagement>
5470
</project>

src/main/java/com/odoojava/api/ReportAdapter.java

+71-17
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,12 @@
2121
import java.io.File;
2222
import java.io.FileOutputStream;
2323
import java.io.IOException;
24+
import java.net.URL;
25+
import java.nio.charset.Charset;
26+
import java.nio.charset.StandardCharsets;
2427
import java.text.SimpleDateFormat;
2528
import java.util.ArrayList;
29+
import java.util.Collections;
2630
import java.util.Arrays;
2731
import java.util.Date;
2832
import java.util.HashMap;
@@ -34,8 +38,11 @@
3438
import java.util.stream.Collectors;
3539
import java.util.stream.Stream;
3640

41+
import javax.xml.bind.DatatypeConverter;
42+
3743
import org.apache.xmlrpc.XmlRpcException;
3844

45+
import com.googlecode.jsonrpc4j.JsonRpcHttpClient;
3946
import com.odoojava.api.Field.FieldType;
4047
import com.odoojava.api.helpers.FilterHelper;
4148

@@ -44,16 +51,18 @@
4451
* Main class for managing reports with the server.
4552
*
4653
* @author Florent THOMAS
47-
* @param: reportListCache
48-
* . Consider Object part that will be set with name/model/type of
49-
* the Odoo report
54+
* @param: reportListCache . Consider Object part that will be set with
55+
* name/model/type of the Odoo report
5056
*/
5157
public class ReportAdapter {
5258

5359
private Session session;
5460
private Version serverVersion;
5561
private Object[] report;
5662
private String reportName;
63+
private String reportModel;
64+
private String reportMethod;
65+
private ObjectAdapter objectReportAdapter;
5766

5867
/**
5968
* @
@@ -66,39 +75,74 @@ public ReportAdapter(Session session) throws XmlRpcException {
6675
this.serverVersion = session.getServerVersion();
6776
try {
6877
getReportList();
78+
6979
} catch (OdooApiException e) {
7080
// TODO Auto-generated catch block
7181
e.printStackTrace();
7282
}
7383
}
7484

7585
/*
76-
* Method listing the available report and their type Purpose is to use the
77-
* list later to check the existence of the report and its type. Appropriate
78-
* methods will be possible regarding the type
86+
* Method listing the available report and their type Purpose is to use the list
87+
* later to check the existence of the report and its type. Appropriate methods
88+
* will be possible regarding the type
7989
*/
8090
private void getReportList() throws XmlRpcException, OdooApiException {
8191
reportListCache.clear();
82-
ObjectAdapter objectAd = this.session.getObjectAdapter("ir.actions.report.xml");
92+
objectReportAdapter = this.session.getObjectAdapter(this.getReportModel());
8393
FilterCollection filters = new FilterCollection();
84-
String[] report_tuple = new String[] { "report_name", "model", "name", "report_type" };
85-
RowCollection reports = objectAd.searchAndReadObject(filters, report_tuple);
94+
String[] report_tuple = new String[] { "id", "report_name", "model", "name", "report_type" };
95+
RowCollection reports = objectReportAdapter.searchAndReadObject(filters, report_tuple);
8696
reports.forEach(report -> {
87-
Object[] repName = new Object[] { report.get("name"), report.get("model"), report.get("report_type") };
97+
Object[] repName = new Object[] { report.get("name"), report.get("model"), report.get("report_type"),
98+
report.get("id") };
8899
reportListCache.put(report.get("report_name").toString(), repName);
89100
});
90101
}
91102

103+
/**
104+
* This method is fully inspire by
105+
* https://github.com/OCA/odoorpc/blob/master/odoorpc/report.py#L113 from
106+
* https://github.com/sebalix
107+
*
108+
* @return string representing the reportModel regarding the version
109+
*/
110+
public String getReportModel() {
111+
reportModel = "ir.actions.report";
112+
if (this.serverVersion.getMajor() < 11) {
113+
reportModel = "ir.actions.report.xml";
114+
}
115+
return reportModel;
116+
}
117+
118+
public String getReportMethod() {
119+
reportMethod = "render";
120+
if (this.serverVersion.getMajor() < 11) {
121+
reportModel = "render_report";
122+
}
123+
return reportMethod;
124+
}
125+
92126
/**
93127
* @param reportName
94128
* @param ids
95129
* @return
96-
* @throws XmlRpcException
97-
* @throws OdooApiException
130+
* @throws Throwable
98131
*/
99-
public byte[] getReportAsByte(String reportName, Object[] ids) throws XmlRpcException, OdooApiException {
132+
public byte[] getPDFReportAsByte(String reportName, Object[] ids) throws Throwable {
100133
checkReportName(reportName);
101-
byte[] reportDatas = session.executeReportService(reportName, ids);
134+
byte[] reportDatas;
135+
if (this.serverVersion.getMajor() < 11) {
136+
reportDatas = session.executeReportService(reportName, this.getReportMethod(), ids);
137+
} else {
138+
ArrayList<Object> reportParams = new ArrayList<Object>();
139+
reportParams.add( getReportID());
140+
reportParams.add( ids);
141+
Object[] result = session.call_report_jsonrpc(getReportModel(), getReportMethod(), reportParams);
142+
143+
String pdf_string= (String) result[0];
144+
reportDatas = pdf_string.getBytes(StandardCharsets.ISO_8859_1);
145+
}
102146
return reportDatas;
103147
}
104148

@@ -107,8 +151,7 @@ public byte[] getReportAsByte(String reportName, Object[] ids) throws XmlRpcExce
107151
* Method to prepare the report to be generated Make some usefull tests
108152
* regarding the
109153
*
110-
* @param reportName:
111-
* can be found in Technical > report > report
154+
* @param reportName: can be found in Technical > report > report
112155
* @throws OdooApiException
113156
* @throws XmlRpcException
114157
*/
@@ -142,19 +185,30 @@ public String getReportType() {
142185
return this.report[2].toString();
143186
}
144187

188+
public Integer getReportID() {
189+
return Integer.valueOf(this.report[3].toString());
190+
}
191+
145192
public String PrintReportToFileName(Object[] ids) throws IOException, XmlRpcException, OdooApiException {
146193

147194
File tmp_file = File.createTempFile("odoo-" + report[1].toString() + "-", getReportType().replace("qweb-", "."),
148195
null);
149196

150-
byte[] report_bytes = getReportAsByte(reportName, ids);
197+
byte[] report_bytes;
151198
FileOutputStream report_stream = new FileOutputStream(tmp_file);
152199
try {
200+
report_bytes = getPDFReportAsByte(reportName, ids);
201+
202+
153203
report_stream.write(report_bytes);
204+
} catch (Throwable e) {
205+
// TODO Auto-generated catch block
206+
e.printStackTrace();
154207
} finally {
155208
report_stream.close();
156209
}
157210

158211
return tmp_file.getAbsolutePath().toString();
159212
}
213+
160214
}

0 commit comments

Comments
 (0)