Skip to content

Commit 8f6ec43

Browse files
authored
Merge pull request DeBortoliWines#41 from DeBortoliWines/context-for-v10
[10.0]Explicit context in RPC call
2 parents 6f30449 + 5a495dd commit 8f6ec43

File tree

4 files changed

+63
-19
lines changed

4 files changed

+63
-19
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
<groupId>com.debortoliwines</groupId>
66
<artifactId>odoo-java-api</artifactId>
7-
<version>2.0.1</version>
7+
<version>2.0.2</version>
88
<packaging>jar</packaging>
99

1010
<name>odoo-java-api</name>

src/main/java/com/debortoliwines/odoo/api/OdooCommand.java

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,16 @@ public Response searchObject(String objectName, Object[] filter, int offset, int
7878
Object orderParam = order == null || order.length() == 0 ? false : order;
7979
// Before Odoo 10 there's a 'context' parameter between order and count
8080
Object[] params = (this.session.getServerVersion().getMajor() < 10)
81-
? new Object[]{filter, offsetParam, limitParam, orderParam, false, count}
81+
? new Object[]{filter, offsetParam, limitParam, orderParam, session.getContext(), count}
8282
: new Object[]{filter, offsetParam, limitParam, orderParam, count};
8383

8484
try {
85-
Response response = new Response(session.executeCommand(objectName, "search", params));
85+
//TODO: test differents version with search on quantity on products
86+
//with location_id in the context to check that it works or not
87+
Response response = (this.session.getServerVersion().getMajor() < 10)
88+
? new Response(session.executeCommand(objectName, "search", params))
89+
: new Response(session.executeCommandWithContext(objectName, "search", params));
90+
8691
return response;
8792
} catch (XmlRpcException e) {
8893
return new Response(e);
@@ -100,7 +105,7 @@ public Response searchObject(String objectName, Object[] filter, int offset, int
100105
@SuppressWarnings("unchecked")
101106
public Map<String, Object> getFields(String objectName, String[] filterFields) throws XmlRpcException {
102107
Map<String, Object> fieldsArray;
103-
if (this.session.getServerVersion().getMajor() >= 8 ) {
108+
if (this.session.getServerVersion().getMajor() >= 8) {
104109
fieldsArray = (Map<String, Object>) session.executeCommand(objectName, "fields_get",
105110
new Object[]{filterFields});
106111
} else {
@@ -122,13 +127,14 @@ public Map<String, Object> getFields(String objectName, String[] filterFields) t
122127
* @throws XmlRpcException
123128
*/
124129
public Object[] readObject(String objectName, Object[] ids, String[] fields) throws XmlRpcException {
125-
Object[] readResult ;
126-
if (this.session.getServerVersion().getMajor() >= 8 ) {
127-
readResult = (Object[]) session.executeCommand(objectName, "read", new Object[]{ids, fields});
130+
Object[] readResult;
131+
if (this.session.getServerVersion().getMajor() >= 8) {
132+
readResult = (Object[]) session.executeCommandWithContext(objectName, "read", new Object[]{ids, fields});
128133
} else {
134+
//TODO: Have to be rewritten/deleted considering the previous call
129135
readResult = (Object[]) session.executeCommand(objectName, "read", new Object[]{ids, fields, session.getContext()});
130136
}
131-
137+
132138
return readResult;
133139
}
134140

@@ -142,7 +148,15 @@ public Object[] readObject(String objectName, Object[] ids, String[] fields) thr
142148
* @throws XmlRpcException
143149
*/
144150
public boolean writeObject(String objectName, int id, Map<String, Object> valueList) throws XmlRpcException {
145-
return (Boolean) session.executeCommand(objectName, "write", new Object[]{id, valueList});
151+
if (this.session.getServerVersion().getMajor() < 10) {
152+
//Prior to the v10, each version have to be adapted if needed
153+
//Some methods on certains class from v8 to v9 don't respect the syntax
154+
return (Boolean) session.executeCommand(objectName, "write", new Object[]{id, valueList});
155+
} else {
156+
//Work perfectly for the v10, please keep this check
157+
return (Boolean) session.executeCommandWithContext(objectName, "write", new Object[]{id, valueList});
158+
}
159+
146160
}
147161

148162
/**
@@ -202,16 +216,15 @@ public boolean unlinkObject(String objectName, Object[] ids) throws XmlRpcExcept
202216
* @throws XmlRpcException
203217
*/
204218
public Object createObject(String objectName, Map<String, Object> values) throws XmlRpcException {
205-
Object readResult ;
206-
if (this.session.getServerVersion().getMajor() >= 8 ) {
219+
Object readResult;
220+
if (this.session.getServerVersion().getMajor() < 10) {
207221
readResult = (Object) session.executeCommand(objectName, "create", new Object[]{values});
208222
} else {
209-
readResult = (Object) session.executeCommand(objectName, "create", new Object[]{values, session.getContext()});
223+
readResult = (Object) session.executeCommandWithContext(objectName, "create", new Object[]{values});
210224
}
211-
225+
212226
return readResult;
213-
214-
227+
215228
}
216229

217230
/**

src/main/java/com/debortoliwines/odoo/api/OdooXmlRpcProxy.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,10 @@ public OdooXmlRpcProxy(RPCProtocol protocol, String host, int port, RPCServices
104104

105105
XmlRpcClientConfigImpl xmlrpcConfigLogin = new XmlRpcClientConfigImpl();
106106

107-
// Odoo does not support extensions
108-
xmlrpcConfigLogin.setEnabledForExtensions(false);
107+
// 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
110+
xmlrpcConfigLogin.setEnabledForExtensions(true);
109111

110112
// The URL is hardcoded and can not be malformed
111113
try {

src/main/java/com/debortoliwines/odoo/api/Session.java

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
import com.debortoliwines.odoo.api.OdooXmlRpcProxy.RPCProtocol;
2727
import com.debortoliwines.odoo.api.OdooXmlRpcProxy.RPCServices;
28+
import java.util.Arrays;
2829

2930
/**
3031
* *
@@ -222,7 +223,10 @@ public static ArrayList<String> getDatabaseList(RPCProtocol protocol, String hos
222223

223224
/**
224225
* Executes any command on the server linked to the /xmlrpc/object service.
225-
* All parameters are prepended by: "databaseName,userID,password"
226+
* All parameters are prepended by: "databaseName,userID,password" This
227+
* method execute the command without the context parameter Its purpose is
228+
* to be used by Odoo version prior to v10 or for v10 methods that mustn't
229+
* use the context
226230
*
227231
* @param objectName Object or model name to execute the command on
228232
* @param commandName Command name to execute
@@ -235,7 +239,8 @@ public Object executeCommand(final String objectName, final String commandName,
235239
Object[] connectionParams = new Object[]{databaseName, userID, password, objectName, commandName};
236240

237241
// Combine the connection parameters and command parameters
238-
Object[] params = new Object[connectionParams.length + (parameters == null ? 0 : parameters.length)];
242+
Object[] params = new Object[connectionParams.length
243+
+ (parameters == null ? 0 : parameters.length)];
239244
System.arraycopy(connectionParams, 0, params, 0, connectionParams.length);
240245

241246
if (parameters != null && parameters.length > 0) {
@@ -244,6 +249,30 @@ public Object executeCommand(final String objectName, final String commandName,
244249
return objectClient.execute("execute", params);
245250
}
246251

252+
/**
253+
* Executes any command on the server linked to the /xmlrpc/object service.
254+
* parameters and Context are prepended .The context MUST NOT have been
255+
* already passed in the parameters.
256+
*
257+
* @param objectName Object or model name to execute the command on
258+
* @param commandName Command name to execute
259+
* @param parameters List of parameters for the command. For easy of use,
260+
* consider the OdooCommand object or ObjectAdapter
261+
* @return The result of the call
262+
* @throws XmlRpcException
263+
*/
264+
public Object executeCommandWithContext(final String objectName, final String commandName, final Object[] parameters) throws XmlRpcException {
265+
Object[] connectionParams = new Object[]{databaseName, userID, password, objectName, commandName};
266+
267+
// Combine the parameters with the context
268+
Object[] params = new Object[1 + (parameters == null ? 0 : parameters.length)];
269+
if (parameters != null && parameters.length > 0) {
270+
System.arraycopy(parameters, 0, params, 0, parameters.length);
271+
}
272+
System.arraycopy(new Object[]{getContext()}, 0, params, parameters.length, 1);
273+
return executeCommand(objectName, commandName, params);
274+
}
275+
247276
/**
248277
* Executes a workflow by sending a signal to the workflow engine for a
249278
* specific object. This functions calls the 'exec_workflow' method on the

0 commit comments

Comments
 (0)