OpenERP XML-RPC Java

Introduction

OpenERP’s communication normally happens in two modes

  • Net-RPC
  • XML-RPC

where, net-rpc is considered to be fast for python based applications.
I will discuss here about the xml-rpc calls from Java.
If you want to see all the xml-rpc calls made from GTK client,
start the client with,

./openerp-client.py -l debug_rpc

Java and XML-RPC:

I chose to use Java examples here with apache-xmlrpc-3.1.3 package.

The actual xml-rpc call made for reading Purchase Order is,

1) Setting up things:


XmlRpcClient xmlrpcLogin = new XmlRpcClient();
XmlRpcClientConfigImpl xmlrpcConfigLogin = new XmlRpcClientConfigImpl();
xmlrpcConfigLogin.setEnabledForExtensions(true);
xmlrpcConfigLogin.setEnabledForExceptions(true);
xmlrpcConfigLogin.setServerURL(new URL(“http”,host,port,”/xmlrpc/common”));
xmlrpcLogin.setConfig(xmlrpcConfigLogin);



host and port are self explanatory. Normally, XML-RPC port is 8069.

2) Login to the system:


Vector params = new Vector();

params.addElement(tinydb);//database
params.addElement(login);//user id
params.addElement(password);//pwd
Object id = xmlrpcLogin.execute(“login”, params);


3) Find all the Purchase Order which are in ‘draft’ state:

The actual call made from GTK client looks like this,

rpc.request:(‘execute’, ‘v14’, 1, ‘admin’, (‘purchase.order’, ‘search’, [(‘state’, ‘=’, ‘draft’)], 0.0, 80.0, 0)),

In java, i could write it as,


XmlRpcClientConfigImpl rpc = new XmlRpcClientConfigImpl();
rpc.setEnabledForExtensions(true);
rpc.setEnabledForExceptions(true);
rpc.setServerURL(new URL(“http”,host,port,”/xmlrpc/object”));
xmlrpcLogin.setConfig(rpc);

Object r[]=new Object[6];
r[0]=tinydb;
r[1]=(Integer)id;
r[2]=password;
r[3]=”purchase.order”;
r[4]=”search”;

Object names[]=new Object[3];


Get the PO in state draft,


names[0]=”state”;
names[1]=”=”;
names[2] = “draft”;

Vector param2 = new Vector();
param2.addElement(names);
r[5]=param2;
Object po_ids;
po_ids=xmlrpcLogin.execute(“execute”, r);
Object[] d = (Object[]) po_ids;

Integer i=0;

for(i=0; i<d.length; i++)
System.out.println((Integer)d[i]);


The last loop will print the purchase order ids.

4) Confirm the Purchase Order:

Now, let me just confirm the first purchase order,

The actual python call made would be,
rpc.request:('exec_workflow', 'energy', 1, 'admin', (u'purchase.order', 'purchase_confirm', 4))

and we could write in Java as,


Object r2[]=new Object[7];
r2[0]=tinydb;
r2[1]=(Integer)id;
r2[2]=password;
r2[3]="purchase.order";
r2[4] = "purchase_confirm";
r2[5] = d[0];
Object po_data;
po_data=xmlrpcLogin.execute("exec_workflow", r2);


The exec_workflow, as the name implies, triggers workflow.

Some better java programmer can write simple code for the same purpose 🙂

This post is made from Gedit Editor

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: