Monday, January 03, 2011

Replace all existing rows in an EO Based VO

UseCase
We have an EO based VO and want to always delete the rows from DB, EO Cache and ViewStorage.Then repopulate the same rows back to the VO again.

Here is the exact code

Solution#1

    public void doReplaceExistingRowsInEOBasedVO1(){
ViewObjectImpl vo = getPopupItemsVO1();
int origRSize = vo.getRangeSize();
vo.setRangeSize(-1);
Row[] allRows = vo.getAllRowsInRange();
vo.getQueryCollection().clear();
System.out.println("Size="+vo.getQueryCollection().size());
List<AttributeListImpl> attrListColl = new ArrayList<AttributeListImpl>();
for(Row row : allRows){
vo.getQueryCollection().add(row);
}
System.out.println("Size="+vo.getQueryCollection().size());
System.out.println(vo.getQueryCollection());
vo.setRangeSize(origRSize);

}


Solution # 2

    public void doReplaceExistingRowsInEOBasedVO(){
ViewObjectImpl vo = getPopupItemsVO1();
int origRSize = vo.getRangeSize();
vo.setRangeSize(-1);
Row[] allRows = vo.getAllRowsInRange();

List<AttributeListImpl> attrListColl = new ArrayList<AttributeListImpl>();
for(Row row : allRows){
AttributeListImpl attrList = new AttributeListImpl();
String[] attrNames = row.getAttributeNames();
for(String attrName : attrNames){
Object val = row.getAttribute(attrName);
System.out.println(attrName+"--->"+val);
attrList.setAttribute(attrName, val);
}
attrListColl.add(attrList);
row.remove();
}

for(AttributeListImpl attrList : attrListColl){
Row newRow = vo.createAndInitRow(attrList);
vo.insertRow(newRow);
}

allRows = vo.getAllRowsInRange();
System.out.println(vo.getQueryCollection());
vo.setRangeSize(origRSize);

}

No comments: