Search in sources :

Example 1 with KeyValueQuery

use of org.springframework.data.keyvalue.core.query.KeyValueQuery in project spring-data-keyvalue by spring-projects.

the class SpelSortAccessor method resolve.

/*
	 * (non-Javadoc)
	 * @see org.springframework.data.keyvalue.core.SortAccessor#resolve(org.springframework.data.keyvalue.core.query.KeyValueQuery)
	 */
@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
public Comparator<?> resolve(KeyValueQuery<?> query) {
    if (query.getSort().isUnsorted()) {
        return null;
    }
    Optional<Comparator<?>> comparator = Optional.empty();
    for (Order order : query.getSort()) {
        SpelPropertyComparator<Object> spelSort = new SpelPropertyComparator<>(order.getProperty(), parser);
        if (Direction.DESC.equals(order.getDirection())) {
            spelSort.desc();
            if (!NullHandling.NATIVE.equals(order.getNullHandling())) {
                spelSort = NullHandling.NULLS_FIRST.equals(order.getNullHandling()) ? spelSort.nullsFirst() : spelSort.nullsLast();
            }
        }
        if (!comparator.isPresent()) {
            comparator = Optional.of(spelSort);
        } else {
            SpelPropertyComparator<Object> spelSortToUse = spelSort;
            comparator = comparator.map(it -> it.thenComparing(spelSortToUse));
        }
    }
    return comparator.orElseThrow(() -> new IllegalStateException("No sort definitions have been added to this CompoundComparator to compare"));
}
Also used : Order(org.springframework.data.domain.Sort.Order) KeyValueQuery(org.springframework.data.keyvalue.core.query.KeyValueQuery) Order(org.springframework.data.domain.Sort.Order) NullHandling(org.springframework.data.domain.Sort.NullHandling) Optional(java.util.Optional) SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) Direction(org.springframework.data.domain.Sort.Direction) Comparator(java.util.Comparator) Assert(org.springframework.util.Assert) Comparator(java.util.Comparator)

Example 2 with KeyValueQuery

use of org.springframework.data.keyvalue.core.query.KeyValueQuery in project spring-data-keyvalue by spring-projects.

the class KeyValuePartTreeQuery method prepareQuery.

@SuppressWarnings({ "rawtypes", "unchecked" })
protected KeyValueQuery<?> prepareQuery(KeyValueQuery<?> instance, Object[] parameters) {
    ParametersParameterAccessor accessor = new ParametersParameterAccessor(getQueryMethod().getParameters(), parameters);
    Object criteria = instance.getCriteria();
    if (criteria instanceof SpelCriteria || criteria instanceof SpelExpression) {
        SpelExpression spelExpression = getSpelExpression(criteria);
        EvaluationContext context = this.evaluationContextProvider.getEvaluationContext(getQueryMethod().getParameters(), parameters);
        criteria = new SpelCriteria(spelExpression, context);
    }
    KeyValueQuery<?> query = new KeyValueQuery(criteria);
    Pageable pageable = accessor.getPageable();
    Sort sort = accessor.getSort();
    query.setOffset(pageable.toOptional().map(Pageable::getOffset).orElse(-1L));
    if (pageable.isPaged()) {
        query.setRows(pageable.getPageSize());
    } else if (instance.getRows() >= 0) {
        query.setRows(instance.getRows());
    }
    query.setSort(sort.isUnsorted() ? instance.getSort() : sort);
    return query;
}
Also used : SpelCriteria(org.springframework.data.keyvalue.core.SpelCriteria) Pageable(org.springframework.data.domain.Pageable) ParametersParameterAccessor(org.springframework.data.repository.query.ParametersParameterAccessor) SpelExpression(org.springframework.expression.spel.standard.SpelExpression) Sort(org.springframework.data.domain.Sort) EvaluationContext(org.springframework.expression.EvaluationContext) KeyValueQuery(org.springframework.data.keyvalue.core.query.KeyValueQuery)

Aggregations

KeyValueQuery (org.springframework.data.keyvalue.core.query.KeyValueQuery)2 Comparator (java.util.Comparator)1 Optional (java.util.Optional)1 Pageable (org.springframework.data.domain.Pageable)1 Sort (org.springframework.data.domain.Sort)1 Direction (org.springframework.data.domain.Sort.Direction)1 NullHandling (org.springframework.data.domain.Sort.NullHandling)1 Order (org.springframework.data.domain.Sort.Order)1 SpelCriteria (org.springframework.data.keyvalue.core.SpelCriteria)1 ParametersParameterAccessor (org.springframework.data.repository.query.ParametersParameterAccessor)1 EvaluationContext (org.springframework.expression.EvaluationContext)1 SpelExpression (org.springframework.expression.spel.standard.SpelExpression)1 SpelExpressionParser (org.springframework.expression.spel.standard.SpelExpressionParser)1 Assert (org.springframework.util.Assert)1