private ComponentTreeQuery toComponentTreeQuery(
      ComponentTreeWsRequest wsRequest, ComponentDto baseComponent) {
    List<String> childrenQualifiers = childrenQualifiers(wsRequest, baseComponent.qualifier());

    List<String> sortsWithoutMetricSort =
        newArrayList(Iterables.filter(wsRequest.getSort(), IsNotMetricSort.INSTANCE));
    sortsWithoutMetricSort =
        sortsWithoutMetricSort.isEmpty() ? singletonList(NAME_SORT) : sortsWithoutMetricSort;

    ComponentTreeQuery.Builder dbQuery =
        ComponentTreeQuery.builder()
            .setBaseUuid(baseComponent.uuid())
            .setPage(wsRequest.getPage())
            .setPageSize(wsRequest.getPageSize())
            .setSortFields(sortsWithoutMetricSort)
            .setAsc(wsRequest.getAsc());

    if (wsRequest.getQuery() != null) {
      dbQuery.setNameOrKeyQuery(wsRequest.getQuery());
    }
    if (childrenQualifiers != null) {
      dbQuery.setQualifiers(childrenQualifiers);
    }
    // load all components if we must sort by metric value
    if (isSortByMetric(wsRequest)) {
      dbQuery.setPage(1);
      dbQuery.setPageSize(Integer.MAX_VALUE);
    }

    return dbQuery.build();
  }
  private static List<ComponentDto> paginateComponents(
      List<ComponentDto> components, ComponentTreeWsRequest wsRequest) {
    if (!isSortByMetric(wsRequest)) {
      return components;
    }

    return from(components)
        .skip(offset(wsRequest.getPage(), wsRequest.getPageSize()))
        .limit(wsRequest.getPageSize())
        .toList();
  }