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.
1919
2020package com .debortoliwines .odoo .api ;
2121
22- import java .util .HashMap ;
2322import java .util .Map ;
2423
2524import 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}
0 commit comments