The examples in this section demonstrate how to perform some simple repository queries. In the Repository API, all queries are performed using 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 supposes we have an item descriptor named user with an integer property named userType. This is how we might perform a simple query to find users whose userType property is 2.
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());
}
Let’s expand the preceding example with a slightly more complicated query. The next code fragment builds on the preceding fragment to create the query userType < 2 AND login STARTS WITH "j":
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);
}
}
}
The following example supposes we have an item descriptor named user with an integer property named userType. This is how we might perform a simple query to find users whose userType property is 2.
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());
}
Let’s expand the preceding example with a slightly more complicated query. The next code fragment builds on the preceding fragment to create the query userType < 2 AND login STARTS WITH "j":
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:
Post a Comment