com.atlassian.crowd.search.builder
Class QueryBuilder

java.lang.Object
  extended by 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(PropertyUtils.ofTypeString("color"), MatchMode.EXACTLY_MATCHES, "red");
     TermRestriction colorBlue = new TermRestriction(PropertyUtils.ofTypeString("color"), MatchMode.EXACTLY_MATCHES, "blue");
     TermRestriction userNameR = new TermRestriction(UserTermKeys.USERNAME, MatchMode.STARTS_WITH, "r");
     TermRestriction userNameB = new TermRestriction(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);
     


Nested Class Summary
static class QueryBuilder.PartialEntityQuery<T>
           
static class QueryBuilder.PartialEntityQueryWithRestriction<T>
           
static class QueryBuilder.PartialEntityQueryWithStartIndex<T>
           
static class QueryBuilder.PartialMembershipQueryWithEntityToMatch<T>
           
static class QueryBuilder.PartialMembershipQueryWithNameToMatch<T>
           
static class QueryBuilder.PartialMembershipQueryWithStartIndex<T>
           
 
Constructor Summary
QueryBuilder()
           
 
Method Summary
static
<T> MembershipQuery<T>
createMembershipQuery(int maxResults, int startIndex, boolean findMembers, EntityDescriptor entityToReturn, Class<T> returnType, EntityDescriptor entityToMatch, String nameToMatch)
           
static
<T> QueryBuilder.PartialEntityQuery<T>
queryFor(Class<T> returnType, EntityDescriptor entity)
           
static
<T> EntityQuery<T>
queryFor(Class<T> returnType, EntityDescriptor entity, SearchRestriction searchRestriction, int startIndex, int maxResults)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

QueryBuilder

public QueryBuilder()
Method Detail

queryFor

public static <T> QueryBuilder.PartialEntityQuery<T> queryFor(Class<T> returnType,
                                                              EntityDescriptor entity)

queryFor

public static <T> EntityQuery<T> queryFor(Class<T> returnType,
                                          EntityDescriptor entity,
                                          SearchRestriction searchRestriction,
                                          int startIndex,
                                          int maxResults)

createMembershipQuery

public static <T> MembershipQuery<T> createMembershipQuery(int maxResults,
                                                           int startIndex,
                                                           boolean findMembers,
                                                           EntityDescriptor entityToReturn,
                                                           Class<T> returnType,
                                                           EntityDescriptor entityToMatch,
                                                           String nameToMatch)


Copyright © 2013 Atlassian. All Rights Reserved.