List l = Portal.getInstance().getPricingService().getAllPriceForProduct(productCode);
List offers = Portal.getInstance().getPricingService().getAllSpecialOfferForProduct(productCode);
Map offerByPrice = new HashMap();
//just to avoid to many fetch on special offer we prepare a map
for (Iterator iter = offers.iterator(); iter.hasNext();) {
SpecialOffer so = (SpecialOffer) iter.next();
offerByPrice.put(String.valueOf(so.getRelatedPrice().getId()), so);
}
int nbYear = 3;
Collections.sort(l, new Comparator() {
public int compare(Object o1, Object o2) {
Price p1 = (Price) o1;
Price p2 = (Price) o2;
if (p1.getMinimunUser() < p2.getMinimunUser()) {
return Integer.MIN_VALUE;
}
if (p1.getMinimunUser() == p2.getMinimunUser()) {
return p1.getDurationInYear() - p2.getDurationInYear();
}
return Integer.MAX_VALUE;
}
});
String[][] table = new String[l.size()/nbYear][];
Iterator iterator = l.iterator();
for (int i=0; i<table.length; i++) {
String[] row = new String[nbYear+1];
table[i] = row;
for (int j = 0; j < row.length; j++) {
Price p = null;
if(iterator.hasNext()) {
p = (Price) iterator.next();
}
if(p!=null) {
if(j == 0) {
row[j++] = p.getMinimunUser() +" - "+ p.getMaximunUser();
}
// To many fetch slow down the page
// SpecialOffer so = Portal.getInstance().getPricingService().getSpecialOffer(p);
SpecialOffer so = (SpecialOffer) offerByPrice.get(String.valueOf(p.getId()));
if(so != null) {
row[j] = "<strike>"+p.getPriceByYear() +"</strike> €"+"<br/>" + specialOfferMsg +" "+so.getNewPrice()+" €";
} else {
row[j] = p.getPriceByYear() +" €";
}
} else {
row[j] = "";