Skip to content
David Moebius edited this page Apr 25, 2014 · 13 revisions

!!! We plan to update and rebuild the Query Object to make it more understandable and usable for all needs. At them moment we suggest to use a query based on a String. The class StringQueryBuilder can help you by creating the query string.

If you want to search for Users or Groups you can do this by a string or by a Query Object.

The Query.Builder will help you to build the needed query to get any search result you need. The Query object also gives you the possibility to move to the next or the prevision page.

Chapters:

Creating a Query Object

A Query Object can be created by

Query query = new Query.Builder(User.class).....build();

or

Query query = new Query.Builder(Group.class).....build();

With query.toString() is it possible to get a complete query with all given Parameters to write it, for example, in log statements.

If you have more Users or Groups than one page will hold (standard 100) you can get with

Query newQuery = query.nextPage();

a new query which will gives you the results of the next page.

In the same way you can get the previous page with

Query newQuery = query.previousPage();

If you try to get the page with the "number" -1 you will get an IllegalStateException.

Query.Builder

The Query.Builder gives you a easy possibility to build any easy or complex Querys you need.

(All Examples will be done with a User Query. A Group query is equivalent)

To get a Query.Builder please call

Query.Builder queryBuilder = new Query.Builder(User.class);

creating the filter (where) statement

To build a query please call the methods of the Query.Builder starting with filter in the way you would build a query.

Query.Builder queryBuilder = new Query.Builder(User.class);
Query.Filter filter = new Query.Filter(User.class, User_.userName.equalTo("Marissa"))
            .and(User_.Emails.value.equalTo("[email protected]"))
            .or(User_.Emails.value.equalTo("[email protected]"));
Query query = queryBuilder.setFilter(filter).build();

creating a negated statement

To build a query with the negated operator please use 'not' from the Query.Filter api.

Not is the only operator which does not necessarily require a left filter part. This means that 'not' can only follow 'and' or 'or'. It is not allowed to use a simple filter like (User_.Groups.display.equalTo("student")), for the left side.

Query.Builder queryBuilder = new Query.Builder(User.class);
Query.Filter filter = new Query.Filter(User.class)
            .not(User_.Groups.display.equalTo("student"))
            .and(User_.Emails.value.startsWith("h"));
Query query = queryBuilder.setFilter(filter).build();

(brackets)

If you need to put some statements into some braces you can do this with a inner Filter

Query.Filter innerFilter = new Query.Filter(User.class, User_userName.equalTo("marissa"))
                     .or(User_.userName.equalTo("bjensen"));

Query.Filter mainFilter = new Query.Filter(User.class, User_.Meta.created.greaterEquals(new Date())).and(innerFilter);

Query query = Query.Builder(User.class).setFilter(mainFilter).build();

The result will be

meta.created ge "2013-05-23T13:12:45.672" and (userName eq "marissa" and userName eq "bjensen")

sortBy

OSIAM gives you the opportunity to sort your result by one field. To sort by one field call

Query.Builder queryBuilder = new Query.Builder(User.class);
queryBuilder.setSortBy(User_.userName);

sortOrder

You can tell OSIAM to sort your result ascending or descending by calling

queryBuilder.setSortOrder(SortOrder.ASCENDING);

or

ueryBuilder.setSortOrder(SortOrder.DESCENDING);

results per page

with

queryBuilder.setCountPerPage(20);

you can define how many Users/Groups you want to get for the actual request.

start index

with

queryBuilder.setStartIndex(10);

you can define the 1-based index of the first search result.

NOTE: At the time of writing the osiam server uses a 0-based index for paging and so does the connector for java.