/** Fetches a match table from a PQL statement and writes it to a file. */
  public static String fetchMatchTable(
      StatementBuilder pqlStatementBuilder,
      PublisherQueryLanguageServiceInterface pqlService,
      String fileName)
      throws Exception {
    // Default for result sets.
    ResultSet combinedResultSet = null;
    ResultSet resultSet;

    do {
      resultSet = pqlService.select(pqlStatementBuilder.toStatement());

      // Combine result sets with previous ones.
      combinedResultSet =
          combinedResultSet == null
              ? resultSet
              : Pql.combineResultSets(combinedResultSet, resultSet);

      pqlStatementBuilder.increaseOffsetBy(StatementBuilder.SUGGESTED_PAGE_LIMIT);
    } while (resultSet.getRows() != null && resultSet.getRows().length > 0);

    // Change to your file location.
    String filePath = File.createTempFile(fileName, ".csv").toString();

    // Write the result set to a CSV.
    CsvFiles.writeCsv(Pql.resultSetToStringArrayList(combinedResultSet), filePath);

    return filePath;
  }
  public static void runExample(DfpServices dfpServices, DfpSession session) throws Exception {
    // Get the ActivityGroupService.
    ActivityGroupServiceInterface activityGroupService =
        dfpServices.get(session, ActivityGroupServiceInterface.class);

    // Create a statement to only select active activity groups.
    StatementBuilder statementBuilder =
        new StatementBuilder()
            .where("status = :status")
            .orderBy("id ASC")
            .limit(StatementBuilder.SUGGESTED_PAGE_LIMIT)
            .withBindVariableValue("status", ActivityGroupStatus.ACTIVE.toString());

    // Default for total result set size.
    int totalResultSetSize = 0;

    do {
      // Get activity groups by statement.
      ActivityGroupPage page =
          activityGroupService.getActivityGroupsByStatement(statementBuilder.toStatement());

      if (page.getResults() != null) {
        totalResultSetSize = page.getTotalResultSetSize();
        int i = page.getStartIndex();
        for (ActivityGroup activityGroup : page.getResults()) {
          System.out.printf(
              "%d) Activity group with ID \"%d\" and name \"%s\" was found.\n",
              i++, activityGroup.getId(), activityGroup.getName());
        }
      }

      statementBuilder.increaseOffsetBy(StatementBuilder.SUGGESTED_PAGE_LIMIT);
    } while (statementBuilder.getOffset() < totalResultSetSize);

    System.out.printf("Number of results found: %d\n", totalResultSetSize);
  }