Search in sources :

Example 1 with Pageable

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

the class ExtendedParameters method create.

public static ExtendedParameters create(Method method, Object[] parameters, RepositoryMethod repositoryMethod) {
    int max = extractSizeRestriction(method, repositoryMethod);
    int first = DEFAULT_FIRST;
    List<Parameter> result = new ArrayList<Parameter>(parameters.length);
    int paramIndex = 1;
    Annotation[][] annotations = method.getParameterAnnotations();
    Class<?>[] parameterTypes = method.getParameterTypes();
    Pageable pageable = null;
    Specification<?> specification = null;
    for (int i = 0; i < parameters.length; i++) {
        // Extended for support of Pageable and Specification parameters
        if (Pageable.class.isAssignableFrom(parameterTypes[i])) {
            pageable = (Pageable) parameters[i];
        } else if (Specification.class.isAssignableFrom(parameterTypes[i])) {
            specification = (Specification) parameters[i];
        } else if (isParameter(annotations[i])) {
            QueryParam qpAnnotation = extractFrom(annotations[i], QueryParam.class);
            if (qpAnnotation != null) {
                result.add(new NamedParameter(qpAnnotation.value(), parameters[i]));
            } else {
                result.add(new IndexedParameter(paramIndex++, parameters[i]));
            }
        } else {
            max = extractInt(parameters[i], annotations[i], MaxResults.class, max);
            first = extractInt(parameters[i], annotations[i], FirstResult.class, first);
        }
    }
    if (pageable == null) {
        if (first > DEFAULT_FIRST || max > 0 && max < Integer.MAX_VALUE) {
            pageable = new OffsetPageRequest(first, max);
        }
    }
    return new ExtendedParameters(result, pageable, specification);
}
Also used : FirstResult(org.apache.deltaspike.data.api.FirstResult) NamedParameter(org.apache.deltaspike.data.impl.param.NamedParameter) MaxResults(org.apache.deltaspike.data.api.MaxResults) ArrayList(java.util.ArrayList) Specification(com.blazebit.persistence.deltaspike.data.Specification) IndexedParameter(org.apache.deltaspike.data.impl.param.IndexedParameter) Pageable(com.blazebit.persistence.deltaspike.data.Pageable) QueryParam(org.apache.deltaspike.data.api.QueryParam) OffsetPageRequest(com.blazebit.persistence.deltaspike.data.base.builder.OffsetPageRequest) NamedParameter(org.apache.deltaspike.data.impl.param.NamedParameter) Parameter(org.apache.deltaspike.data.impl.param.Parameter) IndexedParameter(org.apache.deltaspike.data.impl.param.IndexedParameter)

Example 2 with Pageable

use of com.blazebit.persistence.deltaspike.data.Pageable 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 3 with Pageable

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

the class EntityViewMethodQueryBuilder method createJpaQuery.

private <V> Query createJpaQuery(EntityViewCdiQueryInvocationContext context) {
    ExtendedParameters params = context.getExtendedParams();
    EntityViewQueryRoot root = context.getRepositoryMethodMetadata().getEntityViewQueryRoot();
    Pageable pageable = params.getPageable();
    CriteriaBuilder<?> cb;
    Specification<?> specification = params.getSpecification();
    if (specification == null) {
        cb = context.getCriteriaBuilderFactory().create(context.getEntityManager(), context.getEntityClass());
        root.apply(cb);
    } else {
        BlazeCriteriaBuilder blazeCriteriaBuilder = BlazeCriteria.get(context.getCriteriaBuilderFactory());
        BlazeCriteriaQuery<?> query = blazeCriteriaBuilder.createQuery(context.getEntityClass());
        Root queryRoot = query.from(context.getEntityClass());
        root.apply(queryRoot, query, blazeCriteriaBuilder);
        Predicate predicate = specification.toPredicate(queryRoot, query, blazeCriteriaBuilder);
        if (predicate != null) {
            if (query.getRestriction() == null) {
                query.where(predicate);
            } else {
                query.where(query.getRestriction(), predicate);
            }
        }
        cb = query.createCriteriaBuilder(context.getEntityManager());
    }
    Class<V> entityViewClass = (Class<V>) context.getEntityViewClass();
    // TODO: depending on return type we might need to do keyset extraction
    boolean keysetExtraction = false;
    FullQueryBuilder<?, ?> fullCb = QueryBuilderUtils.getFullQueryBuilder(cb, pageable, context.getEntityViewManager(), entityViewClass, keysetExtraction);
    fullCb = context.applyCriteriaBuilderPostProcessors(fullCb);
    Query q = params.applyTo(context.createQuery(fullCb));
    return context.applyRestrictions(q);
}
Also used : EntityViewQueryRoot(com.blazebit.persistence.deltaspike.data.impl.builder.part.EntityViewQueryRoot) EntityViewQueryRoot(com.blazebit.persistence.deltaspike.data.impl.builder.part.EntityViewQueryRoot) Root(javax.persistence.criteria.Root) Query(javax.persistence.Query) BlazeCriteriaQuery(com.blazebit.persistence.criteria.BlazeCriteriaQuery) BlazeCriteriaBuilder(com.blazebit.persistence.criteria.BlazeCriteriaBuilder) ExtendedParameters(com.blazebit.persistence.deltaspike.data.impl.param.ExtendedParameters) Predicate(javax.persistence.criteria.Predicate) Pageable(com.blazebit.persistence.deltaspike.data.Pageable)

Example 4 with Pageable

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

the class ExtendedParameters method create.

public static ExtendedParameters create(Method method, Object[] parameters, RepositoryMethodMetadata repositoryMethod) {
    int max = extractSizeRestriction(method, repositoryMethod);
    int first = DEFAULT_FIRST;
    List<Parameter> result = new ArrayList<Parameter>(parameters.length);
    int paramIndex = 1;
    Annotation[][] annotations = method.getParameterAnnotations();
    Class<?>[] parameterTypes = method.getParameterTypes();
    Pageable pageable = null;
    Specification<?> specification = null;
    for (int i = 0; i < parameters.length; i++) {
        // Extended for support of Pageable and Specification parameters
        if (Pageable.class.isAssignableFrom(parameterTypes[i])) {
            pageable = (Pageable) parameters[i];
        } else if (Specification.class.isAssignableFrom(parameterTypes[i])) {
            specification = (Specification) parameters[i];
        } else if (isParameter(annotations[i])) {
            QueryParam qpAnnotation = extractFrom(annotations[i], QueryParam.class);
            if (qpAnnotation != null) {
                result.add(new NamedParameter(qpAnnotation.value(), parameters[i]));
            } else {
                result.add(new IndexedParameter(paramIndex++, parameters[i]));
            }
        } else {
            max = extractInt(parameters[i], annotations[i], MaxResults.class, max);
            first = extractInt(parameters[i], annotations[i], FirstResult.class, first);
        }
    }
    if (pageable == null) {
        if (first > DEFAULT_FIRST || max > 0 && max < Integer.MAX_VALUE) {
            pageable = new OffsetPageRequest(first, max);
        }
    }
    return new ExtendedParameters(result, pageable, specification);
}
Also used : FirstResult(org.apache.deltaspike.data.api.FirstResult) NamedParameter(org.apache.deltaspike.data.impl.param.NamedParameter) MaxResults(org.apache.deltaspike.data.api.MaxResults) ArrayList(java.util.ArrayList) Specification(com.blazebit.persistence.deltaspike.data.Specification) IndexedParameter(org.apache.deltaspike.data.impl.param.IndexedParameter) Pageable(com.blazebit.persistence.deltaspike.data.Pageable) QueryParam(org.apache.deltaspike.data.api.QueryParam) OffsetPageRequest(com.blazebit.persistence.deltaspike.data.base.builder.OffsetPageRequest) NamedParameter(org.apache.deltaspike.data.impl.param.NamedParameter) Parameter(org.apache.deltaspike.data.impl.param.Parameter) IndexedParameter(org.apache.deltaspike.data.impl.param.IndexedParameter)

Example 5 with Pageable

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

the class FullEntityViewRepositoryTest method testFindAllWithKeysetExtraction.

@Test
public void testFindAllWithKeysetExtraction() {
    Pageable pageable = new KeysetPageRequest(null, new Sort("id"), 0, 5, true, true);
    Page<PersonView> page = personViewRepository.findAll(pageable);
    KeysetAwarePage<PersonView> keysetAwarePage = (KeysetAwarePage<PersonView>) page;
    assertEquals(5, keysetAwarePage.getKeysetPage().getKeysets().size());
}
Also used : KeysetAwarePage(com.blazebit.persistence.deltaspike.data.KeysetAwarePage) Pageable(com.blazebit.persistence.deltaspike.data.Pageable) KeysetPageable(com.blazebit.persistence.deltaspike.data.KeysetPageable) KeysetPageRequest(com.blazebit.persistence.deltaspike.data.KeysetPageRequest) Sort(com.blazebit.persistence.deltaspike.data.Sort) PersonView(com.blazebit.persistence.deltaspike.data.testsuite.view.PersonView) Test(org.junit.Test)

Aggregations

Pageable (com.blazebit.persistence.deltaspike.data.Pageable)5 ArrayList (java.util.ArrayList)3 KeysetPageRequest (com.blazebit.persistence.deltaspike.data.KeysetPageRequest)2 Sort (com.blazebit.persistence.deltaspike.data.Sort)2 Specification (com.blazebit.persistence.deltaspike.data.Specification)2 OffsetPageRequest (com.blazebit.persistence.deltaspike.data.base.builder.OffsetPageRequest)2 FirstResult (org.apache.deltaspike.data.api.FirstResult)2 MaxResults (org.apache.deltaspike.data.api.MaxResults)2 QueryParam (org.apache.deltaspike.data.api.QueryParam)2 IndexedParameter (org.apache.deltaspike.data.impl.param.IndexedParameter)2 NamedParameter (org.apache.deltaspike.data.impl.param.NamedParameter)2 Parameter (org.apache.deltaspike.data.impl.param.Parameter)2 DefaultKeyset (com.blazebit.persistence.DefaultKeyset)1 DefaultKeysetPage (com.blazebit.persistence.DefaultKeysetPage)1 KeysetPage (com.blazebit.persistence.KeysetPage)1 BlazeCriteriaBuilder (com.blazebit.persistence.criteria.BlazeCriteriaBuilder)1 BlazeCriteriaQuery (com.blazebit.persistence.criteria.BlazeCriteriaQuery)1 KeysetAwarePage (com.blazebit.persistence.deltaspike.data.KeysetAwarePage)1 KeysetPageable (com.blazebit.persistence.deltaspike.data.KeysetPageable)1 PageRequest (com.blazebit.persistence.deltaspike.data.PageRequest)1