Thanks for the reply, I was unaware of beanutils. It looks to be an improvement over the less generalized reflection I've been poking at:

public List list(String ofWhat){
  String peerName = MODEL_CLASS_PREFIX + ofWhat + "Peer";
  try{
    Class peer = Class.forName(peerName);
    Object o = peer.newInstance();
    return ((BasePeer)o).doSelect(new Criteria());
  } catch (Exception e){/*do something*/;}
  return null;
}

I'll look into it & try it.

But I was assuming there was some simpler method of accomplishing the same goal where one doesn't need to know this kind of stuff. This would seem to be a question that would crop up often, especially at the beginning of a project or for proof-of-concept work.


Russell Edens wrote:
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]





--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]



Reply via email to