@SuppressWarnings("unchecked")
@Override
public HoldingSummary findHoldingAggregated(Integer accountId) {
HoldingSummary holdingSummary = new HoldingSummary();
List<HoldingAggregate> holdingRollups = new ArrayList<HoldingAggregate>();
// Filter out the losers (gains =< 0)
Query query = em.createQuery("SELECT h.quoteSymbol, sum(q.price * h.quantity) - SUM(h.purchaseprice * h.quantity) as gain FROM Holding h, Quote q Where h.accountAccountid =:accountId and h.quoteSymbol=q.symbol GROUP BY h.quoteSymbol HAVING SUM(q.price * h.quantity) - SUM(h.purchaseprice * h.quantity) > 0 ORDER BY gain desc");
query.setParameter("accountId", accountId);
BigDecimal totalGains = BigDecimal.ZERO;
totalGains = totalGains.setScale(FinancialUtils.SCALE, FinancialUtils.ROUND);
List<Object[]> result = query.getResultList();
int counter = 0;
// Need to loop over all the aggregated symbols to calculate the totalGain of all the stocks
// but only want the top N stocks returned.
for (Object[] o : result) {
HoldingAggregate summary = new HoldingAggregate();
String symbol = (String) o[0];
BigDecimal gain = (BigDecimal) o[1];
gain = gain.setScale(FinancialUtils.SCALE, FinancialUtils.ROUND);
totalGains = totalGains.add(gain);
if (counter < TOP_N) {
summary.setSymbol(symbol);
summary.setGain(gain);
holdingRollups.add(summary);
}
counter++;
}
holdingSummary.setHoldingsTotalGains(totalGains);
HoldingSummary summary = calculatePercentages(holdingSummary, holdingRollups);
return summary;
}