Repository Query API
• All repositories have the ability to execute queries
• Queries can be built and executed using the Repository API.
• Queries can be represented in a textual form called Repository Query Language (RQL)
• The basic elements of Repository Query API
1. atg.repository.QueryBuilder : an interface
2. atg.repository.QueryOptions : a class
QueryBuilder interface defines the available query operations that repositories support. The QueryBuilder interface enables you to build Query objects that can be passed to the repository. A Query is constructed from QueryExpressions. Each Query relates one or more QueryExpressions and a query operation. Queries can use standard logical query operations such as AND, OR, NOT, EQUALS, GREATER THAN, LESS THAN OR EQUALS, and more complicated query operations such as collection inclusion, and pattern matching.
· Query Creation Example
– Given a RepositoryView, we initialize a QueryBuilder for it:
QueryBuilder b = view.getQueryBuilder();
– Next, create a QueryExpression for the gender property and QueryExpression for the constant female:
QueryExpression female = b.createConstantQueryExpression ("female");
– Create a ComparisonQuery that incorporates the gender and femaleQueryExpressions:
Query femaleQuery = b.createComparisonQuery(gender, female,
QueryBuilder.EQUALS);
– Pass the resulting Query to the RepositoryView for execution:
items = view.executeQuery(femaleQuery);
• The QueryOptions class to specify the ways it can be modified.
– RepositoryItem[] executeQuery(Query equerry, QueryOptions pQueryOptions);
SAMPLE QUERY BUILDER CODE:
SAMPLE QUERY BUILDER CODE:
MutableRepository pRepository = (MutableRepository)ServletUtil.getCurrentRequest().resolveName("/atg/userprofiling/ProfileAdapterRepository"); RepositoryItemDescriptor userDesc = pRepository.getItemDescriptor("user"); RepositoryView userView = userDesc.getRepositoryView(); QueryBuilder userBuilder = userView.getQueryBuilder(); QueryExpression userType = userBuilder.createPropertyQueryExpression("userType"); QueryExpression two = userBuilder.createConstantQueryExpression(new Integer(2)); Query userTypeIsTwo = userBuilder.createComparisonQuery(userType, two, QueryBuilder.EQUALS); 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()); } userType < 2 AND login STARTS WITH "j"
0 comments:
Post a Comment