Class QueryBuilder

java.lang.Object
com.atlassian.crowd.search.builder.QueryBuilder

public class QueryBuilder extends Object
Recommended convenience class to build queries.

Examples are presented below.

  1. Find all users, return results indexed from 0 to 99 or less:
     QueryBuilder.queryFor(User.class, EntityDescriptor.user()).returningAtMost(100);
     
    This is the minimum required structure for a query.
  2. Find all users with the username matching b*, return results indexed from 100 to 109 or less:
     QueryBuilder.queryFor(User.class, EntityDescriptor.user()).with(
          Restriction.on(UserTermKeys.USERNAME).startingWith("b")
      ).startingAt(100).returningAtMost(10);
     
  3. Find all users that have an attribute 'color' with a value that matches red* OR scarlet OR crimson, returning results indexed from 0 to 49 or less:
     QueryBuilder.queryFor(User.class, EntityDescriptor.user()).with(
     Combine.anyOf(
          Restriction.on(PropertyUtils.ofTypeString("color")).startingWith("red"),
          Restriction.on(PropertyUtils.ofTypeString("color")).exactlyMatching("scarlet"),
          Restriction.on(PropertyUtils.ofTypeString("color")).exactlyMatching("crimson")
      )
     ).returningAtMost(50);
     
  4. Find all users that like the color red AND blue, returning results indexed from 0 to 49 (only return usernames):
     QueryBuilder.queryFor(String.class, EntityDescriptor.user()).with(
     Combine.allOf(
          Restriction.on(PropertyUtils.ofTypeString("color")).exactlyMatching("red"),
          Restriction.on(PropertyUtils.ofTypeString("color")).exactlyMatching("blue")
      )
     ).returningAtMost(50);
     
  5. Find all users that like the color red and have a name starting "r" or like blue and have a name starting with "b", returning results indexed from 0 to 9 or less:
     QueryBuilder.queryFor(User.class, EntityDescriptor.user()).with(
      Combine.anyOf(
          Combine.allOf(
              Restriction.on(PropertyUtils.ofTypeString("color")).exactlyMatching("red"),
              Restriction.on(UserTermKeys.USERNAME).startingWith("r")
          ),
          Combine.allOf(
              Restriction.on(PropertyUtils.ofTypeString("color")).exactlyMatching("blue"),
              Restriction.on(UserTermKeys.USERNAME).startingWith("b")
          )
      )
     ).returningAtMost(10);
     

    This is equivalent to verbosely constructing the same query as so:

     TermRestriction colorRed = new TermRestriction<String>(PropertyUtils.ofTypeString("color"), MatchMode.EXACTLY_MATCHES, "red");
     TermRestriction colorBlue = new TermRestriction<String>(PropertyUtils.ofTypeString("color"), MatchMode.EXACTLY_MATCHES, "blue");
     TermRestriction userNameR = new TermRestriction<String>(UserTermKeys.USERNAME, MatchMode.STARTS_WITH, "r");
     TermRestriction userNameB = new TermRestriction<String>(UserTermKeys.USERNAME, MatchMode.STARTS_WITH, "b");
     BooleanRestriction conjuction1 = new BooleanRestrictionImpl(BooleanRestriction.BooleanLogic.AND, colorRed, userNameR);
     BooleanRestriction conjuction2 = new BooleanRestrictionImpl(BooleanRestriction.BooleanLogic.AND, colorBlue, userNameB);
     BooleanRestriction disjunction = new BooleanRestrictionImpl(BooleanRestriction.BooleanLogic.OR,  conjuction1, conjuction2);
     UserQuery query = new UserQuery(User.class, disjunction, 0, 10);
     
Membership Queries
  1. Find first 10 users of a group:
     QueryBuilder.queryFor(User.class, EntityDescriptor.user()).childrenOf(EntityDescriptor.group()).withName("group-name").returningAtMost(10);
     
  2. Find first 10 users of a group (returning just the names):
     QueryBuilder.queryFor(String.class, EntityDescriptor.user()).childrenOf(EntityDescriptor.group()).withName("group-name").returningAtMost(10);
     
  3. Find first 10 groups that are members (children) of a group:
     QueryBuilder.queryFor(Group.class, EntityDescriptor.group()).childrenOf(EntityDescriptor.group()).withName("group-name").returningAtMost(10);
     
  4. Find first 10 groups that a user belongs to:
     QueryBuilder.queryFor(Group.class, EntityDescriptor.group()).parentsOf(EntityDescriptor.user()).withName("user-name").returningAtMost(10);
     
  5. Find first 10 groups that a user belongs to (returning just the names):
     QueryBuilder.queryFor(String.class, EntityDescriptor.group()).parentsOf(EntityDescriptor.user()).withName("user-name").returningAtMost(10);