Search in sources :

Example 6 with HasMoreResourcesMetaInformation

use of io.crnk.core.resource.meta.HasMoreResourcesMetaInformation in project crnk-framework by crnk-project.

the class HasNextBasedPagedLinksInformationTest method testPagingLast.

@Test
public void testPagingLast() throws InstantiationException, IllegalAccessException {
    QuerySpecAdapter querySpec = new QuerySpecAdapter(querySpec(4L, 4L), resourceRegistry);
    JsonApiResponse results = adapter.findAll(querySpec);
    HasMoreResourcesMetaInformation metaInformation = (HasMoreResourcesMetaInformation) results.getMetaInformation();
    Assert.assertFalse(metaInformation.getHasMoreResources());
    PagedLinksInformation linksInformation = (PagedLinksInformation) results.getLinksInformation();
    Assert.assertEquals("http://127.0.0.1/tasks?page[limit]=4", linksInformation.getFirst());
    Assert.assertNull(linksInformation.getLast());
    Assert.assertEquals("http://127.0.0.1/tasks?page[limit]=4", linksInformation.getFirst());
    Assert.assertNull(linksInformation.getNext());
}
Also used : HasMoreResourcesMetaInformation(io.crnk.core.resource.meta.HasMoreResourcesMetaInformation) PagedLinksInformation(io.crnk.core.resource.links.PagedLinksInformation) JsonApiResponse(io.crnk.core.repository.response.JsonApiResponse) QuerySpecAdapter(io.crnk.core.queryspec.internal.QuerySpecAdapter) AbstractQuerySpecTest(io.crnk.core.queryspec.AbstractQuerySpecTest) Test(org.junit.Test)

Example 7 with HasMoreResourcesMetaInformation

use of io.crnk.core.resource.meta.HasMoreResourcesMetaInformation in project crnk-framework by crnk-project.

the class HasNextBasedPagedLinksInformationTest method testPagingFirst.

@Test
public void testPagingFirst() throws InstantiationException, IllegalAccessException {
    QuerySpecAdapter querySpec = new QuerySpecAdapter(querySpec(0L, 3L), resourceRegistry);
    JsonApiResponse results = adapter.findAll(querySpec);
    HasMoreResourcesMetaInformation metaInformation = (HasMoreResourcesMetaInformation) results.getMetaInformation();
    Assert.assertTrue(metaInformation.getHasMoreResources());
    PagedLinksInformation linksInformation = (PagedLinksInformation) results.getLinksInformation();
    Assert.assertEquals("http://127.0.0.1/tasks?page[limit]=3", linksInformation.getFirst());
    Assert.assertNull(linksInformation.getLast());
    Assert.assertNull(linksInformation.getPrev());
    Assert.assertEquals("http://127.0.0.1/tasks?page[limit]=3&page[offset]=3", linksInformation.getNext());
}
Also used : HasMoreResourcesMetaInformation(io.crnk.core.resource.meta.HasMoreResourcesMetaInformation) PagedLinksInformation(io.crnk.core.resource.links.PagedLinksInformation) JsonApiResponse(io.crnk.core.repository.response.JsonApiResponse) QuerySpecAdapter(io.crnk.core.queryspec.internal.QuerySpecAdapter) AbstractQuerySpecTest(io.crnk.core.queryspec.AbstractQuerySpecTest) Test(org.junit.Test)

Example 8 with HasMoreResourcesMetaInformation

use of io.crnk.core.resource.meta.HasMoreResourcesMetaInformation in project crnk-framework by crnk-project.

the class InMemoryEvaluator method eval.

public <T> void eval(Iterable<T> resources, QuerySpec querySpec, ResourceList<T> resultList) {
    Iterator<T> iterator = resources.iterator();
    while (iterator.hasNext()) {
        resultList.add(iterator.next());
    }
    // filter
    if (!querySpec.getFilters().isEmpty()) {
        FilterSpec filterSpec = FilterSpec.and(querySpec.getFilters());
        applyFilter(resultList, filterSpec);
    }
    long totalCount = resultList.size();
    // sort
    applySorting(resultList, querySpec.getSort());
    // offset/limit
    applyPaging(resultList, querySpec);
    // set page information
    if (querySpec.getLimit() != null || querySpec.getOffset() != 0) {
        MetaInformation meta = resultList.getMeta();
        if (meta instanceof PagedMetaInformation) {
            PagedMetaInformation pagedMeta = (PagedMetaInformation) meta;
            pagedMeta.setTotalResourceCount(totalCount);
        }
        if (meta instanceof HasMoreResourcesMetaInformation) {
            HasMoreResourcesMetaInformation pagedMeta = (HasMoreResourcesMetaInformation) meta;
            pagedMeta.setHasMoreResources(totalCount > querySpec.getOffset() + querySpec.getLimit());
        }
    }
}
Also used : PagedMetaInformation(io.crnk.core.resource.meta.PagedMetaInformation) HasMoreResourcesMetaInformation(io.crnk.core.resource.meta.HasMoreResourcesMetaInformation) MetaInformation(io.crnk.core.resource.meta.MetaInformation) PagedMetaInformation(io.crnk.core.resource.meta.PagedMetaInformation) HasMoreResourcesMetaInformation(io.crnk.core.resource.meta.HasMoreResourcesMetaInformation)

Example 9 with HasMoreResourcesMetaInformation

use of io.crnk.core.resource.meta.HasMoreResourcesMetaInformation in project crnk-framework by crnk-project.

the class JpaEntityRepository method findAll.

@Override
public ResourceList<T> findAll(QuerySpec querySpec) {
    Class<?> entityClass = repositoryConfig.getEntityClass();
    QuerySpec filteredQuerySpec = filterQuerySpec(querySpec);
    JpaQueryFactory queryFactory = module.getQueryFactory();
    JpaQuery<?> query = queryFactory.query(entityClass);
    query.setPrivateData(new JpaRequestContext(this, querySpec));
    ComputedAttributeRegistry computedAttributesRegistry = queryFactory.getComputedAttributes();
    Set<String> computedAttrs = computedAttributesRegistry.getForType(entityClass);
    JpaRepositoryUtils.prepareQuery(query, filteredQuerySpec, computedAttrs);
    query = filterQuery(filteredQuerySpec, query);
    JpaQueryExecutor<?> executor = query.buildExecutor();
    boolean fetchNext = isNextFetched(filteredQuerySpec);
    boolean fetchTotal = isTotalFetched(filteredQuerySpec);
    JpaRepositoryUtils.prepareExecutor(executor, filteredQuerySpec, fetchRelations(null));
    if (fetchNext) {
        executor.setLimit(executor.getLimit() + 1);
    }
    executor = filterExecutor(filteredQuerySpec, executor);
    List<Tuple> tuples = executor.getResultTuples();
    Boolean hasNext = null;
    if (fetchNext) {
        hasNext = tuples.size() == querySpec.getLimit() + 1;
        if (hasNext) {
            tuples = tuples.subList(0, querySpec.getLimit().intValue());
        }
    }
    tuples = filterTuples(filteredQuerySpec, tuples);
    ResourceList<T> resources = repositoryConfig.newResultList();
    MetaInformation metaInfo = resources.getMeta();
    fillResourceList(tuples, resources);
    resources = filterResults(filteredQuerySpec, resources);
    if (fetchTotal) {
        long totalRowCount = executor.getTotalRowCount();
        ((PagedMetaInformation) metaInfo).setTotalResourceCount(totalRowCount);
    }
    if (fetchNext) {
        ((HasMoreResourcesMetaInformation) metaInfo).setHasMoreResources(hasNext);
    }
    return resources;
}
Also used : HasMoreResourcesMetaInformation(io.crnk.core.resource.meta.HasMoreResourcesMetaInformation) MetaInformation(io.crnk.core.resource.meta.MetaInformation) PagedMetaInformation(io.crnk.core.resource.meta.PagedMetaInformation) HasMoreResourcesMetaInformation(io.crnk.core.resource.meta.HasMoreResourcesMetaInformation) JpaRequestContext(io.crnk.jpa.internal.JpaRequestContext) PagedMetaInformation(io.crnk.core.resource.meta.PagedMetaInformation) QuerySpec(io.crnk.core.queryspec.QuerySpec)

Example 10 with HasMoreResourcesMetaInformation

use of io.crnk.core.resource.meta.HasMoreResourcesMetaInformation in project crnk-framework by crnk-project.

the class JpaRelationshipRepository method findTargets.

@Override
public MultivaluedMap<I, T> findTargets(Iterable<I> sourceIds, String fieldName, QuerySpec querySpec) {
    List<I> sourceIdLists = new ArrayList<>();
    for (I sourceId : sourceIds) {
        sourceIdLists.add(sourceId);
    }
    if (querySpec.getLimit() != null && sourceIdLists.size() > 1) {
        throw new UnsupportedOperationException("page limit not supported for bulk inclusions");
    }
    // support paging for non-bulk requests
    boolean singleRequest = sourceIdLists.size() == 1;
    boolean pagedSingleRequest = singleRequest && querySpec.getLimit() != null;
    boolean fetchNext = pagedSingleRequest && isNextFetched(querySpec);
    QuerySpec bulkQuerySpec = querySpec.duplicate();
    QuerySpec filteredQuerySpec = filterQuerySpec(bulkQuerySpec);
    JpaQueryFactory queryFactory = module.getQueryFactory();
    JpaQuery<?> query = queryFactory.query(sourceEntityClass, fieldName, sourceIdLists);
    query.setPrivateData(new JpaRequestContext(this, querySpec));
    query.addParentIdSelection();
    query = filterQuery(filteredQuerySpec, query);
    Class<?> entityClass = repositoryConfig.getEntityClass();
    ComputedAttributeRegistry computedAttributesRegistry = queryFactory.getComputedAttributes();
    Set<String> computedAttrs = computedAttributesRegistry.getForType(entityClass);
    JpaRepositoryUtils.prepareQuery(query, filteredQuerySpec, computedAttrs);
    JpaQueryExecutor<?> executor = query.buildExecutor();
    JpaRepositoryUtils.prepareExecutor(executor, filteredQuerySpec, fetchRelations(fieldName));
    executor = filterExecutor(filteredQuerySpec, executor);
    if (fetchNext) {
        executor.setLimit(executor.getLimit() + 1);
    }
    List<Tuple> tuples = executor.getResultTuples();
    Boolean hasNext = null;
    if (fetchNext) {
        hasNext = tuples.size() == querySpec.getLimit() + 1;
        if (hasNext) {
            tuples = tuples.subList(0, querySpec.getLimit().intValue());
        }
    }
    tuples = filterTuples(bulkQuerySpec, tuples);
    MultivaluedMap<I, T> map = mapTuples(tuples);
    if (singleRequest) {
        I sourceId = sourceIdLists.get(0);
        ResourceList<T> iterable;
        if (map.containsKey(sourceId)) {
            iterable = (ResourceList<T>) map.getList(sourceId);
        } else {
            iterable = repositoryConfig.newResultList();
            map.set(sourceId, iterable);
        }
        if (pagedSingleRequest) {
            MetaInformation metaInfo = iterable.getMeta();
            boolean fetchTotal = isTotalFetched(filteredQuerySpec);
            if (fetchTotal) {
                long totalRowCount = executor.getTotalRowCount();
                ((PagedMetaInformation) metaInfo).setTotalResourceCount(totalRowCount);
            }
            if (fetchNext) {
                ((HasMoreResourcesMetaInformation) metaInfo).setHasMoreResources(hasNext);
            }
        }
    }
    return map;
}
Also used : ComputedAttributeRegistry(io.crnk.jpa.query.ComputedAttributeRegistry) HasMoreResourcesMetaInformation(io.crnk.core.resource.meta.HasMoreResourcesMetaInformation) JpaQueryFactory(io.crnk.jpa.query.JpaQueryFactory) ArrayList(java.util.ArrayList) MetaInformation(io.crnk.core.resource.meta.MetaInformation) PagedMetaInformation(io.crnk.core.resource.meta.PagedMetaInformation) HasMoreResourcesMetaInformation(io.crnk.core.resource.meta.HasMoreResourcesMetaInformation) JpaRequestContext(io.crnk.jpa.internal.JpaRequestContext) PagedMetaInformation(io.crnk.core.resource.meta.PagedMetaInformation) QuerySpec(io.crnk.core.queryspec.QuerySpec) Tuple(io.crnk.jpa.query.Tuple)

Aggregations

HasMoreResourcesMetaInformation (io.crnk.core.resource.meta.HasMoreResourcesMetaInformation)10 Test (org.junit.Test)7 AbstractQuerySpecTest (io.crnk.core.queryspec.AbstractQuerySpecTest)5 QuerySpecAdapter (io.crnk.core.queryspec.internal.QuerySpecAdapter)5 JsonApiResponse (io.crnk.core.repository.response.JsonApiResponse)5 PagedLinksInformation (io.crnk.core.resource.links.PagedLinksInformation)5 MetaInformation (io.crnk.core.resource.meta.MetaInformation)3 PagedMetaInformation (io.crnk.core.resource.meta.PagedMetaInformation)3 Task (io.crnk.core.mock.models.Task)2 QuerySpec (io.crnk.core.queryspec.QuerySpec)2 DefaultResourceList (io.crnk.core.resource.list.DefaultResourceList)2 DefaultHasMoreResourcesMetaInformation (io.crnk.core.resource.meta.DefaultHasMoreResourcesMetaInformation)2 JpaRequestContext (io.crnk.jpa.internal.JpaRequestContext)2 ComputedAttributeRegistry (io.crnk.jpa.query.ComputedAttributeRegistry)1 JpaQueryFactory (io.crnk.jpa.query.JpaQueryFactory)1 Tuple (io.crnk.jpa.query.Tuple)1 ArrayList (java.util.ArrayList)1