The examples in this section demonstrate how to perform some simple repository queries. In the Repository API, all queries are performed with Query or QueryExpression objects. A QueryExpression is a building block you can use to create simple or complex queries. A Query is a repository query that can be executed. A Query can also be used as a building block to create queries that are more complicated.

The following example assumes an item descriptor named user with an integer property named userType. The query finds users whose userType property is set to 2:

import atg.repository.*;

MutableRepository pRepository =

    // Queries are created using QueryBuilders and executed by
    // RepositoryViews. A Query is defined in the context of a
    // specific item descriptor and thus must be built and executed with
    // the right QueryBuilder and RepositoryView.

    RepositoryItemDescriptor userDesc = pRepository.getItemDescriptor("user");
    RepositoryView userView = userDesc.getRepositoryView();
    QueryBuilder userBuilder = userView.getQueryBuilder();

    // create a QueryExpression that represents the property userType
    QueryExpression userType =

    // create a QueryExpression that represents the constant 2
    QueryExpression two =
      userBuilder.createConstantQueryExpression(new Integer(2));

    // now we build our query: userType = 2
    Query userTypeIsTwo =
      userBuilder.createComparisonQuery(userType, two, QueryBuilder.EQUALS);

    // finally, execute the query and get the results
    RepositoryItem[] answer = userView.executeQuery(userTypeIsTwo);

    System.out.println("running query: userType = 2");
    if (answer == null)
        System.out.println("no items were found");
        for (int i=0; i<answer.length; i++)
          System.out.println("id: " + answer[i].getRepositoryId());

The preceding example can be expanded to include multiple selection criteria:

userType < 2 AND login STARTS WITH "j"

import atg.repository.*;

MutableRepository pRepository =

    // reuse the building blocks we have to create
    // the "userType < 2" query
    Query userTypeLTTwo =
      userBuilder.createComparisonQuery(userType, two, QueryBuilder.LESS_THAN);

    // create the "login STARTS WITH j" query
    QueryExpression login =

    QueryExpression j =

    //Note that we could make this query case-insensitive by adding another
    //parameter to the createPatternMatchQuery, with a value of true
    Query startsWithJ =
      userBuilder.createPatternMatchQuery(login, j, QueryBuilder.STARTS_WITH);

    // now AND the two pieces together. You can AND together as many
    // Query pieces as you like: we only have two in our example
    Query[] pieces = { userTypeLTTwo, startsWithJ };
    Query andQuery = userBuilder.createAndQuery(pieces);

    // execute the query and get the results
    answer = userView.executeQuery(andQuery);

    System.out.println("running query: userType < 2 AND login STARTS WITH j");
    if (answer == null) {
        System.out.println("no items were found");
    else {
       for (int i=0; i<answer.length; i++)
           RepositoryItem item = answer[i];
           String id = item.getRepositoryId();
           String l = (String)item.getPropertyValue("login");
           Integer a = (Integer)item.getPropertyValue("userType");
           System.out.println("item: " + id + ", login=" + l + ", userType=" + a);