Untitled

mail@pastecode.io avatar
unknown
java
2 years ago
1.8 kB
2
Indexable
Never
public interface UlohelyNextNRepository extends ScaffoldedRepository {

  default <T, QDSLT extends EntityPathBase<T>> List<T> findNextNSeatsGeneral(
      QDSLT candidate,
      List<Function<QDSLT, NumberExpression<Integer>>> filterExpressions,
      IUlohely starterSeat,
      Integer numberOfSeats,
      final Optional<Function<QDSLT, BooleanExpression>> extraFilter) {
    if (starterSeat == null) {
      return Collections.emptyList();
    }

    List<Integer> filterValues =
        List.of(starterSeat.getSzektor(), starterSeat.getSor(), starterSeat.getSzek());
    BooleanExpression filter = null;
    for (int i = 0; i < filterExpressions.size(); i++) {
      BooleanExpression numFilter =
          filterExpressions.get(i).apply(candidate).gt(filterValues.get(i));
      if (i < filterExpressions.size()) {
        numFilter = numFilter.or(filterExpressions.get(i).apply(candidate).eq(filterValues.get(i)));
      }
      if (filter == null) {
        filter = numFilter;
      } else {
        filter = filter.and(numFilter);
      }
    }

    OrderSpecifier<?>[] orderByExpressions = filterExpressions.stream()
            .map(fe -> new OrderSpecifier<>(Order.ASC, fe.apply(candidate)))
            .toArray(OrderSpecifier<?>[]::new);

    if (filter != null) {
      final BooleanExpression finalFilter = filter;
      filter = extraFilter.map(ef -> finalFilter.and(ef.apply(candidate))).orElse(filter);
      return getQueryDslSupport()
          .select(candidate)
          .from(candidate)
          .where(filter)
          .orderBy(orderByExpressions)
          .stream()
          .limit(numberOfSeats)
          .collect(Collectors.toUnmodifiableList());
    }
    return Collections.emptyList();
  }
}