import com.evolveum.midpoint.xml.ns._public.common.common_3.*;
def query = midpoint.queryFor(UserType.class, "activation matches (effectiveStatus = 'enabled' and enableTimestamp > '2022-05-10')") (1)
def result = midpoint.searchObjects(query); (2)
Using MidPoint Query Language in Groovy
You can use midPoint Query Language in Groovy expressions using midpoint.queryFor
and midpoint.preparedQueryFor
functions.
Creating and using Queries
1 | Creates TypedQuery for specified object type (in this case UserType ) |
2 | Search objects using query |
Additional parameters and APIs of Typed Query
TypedQuery API allows user to also specify ordering, paging and options of resulting query.
import com.evolveum.midpoint.xml.ns._public.common.common_3.*;
import com.evolveum.midpoint.prism.query.OrderDirection;
import com.evolveum.midpoint.prism.path.ItemPath;
def prepared = midpoint.preparedQueryFor(UserType.class, "activation matches (effectiveStatus = 'enabled' and enableTimestamp > ?)");
def query = prepared.bind(basic.fromNow("-P1D"));
query.orderBy(ItemPath.create(UserType.F_ACTIVATION, ActivationType.F_ENABLE_TIMESTAMP), OrderDirection.DESCENDING) (1)
query.maxSize(10) (2)
def result = midpoint.searchObjects(query);
1 | Orders result set by activation/enableTimestamp descending (newest first) |
2 | Limit search to 10 items |
Prepared Queries: (with Placeholders)
In order to facilitate better support for use in code, placeholders
are introduced. Placeholders can be bind later to variables and MidPoint code will take care of proper escaping and type checking.
def prepared = midpoint.preparedQueryFor(UserType.class, "activation matches (effectiveStatus = 'enabled' and enableTimestamp > ?)"); (1)
def query = prepared.bind(basic.fromNow("-P1D")); (2)
def result = midpoint.searchObjects(query); (3)
1 | Creates PreparedQuery for specified object type (in this case UserType ) |
2 | Binds positional arguments to PreparedQuery using var args |
3 | Executes search using query |
Named Placeholders
Positional (anonymous placeholders) are fine for simple queries, but for larger queries it may be good idea to have support for named placeholders.
Named placeholders are entered in form :name
and their value could be set by PreparedQuery.set(name, value)
method.
Before query can be used for search it needs to be build using build()
method. (This converts prepared query to normal query).
def prepared = midpoint.preparedQueryFor(UserType.class, "activation matches (effectiveStatus = :status and enableTimestamp > :date)"); (1)
prepared.set("status", "enabled"); (2)
prepared.set("date", basic.fromNow("-P1D")); (3)
def query = prepared.build(); (4)
def result = midpoint.searchObjects(UserType.class, query); (5)
1 | Creates PreparedQuery for specified object type (in this case UserType ) |
2 | Binds status argument to value "enabled" |
3 | Binds date argument to one day in past (same time and hour) |
4 | Builds query |
5 | Executes search using query |