package ca.etsmtl.gti710.openErp;


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

import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;

public class ClientAPI {

    private String host = null;
    private int port = 0;
    private String login = null;
    private String password = null;
    private String database = null;
    private int userId;

    public ClientAPI() {

    }

    public void connect() {

        Object[] params;
        XmlRpcClient xmlrpcLogin = new XmlRpcClient();

        XmlRpcClientConfigImpl xmlrpcConfigLogin = new XmlRpcClientConfigImpl();
        xmlrpcConfigLogin.setEnabledForExtensions(true);
        try {
            xmlrpcConfigLogin.setServerURL(new URL("http", host, port, "/xmlrpc/common"));
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }

        xmlrpcLogin.setConfig(xmlrpcConfigLogin);

        try {
            //Connect
            params = new Object[] {database, login, password};
            Object id = xmlrpcLogin.execute("login", params);
            if (id instanceof Integer) {
                userId = (Integer)id;
            }
        }
        catch (XmlRpcException e) {
            //logger.warn("XmlException Error while logging to OpenERP: ",e);
            System.out.println(e);
        }
        catch (Exception e)
        {
            //logger.warn("Error while logging to OpenERP: ",e);
            System.out.println(e);
        }
    }

    public HashMap<String, Object> readProduct(int product_id) {

        XmlRpcClient xmlrpcLogin = new XmlRpcClient();

        XmlRpcClientConfigImpl xmlrpcConfigLogin = new XmlRpcClientConfigImpl();
        xmlrpcConfigLogin.setEnabledForExtensions(true);
        try {
            xmlrpcConfigLogin.setServerURL(new URL("http", host, port, "/xmlrpc/object"));
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }

        xmlrpcLogin.setConfig(xmlrpcConfigLogin);

        try {
            Object read[]=new Object[7];
            read[0]=database;   //Nom de la base de donnée
            read[1]=userId;   // ID de l'utilisateur
            read[2]=password;  //mot de passe

            //TODO Put those elements in the method parameters
            read[3]="product.product";
            read[4]="read";
            read[5] = product_id;
            read[6] = null;

            @SuppressWarnings("unchecked")
			HashMap<String, Object> result = (HashMap<String, Object>)xmlrpcLogin.execute("execute", read);
            return result;
        }
        catch (XmlRpcException e) {
            //logger.warn("XmlException Error while logging to OpenERP: ",e);
            System.out.println(e);
        }
        catch (Exception e)
        {
            //logger.warn("Error while logging to OpenERP: ",e);
            System.out.println(e);
        }

        return null;
    }

    public void setHost(String host) {
        this.host = host;
    }

    public void setPort(int port) {
        this.port = port;
    }

    public void setLogin(String login) {
        this.login = login;
    }

    public void setPassword(String password) {
        this.password = password;
    }
    public void setDatabase(String database) {
        this.database = database;
    }
}

@@ -2483,18 +2483,17 @@
 d%5B6%5D
- = 
+=
 null;%0A%0A
+%0A
     
@@ -2967,32 +2967,32 @@
 (e);%0A        %7D%0A%0A
-
         return n
@@ -2995,32 +2995,1472 @@
 rn null;%0A    %7D%0A%0A
+    public void create() %7B%0A%0A        XmlRpcClient xmlrpcLogin = new XmlRpcClient();%0A%0A        XmlRpcClientConfigImpl xmlrpcConfigLogin = new XmlRpcClientConfigImpl();%0A        xmlrpcConfigLogin.setEnabledForExtensions(true);%0A        try %7B%0A            xmlrpcConfigLogin.setServerURL(new URL(%22http%22, host, port, %22/xmlrpc/object%22));%0A        %7D catch (MalformedURLException e) %7B%0A            e.printStackTrace();%0A        %7D%0A%0A        xmlrpcLogin.setConfig(xmlrpcConfigLogin);%0A%0A        try %7B%0A            Object read%5B%5D=new Object%5B7%5D;%0A            read%5B0%5D=database;   //Nom de la base de donn%C3%A9e%0A            read%5B1%5D=userId;   // ID de l'utilisateur%0A            read%5B2%5D=password;  //mot de passe%0A%0A            //TODO Put those elements in the method parameters%0A            read%5B3%5D=%22res.partner%22;%0A            read%5B4%5D=%22create%22;%0A            HashMap%3CString, Object%3E partner = new HashMap%3CString, Object%3E();%0A            partner.put(%22name%22, %22Test nom%22);%0A            partner.put(%22lang%22, %22fr_FR%22);%0A            read%5B5%5D = partner;%0A%0A%0A            Object clientID = xmlrpcLogin.execute(%22execute%22, read);%0A            System.out.println(clientID);%0A        %7D%0A        catch (XmlRpcException e) %7B%0A            //logger.warn(%22XmlException Error while logging to OpenERP: %22,e);%0A            System.out.println(e);%0A        %7D%0A        catch (Exception e)%0A        %7B%0A            //logger.warn(%22Error while logging to OpenERP: %22,e);%0A            System.out.println(e);%0A        %7D%0A%0A    %7D%0A%0A
     public void 

Create exemple