-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathOdooConnect.java
341 lines (299 loc) · 12.5 KB
/
OdooConnect.java
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
package com.example.eduard.odooconnect;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import de.timroes.axmlrpc.XMLRPCClient;
import de.timroes.axmlrpc.XMLRPCException;
import android.content.ContentValues;
import android.util.Log;
import static java.util.Arrays.asList;
import static java.util.Collections.emptyMap;
/**
* Original Class: https://github.com/zikzakmedia/android-openerp
* References Odoo10 Web Service API: https://www.odoo.com/documentation/10.0/api_integration.html
*
* This class provides access to basic methods in OpenObject, so you can use
* them from an Android device. The operations supported are: <br>
* <ul>
* <li>login</li>
* <li>create</li>
* <li>search / read</li>
* <li>count</li>
* <li>write</li>
* <li>unlink</li>
* <li>call (This is a generic method to call whatever you need)</li>
* </ul>
*
* You can extend OdooConnect to implement more specific methods of your need.
*
* @author Edu <eduojerb@gmail.com>
*/
public class OdooConnect {
private String mServer;
private Integer mPort;
private String mDatabase;
private String mUserName;
private String mPassword;
private Integer mUserId;
private URL mUrl;
private static final String CONNECTOR_NAME = "OdooConnect";
/**
* You should not use the constructor directly, use connect() instead
*/
private OdooConnect(String server, Integer port, String db, String user, String pass, Integer id) throws MalformedURLException {
mServer = server;
mPort = port;
mDatabase = db;
mUserName = user;
mPassword = pass;
mUserId = id;
mUrl = new URL(String.format("http://%s:%s/xmlrpc/2/object", server, port));
}
/**
* @return An OdooConnect instance, which you will use to call the methods.
*/
public static OdooConnect connect(String server, Integer port, String db, String user, String pass) {
return login(server, port, db, user, pass);
}
public static OdooConnect connect(ContentValues connectionParams) {
return login(connectionParams);
}
/**
* @return true if the connection could be established, else returns false. The connection will not be stored.
*/
public static Boolean testConnection(String server, Integer port, String db, String user, String pass) {
return login(server, port, db, user, pass) != null;
}
public static Boolean testConnection(ContentValues connectionParams) {
return login(connectionParams) != null;
}
private static OdooConnect login(ContentValues connectionParams) {
return login(
connectionParams.getAsString("server"),
connectionParams.getAsInteger("port"),
connectionParams.getAsString("database"),
connectionParams.getAsString("username"),
connectionParams.getAsString("password")
);
}
private static OdooConnect login(String server, Integer port, String db, String user, String pass) {
OdooConnect connection = null;
try {
URL loginUrl = new URL(String.format("http://%s:%s/xmlrpc/2/common", server, port));
XMLRPCClient client = new XMLRPCClient(loginUrl);
Object[] list = {db, user, pass, emptyMap()};
int id = (int) client.call("authenticate", list);
connection = new OdooConnect(server, port, db, user, pass, id);
} catch (XMLRPCException e) {
Log.d(CONNECTOR_NAME, e.toString());
} catch (MalformedURLException e) {
Log.d(CONNECTOR_NAME, e.toString());
} catch (ClassCastException e) {
Log.d(CONNECTOR_NAME, e.toString()); // Bad login or password
}
return connection;
}
/**
* @param conditions You can pass new Object[0] to specify an empty list of conditions and no fields,
* which will return all the ids for that model.
* @param fields All the fields that you want to show
* @return The fields of matching objects and if have relations with other model returns the name of the relation.
*/
public List<HashMap<String, Object>> search_read(String model, Object[] conditions, String... fields) {
if (fields.length == 0) {
Object[] field = {"id"};
return search_read(model, 0, 0, conditions, field);
}
ArrayList<String> field = new ArrayList<String>();
for (String par : fields) {
field.add(par);
}
Object[] fieldsL = field.toArray();
return search_read(model, 0, 0, conditions, fieldsL);
}
/**
* @param offset Record where start to show
* @param limit Number of records to show: 0 -> all
*/
public List<HashMap<String, Object>> search_read(String model, Integer offset, Integer limit,
Object[] conditions, String... fields) {
if (fields.length == 0) {
Object[] field = {"id"};
return search_read(model, offset, limit, conditions, field);
}
ArrayList<String> field = new ArrayList<String>();
for (String par : fields) {
field.add(par);
}
Object[] fieldsL = field.toArray();
return search_read(model, offset, limit, conditions, fieldsL);
}
@SuppressWarnings("unchecked")
private List<HashMap<String, Object>> search_read(String model, final Integer offset,
final Integer limit, Object[] conditions,
final Object[] field) {
List<HashMap<String, Object>> result = null;
try {
XMLRPCClient client = new XMLRPCClient(mUrl);
Object[] parameters = {mDatabase, mUserId, mPassword,
model, "search_read", conditions, new HashMap() {{
put("fields", field);
put("limit", limit);
put("offset", offset);
}}};
Object[] record = (Object[]) client.call("execute_kw", parameters);
result = new ArrayList<>(record.length);
for (Object oField : record) {
HashMap<String, Object> listFields = (HashMap<String, Object>) oField;
Set<String> keys = listFields.keySet();
Object[] param = {mDatabase, mUserId, mPassword,
model, "fields_get", new Object[]{keys}, new HashMap() {{
put("attributes", new Object[]{"relation", "type"});
}}};
Map<String, Map<String, Object>> attrRelation =
(Map<String, Map<String, Object>>) client.call("execute_kw", param);
for (String key : keys) {
if (attrRelation.get(key).containsValue("many2one")) {
List fRelation = asList((Object[]) listFields.get(key));
Object f = (Object) fRelation.get(1); // 1 => name
listFields.put(key, f);
} else if (attrRelation.get(key).containsValue("many2many") ||
attrRelation.get(key).containsValue("one2many")) {
List fRelation = asList((Object[]) listFields.get(key));
String modelR = attrRelation.get(key).get("relation").toString();
final Object[] fieldR = {"name"};
Object[] parame = {mDatabase, mUserId, mPassword,
modelR, "read", new Object[]{fRelation}, new HashMap() {{
put("fields", fieldR);
}}};
Object[] recordd = (Object[]) client.call("execute_kw", parame);
/*
* You can change the string format of this result like you prefer.
*/
String extra = "";
for (Object r : recordd){
extra += r;
}
Object fResult = (Object) extra;
listFields.put(key, fResult);
}
}
result.add((HashMap<String, Object>) oField);
}
} catch (XMLRPCException e) {
Log.d(CONNECTOR_NAME, e.toString());
}
return result;
}
/**
* You can pass new Object[0] to specify an empty list of conditions,
* which will return all the ids for that model.
*
* @return The ids of matching objects.
*/
public Integer search_count(String model, Object[] conditions) {
Integer result = null;
try {
XMLRPCClient client = new XMLRPCClient(mUrl);
Object[] parameters = {mDatabase, mUserId, mPassword, model, "search_count", conditions};
result = (Integer) client.call("execute_kw", parameters);
} catch (XMLRPCException e) {
Log.d(CONNECTOR_NAME, e.toString());
}
return result;
}
/**
* Creates a new record for the given model width the values supplied.
*
* @param values
* Remember: In order to add different types in a Collection use Object, e.g. <br>
* <code>
* HashMap values = new HashMap(); <br>
* values.put("name", "hello"); <br>
* values.put("number", 10); <br>
* </code>
*/
public Integer create(String model, HashMap values) {
Integer newObjectId = null;
try {
XMLRPCClient client = new XMLRPCClient(mUrl);
Object[] parameters = {mDatabase, mUserId, mPassword, model, "create", new Object[]{values}};
newObjectId = (Integer) client.call("execute_kw", parameters);
} catch (XMLRPCException e) {
Log.d(CONNECTOR_NAME, e.toString());
}
return newObjectId;
}
/**
* Used to modify an existing object.
*
* @param values fields to change
*/
public Boolean write(String model, Object[] id, HashMap values) {
Boolean writeOk = false;
try {
XMLRPCClient client = new XMLRPCClient(mUrl);
Object[] parameters = {mDatabase, mUserId, mPassword, model, "write", new Object[]{
id, values}};
writeOk = (Boolean) client.call("execute_kw", parameters);
} catch (XMLRPCException e) {
Log.d(CONNECTOR_NAME, e.toString());
}
return writeOk;
}
/**
* A method to delete the matching records width the ids given
*/
public Boolean unlink(String model, Object[] ids) {
Boolean unlinkOk = false;
try {
XMLRPCClient client = new XMLRPCClient(mUrl);
Object[] parameters = {mDatabase, mUserId, mPassword, model, "unlink",
new Object[]{ids}};
unlinkOk = (Boolean) client.call("execute_kw", parameters);
} catch (XMLRPCException e) {
Log.d(CONNECTOR_NAME, e.toString());
}
return unlinkOk;
}
/**
* This is a generic method to call any WS.
*/
@SuppressWarnings("unchecked")
public Object[] call(String model, String method, final Integer offset,
final Integer limit, Object[] conditions,
final Object[] field) {
Object[] response = null;
try {
XMLRPCClient client = new XMLRPCClient(mUrl);
Object[] parameters = {mDatabase, mUserId, mPassword,
model, method, conditions, new HashMap() {{
put("fields", field);
put("limit", limit);
put("offset", offset);
}}};
response = (Object[]) client.call("execute_kw", parameters);
} catch (XMLRPCException e) {
Log.d(CONNECTOR_NAME, e.toString());
}
return response;
}
/**
* @return String representation of the OdooConnection instance, good for
* debugging purposes. You can comment the password if you want.
*/
public String toString() {
StringBuilder stringConn = new StringBuilder();
stringConn.append("server: " + mServer + "\n");
stringConn.append("port: " + mPort + "\n");
stringConn.append("database: " + mDatabase + "\n");
stringConn.append("user: " + mUserName + "\n");
stringConn.append("password: " + mPassword + "\n");
stringConn.append("id: " + mUserId + "\n");
return stringConn.toString();
}
}