*/
private static void createListingWithQuery(ModelRequest request, ModelResponse response, ListingDescriptor listing,
ListingHandler handler, ListContext context) throws ModelException, PersistenceException
{
StandardDao standardDao = (StandardDao) SpringTools.getBean(StandardDao.ID);
QueryDescriptor query = listing.getQuery();
Properties queryParams = ExpressionLanguageContext.evalExpressionLanguageValue(context, request, query
.getParams());
int count = 0;
if (query.getDaoName() != null)
{
try
{
java.util.List list = (java.util.List) MethodUtils.invokeExactMethod(SpringTools.getBean(query
.getDaoName()), query.getDaoMethodName(), queryParams);
count = list.size();
}
catch (NoSuchMethodException x)
{
throw new ModelException(x);
}
catch (IllegalAccessException x)
{
throw new ModelException(x);
}
catch (InvocationTargetException x)
{
throw new ModelException(x.getTargetException());
}
}
else if (query.getCountName() != null)
{
count = (int) standardDao.countByNamedQuery(query.getCountName(), queryParams);
}
else if (query.getName() != null)
{
count = (int) standardDao.countByNamedFindQuery(query.getName(), queryParams);
}
else
{
if (query.getCountQuery() != null)
{
count = (int) standardDao.countByQuery(query.getCountQuery(), queryParams);
}
else
{
count = (int) standardDao.countByFindQuery(query.getQuery(), queryParams);
}
}
int page = Math.max(listing.getPage(), 1);
int maxPage = (Math.max(0, count - 1) / context.getResultsPerPage()) + 1;
page = Math.min(page, maxPage);
context.setPage(page);
context.setFirstResult((page - 1) * context.getResultsPerPage());
Output outList = createHeaderElements(request, response, listing, context);
outList.setAttribute("pageCount", new Integer(maxPage));
outList.setAttribute("page", String.valueOf(page));
int firstResult = (page - 1) * context.getResultsPerPage();
int maxResults = context.getResultsPerPage();
java.util.List res = null;
if (query.getDaoName() != null)
{
try
{
res = (java.util.List) MethodUtils.invokeExactMethod(SpringTools.getBean(query.getDaoName()), query
.getDaoMethodName(), queryParams);
}
catch (NoSuchMethodException x)
{
throw new ModelException(x);
}
catch (IllegalAccessException x)
{
throw new ModelException(x);
}
catch (InvocationTargetException x)
{
throw new ModelException(x.getTargetException());
}
}
else if (query.getName() != null)
{
res = standardDao.findByNamedQuery(query.getName(), queryParams, firstResult, maxResults, listing
.getSortColumnName(), listing.getSortOrder());
}
else
{
res = standardDao.findByQuery(query.getQuery(), queryParams, firstResult, maxResults, listing
.getSortColumnName(), listing.getSortOrder());
}
int rowNum = 1;