Skip to content

Commit b055d18

Browse files
committed
[IMP]Add explicit method to call or not the RPC execute with context
Purpose is to deal with main methods that could have different signature regarding the version. tested successfully on v10
1 parent 186cc7a commit b055d18

File tree

2 files changed

+57
-30
lines changed

2 files changed

+57
-30
lines changed

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

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,12 @@ public Response searchObject(String objectName, Object[] filter, int offset, int
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 ) {
207-
readResult = (Object) session.executeCommand(objectName, "create", new Object[]{values, session.getContext()});
219+
Object readResult;
220+
if (this.session.getServerVersion().getMajor() < 10) {
221+
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/Session.java

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,10 @@ public static ArrayList<String> getDatabaseList(RPCProtocol protocol, String hos
223223

224224
/**
225225
* Executes any command on the server linked to the /xmlrpc/object service.
226-
* 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
227230
*
228231
* @param objectName Object or model name to execute the command on
229232
* @param commandName Command name to execute
@@ -236,29 +239,40 @@ public Object executeCommand(final String objectName, final String commandName,
236239
Object[] connectionParams = new Object[]{databaseName, userID, password, objectName, commandName};
237240

238241
// Combine the connection parameters and command parameters
239-
// Object[] params;
240-
// if ()
241-
Object[] params = new Object[connectionParams.length + (parameters == null ? 0 : parameters.length)];
242+
Object[] params = new Object[connectionParams.length
243+
+ (parameters == null ? 0 : parameters.length)];
242244
System.arraycopy(connectionParams, 0, params, 0, connectionParams.length);
243245

244246
if (parameters != null && parameters.length > 0) {
245247
System.arraycopy(parameters, 0, params, connectionParams.length, parameters.length);
246248
}
247-
if (this.getServerVersion().getMajor() == 9 && this.context.size() > 0) {
248-
Integer initial_length = params.length;
249-
params = Arrays.copyOf(params, initial_length + this.context.size());
250-
System.arraycopy(this.context.toString(), 0, params, initial_length, this.context.size());
251-
}
252-
253-
//Try to always deal with the context
254-
// (this.getServerVersion().getMajor() < 10)
255-
// ? new Object[]{filter, offsetParam, limitParam, orderParam, false, count}
256-
// : new Object[]{filter, offsetParam, limitParam, orderParam, count};
257-
// this.getServerVersion()
258-
// System.arraycopy(this.getContext(), 0, params, connectionParams.length, parameters.length);
259249
return objectClient.execute("execute", params);
260250
}
261251

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+
262276
/**
263277
* Executes a workflow by sending a signal to the workflow engine for a
264278
* specific object. This functions calls the 'exec_workflow' method on the

0 commit comments

Comments
 (0)