Saturday, December 6, 2008

Learnt a new thing today...

I have a relationship like so PortfolioVo has many stockVOs and Each stockVo has a code,quantity ,dateBought e.t.c
Now in my JSP I allow the user to enter multiple stocks for each Portfolio by adding a row dynamically to the table. In my controller I could get a list of StockCodes , a list of quantity and list of Dates.Thats one way of doing it .Now in my controller I then iterate through each stockCode in a for loop get the stockCode and then create my StockObject.

public class PortfolioVo{
private String[] stockCodes;
private String[] quantity;
private String[] price;
}

Another way of doing it would be that for each row I add in my table I get back a StockVo that has a stockCode , quantity and price


public class StockVO implements Serializable {

private String stockCode;
private String quantity;
private String price;
}
public class PortfolioVO implements Serializable {
private List vo= LazyList.decorate(new ArrayList(),
FactoryUtils.instantiateFactory(StockVO.class));
}
TODO :Readup on
http://commons.apache.org/collections/api-release/org/apache/commons/collections/list/LazyList.html

Also I add more magic to my javascript to name my dynamic text fields in my table
like so
function insertPrice(row,lastRow){
var cellPrice = row.insertCell(2);
textNode = createElementWithName("input",'vo['+lastRow+'].price');
cellPrice.appendChild(textNode);

}

And then at last in my controller I get a list of objects of type StockVo in my PortfolioVo .