Rankin said:
> I have many tables, which for the moment are separately defined. I want
> one template, DisplayTable.vm, to display any one of these tables. So,
> DisplayTable.java has:
>
> public void doBuildTemplate(){
> ...
> String tablename = data.getParameters().getString("tablename",null);
> Criteria crit = new Criteria();
> List list = [**tablename**]Peer.doSelect(crit);
> context.put("list", list);
> TableMap tmap = Torque.getDatabaseMap("mydb").getTable(tablename);
> ColumnMap[] cMap = tmap.getColumns();
> Vector names;
> for(int i = cMap.length -1; i >=0; i--)
> names.add(cMap[i].getColumnName());
> context.put("columnNames", names.toArray());
> ...
> }
>
> and DisplayTable.vm has
>
> <tr>
> #foreach ($columName in $columnNames)
> <th>$columnName</th>
> #end
> </tr>
> #foreach ($item in $itemList)
> <TR>
> #foreach ($attribute in $columnNames)
> <TD>$!{item.$attribute}</TD>
> #end
> </TR>
> #end
>
> I'm sure this has been done many times far more easily and other
> suggestions would be greatly appreciated ... However - how does one do
> the "[tablename]Peer.doSelect(crit)" bit?
>
> Suggestions??
>
If you want to do it generically reflection is your best bet.
You might consider the org.apache.commons.beanutils.MethodUtils class for
making these calls. Un-tested example:
private List invokeDoSelect(String fullyQualifiedTorquePeerObjectName,
Criteria crit)
{
final Class[] doSelectParamTypes = { Criteria.class };
final Object[] doSelectParams = { crit };
Object results = null;
try
{
Class peerClass = Class.forName(fullyQualifiedTorquePeerObjectName);
results = MethodUtils.invokeExactMethod(peerClass, "doSelect",
doSelectParams, doSelectParamTypes);
}
catch (NoSuchMethodException e)
{
// TODO handle this
}
catch (IllegalAccessException e)
{
// TODO handle this
}
catch (InvocationTargetException e)
{
// TODO handle this
}
catch (ClassNotFoundException e)
{
// TODO handle this
}
return (List)results;
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]