Advertisement
Guest User

Untitled

a guest
Mar 21st, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.54 KB | None | 0 0
  1. import org.apache.xmlrpc.XmlRpcException;
  2. import org.apache.xmlrpc.client.*;
  3.  
  4. import java.net.MalformedURLException;
  5. import java.net.URL;
  6. import java.util.HashMap;
  7. import java.util.List;
  8. import java.util.Map;
  9.  
  10. import static java.util.Arrays.asList;
  11.  
  12. public class TestOdoo {
  13.  
  14. public static void main(String[] args) throws MalformedURLException, XmlRpcException {
  15. String url = "http://10.3.56.6:8069"; // work with odoo.com account!!
  16. String db = "ErrasmusHB";
  17. String username = "anthony.moortgat@student.ehb.be";
  18. String password = "";
  19. System.out.println("Get database list");
  20. System.out.println("Login");
  21. System.out.println("--------------");
  22. int uid = login(url,db,username,password);
  23. if (uid >0) {
  24. System.out.println("Login Ok");
  25. } else {
  26. System.out.println("Login Fail");
  27. }
  28.  
  29. final XmlRpcClient models = new XmlRpcClient() {{
  30. setConfig(new XmlRpcClientConfigImpl() {{
  31. setServerURL(new URL(String.format("%s/xmlrpc/2/object", url)));
  32. }});
  33. }};
  34.  
  35. final List ids = asList((Object[])models.execute(
  36. "execute_kw", asList(
  37. db, uid, password,
  38. "res.partner", "search",
  39. asList(asList(
  40. asList("customer", "=", true))),
  41. new HashMap() {{ put("limit", 1); }})));
  42. final Map record = (Map)((Object[])models.execute(
  43. "execute_kw", asList(
  44. db, uid, password,
  45. "res.partner", "read",
  46. asList(ids)
  47. )
  48. ))[0];
  49. // count the number of fields fetched by default
  50. System.out.println(record.size());
  51.  
  52. var list = asList((Object[])models.execute("execute_kw", asList(
  53. db, uid, password,
  54. "res.partner", "read",
  55. asList(ids),
  56. new HashMap() {{
  57. put("fields", asList("name", "country_name","phone","email"));
  58. }}
  59. )));
  60.  
  61. System.out.println(list);
  62.  
  63. // final Integer id = (Integer)models.execute("execute_kw", asList(
  64. // db, uid, password,
  65. // "res.partner", "create",
  66. // asList(new HashMap() {
  67. // { put("name", "Anas");
  68. // put("phone","129038109283");
  69. // put("street","Swag");
  70. // put("city","Brussel");
  71. // put("postcode","1755");
  72. // put("country","Belgium");
  73. // }})
  74. // )); // creating a customer with certain criterias
  75.  
  76. var customers = asList((Object[])models.execute("execute_kw", asList(
  77. db, uid, password,
  78. "res.partner", "search",
  79. asList(asList(
  80. asList("name", "=", "James"),
  81. asList("customer", "=", true)))
  82. )));
  83. System.out.println(customers);
  84.  
  85. // going through the whole list of customers and returning their id's
  86. // ( i gave Name as an parameter to get a certain user back.)
  87.  
  88.  
  89. models.execute("execute_kw", asList(
  90. db, uid, password,
  91. "res.partner", "unlink",
  92. asList(asList(13))));
  93. // check if the deleted record is still in the database
  94. asList((Object[])models.execute("execute_kw", asList(
  95. db, uid, password,
  96. "res.partner", "search",
  97. asList(asList(asList("id", "=", 11)))
  98. ))); // deleting an customer using its unique ID
  99. }
  100. // login
  101. static int login(String url, String db, String login, String password) throws XmlRpcException, MalformedURLException {
  102. XmlRpcClient client = new XmlRpcClient();
  103. XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();
  104. config.setEnabledForExtensions(true);
  105. //config.setServerURL(new URL(url+"/xmlrpc/common"));
  106. config.setServerURL(new URL(url+"/xmlrpc/2/common"));
  107. client.setConfig(config);
  108. //Connect
  109. //Object[] empty = null; // Ok
  110. //Object[] params = new Object[] {db,login,password, empty}; // Ok
  111. Object[] params = new Object[] {db,login,password}; // Ok & simple
  112. Object uid = client.execute("login", params);
  113. if (uid instanceof Integer)
  114. return (int) uid;
  115. return -1;
  116. }
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement