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

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