private void mergeClient(
      final JLGClientData clientSavingsData, List<JLGClientData> clientsLoanData) {
    final int index = clientsLoanData.indexOf(clientSavingsData);

    if (index < 0) return;

    JLGClientData clientLoanData = clientsLoanData.get(index);
    clientLoanData.setSavings(clientSavingsData.getSavings());
  }
 private Collection<SavingsProductData> retrieveSavingsProducts(
     Collection<JLGGroupData> groupsWithSavingsData) {
   List<SavingsProductData> savingsProducts = new ArrayList<>();
   for (JLGGroupData groupSavingsData : groupsWithSavingsData) {
     Collection<JLGClientData> clientsSavingsData = groupSavingsData.getClients();
     for (JLGClientData clientSavingsData : clientsSavingsData) {
       Collection<SavingsDueData> savingsDatas = clientSavingsData.getSavings();
       for (SavingsDueData savingsDueData : savingsDatas) {
         final SavingsProductData savingsProduct =
             SavingsProductData.lookup(savingsDueData.productId(), savingsDueData.productName());
         if (!savingsProducts.contains(savingsProduct)) {
           savingsProducts.add(savingsProduct);
         }
       }
     }
   }
   return savingsProducts;
 }
    @Override
    public JLGClientData mapRow(ResultSet rs, @SuppressWarnings("unused") int rowNum)
        throws SQLException {

      final String clientName = rs.getString("clientName");
      final Long clientId = JdbcSupport.getLong(rs, "clientId");
      // final Integer attendanceTypeId = rs.getInt("attendanceTypeId");
      // final EnumOptionData attendanceType =
      // AttendanceEnumerations.attendanceType(attendanceTypeId);
      final EnumOptionData attendanceType = null;

      return JLGClientData.instance(clientId, clientName, attendanceType);
    }
    public JLGClientData mapRowData(ResultSet rs, int rowNum) throws SQLException {

      List<SavingsDueData> savings = new ArrayList<>();

      JLGClientData client = this.mapRow(rs, rowNum);
      final Long previousClientId = client.getClientId();

      // first savings row of new client record
      SavingsDueData saving = savingsDueDataMapper.mapRow(rs, rowNum);
      savings.add(saving);

      while (rs.next()) {
        final Long clientId = JdbcSupport.getLong(rs, "clientId");
        if (previousClientId != null && clientId.compareTo(previousClientId) != 0) {
          // client id changes then return for next client data
          return JLGClientData.withSavings(client, savings);
        }
        saving = savingsDueDataMapper.mapRow(rs, rowNum);
        savings.add(saving);
      }
      return JLGClientData.withSavings(client, savings);
    }
  /*
   * Reads all the loans which are due for disbursement or collection and
   * builds hierarchical data structure for collections sheet with hierarchy
   * Groups >> Clients >> Loans.
   */
  @SuppressWarnings("null")
  private JLGCollectionSheetData buildJLGCollectionSheet(
      final LocalDate dueDate,
      final Collection<JLGCollectionSheetFlatData> jlgCollectionSheetFlatData) {

    boolean firstTime = true;
    Long prevGroupId = null;
    Long prevClientId = null;

    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,
              this.attendanceDropdownReadPlatformService.retrieveAttendanceTypeOptions());
    }

    return jlgCollectionSheetData;
  }