Thursday, March 6, 2014

Quick Tip: Best Way to Loop Through View Object Rows Programatically

You may have noticed there are multiple ways to do one task in ADF in many situations. Especially once you get involved on the Java side of ADF. While there are multiple ways to do something, it doesnt mean all of them are necessarily the best.

One question, which can be fairly common among new ADF developers, whats the best way to programatically loop through a View object's rows without affecting the View Object's root RowSetIterator? After all, you have what seems to be too many ways to do this:
  • ViewObjectImpl.getAllRowsInRange, returning Row[]
  • ViewObjectImpl.getRowSet, returning RowSet
  • ViewObjectImpl.getRowSetIterator, returning RowSetIterator
  • ViewObjectImpl.createRowSetIterator, returning RowSetIterator
While all ways could work per the above, only one is the best, ViewObjectImpl.createRowSetIterator. It delivers the most consistent results and it won't affect the currently selected row of the View Object.

Here's how we use it:

  1. Create RowSetIterator from your View Object class: ViewObjectImpl.createRowSetIterator()
  2. Reset the RowSetIterator - just in case
  3. Loop through the iterator: while(RowSetIterator.hasNext()) { .. }
  4. After your loop is finished, close the RowSetIterator: RowSetIterator.closeRowSetIterator()

See this example, where i look up a view object row by attribute value:

public Row getRowByAttribute(ViewObjectImpl vo, String attributeName, Object attributeValue) {
RowSetIterator rsi = vo.createRowSetIterator(null);
rsi.reset();
Row currRow=null;
while(rsi.hasNext()) {
currRow = rsi.next();
if (currRow.getAttribute(attributeName).equals(attributeValue)) {
rsi.closeRowSetIterator();
return currRow;
}               
}
rsi.closeRowSetIterator();
return null;        
}

Happy JDeveloping!

1 comment: