Search in sources :

Example 36 with Order

use of org.springframework.data.domain.Sort.Order 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 37 with Order

use of org.springframework.data.domain.Sort.Order in project irida by phac-nml.

the class CRUDServiceImplTest method testSearchSortSetProperty.

@Test
@SuppressWarnings("unchecked")
public void testSearchSortSetProperty() {
    int page = 1;
    int size = 1;
    String property = "nonNull";
    Direction order = Direction.ASC;
    Page<IdentifiableTestEntity> idPage = new PageImpl<>(Lists.newArrayList(new IdentifiableTestEntity(), new IdentifiableTestEntity()));
    when(crudRepository.findAll(any(Specification.class), any(Pageable.class))).thenReturn(idPage);
    Page<IdentifiableTestEntity> search = crudService.search(IdentifiableTestEntitySpecification.search(), page, size, order, property);
    assertEquals(2, search.getTotalElements());
    ArgumentCaptor<Pageable> pageArgument = ArgumentCaptor.forClass(Pageable.class);
    verify(crudRepository).findAll(any(Specification.class), pageArgument.capture());
    // ensure a created date sort property is set
    Pageable pagable = pageArgument.getValue();
    Order sort = pagable.getSort().iterator().next();
    assertEquals(property, sort.getProperty());
}
Also used : PageImpl(org.springframework.data.domain.PageImpl) Order(org.springframework.data.domain.Sort.Order) IdentifiableTestEntity(ca.corefacility.bioinformatics.irida.utils.model.IdentifiableTestEntity) Pageable(org.springframework.data.domain.Pageable) IdentifiableTestEntitySpecification(ca.corefacility.bioinformatics.irida.utils.model.IdentifiableTestEntitySpecification) Specification(org.springframework.data.jpa.domain.Specification) Direction(org.springframework.data.domain.Sort.Direction) Test(org.junit.Test)

Example 38 with Order

use of org.springframework.data.domain.Sort.Order in project irida by phac-nml.

the class CRUDServiceImplTest method testSearchSortEmptyArray.

@Test
@SuppressWarnings("unchecked")
public void testSearchSortEmptyArray() {
    int page = 1;
    int size = 1;
    Direction order = Direction.ASC;
    Page<IdentifiableTestEntity> idPage = new PageImpl<>(Lists.newArrayList(new IdentifiableTestEntity(), new IdentifiableTestEntity()));
    when(crudRepository.findAll(any(Specification.class), any(Pageable.class))).thenReturn(idPage);
    Page<IdentifiableTestEntity> search = crudService.search(IdentifiableTestEntitySpecification.search(), page, size, order, new String[0]);
    assertEquals(2, search.getTotalElements());
    ArgumentCaptor<Pageable> pageArgument = ArgumentCaptor.forClass(Pageable.class);
    verify(crudRepository).findAll(any(Specification.class), pageArgument.capture());
    // ensure a created date sort property is set
    Pageable pagable = pageArgument.getValue();
    Order sort = pagable.getSort().iterator().next();
    assertEquals("createdDate", sort.getProperty());
}
Also used : PageImpl(org.springframework.data.domain.PageImpl) Order(org.springframework.data.domain.Sort.Order) IdentifiableTestEntity(ca.corefacility.bioinformatics.irida.utils.model.IdentifiableTestEntity) Pageable(org.springframework.data.domain.Pageable) IdentifiableTestEntitySpecification(ca.corefacility.bioinformatics.irida.utils.model.IdentifiableTestEntitySpecification) Specification(org.springframework.data.jpa.domain.Specification) Direction(org.springframework.data.domain.Sort.Direction) Test(org.junit.Test)

Aggregations

Order (org.springframework.data.domain.Sort.Order)38 Sort (org.springframework.data.domain.Sort)20 PageRequest (org.springframework.data.domain.PageRequest)14 Test (org.junit.Test)13 Direction (org.springframework.data.domain.Sort.Direction)9 PageImpl (org.springframework.data.domain.PageImpl)8 ArrayList (java.util.ArrayList)6 IdentifiableTestEntity (ca.corefacility.bioinformatics.irida.utils.model.IdentifiableTestEntity)4 IdentifiableTestEntitySpecification (ca.corefacility.bioinformatics.irida.utils.model.IdentifiableTestEntitySpecification)4 Pageable (org.springframework.data.domain.Pageable)4 Specification (org.springframework.data.jpa.domain.Specification)4 Document (org.bson.Document)2 OrderSpecifier (com.querydsl.core.types.OrderSpecifier)1 PathBuilderFactory (com.querydsl.core.types.dsl.PathBuilderFactory)1 StringPath (com.querydsl.core.types.dsl.StringPath)1 ResourcePage (eu.bcvsolutions.idm.core.api.rest.domain.ResourcePage)1 WorkflowHistoricProcessInstanceDto (eu.bcvsolutions.idm.core.workflow.model.dto.WorkflowHistoricProcessInstanceDto)1 WorkflowHistoricTaskInstanceDto (eu.bcvsolutions.idm.core.workflow.model.dto.WorkflowHistoricTaskInstanceDto)1 WorkflowProcessInstanceDto (eu.bcvsolutions.idm.core.workflow.model.dto.WorkflowProcessInstanceDto)1 Comparator (java.util.Comparator)1