Java interface to OpenERP

This exercise was merely a simple test to see how to interface a simple Java script and OpenERP as a basis for an Android App. 

The approach I took is a follows to setup the development environment:
  • Download the Android SDK.
  • Install the ADT plugin for Eclipse (if you’ll use the Eclipse IDE).
  • Download the latest SDK tools and platforms using the SDK Manager.
  • To get a feel for the Android Development Tools, follow the training instructions -> here
  • Download and Install the apache-xlmrpc-3.1.3.jar   library  (or latest version but ensure that the syntax hasn't changed)
Here is the simple java code used for searching a BoM database. The filename needs to be called Search.java to trigger the search functionality. 
  • Just select menu /new/file 
  • Copy and paste the code. 

In the code change the lines in red to the name of your demo database. The username and password are assumed to be "admin". The IP address will need to be changed accordingly. 

Search.java

package com.xmlrpc.client;

import java.net.URL;
import java.util.Vector;

import org.apache.xmlrpc.client.XmlRpcClient;
import org.apache.xmlrpc.client.XmlRpcClientConfigImpl;

public class Search {

public static void main(String args[]) throws Exception {

Vector params1 = new Vector();
params1.addElement("your-database-name");
params1.addElement("admin");
params1.addElement("admin");

XmlRpcClient xmlrpcLogin = new XmlRpcClient();
XmlRpcClientConfigImpl xmlrpcConfigLogin = new XmlRpcClientConfigImpl();
xmlrpcConfigLogin.setEnabledForExtensions(true);
xmlrpcConfigLogin.setServerURL(new URL("http", "192.168.178.29", 8069, "/xmlrpc/common"));
xmlrpcLogin.setConfig(xmlrpcConfigLogin);

Object id = xmlrpcLogin.execute("login", params1);
System.out.println("Login Id : " + id.toString());

System.out.println("------------------------");

XmlRpcClient client = new XmlRpcClient();
XmlRpcClientConfigImpl clientConfig = new XmlRpcClientConfigImpl();
clientConfig.setEnabledForExtensions(true);
clientConfig.setServerURL(new URL("http", "192.168.178.29", 8069, "/xmlrpc/object"));
client.setConfig(clientConfig);


Object[] params2 = { "create_uid", "=", 1 };
Vector<Object> params = new Vector<Object>();
params.add(params2);
Vector<Object> arg = new Vector<Object>();
arg.add("your-database-name");
arg.add(1);
arg.add("admin"); //the password of the database
arg.add("product.product");
arg.add("search");
arg.add(params);

Object[] ids = (Object[]) client.execute("execute", arg);
System.out.println("partner addressees with create_uid 1 :");
for (Object obj : ids) {
int a = Integer.parseInt(obj.toString());
System.out.println(a);
}

Object[] params3 = { "bom_lines", "!=", "" };
Vector<Object> params4 = new Vector<Object>();
params.add(params3);
Vector<Object> arg2 = new Vector<Object>();
arg2.add("your-database-name");
arg2.add(1); //assumes the first user id is admin
arg2.add("admin"); // The Database password 
arg2.add("mrp.bom");
arg2.add("search");
arg2.add(params4);

Object[] ids2 = (Object[]) client.execute("execute", arg2);
System.out.println("BomID's :");
for (Object obj2 : ids2) {
int b = Integer.parseInt(obj2.toString());
System.out.print(b);
System.out.print(",");
}
System.out.println(""); //new line
}
}


Eclipse will save the file in the directory C:\Users\<your name>\workspace1\My1stJavaProject\src\com\xmlrpc\client

Run the program directly from the IDE (by clicking on the right green arrow), but make sure you can see the messages in the debug console at the bottom. 

Android Development Tools



Another example is to list all the databases in an instance:

FindDB.java

package com.xmlrpc.client;

import java.net.URL;
import java.util.Vector;

import org.apache.xmlrpc.XmlRpcException;
import org.apache.xmlrpc.client.XmlRpcClient;
import org.apache.xmlrpc.client.XmlRpcClientConfigImpl;

public class FindDB {

public static void main(String args[]) throws Exception {

XmlRpcClient xmlrpcDb = new XmlRpcClient();
XmlRpcClientConfigImpl xmlrpcConfigDb = new XmlRpcClientConfigImpl();
xmlrpcConfigDb.setEnabledForExtensions(true);
xmlrpcConfigDb.setServerURL((new URL("http", "192.168.178.29", 8069, "/xmlrpc/db")));
xmlrpcDb.setConfig(xmlrpcConfigDb);
try {
// Retrieve databases
Vector<Object> params = new Vector<Object>();
Object result = xmlrpcDb.execute("list", params);
Object[] a = (Object[]) result;
Vector<String> res = new Vector<String>();
for (int i = 0; i < a.length; i++) {
if (a[i] instanceof String) {
res.addElement((String) a[i]);
System.out.println(a[i]);
}
}
} catch (XmlRpcException e) {
e.printStackTrace();
}
}
}




Comments