AccountEntryPage fetchAccountEntries()

in api/src/main/java/org/apache/fineract/cn/accounting/api/v1/client/LedgerManager.java [205:243]


  AccountEntryPage fetchAccountEntries(@PathVariable("identifier") final String identifier,
                                       @RequestParam(value = "dateRange", required = false) final String dateRange,
                                       @RequestParam(value = "message", required = false) final String message,
                                       @RequestParam(value = "pageIndex", required = false) final Integer pageIndex,
                                       @RequestParam(value = "size", required = false) final Integer size,
                                       @RequestParam(value = "sortColumn", required = false) final String sortColumn,
                                       @RequestParam(value = "sortDirection", required = false) final String sortDirection);

  // These helper functions are implemented here rather than in the client because it is easier to test
  // and mock if it's part of the accounting interface, rather than part of the client calling it.
    default Stream<Account> streamAccountsOfLedger(
      final String ledgerIdentifer,
      final String sortDirection) {
    final AccountPage firstPage = this.fetchAccountsOfLedger(
        ledgerIdentifer,
        0,
        10,
        null,
        null);
    final Integer pageCount = firstPage.getTotalPages();
    switch (sortDirection) {
      case "ASC":
        // Sort column is always date and order always ascending so that the order and adjacency of account
        // entries is always stable. This has the advantage that the set of account entries included in the
        // stream is set the moment the first call to fetchAccountEntries (above) is made.
        return Stream.iterate(0, (i) -> i + 1).limit(pageCount)
            .map(i -> this.fetchAccountsOfLedger(ledgerIdentifer, i, 10, "lastModifiedOn", "ASC"))
            .flatMap(pageI -> pageI.getAccounts().stream());
      case "DESC":
        return Stream.iterate(pageCount - 1, (i) -> i - 1).limit(pageCount)
            .map(i -> this.fetchAccountsOfLedger(ledgerIdentifer, i, 10, "lastModifiedOn", "DESC"))
            .flatMap(pageI -> {
              Collections.reverse(pageI.getAccounts());
              return pageI.getAccounts().stream();
            });
      default:
        throw new IllegalArgumentException();
    }
  }