Search in sources :

Example 1 with KeysetPageable

use of com.blazebit.persistence.deltaspike.data.KeysetPageable in project blaze-persistence by Blazebit.

the class AbstractEntityViewAwareRepositoryHandler method findAll.

@Override
public Page<V> findAll(Specification<E> specification, Pageable pageable) {
    CriteriaBuilder<E> cb;
    if (specification == null) {
        cb = createCriteriaBuilder();
    } else {
        BlazeCriteriaBuilder blazeCriteriaBuilder = BlazeCriteria.get(criteriaBuilderFactory());
        BlazeCriteriaQuery<?> query = blazeCriteriaBuilder.createQuery(entityClass());
        Root queryRoot = query.from(entityClass());
        Predicate predicate = specification.toPredicate(queryRoot, query, blazeCriteriaBuilder);
        if (predicate != null) {
            if (query.getRestriction() == null) {
                query.where(predicate);
            } else {
                query.where(query.getRestriction(), predicate);
            }
        }
        cb = (CriteriaBuilder<E>) query.createCriteriaBuilder(entityManager());
    }
    String[] fetches = getFetches();
    if (fetches.length != 0) {
        cb.fetch(fetches);
    }
    boolean withCountQuery = true;
    boolean withKeysetExtraction = false;
    boolean withExtractAllKeysets = false;
    TypedQuery<V> query;
    if (viewClass() == null) {
        PaginatedCriteriaBuilder<V> pcb;
        if (pageable instanceof KeysetPageable) {
            KeysetPageable keysetPageable = (KeysetPageable) pageable;
            pcb = (PaginatedCriteriaBuilder<V>) cb.page(keysetPageable.getKeysetPage(), pageable.getOffset(), pageable.getPageSize());
            withCountQuery = keysetPageable.isWithCountQuery();
            withKeysetExtraction = true;
            withExtractAllKeysets = keysetPageable.isWithExtractAllKeysets();
        } else {
            pcb = (PaginatedCriteriaBuilder<V>) cb.page(pageable.getOffset(), pageable.getPageSize());
        }
        QueryBuilderUtils.applySort(pageable.getSort(), pcb);
        pcb.withCountQuery(withCountQuery);
        pcb.withKeysetExtraction(withKeysetExtraction);
        pcb.withExtractAllKeysets(withExtractAllKeysets);
        query = pcb.getQuery();
    } else {
        EntityViewSetting<V, PaginatedCriteriaBuilder<V>> setting = EntityViewSetting.create(viewClass(), pageable.getOffset(), pageable.getPageSize());
        if (pageable instanceof KeysetPageable) {
            KeysetPageable keysetPageable = (KeysetPageable) pageable;
            setting.withKeysetPage(keysetPageable.getKeysetPage());
            withCountQuery = keysetPageable.isWithCountQuery();
            withKeysetExtraction = true;
            withExtractAllKeysets = keysetPageable.isWithExtractAllKeysets();
        }
        QueryBuilderUtils.applySort(pageable.getSort(), setting);
        PaginatedCriteriaBuilder<V> pcb = applySetting(setting, cb);
        pcb.withCountQuery(withCountQuery);
        pcb.withKeysetExtraction(withKeysetExtraction);
        pcb.withExtractAllKeysets(withExtractAllKeysets);
        query = pcb.getQuery();
    }
    applyQueryHints(query, fetches.length == 0);
    PagedList<V> resultList = (PagedList<V>) query.getResultList();
    return new KeysetAwarePageImpl<>(resultList, pageable);
}
Also used : Root(javax.persistence.criteria.Root) PagedList(com.blazebit.persistence.PagedList) BlazeCriteriaBuilder(com.blazebit.persistence.criteria.BlazeCriteriaBuilder) PaginatedCriteriaBuilder(com.blazebit.persistence.PaginatedCriteriaBuilder) Predicate(javax.persistence.criteria.Predicate) KeysetPageable(com.blazebit.persistence.deltaspike.data.KeysetPageable)

Example 2 with KeysetPageable

use of com.blazebit.persistence.deltaspike.data.KeysetPageable in project blaze-persistence by Blazebit.

the class FullEntityViewRepositoryTest method testFindByNameKeysetPaginated.

@Test
@Category(NoH2.class)
public void testFindByNameKeysetPaginated() {
    // we do not test DeltaSpike's findAll(int, int) method here because its results are non-deterministic
    Page<PersonView> actual = personViewRepository.findByNameLike("John %", new KeysetPageRequest(null, new PageRequest(0, 1, Sort.Direction.ASC, "id")));
    assertTrue(actual instanceof KeysetAwarePage<?>);
    assertEquals(1, actual.getNumberOfElements());
    assertEquals("John Doe", actual.getContent().get(0).getName());
    actual = personViewRepository.findByNameLike("John %", (KeysetPageable) actual.nextPageable());
    assertEquals(1, actual.getNumberOfElements());
    assertEquals("John Smith", actual.getContent().get(0).getName());
}
Also used : KeysetPageRequest(com.blazebit.persistence.deltaspike.data.KeysetPageRequest) PageRequest(com.blazebit.persistence.deltaspike.data.PageRequest) KeysetPageRequest(com.blazebit.persistence.deltaspike.data.KeysetPageRequest) KeysetPageable(com.blazebit.persistence.deltaspike.data.KeysetPageable) PersonView(com.blazebit.persistence.deltaspike.data.testsuite.view.PersonView) Category(org.junit.experimental.categories.Category) Test(org.junit.Test)

Example 3 with KeysetPageable

use of com.blazebit.persistence.deltaspike.data.KeysetPageable in project blaze-persistence by Blazebit.

the class FullEntityViewRepositoryTest method testFindByNameKeysetPaginatedEntity.

@Test
public void testFindByNameKeysetPaginatedEntity() {
    // we do not test DeltaSpike's findAll(int, int) method here because its results are non-deterministic
    Page<Person> actual = personRepository.findByNameLike("John %", new KeysetPageRequest(null, new PageRequest(0, 1, Sort.Direction.ASC, "id")));
    assertTrue(actual instanceof KeysetAwarePage<?>);
    assertEquals(1, actual.getNumberOfElements());
    assertEquals("John Doe", actual.getContent().get(0).getName());
    actual = personRepository.findByNameLike("John %", (KeysetPageable) actual.nextPageable());
    assertEquals(1, actual.getNumberOfElements());
    assertEquals("John Smith", actual.getContent().get(0).getName());
}
Also used : KeysetPageRequest(com.blazebit.persistence.deltaspike.data.KeysetPageRequest) PageRequest(com.blazebit.persistence.deltaspike.data.PageRequest) KeysetPageRequest(com.blazebit.persistence.deltaspike.data.KeysetPageRequest) KeysetPageable(com.blazebit.persistence.deltaspike.data.KeysetPageable) Person(com.blazebit.persistence.deltaspike.data.testsuite.entity.Person) Test(org.junit.Test)

Aggregations

KeysetPageable (com.blazebit.persistence.deltaspike.data.KeysetPageable)3 KeysetPageRequest (com.blazebit.persistence.deltaspike.data.KeysetPageRequest)2 PageRequest (com.blazebit.persistence.deltaspike.data.PageRequest)2 Test (org.junit.Test)2 PagedList (com.blazebit.persistence.PagedList)1 PaginatedCriteriaBuilder (com.blazebit.persistence.PaginatedCriteriaBuilder)1 BlazeCriteriaBuilder (com.blazebit.persistence.criteria.BlazeCriteriaBuilder)1 Person (com.blazebit.persistence.deltaspike.data.testsuite.entity.Person)1 PersonView (com.blazebit.persistence.deltaspike.data.testsuite.view.PersonView)1 Predicate (javax.persistence.criteria.Predicate)1 Root (javax.persistence.criteria.Root)1 Category (org.junit.experimental.categories.Category)1