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;
for (Object row : res)
{
context.setIt(row);
Output outItem = response.createOutput("item");
outList.add(outItem);
String idExpression = listing.getIdColumn();
if (idExpression == null)
{
idExpression = "#{it.id}";
}
String id = ExpressionLanguageContext.evalExpressionLanguageValue(context, request, idExpression)
.toString();
try
{
outItem.setAttribute("id", id);
}
catch (Exception x)
{
throw new ModelException("Unable to retrieve id property in listing '" + listing.getId() + "' ("
+ x.getMessage() + ")");
}
outItem.setAttribute("odd", new Boolean(rowNum % 2 == 1));
outItem.setAttribute("even", new Boolean(rowNum % 2 == 0));
++rowNum;
int colNum = 0;
for (ColumnDescriptor column : listing.getColumns())
{
Output outColumn = response.createOutput(String.valueOf(colNum++));
outItem.add(outColumn);
if ("custom".equals(column.getViewer()))
{
outColumn.setAttribute("viewer", "text");
if (handler != null)
{
try
{
CellData cellData = handler.handleResult(request, response, listing, new BeanRowData(
listing, row), column);
outColumn.setContent(cellData.getValue());
outColumn.setAttribute("viewer", cellData.getViewer().toString());
outColumn.setAttribute("bundle", cellData.getBundle());
}
catch (Exception x)
{
outColumn.setAttribute("viewer", "error");
outColumn.setContent(x.getMessage());
}
}
else
{
outColumn.setContent("");
}
}
else
{
String valueExpression = column.getValue();
if (valueExpression == null)
{
valueExpression = "#{it." + column.getName() + "}";
}
Object value = ExpressionLanguageContext.evalExpressionLanguageValue(context, request,
valueExpression);
if ("combo".equals(column.getViewer()))
{
outColumn.setAttribute("viewer", column.getViewer());
outColumn.setAttribute("bundle", column.getBundle());
outColumn.setContent(! StringTools.isTrimEmpty(value) ? "opt" + value : "-");
}
else if ("message".equals(column.getViewer()))
{
outColumn.setAttribute("viewer", column.getViewer());
outColumn.setAttribute("bundle", column.getBundle());
String[] parts = value.toString().split("\\|");
StringBuffer sb = new StringBuffer(parts[0]);
for (int j = 1; j < parts.length; ++j)
{
if (parts[j].startsWith("#{") && parts[j].endsWith("}"))
{
sb.append("|"
+ ExpressionLanguageContext.evalExpressionLanguageValue(context,
request, parts[j]));
}
else
{
sb.append("|" + parts[j]);
}
}
outColumn.setContent(sb.toString());
}
else if (column.getViewer().startsWith("icon:"))
{
String viewer = column.getViewer();
int slashPos = viewer.indexOf('|');
String iconTrue = viewer.substring(5, slashPos > 0 ? slashPos : viewer.length());
String iconFalse = slashPos > 0 ? viewer.substring(slashPos + 1, viewer.length()) : "blank";
outColumn.setAttribute("viewer", "icon");
outColumn.setContent(NumberTools.toBool(value, false) ? iconTrue : iconFalse);
}
else if (column.getViewer().startsWith("handler:"))
{
try
{
CellData cellData = (CellData) MethodUtils.invokeMethod(handler, column.getViewer()
.substring(8), value);
outColumn.setContent(cellData.getValue());
outColumn.setAttribute("viewer", cellData.getViewer() != null ? cellData.getViewer()
.toString() : "text");
outColumn.setAttribute("bundle", cellData.getBundle() != null ? cellData.getBundle()
: listing.getBundle());
}
catch (InvocationTargetException x)
{
outColumn.setContent(x.getTargetException().toString());
}
catch (Exception x)
{
outColumn.setContent(x.toString());
}
}
else if (column.getViewer().startsWith("js:"))
{
String[] params = column.getViewer().split(":");
outColumn.setAttribute("viewer", "js");
outColumn.setContent(params[1]);
outColumn.setAttribute("name", column.getName());
outColumn.setAttribute("id", id);
}
else if ("province".equals(column.getViewer()))
{
// outColumn.setAttribute ("viewer", column.getViewer ());
// Object country = null;
// try
// {
// country = set.getObject (column.getAs () + "country");
// }
// catch (Exception ignored)
// {
// }
// Object value = set.getObject (column.getAs ());
// if (! StringTools.isTrimEmpty (value))
// {
// outColumn.setContent ((! StringTools.isTrimEmpty (country) ? country + "." : "") +
// StringTools.trim (value));
// }
// else
// {
// outColumn.setContent ("");
// }
outColumn.setContent("");
}
else
{
outColumn.setAttribute("viewer", column.getViewer());
outColumn.setContent(! StringTools.isTrimEmpty(value) ? value : "");
}
}
if (! column.isVisible())
{
outColumn.setAttribute("hide", "Y");
}
}
CommandInfo viewCommand = listing.getCommand(ListingDescriptor.COMMAND_VIEW);
if (viewCommand != null)
{
Command cmd = viewCommand.createCommand(request, response, context);
cmd.setName("command");
cmd.setParameter(listing.getKeyName(), id);
cmd.setParameter("link", "Y");
outItem.setAttribute("command", cmd);
}
}
while (rowNum <= context.getResultsPerPage())
{
Output outItem = response.createOutput("item");
outList.add(outItem);
outItem.setAttribute("empty", Boolean.TRUE);
++rowNum;
}
createListingControls(request, response, listing, outList, 1, maxPage, page, context);
}