Example of a Droplet Using Query Builder to Find Users by Name
Note: You'ld actually use RQLQueryForEach to do this job. This is just an example of the technology!
This sample implements a droplet which takes a search term (match), finds users whose login, firstName or lastName property matches the term and then iterates over them similarly to a ForEach droplet.
Components of this type could be configured at global scope since the droplet has no state to be set by the page designer. The repository and itemDescriptorName properties are configured in the component. While limit, start, empty, finish, match and person are configured via page parameters.
This sample implements a droplet which takes a search term (match), finds users whose login, firstName or lastName property matches the term and then iterates over them similarly to a ForEach droplet.
Components of this type could be configured at global scope since the droplet has no state to be set by the page designer. The repository and itemDescriptorName properties are configured in the component. While limit, start, empty, finish, match and person are configured via page parameters.
Source Code - PeopleFinder.javaimport atg.repository.*;
import javax.servlet.*; // ServletException
import atg.servlet.*; // DynamoServlet
import java.io.*; // IOException
public class PeopleFinder extends DynamoServlet
{
Repository mRepository = null;
public Repository getRepository()
{
return mRepository;
}
public void setRepository(Repository r)
{
mRepository = r;
}
String mstrItemDescriptorName = "user";
public void setItemDescriptorName(String s)
{
mstrItemDescriptorName = s;
}
public String getItemDescriptorName()
{
return mstrItemDescriptorName;
}
void show(RepositoryItem[] ris,
int nMaxLimit,
DynamoHttpServletRequest req,
DynamoHttpServletResponse res)
{
try
{
int nMax = ris.length;
if((nMaxLimit > 0)&&(ris.length > nMaxLimit))
{
nMax = nMaxLimit;
}
req.setParameter("count", new Integer(ris.length));
req.setParameter("limit", new Integer(nMaxLimit));
if(nMax > 0)
{
req.serviceLocalParameter("start",req,res);
}
for(int i=0; i <> 0)
{
req.serviceLocalParameter("finish",req,res);
}
}
catch(Exception ex)
{
logError(ex);
}
}
public void service(DynamoHttpServletRequest req,
DynamoHttpServletResponse res)
throws IOException, ServletException
{
try
{
String strMatch = req.getParameter("match");
String strMaxLimit = req.getParameter("limit");
Integer nMaxLimit = new Integer((strMaxLimit != null) ? strMaxLimit : "0");
if(strMatch != null)
{
if(getRepository() != null)
{
if(getItemDescriptorName() != null)
{
RepositoryView view = getRepository().getView(getItemDescriptorName());
if(view != null)
{
// Get the builder
QueryBuilder userBuilder = view.getQueryBuilder();
// Expressions
QueryExpression
firstName = userBuilder.createPropertyQueryExpression("firstName");
QueryExpression
lastName = userBuilder.createPropertyQueryExpression("lastName");
QueryExpression
login = userBuilder.createPropertyQueryExpression("login");
QueryExpression
match = userBuilder.createConstantQueryExpression(strMatch);
// Build queries for OR operation
Query [] queries = new Query[3];
queries[0] = userBuilder.createPatternMatchQuery(firstName, match,
QueryBuilder.CONTAINS);
queries[1] = userBuilder.createPatternMatchQuery(login, match,
QueryBuilder.CONTAINS);
queries[2] = userBuilder.createPatternMatchQuery(lastName, match,
QueryBuilder.CONTAINS);
// Create the main query
Query query = userBuilder.createOrQuery(queries);
// finally, execute the query and get the results
//
RepositoryItem[] people = view.executeQuery(query);
if(people == null)
{
req.serviceLocalParameter("empty",req,res);
}
else
{
show(people, nMaxLimit.intValue(), req, res);
}
}
else
{
throw new ServletException("No view found called for '"
+ getItemDescriptorName() + "'");
}
}
else
{
throw new ServletException("'itemDescriptorName' property not configured");
}
}
else
{
throw new ServletException("'repository' property not configured");
}
}
else
{
throw new ServletException("'match' is a required parameter for this droplet");
}
}
catch(Exception e)
{
logError(e);
}
}
public PeopleFinder()
{
}
}
Droplet Bean Info Source Code - PeopleFinderBeanInfo.javaimport java.beans.*;
import atg.servlet.DynamoServlet;
import atg.droplet.DropletBeanInfo;
import atg.droplet.ParamDescriptor;
public class PeopleFinderBeanInfo extends DropletBeanInfo
{
//-------------------------------------
// CONSTANTS
//-------------------------------------
public static final String CLASS_VERSION = "0.9";
//-------------------------------------
// FIELDS
//-------------------------------------
private final static ParamDescriptor[] sWrapperDescriptors = {
new ParamDescriptor("count", "The number of people in the result list",
Integer.class, false, true)
};
private final static ParamDescriptor[] sOutputDescriptors = {
new ParamDescriptor("element",
"The repository item for this person matching the search term",
atg.repository.RepositoryItem.class, false, true),
new ParamDescriptor("index", "The index of this person in the result list",
Integer.class, false, true)
};
private final static ParamDescriptor[] sParamDescriptors = {
new ParamDescriptor("limit",
"Specify > 0 to limit the maximum number of people returned",
Integer.class, false, true),
new ParamDescriptor("match", "Search Term for First and Last Name",
String.class, false, true),
new ParamDescriptor("person", "Rendered once between local and stack parameters",
DynamoServlet.class, false, true, sOutputDescriptors),
new ParamDescriptor("start", "Rendered before found matches",
DynamoServlet.class, false, true, sWrapperDescriptors),
new ParamDescriptor("finish", "Rendered after found matches",
DynamoServlet.class, false, true, sWrapperDescriptors),
new ParamDescriptor("empty", "Rendered when no matches are found",
DynamoServlet.class, false, true),
};
private final static BeanDescriptor sBeanDescriptor =
createBeanDescriptor(PeopleFinder.class,
null,
"This servlet iterates over the matching people and"
+ " calls person once for each match, setting element to"
+ " the repository item for that person. If no matches"
+ " are found the empty oparam is rendered",
sParamDescriptors,
"YourDropletCategoryName");
//-------------------------------------
// METHODS
//-------------------------------------
//-------------------------------------
/**
* Returns the BeanDescriptor for this bean, which will in turn
* contain ParamDescriptors for the droplet.
**/
public BeanDescriptor getBeanDescriptor() {
return sBeanDescriptor;
}
//----------------------------------------
}
JHTML Sample
People Finder No Matches Found : :
import javax.servlet.*; // ServletException
import atg.servlet.*; // DynamoServlet
import java.io.*; // IOException
public class PeopleFinder extends DynamoServlet
{
Repository mRepository = null;
public Repository getRepository()
{
return mRepository;
}
public void setRepository(Repository r)
{
mRepository = r;
}
String mstrItemDescriptorName = "user";
public void setItemDescriptorName(String s)
{
mstrItemDescriptorName = s;
}
public String getItemDescriptorName()
{
return mstrItemDescriptorName;
}
void show(RepositoryItem[] ris,
int nMaxLimit,
DynamoHttpServletRequest req,
DynamoHttpServletResponse res)
{
try
{
int nMax = ris.length;
if((nMaxLimit > 0)&&(ris.length > nMaxLimit))
{
nMax = nMaxLimit;
}
req.setParameter("count", new Integer(ris.length));
req.setParameter("limit", new Integer(nMaxLimit));
if(nMax > 0)
{
req.serviceLocalParameter("start",req,res);
}
for(int i=0; i <> 0)
{
req.serviceLocalParameter("finish",req,res);
}
}
catch(Exception ex)
{
logError(ex);
}
}
public void service(DynamoHttpServletRequest req,
DynamoHttpServletResponse res)
throws IOException, ServletException
{
try
{
String strMatch = req.getParameter("match");
String strMaxLimit = req.getParameter("limit");
Integer nMaxLimit = new Integer((strMaxLimit != null) ? strMaxLimit : "0");
if(strMatch != null)
{
if(getRepository() != null)
{
if(getItemDescriptorName() != null)
{
RepositoryView view = getRepository().getView(getItemDescriptorName());
if(view != null)
{
// Get the builder
QueryBuilder userBuilder = view.getQueryBuilder();
// Expressions
QueryExpression
firstName = userBuilder.createPropertyQueryExpression("firstName");
QueryExpression
lastName = userBuilder.createPropertyQueryExpression("lastName");
QueryExpression
login = userBuilder.createPropertyQueryExpression("login");
QueryExpression
match = userBuilder.createConstantQueryExpression(strMatch);
// Build queries for OR operation
Query [] queries = new Query[3];
queries[0] = userBuilder.createPatternMatchQuery(firstName, match,
QueryBuilder.CONTAINS);
queries[1] = userBuilder.createPatternMatchQuery(login, match,
QueryBuilder.CONTAINS);
queries[2] = userBuilder.createPatternMatchQuery(lastName, match,
QueryBuilder.CONTAINS);
// Create the main query
Query query = userBuilder.createOrQuery(queries);
// finally, execute the query and get the results
//
RepositoryItem[] people = view.executeQuery(query);
if(people == null)
{
req.serviceLocalParameter("empty",req,res);
}
else
{
show(people, nMaxLimit.intValue(), req, res);
}
}
else
{
throw new ServletException("No view found called for '"
+ getItemDescriptorName() + "'");
}
}
else
{
throw new ServletException("'itemDescriptorName' property not configured");
}
}
else
{
throw new ServletException("'repository' property not configured");
}
}
else
{
throw new ServletException("'match' is a required parameter for this droplet");
}
}
catch(Exception e)
{
logError(e);
}
}
public PeopleFinder()
{
}
}
Droplet Bean Info Source Code - PeopleFinderBeanInfo.javaimport java.beans.*;
import atg.servlet.DynamoServlet;
import atg.droplet.DropletBeanInfo;
import atg.droplet.ParamDescriptor;
public class PeopleFinderBeanInfo extends DropletBeanInfo
{
//-------------------------------------
// CONSTANTS
//-------------------------------------
public static final String CLASS_VERSION = "0.9";
//-------------------------------------
// FIELDS
//-------------------------------------
private final static ParamDescriptor[] sWrapperDescriptors = {
new ParamDescriptor("count", "The number of people in the result list",
Integer.class, false, true)
};
private final static ParamDescriptor[] sOutputDescriptors = {
new ParamDescriptor("element",
"The repository item for this person matching the search term",
atg.repository.RepositoryItem.class, false, true),
new ParamDescriptor("index", "The index of this person in the result list",
Integer.class, false, true)
};
private final static ParamDescriptor[] sParamDescriptors = {
new ParamDescriptor("limit",
"Specify > 0 to limit the maximum number of people returned",
Integer.class, false, true),
new ParamDescriptor("match", "Search Term for First and Last Name",
String.class, false, true),
new ParamDescriptor("person", "Rendered once between local and stack parameters",
DynamoServlet.class, false, true, sOutputDescriptors),
new ParamDescriptor("start", "Rendered before found matches",
DynamoServlet.class, false, true, sWrapperDescriptors),
new ParamDescriptor("finish", "Rendered after found matches",
DynamoServlet.class, false, true, sWrapperDescriptors),
new ParamDescriptor("empty", "Rendered when no matches are found",
DynamoServlet.class, false, true),
};
private final static BeanDescriptor sBeanDescriptor =
createBeanDescriptor(PeopleFinder.class,
null,
"This servlet iterates over the matching people and"
+ " calls person once for each match, setting element to"
+ " the repository item for that person. If no matches"
+ " are found the empty oparam is rendered",
sParamDescriptors,
"YourDropletCategoryName");
//-------------------------------------
// METHODS
//-------------------------------------
//-------------------------------------
/**
* Returns the BeanDescriptor for this bean, which will in turn
* contain ParamDescriptors for the droplet.
**/
public BeanDescriptor getBeanDescriptor() {
return sBeanDescriptor;
}
//----------------------------------------
}
JHTML Sample
No comments:
Post a Comment