final List<JLGGroupData> jlgGroupsData = new ArrayList<>();
List<JLGClientData> clientsData = new ArrayList<>();
List<LoanDueData> loansDueData = new ArrayList<>();
JLGCollectionSheetData jlgCollectionSheetData = null;
JLGCollectionSheetFlatData prevCollectioSheetFlatData = null;
JLGCollectionSheetFlatData corrCollectioSheetFlatData = null;
final Set<LoanProductData> loanProducts = new HashSet<>();
if (jlgCollectionSheetFlatData != null) {
for (final JLGCollectionSheetFlatData collectionSheetFlatData : jlgCollectionSheetFlatData) {
if (collectionSheetFlatData.getProductId() != null) {
loanProducts.add(LoanProductData.lookupWithCurrency(collectionSheetFlatData.getProductId(),
collectionSheetFlatData.getProductShortName(), collectionSheetFlatData.getCurrency()));
}
corrCollectioSheetFlatData = collectionSheetFlatData;
if (firstTime || collectionSheetFlatData.getGroupId().equals(prevGroupId)) {
if (firstTime || collectionSheetFlatData.getClientId().equals(prevClientId)) {
if (collectionSheetFlatData.getLoanId() != null) {
loansDueData.add(collectionSheetFlatData.getLoanDueData());
}
} else {
final JLGClientData clientData = prevCollectioSheetFlatData.getClientData();
clientData.setLoans(loansDueData);
clientsData.add(clientData);
loansDueData = new ArrayList<>();
if (collectionSheetFlatData.getLoanId() != null) {
loansDueData.add(collectionSheetFlatData.getLoanDueData());
}
}
} else {
final JLGClientData clientData = prevCollectioSheetFlatData.getClientData();
clientData.setLoans(loansDueData);
clientsData.add(clientData);
final JLGGroupData jlgGroupData = prevCollectioSheetFlatData.getJLGGroupData();
jlgGroupData.setClients(clientsData);
jlgGroupsData.add(jlgGroupData);
loansDueData = new ArrayList<>();
clientsData = new ArrayList<>();
if (collectionSheetFlatData.getLoanId() != null) {
loansDueData.add(collectionSheetFlatData.getLoanDueData());
}
}
prevClientId = collectionSheetFlatData.getClientId();
prevGroupId = collectionSheetFlatData.getGroupId();
prevCollectioSheetFlatData = collectionSheetFlatData;
firstTime = false;
}
// FIXME Need to check last loan is added under previous
// client/group or new client / previous group or new client / new
// group
if (corrCollectioSheetFlatData != null) {
final JLGClientData lastClientData = corrCollectioSheetFlatData.getClientData();
lastClientData.setLoans(loansDueData);
clientsData.add(lastClientData);
final JLGGroupData jlgGroupData = corrCollectioSheetFlatData.getJLGGroupData();
jlgGroupData.setClients(clientsData);
jlgGroupsData.add(jlgGroupData);
}
jlgCollectionSheetData = JLGCollectionSheetData.instance(dueDate, loanProducts, jlgGroupsData,