Skip to content

Commit f445631

Browse files
authored
Merge pull request #29 from fpoyer/resultset
OdooCommand#callObjectFunction returns new class Response
2 parents 92c15b8 + 0b51ecb commit f445631

File tree

12 files changed

+372
-207
lines changed

12 files changed

+372
-207
lines changed

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import java.util.ArrayList;
2323
import java.util.HashMap;
24+
import java.util.Map;
2425

2526
/***
2627
* Provides OpenERP field properties like data types, selection fields etc.
@@ -39,9 +40,9 @@ public enum FieldType {
3940
}
4041

4142
private final String name;
42-
private final HashMap<String, Object> openERPFieldData;
43+
private final Map<String, Object> openERPFieldData;
4344

44-
public Field(String fieldName, HashMap<String, Object> openERPFieldData){
45+
public Field(String fieldName, Map<String, Object> openERPFieldData) {
4546
this.openERPFieldData = openERPFieldData;
4647
this.name = fieldName;
4748
}

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,14 @@ public enum FilterOperator{
5959
* @param fieldName Name of the model that should be filtered on
6060
* @param comparison For example =, !=, >, >=, <, <=, like, ilike, in, not in, child_of, parent_left, parent_right
6161
* @param value value that will be compared to 'fieldName' using the 'comparison'
62-
* @throws OpeneERPApiException
62+
* @throws OdooApiException
6363
*/
64-
public void add(int index, String fieldName, String comparison, Object value) throws OpeneERPApiException{
64+
public void add(int index, String fieldName, String comparison, Object value) throws OdooApiException{
6565
if (fieldName == null)
66-
throw new OpeneERPApiException("First filter parameter is mandatory. Please read the OpenERP help.");
66+
throw new OdooApiException("First filter parameter is mandatory. Please read the OpenERP help.");
6767

6868
if (comparison == null)
69-
throw new OpeneERPApiException("Second filter parameter is mandatory. Please read the OpenERP help.");
69+
throw new OdooApiException("Second filter parameter is mandatory. Please read the OpenERP help.");
7070

7171
Object [] filter = new Object[] {fieldName,comparison,value};
7272
filters.add(index, filter);
@@ -77,9 +77,9 @@ public void add(int index, String fieldName, String comparison, Object value) th
7777
* @param fieldName Name of the model that should be filtered on
7878
* @param comparison For example =, !=, >, >=, <, <=, like, ilike, in, not in, child_of, parent_left, parent_right
7979
* @param value value that will be compared to 'fieldName' using the 'comparison'
80-
* @throws OpeneERPApiException
80+
* @throws OdooApiException
8181
*/
82-
public void add(String fieldName, String comparison, Object value) throws OpeneERPApiException{
82+
public void add(String fieldName, String comparison, Object value) throws OdooApiException{
8383
add(filters.size(), fieldName, comparison, value);
8484
}
8585

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

Lines changed: 121 additions & 105 deletions
Large diffs are not rendered by default.

src/main/java/com/debortoliwines/odoo/api/OpeneERPApiException.java renamed to src/main/java/com/debortoliwines/odoo/api/OdooApiException.java

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* Copyright 2011, 2014 De Bortoli Wines Pty Limited (Australia)
33
*
4-
* This file is part of OpenERPJavaAPI.
4+
* This file is part of OdooJavaAPI.
55
*
66
* Licensed under the Apache License, Version 2.0 (the "License");
77
* you may not use this file except in compliance with the License.
@@ -20,27 +20,25 @@
2020
package com.debortoliwines.odoo.api;
2121

2222
/**
23-
* Exception class for OpenERP API errors
23+
* Exception class for Odoo API errors
24+
*
2425
* @author Pieter van der Merwe
2526
*
2627
*/
27-
public class OpeneERPApiException extends Exception {
28+
public class OdooApiException extends Exception {
2829

2930
private static final long serialVersionUID = 3148147969903379455L;
3031

31-
public OpeneERPApiException(String message) {
32+
public OdooApiException(String message) {
3233
super(message);
33-
// TODO Auto-generated constructor stub
3434
}
3535

36-
public OpeneERPApiException(Throwable cause) {
36+
public OdooApiException(Throwable cause) {
3737
super(cause);
38-
// TODO Auto-generated constructor stub
3938
}
4039

41-
public OpeneERPApiException(String message, Throwable cause) {
40+
public OdooApiException(String message, Throwable cause) {
4241
super(message, cause);
43-
// TODO Auto-generated constructor stub
4442
}
4543

4644
}

src/main/java/com/debortoliwines/odoo/api/OpenERPCommand.java renamed to src/main/java/com/debortoliwines/odoo/api/OdooCommand.java

Lines changed: 72 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* Copyright 2011, 2013-2014 De Bortoli Wines Pty Limited (Australia)
33
*
4-
* This file is part of OpenERPJavaAPI.
4+
* This file is part of OdooJavaAPI.
55
*
66
* Licensed under the Apache License, Version 2.0 (the "License");
77
* you may not use this file except in compliance with the License.
@@ -19,25 +19,28 @@
1919

2020
package com.debortoliwines.odoo.api;
2121

22-
import java.util.HashMap;
2322
import java.util.Map;
2423

2524
import org.apache.xmlrpc.XmlRpcException;
2625

2726
/**
28-
* Wrapper class for OpenERP commands. It uses the session object to make the call, but builds the parameters in this class
27+
* Wrapper class for Odoo commands. It uses the session object to make the call,
28+
* but builds the parameters in this class
29+
*
2930
* @author Pieter van der Merwe
3031
*
3132
*/
32-
public class OpenERPCommand {
33+
public class OdooCommand {
3334

3435
private final Session session;
3536

3637
/**
3738
* Main constructor
38-
* @param session Session object that will be used to make the calls to OpenERP.
39+
*
40+
* @param session
41+
* Session object that will be used to make the calls to Odoo.
3942
*/
40-
public OpenERPCommand(Session session){
43+
public OdooCommand(Session session){
4144
this.session = session;
4245
}
4346

@@ -64,28 +67,40 @@ public Object[] searchObject(String objectName, Object [] filter) throws XmlRpcE
6467
* @throws XmlRpcException
6568
*/
6669
public Object searchObject(String objectName, Object [] filter, int offset, int limit, String order, boolean count) throws XmlRpcException {
67-
Object[] params = new Object[] {filter, (offset < 0 ? false : offset), (limit < 0 ? false : limit), (order == null || order.length() == 0 ? false : order), session.getContext(), count};
70+
Object offsetParam = offset < 0 ? false : offset;
71+
Object limitParam = limit < 0 ? false : limit;
72+
Object orderParam = order == null || order.length() == 0 ? false : order;
73+
Object[] params = new Object[] {filter, offsetParam, limitParam, orderParam, session.getContext(), count};
6874
return session.executeCommand(objectName, "search", params);
6975
}
7076

7177
/**
72-
* Fetches field information for an object n OpenERP
73-
* @param objectName Object or model name to fetch field information for
74-
* @param filterFields Only return data for files in the filter list
78+
* Fetches field information for an object n Odoo
79+
*
80+
* @param objectName
81+
* Object or model name to fetch field information for
82+
* @param filterFields
83+
* Only return data for files in the filter list
7584
* @return A HashMap of field-value pairs.
7685
* @throws XmlRpcException
7786
*/
7887
@SuppressWarnings("unchecked")
79-
public HashMap<String, Object> getFields(String objectName, String[] filterFields) throws XmlRpcException {
80-
return (HashMap<String, Object>) session.executeCommand(objectName, "fields_get", new Object[]{filterFields, session.getContext() });
88+
public Map<String, Object> getFields(String objectName, String[] filterFields) throws XmlRpcException {
89+
return (Map<String, Object>) session.executeCommand(objectName, "fields_get",
90+
new Object[] { filterFields, session.getContext() });
8191
}
8292

8393
/**
84-
* Reads object data from the OpenERP server
85-
* @param objectName Name of the object to return data for
86-
* @param ids List of id to fetch data for. Call searchObject to get a potential list
87-
* @param fields List of fields to return data for
88-
* @returnA collection of rows for an OpenERP object
94+
* Reads object data from the Odoo server
95+
*
96+
* @param objectName
97+
* Name of the object to return data for
98+
* @param ids
99+
* List of id to fetch data for. Call searchObject to get a
100+
* potential list
101+
* @param fields
102+
* List of fields to return data for
103+
* @returnA collection of rows for an Odoo object
89104
* @throws XmlRpcException
90105
*/
91106
public Object[] readObject(String objectName, Object [] ids, String [] fields) throws XmlRpcException {
@@ -101,8 +116,7 @@ public Object[] readObject(String objectName, Object [] ids, String [] fields) t
101116
* @throws XmlRpcException
102117
*/
103118
public boolean writeObject(String objectName, int id, Map<String, Object> valueList) throws XmlRpcException {
104-
boolean result = (Boolean) session.executeCommand(objectName, "write", new Object[] { id, valueList });
105-
return result;
119+
return (Boolean) session.executeCommand(objectName, "write", new Object[] { id, valueList });
106120
}
107121

108122
/**
@@ -111,7 +125,6 @@ public boolean writeObject(String objectName, int id, Map<String, Object> valueL
111125
* @param fieldList List of fields to update. The import function has some specific naming conventions. Consider using the ObjectAdapter
112126
* @param rows Rows to import. Fields must be in the same order as the 'fieldList' parameter
113127
* @return The result returned from the server. Either returns the number of successfully imported rows or returns the error.
114-
* @throws OpeneERPApiException
115128
* @throws XmlRpcException
116129
*/
117130

@@ -120,26 +133,33 @@ public boolean writeObject(String objectName, int id, Map<String, Object> valueL
120133
}
121134

122135
@SuppressWarnings("unchecked")
123-
public HashMap<String, Object> Load(String objectName, String[] fieldList, Object [][] rows) throws XmlRpcException {
136+
public Map<String, Object> load(String objectName, String[] fieldList, Object[][] rows) throws XmlRpcException {
124137
Object o = session.executeCommand(objectName, "load", new Object[] {fieldList, rows});
125-
return (HashMap<String, Object>) o;
138+
return (Map<String, Object>) o;
126139
}
127140

128141
/**
129-
* Returns the name_get result of an object in the OpenERP server.
130-
* @param objectName Object name to invoke the name_get on
131-
* @param ids Database IDs to invoke the name_get for
132-
* @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
142+
* Returns the name_get result of an object in the Odoo server.
143+
*
144+
* @param objectName
145+
* Object name to invoke the name_get on
146+
* @param ids
147+
* Database IDs to invoke the name_get for
148+
* @return An Object[] with an entry for each ID. Each entry is another
149+
* Object [] with index 0 being the ID and index 1 being the Name
133150
* @throws XmlRpcException
134151
*/
135152
public Object[] nameGet(String objectName, Object[] ids) throws XmlRpcException{
136153
return (Object[]) session.executeCommand(objectName, "name_get", new Object[] {ids});
137154
}
138155

139156
/**
140-
* Deletes objects from the OpenERP Server
141-
* @param objectName Object name to delete rows from
142-
* @param ids List of ids to delete data from
157+
* Deletes objects from the Odoo Server
158+
*
159+
* @param objectName
160+
* Object name to delete rows from
161+
* @param ids
162+
* List of ids to delete data from
143163
* @return If the command was successful
144164
* @throws XmlRpcException
145165
*/
@@ -149,27 +169,38 @@ public boolean unlinkObject(String objectName, Object [] ids) throws XmlRpcExcep
149169

150170
/**
151171
* Creates a single object
152-
* @param objectName Name of the object to create
153-
* @param values HashMap of values to assign to the new object
172+
*
173+
* @param objectName
174+
* Name of the object to create
175+
* @param values
176+
* Map of values to assign to the new object
154177
* @return The database ID of the new object
155178
* @throws XmlRpcException
156179
*/
157-
public Object createObject(String objectName, HashMap<String, Object> values) throws XmlRpcException{
180+
public Object createObject(String objectName, Map<String, Object> values) throws XmlRpcException {
158181
return session.executeCommand(objectName, "create", new Object[] {values, session.getContext()});
159182
}
160183

161184
/**
162-
* Calls any function on an object.
163-
* The function OpenERP must have the signature like (self, cr, uid, *param) and return a dictionary or object.
164-
* *param can be replaced by separate parameters if you are sure of the number of parameters expected
165-
* @param objectName Object name where the function exists
166-
* @param functionName function to call
167-
* @param parameters Additional parameters that will be passed to the object
185+
* Calls any function on an object. The function Odoo must have the
186+
* signature like (self, cr, uid, *param) and return a dictionary or object.
187+
* *param can be replaced by separate parameters if you are sure of the
188+
* number of parameters expected
189+
*
190+
* @param objectName
191+
* Object name where the function exists
192+
* @param functionName
193+
* function to call
194+
* @param parameters
195+
* Additional parameters that will be passed to the object
168196
* @return An Object array of values
169-
* @throws XmlRpcException
170197
*/
171-
public Object[] callObjectFunction(String objectName, String functionName, Object[] parameters) throws XmlRpcException {
172-
return (Object[]) session.executeCommand(objectName, functionName, parameters);
198+
public Response callObjectFunction(String objectName, String functionName, Object[] parameters) {
199+
try {
200+
return new Response( session.executeCommand(objectName, functionName, parameters) );
201+
} catch (XmlRpcException e) {
202+
return new Response(e);
203+
}
173204
}
174205

175206
/**
@@ -184,33 +215,5 @@ public Object[] callObjectFunction(String objectName, String functionName, Objec
184215
public void executeWorkflow(final String objectName, final String signal, final int objectID) throws XmlRpcException {
185216
session.executeWorkflow(objectName, signal, objectID);
186217
}
187-
188-
/***
189-
* Left here for later use if required
190-
191-
public void nameSearch() throws XmlRpcException{
192-
Object result = null;
193-
194-
OpenERPClient objectClient = new OpenERPClient(host, port, RPCServices.RPC_OBJECT);
195-
HashMap<Object, String> values = new HashMap<Object, String> ();
196-
values.put("user_email", "[email protected]");
197-
198-
HashMap<String, Object> rows = new HashMap<String, Object> ();
199-
rows.put("28", values);
200-
201-
Object[] params = new Object[] {databaseName, userID, password, "res.users", "name_search", "Pieter van der Merwe"};
202-
result = objectClient.execute("execute", params);
203-
204-
//return result;
205-
}
206-
207-
public Object getDefaults() throws XmlRpcException{
208-
Object result = null;
209-
OpenERPClient objectClient = new OpenERPClient(host, port, RPCServices.RPC_OBJECT);
210-
Object[] params = new Object[] {databaseName, userID, password, "res.users", "default_get", new Object[]{"active"}};
211-
result = objectClient.execute("execute", params);
212-
return result; }
213-
214-
***/
215218

216219
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.debortoliwines.odoo.api;
2+
3+
public class Response {
4+
5+
private final boolean isSuccessful;
6+
private final Exception errorCause;
7+
private final Object responseObject;
8+
private final Object[] responseObjectAsArray;
9+
10+
public Response(final Exception errorCause) {
11+
this.isSuccessful = false;
12+
this.errorCause = errorCause;
13+
this.responseObject = null;
14+
this.responseObjectAsArray = new Object[0];
15+
}
16+
17+
public Response(final Object responseObject) {
18+
this.isSuccessful = true;
19+
this.errorCause = null;
20+
this.responseObject = responseObject;
21+
if (responseObject instanceof Object[]) {
22+
this.responseObjectAsArray = (Object[]) responseObject;
23+
} else {
24+
this.responseObjectAsArray = new Object[] { responseObject };
25+
}
26+
}
27+
28+
public boolean isSuccessful() {
29+
return isSuccessful;
30+
}
31+
32+
public Throwable getErrorCause() {
33+
return errorCause;
34+
}
35+
36+
public Object getResponseObject() {
37+
return responseObject;
38+
}
39+
40+
public Object[] getResponseObjectAsArray() {
41+
return responseObjectAsArray;
42+
}
43+
}

0 commit comments

Comments
 (0)