diff --git a/java-odoo-json-rpc-client/src/main/java/ch/helvethink/odoo4java/jsonrpc/JsonRPCRequestBuilder.java b/java-odoo-json-rpc-client/src/main/java/ch/helvethink/odoo4java/jsonrpc/JsonRPCRequestBuilder.java index cdc37ab..67c4e7a 100644 --- a/java-odoo-json-rpc-client/src/main/java/ch/helvethink/odoo4java/jsonrpc/JsonRPCRequestBuilder.java +++ b/java-odoo-json-rpc-client/src/main/java/ch/helvethink/odoo4java/jsonrpc/JsonRPCRequestBuilder.java @@ -68,7 +68,8 @@ public JsonRPCRequestBuilder withParamArgs(final JsonElement args) { * @return The builder instance */ public JsonRPCRequestBuilder withParamArgs(final Object... args) { - return withParamArgs(new Gson().toJsonTree(args)); + final Gson gson = new Gson().newBuilder().disableHtmlEscaping().create(); + return withParamArgs(gson.toJsonTree(args)); } /** @@ -80,7 +81,7 @@ public RequestBody buildRequest() { requestBody.add("params", params); requestBody.addProperty("id", ThreadBasedIdGenerator.generateId()); - final String jsonRequest = new Gson().toJson(requestBody); + final String jsonRequest = new Gson().newBuilder().disableHtmlEscaping().create().toJson(requestBody); LOG.debug("The following request will be sent: {}", jsonRequest); if(isDebugging) { sentRequests.push(jsonRequest); diff --git a/java-odoo-json-rpc-client/src/main/java/ch/helvethink/odoo4java/jsonrpc/OdooClient.java b/java-odoo-json-rpc-client/src/main/java/ch/helvethink/odoo4java/jsonrpc/OdooClient.java index 7a62c57..1b0af44 100644 --- a/java-odoo-json-rpc-client/src/main/java/ch/helvethink/odoo4java/jsonrpc/OdooClient.java +++ b/java-odoo-json-rpc-client/src/main/java/ch/helvethink/odoo4java/jsonrpc/OdooClient.java @@ -41,7 +41,6 @@ import okhttp3.RequestBody; import okhttp3.Response; import org.codehaus.plexus.util.StringUtils; -import org.jetbrains.annotations.NotNull; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -160,13 +159,12 @@ private JsonObject getResult(final Request request) throws IOException { } @Override - public List findByCriteria(final int limit, final int page, final Class classToConvert, final String... criteria) { + public List findByCriteria(final int limit, final int page, final Class classToConvert, final Object... criteria) { return findByCriteria(limit, page, "", classToConvert, criteria); } @Override - public int countByCriteria(final Class objectType, final String... criteria) { - // Warn, some of the json apis do not accept the limit field (and it produces a silent error...) + public int countByCriteria(final Class objectType, final Object... criteria) { JsonObject requestArgs = new JsonObject(); final RequestBody requestBody = @@ -191,18 +189,16 @@ public int countByCriteria(final Class objectType, final Stri /** * {@inheritDoc} */ - public List findByCriteria(final int limit, final Class classToConvert, final String... criteria) { + public List findByCriteria(final int limit, final Class classToConvert, final Object... criteria) { return findByCriteria(limit, 0, "", classToConvert, criteria); } - /** - * {@inheritDoc} - */ @Override - public List findByCriteria(final int limit, final int page, final String sortByField, final Class classToConvert, final String... criteria) { - return genericCall(limit, page, sortByField, classToConvert, ODOO_SEARCH_READ_API, criteria); + public List findByCriteria(final int limit, final int page, String sort, final Class classToConvert, final Object... criteria) { + return genericCall(limit, page, sort, classToConvert, ODOO_SEARCH_READ_API, criteria); } + /** * {@inheritDoc} */ diff --git a/java-odoo-json-rpc-client/src/test/java/ch/helvethink/odoo4java/jsonrpc/OdooClientTest.java b/java-odoo-json-rpc-client/src/test/java/ch/helvethink/odoo4java/jsonrpc/OdooClientTest.java index 9a5101d..13324c7 100644 --- a/java-odoo-json-rpc-client/src/test/java/ch/helvethink/odoo4java/jsonrpc/OdooClientTest.java +++ b/java-odoo-json-rpc-client/src/test/java/ch/helvethink/odoo4java/jsonrpc/OdooClientTest.java @@ -238,4 +238,18 @@ void testDeleteOdooObject() throws IOException { final String result = sentRequests.pop(); assertEquals(expected.substring(0, expected.lastIndexOf("id")), result.substring(0, result.lastIndexOf("id"))); } + + @Test + void testCountObjects() throws IOException { + String mockResponseJson = "{\"result\": 10}"; + + when(mockResponse.body().string()).thenReturn(mockResponseJson); + + int count = odooClient.countByCriteria(Project.class, "id", ">", "0"); + + assertEquals(10, count); + final String expected = Files.readString(Path.of("src/test/resources/countProjectTest.json")); + final String result = sentRequests.pop(); + assertEquals(expected.substring(0, expected.lastIndexOf("id")), result.substring(0, result.lastIndexOf("id"))); + } } diff --git a/java-odoo-json-rpc-client/src/test/resources/countProjectTest.json b/java-odoo-json-rpc-client/src/test/resources/countProjectTest.json new file mode 100644 index 0000000..4d8392e --- /dev/null +++ b/java-odoo-json-rpc-client/src/test/resources/countProjectTest.json @@ -0,0 +1 @@ +{"jsonrpc":"2.0","params":{"method":"execute_kw","service":"object","args":["test_db",1,"password","project.project","search_count",[[["id",">","0"]]],{}]},"id":3218175263535857666} \ No newline at end of file diff --git a/java-odoo-rpc-core/src/main/java/ch/helvethink/odoo4java/rpc/OdooRpcClient.java b/java-odoo-rpc-core/src/main/java/ch/helvethink/odoo4java/rpc/OdooRpcClient.java index 93c27db..02cf6e4 100644 --- a/java-odoo-rpc-core/src/main/java/ch/helvethink/odoo4java/rpc/OdooRpcClient.java +++ b/java-odoo-rpc-core/src/main/java/ch/helvethink/odoo4java/rpc/OdooRpcClient.java @@ -46,10 +46,15 @@ public interface OdooRpcClient { * @param The target type * @return List of corresponding objects */ - List findByCriteria(final int limit, final int page, final Class classToConvert, final String... criteria); + List findByCriteria(final int limit, final int page, final Class classToConvert, final Object... criteria); - - int countByCriteria(Class objectType, String... criteria); + /** + * Count objects that respects the criteria (useful for pagination for instance) + * @param objectType Odoo objects we want to count + * @param criteria Criteria of the request + * @return number of objects + */ + int countByCriteria(Class objectType, Object... criteria); /** * Find an Odoo object using criteria. @@ -61,9 +66,18 @@ public interface OdooRpcClient { * @param The target type * @return List of corresponding objects */ - List findByCriteria(final int limit, final Class classToConvert, final String... criteria); + List findByCriteria(final int limit, final Class classToConvert, final Object... criteria); - List findByCriteria(int limit, int page, String sortByField, Class classToConvert, String... criteria); + /** + * + * @param limit Number of objects we want to retrieve + * @param page Page we want to retrieve + * @param sort Field to sort results + * @param classToConvert Type of the target object + * @param criteria The search criteria + * @return List of corresponding objects + */ + List findByCriteria(int limit, int page, String sort, Class classToConvert, Object... criteria); /** * Fetch an object by its id diff --git a/java-odoo-xml-rpc-core/src/main/java/ch/helvethink/odoo4java/xmlrpc/OdooClient.java b/java-odoo-xml-rpc-core/src/main/java/ch/helvethink/odoo4java/xmlrpc/OdooClient.java index 2b9defd..3b83f12 100644 --- a/java-odoo-xml-rpc-core/src/main/java/ch/helvethink/odoo4java/xmlrpc/OdooClient.java +++ b/java-odoo-xml-rpc-core/src/main/java/ch/helvethink/odoo4java/xmlrpc/OdooClient.java @@ -250,7 +250,7 @@ XML_RPC_EXECUTE_METHOD_NAME, asList( * {@inheritDoc} */ @Override - public List findByCriteria(final int limit, final Class classToConvert, final String... criteria) { + public List findByCriteria(final int limit, final Class classToConvert, final Object... criteria) { return findByCriteria(limit, 0, classToConvert, criteria); } @@ -258,14 +258,13 @@ public List findByCriteria(final int limit, final Class List findByCriteria(final int limit, final int page, final Class classToConvert, final String... criteria) { + public List findByCriteria(final int limit, final int page, final Class classToConvert, final Object... criteria) { return findByCriteria(limit, page, "", classToConvert, criteria); } - // TODO fix criteria!!! @Override - public int countByCriteria(final Class objectType, final String... criteria) { - final List>> crits = (criteria != null && criteria.length > 0) ? CriteriaTools.groupCriteria(criteria) : + public int countByCriteria(final Class objectType, final Object... criteria) { + final List>> crits = (criteria != null && criteria.length > 0) ? CriteriaTools.groupCriteria(criteria) : List.of(List.of(asList("id", ">=", "0"))); LOG.debug("{}", crits); @@ -280,8 +279,8 @@ XML_RPC_EXECUTE_METHOD_NAME, asList(dbName, uid, password, * {@inheritDoc} */ @Override - public List findByCriteria(final int limit, final int page, final String sortByField, final Class classToConvert, final String... criteria) { - final List>> crits = (criteria != null && criteria.length > 0) ? CriteriaTools.groupCriteria(criteria) : + public List findByCriteria(final int limit, final int page, final String sortByField, final Class classToConvert, final Object... criteria) { + final List>> crits = (criteria != null && criteria.length > 0) ? CriteriaTools.groupCriteria(criteria) : List.of(List.of(asList("id", ">=", "0"))); LOG.debug("{}", crits); final Object[] resultFromXmlRpc = (Object[]) objectXmlRpcClient.execute(