Search in sources :

Example 1 with PageRequest

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

the class FullEntityViewRepositoryTest method testFindSliceKeyset.

@Test
@Category(NoH2.class)
public void testFindSliceKeyset() {
    Slice<PersonView> actual = personViewRepository.findByIdIsNotNull(new KeysetPageRequest(null, new PageRequest(0, 1, Sort.Direction.ASC, "id")));
    assertEquals(1, actual.getSize());
    assertEquals("Mother", actual.getContent().get(0).getName());
    actual = personViewRepository.findByIdIsNotNull(actual.nextPageable());
    assertEquals(1, actual.getSize());
    assertEquals("John Doe", 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) PersonView(com.blazebit.persistence.deltaspike.data.testsuite.view.PersonView) Category(org.junit.experimental.categories.Category) Test(org.junit.Test)

Example 2 with PageRequest

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

the class FullEntityViewRepositoryTest method testFindSliceKeysetEntity.

@Test
public void testFindSliceKeysetEntity() {
    Slice<Person> actual = personRepository.findByIdIsNotNull(new KeysetPageRequest(null, new PageRequest(0, 1, Sort.Direction.ASC, "id")));
    assertEquals(1, actual.getSize());
    assertEquals("Mother", actual.getContent().get(0).getName());
    actual = personRepository.findByIdIsNotNull(actual.nextPageable());
    assertEquals(1, actual.getSize());
    assertEquals("John Doe", 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) Person(com.blazebit.persistence.deltaspike.data.testsuite.entity.Person) Test(org.junit.Test)

Example 3 with PageRequest

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

the class KeysetPageableParamConverter method fromString.

@Override
public Pageable fromString(String value) {
    MultivaluedMap<String, String> queryParameters = requestUriInfo.getQueryParameters();
    Pageable defaultOrFallback = pageableConfiguration.getFallbackPageable();
    String offsetString = queryParameters.getFirst(pageableConfiguration.getPrefix() + pageableConfiguration.getOffsetParameterName());
    String pageString = queryParameters.getFirst(pageableConfiguration.getPrefix() + pageableConfiguration.getPageParameterName());
    String pageSizeString = queryParameters.getFirst(pageableConfiguration.getPrefix() + pageableConfiguration.getSizeParameterName());
    boolean pageAndSizeGiven = (!StringUtils.isEmpty(pageString) || !StringUtils.isEmpty(offsetString)) && !StringUtils.isEmpty(pageSizeString);
    if (!pageAndSizeGiven && defaultOrFallback == null) {
        return null;
    }
    int maxPageSize = pageableConfiguration.getMaxPageSize();
    int pageSize = !StringUtils.isEmpty(pageSizeString) ? parseAndApplyBoundaries(pageSizeString, maxPageSize, false) : defaultOrFallback.getPageSize();
    // Limit lower bound
    pageSize = pageSize < 1 ? defaultOrFallback.getPageSize() : pageSize;
    // Limit upper bound
    pageSize = pageSize > maxPageSize ? maxPageSize : pageSize;
    int offset;
    if (!StringUtils.isEmpty(offsetString)) {
        offset = parseAndApplyBoundaries(pageString, Integer.MAX_VALUE, false);
    } else if (!StringUtils.isEmpty(pageString)) {
        offset = pageSize * parseAndApplyBoundaries(pageString, Integer.MAX_VALUE, true);
    } else {
        offset = pageSize * defaultOrFallback.getPageNumber();
    }
    Sort sort = resolveSort(queryParameters.get(pageableConfiguration.getSortParameterName()));
    // Default if necessary and default configured
    sort = sort == null && defaultOrFallback != null ? defaultOrFallback.getSort() : sort;
    KeysetPage keysetPage = null;
    Iterator<Sort.Order> iterator;
    if (keysetClass != null) {
        if (sort != null && (iterator = sort.iterator()).hasNext()) {
            String previousOffsetString = queryParameters.getFirst(keysetPageableConfiguration.getPrefix() + keysetPageableConfiguration.getPreviousOffsetParameterName());
            String previousPageString = queryParameters.getFirst(keysetPageableConfiguration.getPrefix() + keysetPageableConfiguration.getPreviousPageParameterName());
            if (!StringUtils.isEmpty(previousOffsetString) || !StringUtils.isEmpty(previousPageString)) {
                String previousPageSizeString = queryParameters.getFirst(keysetPageableConfiguration.getPrefix() + keysetPageableConfiguration.getPreviousSizeParameterName());
                int previousPageSize = StringUtils.isEmpty(previousPageSizeString) ? pageSize : parseAndApplyBoundaries(previousPageSizeString, maxPageSize, false);
                int previousOffset;
                if (!StringUtils.isEmpty(previousOffsetString)) {
                    previousOffset = parseAndApplyBoundaries(previousOffsetString, Integer.MAX_VALUE, false);
                } else {
                    int previousPage = parseAndApplyBoundaries(previousPageString, Integer.MAX_VALUE, true);
                    previousOffset = previousPage * previousPageSize;
                }
                String lowestString = queryParameters.getFirst(keysetPageableConfiguration.getPrefix() + keysetPageableConfiguration.getLowestParameterName());
                String highestString = queryParameters.getFirst(keysetPageableConfiguration.getPrefix() + keysetPageableConfiguration.getHighestParameterName());
                if (!StringUtils.isEmpty(lowestString) && !StringUtils.isEmpty(highestString)) {
                    List<Serializable> lowest = new ArrayList<>();
                    List<Serializable> highest = new ArrayList<>();
                    JsonNode lowestObject;
                    JsonNode highestObject;
                    try {
                        lowestObject = mapper.readTree(lowestString);
                    } catch (IOException ex) {
                        throw new IllegalArgumentException("Invalid lowest object!", ex);
                    }
                    try {
                        highestObject = mapper.readTree(highestString);
                    } catch (IOException ex) {
                        throw new IllegalArgumentException("Invalid highest object!", ex);
                    }
                    while (iterator.hasNext()) {
                        Sort.Order o = iterator.next();
                        JsonNode low = lowestObject;
                        JsonNode high = highestObject;
                        String[] propertyParts = o.getPath().split("\\.");
                        Class<? extends Serializable> propertyType = getPropertyType(keysetClass, o.getPath());
                        for (int i = 0; i < propertyParts.length; i++) {
                            low = low == null ? null : low.get(propertyParts[i]);
                            high = high == null ? null : high.get(propertyParts[i]);
                        }
                        lowest.add(low == null ? null : convert(low, propertyType));
                        highest.add(high == null ? null : convert(high, propertyType));
                    }
                    keysetPage = new DefaultKeysetPage(previousOffset, previousPageSize, new DefaultKeyset(lowest.toArray(new Serializable[lowest.size()])), new DefaultKeyset(highest.toArray(new Serializable[highest.size()])));
                }
            }
        }
        return new KeysetPageRequest(keysetPage, sort, offset, pageSize);
    }
    return new PageRequest(sort, offset, pageSize);
}
Also used : Serializable(java.io.Serializable) DefaultKeysetPage(com.blazebit.persistence.DefaultKeysetPage) ArrayList(java.util.ArrayList) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException) KeysetPage(com.blazebit.persistence.KeysetPage) DefaultKeysetPage(com.blazebit.persistence.DefaultKeysetPage) KeysetPageRequest(com.blazebit.persistence.deltaspike.data.KeysetPageRequest) PageRequest(com.blazebit.persistence.deltaspike.data.PageRequest) Pageable(com.blazebit.persistence.deltaspike.data.Pageable) DefaultKeyset(com.blazebit.persistence.DefaultKeyset) KeysetPageRequest(com.blazebit.persistence.deltaspike.data.KeysetPageRequest) Sort(com.blazebit.persistence.deltaspike.data.Sort)

Example 4 with PageRequest

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

the class SampleTest method sampleTest.

@Test
public void sampleTest() {
    transactional(em -> {
        Page<CatWithOwnerView> list = catRepository.findAll(null, new PageRequest(0, 10, Sort.Direction.ASC, "id"));
        System.out.println(list);
        Assert.assertEquals(6, list.getContent().size());
    });
}
Also used : PageRequest(com.blazebit.persistence.deltaspike.data.PageRequest) CatWithOwnerView(com.blazebit.persistence.examples.deltaspike.data.rest.view.CatWithOwnerView) Test(org.junit.Test)

Example 5 with PageRequest

use of com.blazebit.persistence.deltaspike.data.PageRequest 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)

Aggregations

PageRequest (com.blazebit.persistence.deltaspike.data.PageRequest)11 KeysetPageRequest (com.blazebit.persistence.deltaspike.data.KeysetPageRequest)10 Test (org.junit.Test)9 Person (com.blazebit.persistence.deltaspike.data.testsuite.entity.Person)4 PersonView (com.blazebit.persistence.deltaspike.data.testsuite.view.PersonView)4 KeysetPageable (com.blazebit.persistence.deltaspike.data.KeysetPageable)2 Sort (com.blazebit.persistence.deltaspike.data.Sort)2 ArrayList (java.util.ArrayList)2 Category (org.junit.experimental.categories.Category)2 DefaultKeyset (com.blazebit.persistence.DefaultKeyset)1 DefaultKeysetPage (com.blazebit.persistence.DefaultKeysetPage)1 KeysetPage (com.blazebit.persistence.KeysetPage)1 Pageable (com.blazebit.persistence.deltaspike.data.Pageable)1 SortDefault (com.blazebit.persistence.deltaspike.data.rest.SortDefault)1 CatWithOwnerView (com.blazebit.persistence.examples.deltaspike.data.rest.view.CatWithOwnerView)1 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 IOException (java.io.IOException)1 Serializable (java.io.Serializable)1