Skip to content

Commit

Permalink
fix criteria search, count, ...
Browse files Browse the repository at this point in the history
  • Loading branch information
pascallibenzi committed Jan 5, 2025
1 parent f8e0b31 commit 91c8778
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

/**
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -160,13 +159,12 @@ private JsonObject getResult(final Request request) throws IOException {
}

@Override
public <T extends OdooObj> List<T> findByCriteria(final int limit, final int page, final Class<T> classToConvert, final String... criteria) {
public <T extends OdooObj> List<T> findByCriteria(final int limit, final int page, final Class<T> classToConvert, final Object... criteria) {
return findByCriteria(limit, page, "", classToConvert, criteria);
}

@Override
public int countByCriteria(final Class<? extends OdooObj> 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<? extends OdooObj> objectType, final Object... criteria) {
JsonObject requestArgs = new JsonObject();

final RequestBody requestBody =
Expand All @@ -191,18 +189,16 @@ public int countByCriteria(final Class<? extends OdooObj> objectType, final Stri
/**
* {@inheritDoc}
*/
public <T extends OdooObj> List<T> findByCriteria(final int limit, final Class<T> classToConvert, final String... criteria) {
public <T extends OdooObj> List<T> findByCriteria(final int limit, final Class<T> classToConvert, final Object... criteria) {
return findByCriteria(limit, 0, "", classToConvert, criteria);
}

/**
* {@inheritDoc}
*/
@Override
public <T extends OdooObj> List<T> findByCriteria(final int limit, final int page, final String sortByField, final Class<T> classToConvert, final String... criteria) {
return genericCall(limit, page, sortByField, classToConvert, ODOO_SEARCH_READ_API, criteria);
public <T extends OdooObj> List<T> findByCriteria(final int limit, final int page, String sort, final Class<T> classToConvert, final Object... criteria) {
return genericCall(limit, page, sort, classToConvert, ODOO_SEARCH_READ_API, criteria);
}


/**
* {@inheritDoc}
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")));
}
}
Original file line number Diff line number Diff line change
@@ -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}
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,15 @@ public interface OdooRpcClient {
* @param <T> The target type
* @return List of corresponding objects
*/
<T extends OdooObj> List<T> findByCriteria(final int limit, final int page, final Class<T> classToConvert, final String... criteria);
<T extends OdooObj> List<T> findByCriteria(final int limit, final int page, final Class<T> classToConvert, final Object... criteria);


int countByCriteria(Class<? extends OdooObj> 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<? extends OdooObj> objectType, Object... criteria);

/**
* Find an Odoo object using criteria.
Expand All @@ -61,9 +66,18 @@ public interface OdooRpcClient {
* @param <T> The target type
* @return List of corresponding objects
*/
<T extends OdooObj> List<T> findByCriteria(final int limit, final Class<T> classToConvert, final String... criteria);
<T extends OdooObj> List<T> findByCriteria(final int limit, final Class<T> classToConvert, final Object... criteria);

<T extends OdooObj> List<T> findByCriteria(int limit, int page, String sortByField, Class<T> 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
*/
<T extends OdooObj> List<T> findByCriteria(int limit, int page, String sort, Class<T> classToConvert, Object... criteria);

/**
* Fetch an object by its id
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -250,22 +250,21 @@ XML_RPC_EXECUTE_METHOD_NAME, asList(
* {@inheritDoc}
*/
@Override
public <T extends OdooObj> List<T> findByCriteria(final int limit, final Class<T> classToConvert, final String... criteria) {
public <T extends OdooObj> List<T> findByCriteria(final int limit, final Class<T> classToConvert, final Object... criteria) {
return findByCriteria(limit, 0, classToConvert, criteria);
}

/**
* {@inheritDoc}
*/
@Override
public <T extends OdooObj> List<T> findByCriteria(final int limit, final int page, final Class<T> classToConvert, final String... criteria) {
public <T extends OdooObj> List<T> findByCriteria(final int limit, final int page, final Class<T> classToConvert, final Object... criteria) {
return findByCriteria(limit, page, "", classToConvert, criteria);
}

// TODO fix criteria!!!
@Override
public int countByCriteria(final Class<? extends OdooObj> objectType, final String... criteria) {
final List<List<List<String>>> crits = (criteria != null && criteria.length > 0) ? CriteriaTools.groupCriteria(criteria) :
public int countByCriteria(final Class<? extends OdooObj> objectType, final Object... criteria) {
final List<List<List<Object>>> crits = (criteria != null && criteria.length > 0) ? CriteriaTools.groupCriteria(criteria) :
List.of(List.of(asList("id", ">=", "0")));
LOG.debug("{}", crits);

Expand All @@ -280,8 +279,8 @@ XML_RPC_EXECUTE_METHOD_NAME, asList(dbName, uid, password,
* {@inheritDoc}
*/
@Override
public <T extends OdooObj> List<T> findByCriteria(final int limit, final int page, final String sortByField, final Class<T> classToConvert, final String... criteria) {
final List<List<List<String>>> crits = (criteria != null && criteria.length > 0) ? CriteriaTools.groupCriteria(criteria) :
public <T extends OdooObj> List<T> findByCriteria(final int limit, final int page, final String sortByField, final Class<T> classToConvert, final Object... criteria) {
final List<List<List<Object>>> 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(
Expand Down

0 comments on commit 91c8778

Please sign in to comment.