private InvDatasetImpl getLeafDataset(InvDatasetImpl ds) {
    if (ds == null) return null;
    System.out.println("getLeafDataset " + ds.getFullName());
    List nestedList = ds.getDatasets();
    for (int i = 0; i < nestedList.size(); i++) {
      InvDatasetImpl nestedDS = (InvDatasetImpl) nestedList.get(i);

      if (nestedDS.hasAccess() && !nestedDS.hasNestedDatasets()) {
        InvDatasetImpl result =
            chooseLeafDataset(nestedList); // this assumes that they are uniform !!
        return (result.hasAccess()) ? result : nestedDS;
      }

      // depth first
      InvDatasetImpl leaf = getLeafDataset(nestedDS);
      if (leaf != null) return leaf;
    }

    return null;
  }