All Downloads are FREE. Search and download functionalities are using the official Maven repository.

pro.taskana.common.rest.QuerySortParameter Maven / Gradle / Ivy

The newest version!
package pro.taskana.common.rest;

import com.fasterxml.jackson.annotation.JsonProperty;
import io.swagger.v3.oas.annotations.media.Schema;
import java.util.List;
import pro.taskana.common.api.BaseQuery;
import pro.taskana.common.api.BaseQuery.SortDirection;
import pro.taskana.common.api.exceptions.InvalidArgumentException;

public class QuerySortParameter, S extends QuerySortBy>
    implements QueryParameter {

  @Schema(
      name = "sort-by",
      description =
          "Sort the result by a given field. Multiple sort values can be declared. When the "
              + "primary sort value is the same, the second one will be used.")
  @JsonProperty("sort-by")
  private final List sortBy;

  @Schema(
      name = "order",
      description =
          "The order direction for each sort value. This value requires the use of 'sort-by'. The"
              + " amount of sort-by and order declarations have to match. Alternatively the value"
              + " can be omitted. If done so the default sort order (ASCENDING) will be applied to"
              + " every sort-by value.")
  @JsonProperty("order")
  private final List order;

  // this is only necessary because spring-auto-rest-docs can't resolve Enum[] data types.
  // See https://github.com/ScaCap/spring-auto-restdocs/issues/423
  public QuerySortParameter(List sortBy, List order)
      throws InvalidArgumentException {
    this.sortBy = sortBy;
    this.order = order;
    verifyNotOnlyOrderByExists(sortBy, order);
    verifyAmountOfSortByAndOrderByMatches(sortBy, order);
  }

  @Override
  public Void apply(Q query) {
    if (sortBy != null) {
      for (int i = 0; i < sortBy.size(); i++) {
        SortDirection sortDirection =
            order == null || order.isEmpty() ? SortDirection.ASCENDING : order.get(i);
        sortBy.get(i).applySortByForQuery(query, sortDirection);
      }
    }
    return null;
  }

  // this method is only static because there exists no query for the task comment entity
  public static  void verifyAmountOfSortByAndOrderByMatches(
      List sortBy, List order) throws InvalidArgumentException {
    if (sortBy != null && order != null && sortBy.size() != order.size() && !order.isEmpty()) {
      throw new InvalidArgumentException(
          "The amount of 'sort-by' and 'order' does not match. "
              + "Please specify an 'order' for each 'sort-by' or no 'order' parameters at all.");
    }
  }

  // this method is only static because there exists no query for the task comment entity
  public static  void verifyNotOnlyOrderByExists(List sortBy, List order)
      throws InvalidArgumentException {
    if (sortBy == null && order != null) {
      throw new InvalidArgumentException(
          "Only 'order' parameters were provided. Please also provide 'sort-by' parameter(s)");
    }
  }

  @JsonProperty("sort-by")
  public List getSortBy() {
    return sortBy;
  }

  public List getOrder() {
    return order;
  }
}