/** * Converts the given {@link Sort} to {@link OrderSpecifier}. * * @param sort * @return */ protected List<OrderSpecifier<?>> toOrderSpecifiers(Sort sort) { if (sort instanceof QSort) { return ((QSort) sort).getOrderSpecifiers(); } return sort.stream().map(this::toOrder).collect(Collectors.toList()); }
/** * Adds a {@link Sort} to the {@link Query} instance. * * @param sort * @return */ public Query with(Sort sort) { Assert.notNull(sort, "Sort must not be null!"); if (sort.isUnsorted()) { return this; } sort.stream().filter(Order::isIgnoreCase).findFirst().ifPresent(it -> { throw new IllegalArgumentException(String.format("Given sort contained an Order for %s with ignore case! " + "MongoDB does not support sorting ignoring case currently!", it.getProperty())); }); this.sort = this.sort.and(sort); return this; }
/** * @return the sort {@link Document}. */ public Document getSortObject() { if (this.sort.isUnsorted()) { return new Document(); } Document document = new Document(); this.sort.stream()// .forEach(order -> document.put(order.getProperty(), order.isAscending() ? 1 : -1)); return document; }
private String getSortOrder(Sort sort) { return sort.stream() .map(order -> order.getProperty() + " " + order.getDirection()) .collect(Collectors.joining(", ")); }
/** * Applies the given {@link Sort} to the given {@link SpringDataMongodbQuery}. * * @param query * @param sort * @return */ private SpringDataMongodbQuery<T> applySorting(SpringDataMongodbQuery<T> query, Sort sort) { // TODO: find better solution than instanceof check if (sort instanceof QSort) { List<OrderSpecifier<?>> orderSpecifiers = ((QSort) sort).getOrderSpecifiers(); query.orderBy(orderSpecifiers.toArray(new OrderSpecifier<?>[orderSpecifiers.size()])); return query; } sort.stream().map(this::toOrder).forEach(query::orderBy); return query; }
/** * Adds a {@link Sort} to the {@link Query} instance. * * @param sort * @return */ public Query with(Sort sort) { Assert.notNull(sort, "Sort must not be null!"); if (sort.isUnsorted()) { return this; } sort.stream().filter(Order::isIgnoreCase).findFirst().ifPresent(it -> { throw new IllegalArgumentException(String.format("Given sort contained an Order for %s with ignore case! " + "MongoDB does not support sorting ignoring case currently!", it.getProperty())); }); this.sort = this.sort.and(sort); return this; }
/** * @return the sort {@link Document}. */ public Document getSortObject() { if (this.sort.isUnsorted()) { return new Document(); } Document document = new Document(); this.sort.stream()// .forEach(order -> document.put(order.getProperty(), order.isAscending() ? 1 : -1)); return document; }
private String generateQuerySort(@NonNull Sort sort) { if (sort.isUnsorted()) { return ""; } final String queryTail = "ORDER BY"; final List<String> subjects = sort.stream().map(this::getParameter).collect(Collectors.toList()); return queryTail + " " + String.join(",", subjects); }
protected String buildStandardOrderBy(Sort sort) { if (null == sort || sort.isUnsorted()) { return ""; } final Map<String, String> map = findNormalColumns().stream() .collect(Collectors.toMap(p -> p.getName(), p -> p.getColumnName())); return " order by " + sort.stream() .map(order -> map.getOrDefault(order.getProperty(), order.getProperty()) + ' ' + order.getDirection().name().toLowerCase()) .collect(Collectors.joining(",")); }
public static void applyQueryOptions(StructuredQuery.Builder builder, DatastoreQueryOptions queryOptions, DatastorePersistentEntity<?> persistentEntity) { if (queryOptions == null) { return; } if (queryOptions.getLimit() != null) { builder.setLimit(queryOptions.getLimit()); } if (queryOptions.getOffset() != null) { builder.setOffset(queryOptions.getOffset()); } if (queryOptions.getSort() != null && persistentEntity != null) { queryOptions.getSort().stream() .map((order) -> createOrderBy(persistentEntity, order)) .forEachOrdered((orderBy) -> builder.addOrderBy(orderBy)); } }
public static void applyQueryOptions(StructuredQuery.Builder builder, DatastoreQueryOptions queryOptions, DatastorePersistentEntity<?> persistentEntity) { if (queryOptions == null) { return; } if (queryOptions.getLimit() != null) { builder.setLimit(queryOptions.getLimit()); } if (queryOptions.getOffset() != null) { builder.setOffset(queryOptions.getOffset()); } if (queryOptions.getSort() != null && persistentEntity != null) { queryOptions.getSort().stream() .map((order) -> createOrderBy(persistentEntity, order)) .forEachOrdered((orderBy) -> builder.addOrderBy(orderBy)); } }