Skip to content

Commit 457af67

Browse files
committed
Merging upstream 3.1.2
2 parents 3b4f923 + 249cacb commit 457af67

File tree

5 files changed

+382
-162
lines changed

5 files changed

+382
-162
lines changed

README.md

Lines changed: 68 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,17 @@ Odoo Java Api
33

44
This repo has been forked because of the project https://github.com/DeBortoliWines/openerp-java-api/issues/31
55

6-
76
A Java API to connect to Odoo and manage data using the XMLRPC interface.
87

98
The API reduces the amount of boiler plate code needed to communicate with Odoo
109
by doing error checking and type conversions.
1110

11+
1212
The Api IS known to work perfectly :
1313
* openerp-java-api-1.3.0 works perfectly up to OpenERP v7 : https://github.com/DeBortoliWines/openerp-java-api/
1414
* openerp-java-api-2.0.x si supposed to work with the new API introduced in odoo v8 : https://github.com/DeBortoliWines/openerp-java-api/
1515
** Take care, the package name have replace openerp by odoo so that compatibility is broken
16-
* openerp-java-api-3.0.x , package renamed, tested on Odoo v10
16+
* openerp-java-api-3.0.x , package renamed, tested on Odoo v10 and Odoov12
1717
** purposed is to be pushed on maven for pentaho integration
1818

1919
For more information, including a Wiki please see the project on SourceForge:
@@ -22,3 +22,69 @@ For more information, including a Wiki please see the project on SourceForge:
2222
The project is realeased under the Apache V2 license starting from version 1.5.
2323
Earlier versions are dual licenced and developers can choose between LGPL3 (original license) and Apache V2.
2424

25+
26+
# Dependencies
27+
28+
This project depends on the XMLRPC library by Apache: http://ws.apache.org/xmlrpc
29+
30+
Download the latest version from apache and extract the tar.
31+
32+
The required jar files are:
33+
xmlrpc-client-3.1.3.jar
34+
xmlrpc-common-3.1.3.jar
35+
ws-commons-util-1.0.2.jar
36+
37+
Add those jar files to your classpath and you should be ok.
38+
39+
# Examples
40+
41+
## Context manipulation
42+
43+
Values must cast in correct forma
44+
45+
```
46+
Map inputMap = new java.util.HashMap();
47+
inputMap.put("active_id", Integer.valueOf(move_id.toString()));
48+
inputMap.put("active_ids", new Object[]{ Integer.valueOf(move_id.toString()) } );
49+
openERPSession.getContext().putAll(inputMap);
50+
```
51+
52+
## Get Many2one results
53+
54+
```
55+
/**
56+
* Get id of a Many2One property of an object row. Static method.
57+
*
58+
* @param row object row
59+
* @param property should be something like "xxx_id"
60+
* @return propertyId the id of the property
61+
*/
62+
public static Integer getMany2OneId(final Row row, final String property) {
63+
if (row != null && property != null) {
64+
Object[] propertyIdName = (Object[])row.get(property);
65+
// propertyIdName[0] = id of Many2One linked object
66+
// propertyIdName[1] = name of Many2One linked object
67+
return (propertyIdName == null ? null : (Integer)propertyIdName[0]);
68+
}
69+
return null;
70+
}
71+
```
72+
73+
## Execute command
74+
75+
```
76+
openERPSession.executeCommand("stock.inventory", "action_done",
77+
new Object[]{Integer.valueOf(inventory_id.toString())});
78+
```
79+
Or with context forced :
80+
```
81+
openERPSession.executeCommandWithContext("account.move.reversal", "reverse_moves",
82+
new Object[]{ remove_id }
83+
);
84+
```
85+
86+
87+
# Other ressources [legacy]
88+
89+
* https://sourceforge.net/p/openerpjavaapi/wiki/Dependencies/
90+
* https://sourceforge.net/p/openerpjavaapi/wiki/Examples/

pom.xml

Lines changed: 65 additions & 49 deletions
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.0.3</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/DeBortoliWines/openerp-java-api</url>
50-
</scm>
51-
<issueManagement>
52-
<url>https://github.com/DeBortoliWines/openerp-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

Lines changed: 71 additions & 17 deletions
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)