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 boolean isSortByMetric(ComponentTreeWsRequest wsRequest) {
   requireNonNull(wsRequest.getSort());
   return wsRequest.getSort().contains(METRIC_SORT)
       || wsRequest.getSort().contains(METRIC_PERIOD_SORT);
 }