forked from DeBortoliWines/openerp-java-api
-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathOdooCommand.java
More file actions
264 lines (239 loc) · 10.9 KB
/
Copy pathOdooCommand.java
File metadata and controls
264 lines (239 loc) · 10.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/*
* Copyright 2011, 2013-2014 De Bortoli Wines Pty Limited (Australia)
*
* This file is part of OdooJavaAPI.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package com.odoojava.api;
import java.util.Map;
import org.apache.xmlrpc.XmlRpcException;
/**
* Wrapper class for Odoo commands. It uses the session object to make the call,
* but builds the parameters in this class
*
* @author Pieter van der Merwe
*
*/
public class OdooCommand {
private final Session session;
/**
* Main constructor
*
* @param session Session object that will be used to make the calls to
* Odoo.
*/
public OdooCommand(Session session) {
this.session = session;
}
/**
* Searches for objects that satisfies the filter. These IDs are typically
* used in a following readObject call to the server to get the data
*
* @param objectName The object name to do a search for
* @param filter A filter array that contains a list of filters to be
* applied.
* @return Response that need to be parsed from the calling method to check
* if successful and then adapt the result as an array.
*/
public Response searchObject(String objectName, Object[] filter) throws XmlRpcException {
return (Response) searchObject(objectName, filter, -1, -1, null, false);
}
/**
* Searches for objects that satisfies the filter. These IDs are typically
* used in a following readObject call to the server to get the data
*
* @param objectName The object name to do a search for
* @param filter A filter array that contains a list of filters to be
* applied.
* @param offset Number of records to skip. -1 for no offset.
* @param limit Maximum number of rows to return. -1 for no limit.
* @param order Field name to order on
* @param count If the count should be returned, in stead of the IDs
* @return If count = true, a integer is returned, otherwise a Response is
* returned and could be parsed as Object[] of IDs if response is
* successfull
*/
public Response searchObject(String objectName, Object[] filter, int offset, int limit, String order, boolean count) throws XmlRpcException {
Object offsetParam = offset < 0 ? false : offset;
Object limitParam = limit < 0 ? false : limit;
Object orderParam = order == null || order.length() == 0 ? false : order;
// Before Odoo 10 there's a 'context' parameter between order and count
Object[] params = (this.session.getServerVersion().getMajor() < 10)
? new Object[]{filter, offsetParam, limitParam, orderParam, session.getContext(), count}
: new Object[]{filter, offsetParam, limitParam, orderParam, count};
try {
//TODO: test differents version with search on quantity on products
//with location_id in the context to check that it works or not
Response response = (this.session.getServerVersion().getMajor() < 10)
? new Response(session.executeCommand(objectName, "search", params))
: new Response(session.executeCommandWithContext(objectName, "search", params));
return response;
} catch (XmlRpcException e) {
return new Response(e);
}
}
/**
* Fetches field information for an object n Odoo
*
* @param objectName Object or model name to fetch field information for
* @param filterFields Only return data for files in the filter list
* @return A HashMap of field-value pairs.
* @throws XmlRpcException
*/
@SuppressWarnings("unchecked")
public Map<String, Object> getFields(String objectName, String[] filterFields) throws XmlRpcException {
Map<String, Object> fieldsArray;
if (this.session.getServerVersion().getMajor() >= 8) {
fieldsArray = (Map<String, Object>) session.executeCommand(objectName, "fields_get",
new Object[]{filterFields});
} else {
fieldsArray = (Map<String, Object>) session.executeCommand(objectName, "fields_get",
new Object[]{filterFields, session.getContext()});
}
return fieldsArray;
}
/**
* Reads object data from the Odoo server
*
* @param objectName Name of the object to return data for
* @param ids List of id to fetch data for. Call searchObject to get a
* potential list
* @param fields List of fields to return data for
* @returnA collection of rows for an Odoo object
* @throws XmlRpcException
*/
public Object[] readObject(String objectName, Object[] ids, String[] fields) throws XmlRpcException {
Object[] readResult;
if (this.session.getServerVersion().getMajor() >= 8) {
readResult = (Object[]) session.executeCommandWithContext(objectName, "read", new Object[]{ids, fields});
} else {
//TODO: Have to be rewritten/deleted considering the previous call
readResult = (Object[]) session.executeCommand(objectName, "read", new Object[]{ids, fields, session.getContext()});
}
return readResult;
}
/**
* Updates object values
*
* @param objectName Name of the object to update
* @param id Database ID number of the object to update
* @param valueList Field/Value pairs to update on the object
* @return True if the update was successful
* @throws XmlRpcException
*/
public boolean writeObject(String objectName, int id, Map<String, Object> valueList) throws XmlRpcException {
if (this.session.getServerVersion().getMajor() < 10) {
//Prior to the v10, each version have to be adapted if needed
//Some methods on certains class from v8 to v9 don't respect the syntax
return (Boolean) session.executeCommand(objectName, "write", new Object[]{id, valueList});
} else {
//Work perfectly for the v10, please keep this check
return (Boolean) session.executeCommandWithContext(objectName, "write", new Object[]{id, valueList});
}
}
/**
* Calls the import function on the server to bulk create/update records
*
* @param objectName Name of the object to update
* @param fieldList List of fields to update. The import function has some
* specific naming conventions. Consider using the ObjectAdapter
* @param rows Rows to import. Fields must be in the same order as the
* 'fieldList' parameter
* @return The result returned from the server. Either returns the number of
* successfully imported rows or returns the error.
* @throws XmlRpcException
*/
public Object[] importData(String objectName, String[] fieldList, Object[][] rows) throws XmlRpcException {
//TODO : deal with v10 version and context
return (Object[]) session.executeCommand(objectName, "import_data", new Object[]{fieldList, rows, "init", "", false, session.getContext()});
}
@SuppressWarnings("unchecked")
public Map<String, Object> load(String objectName, String[] fieldList, Object[][] rows) throws XmlRpcException {
Object o = session.executeCommand(objectName, "load", new Object[]{fieldList, rows});
return (Map<String, Object>) o;
}
/**
* Returns the name_get result of an object in the Odoo server.
*
* @param objectName Object name to invoke the name_get on
* @param ids Database IDs to invoke the name_get for
* @return An Object[] with an entry for each ID. Each entry is another
* Object [] with index 0 being the ID and index 1 being the Name
* @throws XmlRpcException
*/
public Object[] nameGet(String objectName, Object[] ids) throws XmlRpcException {
return (Object[]) session.executeCommand(objectName, "name_get", new Object[]{ids});
}
/**
* Deletes objects from the Odoo Server
*
* @param objectName Object name to delete rows from
* @param ids List of ids to delete data from
* @return If the command was successful
* @throws XmlRpcException
*/
public boolean unlinkObject(String objectName, Object[] ids) throws XmlRpcException {
return (Boolean) session.executeCommand(objectName, "unlink", new Object[]{ids});
}
/**
* Creates a single object
*
* @param objectName Name of the object to create
* @param values Map of values to assign to the new object
* @return The database ID of the new object
* @throws XmlRpcException
*/
public Object createObject(String objectName, Map<String, Object> values) throws XmlRpcException {
Object readResult;
if (this.session.getServerVersion().getMajor() < 10) {
readResult = (Object) session.executeCommand(objectName, "create", new Object[]{values});
} else {
readResult = (Object) session.executeCommandWithContext(objectName, "create", new Object[]{values});
}
return readResult;
}
/**
* Calls any function on an object. The function Odoo must have the
* signature like (self, cr, uid, *param) and return a dictionary or object.
* *param can be replaced by separate parameters if you are sure of the
* number of parameters expected
*
* @param objectName Object name where the function exists
* @param functionName function to call
* @param parameters Additional parameters that will be passed to the object
* @return An Object array of values
*/
public Response callObjectFunction(String objectName, String functionName, Object[] parameters) {
try {
return new Response(session.executeCommand(objectName, functionName, parameters));
} catch (XmlRpcException e) {
return new Response(e);
}
}
/**
* Executes a workflow by sending a signal to the workflow engine for a
* specific object. All parameters are prepended by:
* "databaseName,userID,password"
*
* @param objectName Object or model name to send the signal for
* @param signal Signal name to send, for example order_confirm
* @param objectID Specific object ID to send the signal for
* @return
* @throws XmlRpcException
*/
public void executeWorkflow(final String objectName, final String signal, final int objectID) throws XmlRpcException {
session.executeWorkflow(objectName, signal, objectID);
}
}