How to use custom and other filters in Java sdk that are provided in REST Endpoints

Product/components used and version/fix level:

Cumulocity Production

Detailed explanation of the problem:

Hi, My use case is I want to fetch devices which were created in last 24 hours. Now I can do that with REST endpoint by fetching all the devices and compare the time with creation Time fragment. I want to know:-

  1. How to use the custom filter to fetch the devices. I want to use the custom filter something like creationTime between 2023-06-12T12:00:00.000Z and 2023-06-12T12:59:59.999Z so that I can get the devices which were created in 24 hours.
    I tried this with rest endpoint : /inventory/managedObjects?q=$filter = creationTime eq '2023-05-17T13:14:17.471Z'
    but got this error:
{
    "error": "inventory/Invalid Data",
    "message": "Find by filter query failed : Query '$filter = creationTime eq '2023-05-17T13:14:17.471Z'' could not be understood. Please try again.",
    "info": "https://www.cumulocity.com/guides/reference/rest-implementation//#a-name-error-reporting-a-error-reporting"
}

I hope you understand what I am trying to achieve with this.

  1. How to use this same (1.) custom filter in the JAVA SDK, is there any method?

  2. How to use the withParents = true filter, in JAVA SDK. In rest endpoint, I am able to hit /inventory/managedObjects?fragmentType=c8y_IsDevice&pageSize=2000&withParents=true
    but how can i use the withParents = true filter in sdk.

Hi @Sam123 ,

  1. for a time range you can use it as follows:
    Be aware of using “creationTime.date”, check API documentation.

creationTime.date gt '2023-06-12T12:00:00.000Z' and creationTime.date le '2023-06-12T12:59:59.999Z

  1. See below and use “query” and just put the query above as value.

  2. In the “get” method you can set any query parameter like this

The first argument can be the pageSize, here it is 50.

import com.cumulocity.sdk.client.Filter;
import com.cumulocity.sdk.client.QueryParam;

QueryParam queryParam = new QueryParam(new Param() {
		@Override
		public String getName() {
			return "withParents";
		}
	}, "true");

inventoryApi
.getManagedObjects()
.get(50, queryParam)
.allPages();

As this code gets quite huge when using more parameters I can share a convenience class:

public enum CustomQueryParam implements Param {
	WITH_TOTAL_PAGES("withTotalPages"),
	PAGE_SIZE("pageSize"),
	QUERY("query"),
	DEVICE_QUERY("q"),
	DATE_FROM("dateFrom"),
	STATUS("status"),
	FRAGMENT_TYPE("fragmentType"),
	DEVICE_ID("deviceId"),
	REVERT("revert"),
	;

	private String name;
	
	private String value;
	
	public String getValue() {
		return value;
	}

	private CustomQueryParam(final String name) {
		this.name = name;
	}

	@Override
	public String getName() {
		return name;
	}

	public CustomQueryParam setValue(final String value) {
		this.value = value;
		return this;
	}

	public QueryParam toQueryParam() {
		return new QueryParam(this, Filter.encode(value));
	}

}

By this you can use it as such:

QueryParam queryParam = CustomQueryParam.WITH_PARENTS.setValue("true").toQueryParam();
		
inventoryApi
.getManagedObjects()
.get(50, queryParam)
.allPages();

I might created a knowledge base article for this :slight_smile:

Hope it helps with this quick intro.

Regards
Kai

1 Like

Hi Kai,

I have already implemented it. Thank you for your response, it might help someone else :slight_smile:

Thanks & Regards,
Samanyu

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.