Skip to content

Commit 983bce1

Browse files
committed
Rename and add report functionnality
1 parent 563152d commit 983bce1

File tree

7 files changed

+673
-310
lines changed

7 files changed

+673
-310
lines changed

pom.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,14 @@
4141
<scope>test</scope>
4242
</dependency>
4343
</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>
4454
</project>

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ private static void refreshSignalCache(OdooCommand command) throws OdooApiExcept
164164
/*
165165
* Get the parent workflow to work out get the object name
166166
*/
167+
//TODO: Check the latest syntax or workflow
167168
int wkfId = Integer.parseInt(((Object[]) ((HashMap<String, Object>) row).get("wkf_id"))[0].toString());
168169
Object[] workflow = command.readObject("workflow", new Object[]{wkfId}, new String[]{"osv"});
169170

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

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -37,28 +37,26 @@
3737
* @author fpoyer
3838
*/
3939
public class OdooXmlRpcProxy extends XmlRpcClient {
40-
40+
4141
/**
4242
* Enum for the main RPC services that Odoo expose
4343
*/
4444
public enum RPCServices {
45-
RPC_COMMON,
46-
RPC_OBJECT,
47-
RPC_DATABASE
45+
RPC_COMMON, RPC_OBJECT, RPC_DATABASE, RPC_REPORT
4846
}
49-
47+
5048
/**
5149
* Enum for the RPC protocol used to connect to Odoo
5250
*/
53-
public enum RPCProtocol {
54-
RPC_HTTP,
55-
RPC_HTTPS
56-
}
57-
51+
public enum RPCProtocol {
52+
RPC_HTTP, RPC_HTTPS
53+
}
54+
5855
private final String RPC_COMMON_URL = "/xmlrpc/2/common";
5956
private final String RPC_OBJECT_URL = "/xmlrpc/2/object";
57+
private final String RPC_REPORT_URL = "/xmlrpc/2/report";
6058
private final String RPC_DATABASE_URL = "/xmlrpc/2/db";
61-
59+
6260
/**
6361
* Proxy object to handle calls to and from the Odoo server
6462
*
@@ -74,21 +72,24 @@ public enum RPCProtocol {
7472
*/
7573
public OdooXmlRpcProxy(RPCProtocol protocol, String host, int port, RPCServices service) {
7674
super();
77-
75+
7876
String URL = "";
79-
77+
8078
switch (service) {
8179
case RPC_COMMON:
8280
URL = this.RPC_COMMON_URL;
8381
break;
8482
case RPC_OBJECT:
8583
URL = this.RPC_OBJECT_URL;
8684
break;
85+
case RPC_REPORT:
86+
URL = this.RPC_REPORT_URL;
87+
break;
8788
case RPC_DATABASE:
8889
URL = this.RPC_DATABASE_URL;
8990
break;
9091
}
91-
92+
9293
String protocol_str = "";
9394
switch (protocol) {
9495
case RPC_HTTP:
@@ -103,18 +104,18 @@ public OdooXmlRpcProxy(RPCProtocol protocol, String host, int port, RPCServices
103104
useProxyIfAvailable(protocol);
104105

105106
XmlRpcClientConfigImpl xmlrpcConfigLogin = new XmlRpcClientConfigImpl();
106-
107+
107108
// Odoo seems to be documented for supporting extensions
108-
//https://doc.odoo.com/v6.0/developer/6_22_XML-RPC_web_services/index.html/
109-
//https://www.odoo.com/fr_FR/forum/aide-1/question/using-openerp-xml-rpc-api-with-java-2872
109+
// https://doc.odoo.com/v6.0/developer/6_22_XML-RPC_web_services/index.html/
110+
// https://www.odoo.com/fr_FR/forum/aide-1/question/using-openerp-xml-rpc-api-with-java-2872
110111
xmlrpcConfigLogin.setEnabledForExtensions(true);
111-
112+
112113
// The URL is hardcoded and can not be malformed
113114
try {
114115
xmlrpcConfigLogin.setServerURL(new URL(protocol_str, host, port, URL));
115116
} catch (MalformedURLException e) {
116117
}
117-
118+
118119
this.setConfig(xmlrpcConfigLogin);
119120
}
120121

@@ -164,8 +165,8 @@ void useProxyIfAvailable(RPCProtocol protocol) {
164165
System.err.println("No transport factory or not compatible with Proxy support!");
165166
}
166167
}
167-
168-
/***
168+
169+
/***
169170
* Returns the Odoo server version. For example 7.0-20130216-002451 or 6.1-1
170171
*
171172
* @param protocol
@@ -178,10 +179,9 @@ void useProxyIfAvailable(RPCProtocol protocol) {
178179
* @return The version number as a String
179180
* @throws XmlRpcException
180181
*/
181-
public static Version getServerVersion(RPCProtocol protocol, String host, int port) throws XmlRpcException
182-
{
183-
OdooXmlRpcProxy client = new OdooXmlRpcProxy(protocol, host, port, RPCServices.RPC_DATABASE);
184-
185-
return new Version(client.execute("server_version", new Object[] {}).toString());
186-
}
182+
public static Version getServerVersion(RPCProtocol protocol, String host, int port) throws XmlRpcException {
183+
OdooXmlRpcProxy client = new OdooXmlRpcProxy(protocol, host, port, RPCServices.RPC_DATABASE);
184+
185+
return new Version(client.execute("server_version", new Object[] {}).toString());
186+
}
187187
}
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
/*
2+
* Copyright 2018, 2020 Mind And Go
3+
*
4+
* This file is part of OdooJavaAPI.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*
18+
*/
19+
package com.odoojava.api;
20+
21+
import java.io.File;
22+
import java.io.FileOutputStream;
23+
import java.io.IOException;
24+
import java.text.SimpleDateFormat;
25+
import java.util.ArrayList;
26+
import java.util.Arrays;
27+
import java.util.Date;
28+
import java.util.HashMap;
29+
import java.util.List;
30+
import java.util.Map;
31+
import java.util.Map.Entry;
32+
import java.util.TimeZone;
33+
import java.util.concurrent.ConcurrentHashMap;
34+
import java.util.stream.Collectors;
35+
import java.util.stream.Stream;
36+
37+
import org.apache.xmlrpc.XmlRpcException;
38+
39+
import com.odoojava.api.Field.FieldType;
40+
import com.odoojava.api.helpers.FilterHelper;
41+
42+
/**
43+
*
44+
* Main class for managing reports with the server.
45+
*
46+
* @author Florent THOMAS
47+
* @param: reportListCache
48+
* . Consider Object part that will be set with name/model/type of
49+
* the Odoo report
50+
*/
51+
public class ReportAdapter {
52+
53+
private Session session;
54+
private Version serverVersion;
55+
private Object[] report;
56+
private String reportName;
57+
58+
/**
59+
* @
60+
*/
61+
private static final HashMap<String, Object[]> reportListCache = new HashMap<String, Object[]>();
62+
63+
public ReportAdapter(Session session) throws XmlRpcException {
64+
super();
65+
this.session = session;
66+
this.serverVersion = session.getServerVersion();
67+
try {
68+
getReportList();
69+
} catch (OdooApiException e) {
70+
// TODO Auto-generated catch block
71+
e.printStackTrace();
72+
}
73+
}
74+
75+
/*
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
79+
*/
80+
private void getReportList() throws XmlRpcException, OdooApiException {
81+
reportListCache.clear();
82+
ObjectAdapter objectAd = this.session.getObjectAdapter("ir.actions.report.xml");
83+
FilterCollection filters = new FilterCollection();
84+
String[] report_tuple = new String[] { "report_name", "model", "name", "report_type" };
85+
RowCollection reports = objectAd.searchAndReadObject(filters, report_tuple);
86+
reports.forEach(report -> {
87+
Object[] repName = new Object[] { report.get("name"), report.get("model"), report.get("report_type") };
88+
reportListCache.put(report.get("report_name").toString(), repName);
89+
});
90+
}
91+
92+
/**
93+
* @param reportName
94+
* @param ids
95+
* @return
96+
* @throws XmlRpcException
97+
* @throws OdooApiException
98+
*/
99+
public byte[] getReportAsByte(String reportName, Object[] ids) throws XmlRpcException, OdooApiException {
100+
checkReportName(reportName);
101+
byte[] reportDatas = session.executeReportService(reportName, ids);
102+
return reportDatas;
103+
}
104+
105+
/**
106+
*
107+
* Method to prepare the report to be generated Make some usefull tests
108+
* regarding the
109+
*
110+
* @param reportName:
111+
* can be found in Technical > report > report
112+
* @throws OdooApiException
113+
* @throws XmlRpcException
114+
*/
115+
private void checkReportName(String reportName) throws OdooApiException, XmlRpcException {
116+
// TODO Auto-generated method stub
117+
// refresh
118+
getReportList();
119+
if (reportName == null) {
120+
throw new OdooApiException("Report Name is mandatory. Please read the Odoo help.");
121+
}
122+
Object[] report = reportListCache.get(reportName);
123+
if (report == null) {
124+
throw new OdooApiException(
125+
"Your report don't seems to exist in the Odoo Database." + "Please check your configuration");
126+
}
127+
if (!Arrays.asList("qweb-pdf", "qweb-html").contains(report[2])) {
128+
throw new OdooApiException(
129+
"Your report type is obsolete. Only QWEB report are allowed." + "Please check your configuration");
130+
}
131+
132+
}
133+
134+
public void setReport(String reportName) throws OdooApiException, XmlRpcException {
135+
checkReportName(reportName);
136+
Object[] report = reportListCache.get(reportName);
137+
this.report = report;
138+
this.reportName = reportName;
139+
}
140+
141+
public String getReportType() {
142+
return this.report[2].toString();
143+
}
144+
145+
public String PrintReportToFileName(Object[] ids) throws IOException, XmlRpcException, OdooApiException {
146+
147+
File tmp_file = File.createTempFile("odoo-" + report[1].toString() + "-", getReportType().replace("qweb-", "."),
148+
null);
149+
150+
byte[] report_bytes = getReportAsByte(reportName, ids);
151+
FileOutputStream report_stream = new FileOutputStream(tmp_file);
152+
try {
153+
report_stream.write(report_bytes);
154+
} finally {
155+
report_stream.close();
156+
}
157+
158+
return tmp_file.getAbsolutePath().toString();
159+
}
160+
}

0 commit comments

Comments
 (0)