Tuesday, December 27, 2011

Repository Query Examples



import atg.repository.*;

MutableRepository pRepository =
  (MutableRepository)ServletUtil.getCurrentRequest().resolveName
  ("/atg/userprofiling/ProfileAdapterRepository");

    // 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 =
      userBuilder.createPropertyQueryExpression("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");
      }
    else
      {
        for (int i=0; i<answer.length; i++)
          System.out.println("id: " + answer[i].getRepositoryId());
      }




import atg.repository.*;

MutableRepository pRepository =
  (MutableRepository)ServletUtil.getCurrentRequest().resolveName
  ("/atg/userprofiling/ProfileAdapterRepository");

    // 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 =
      userBuilder.createPropertyQueryExpression("login");

    QueryExpression j =
      userBuilder.createConstantQueryExpression("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);
       }
    }

No comments:

Popular Posts