Search in sources :

Example 1 with Tuple

use of io.crnk.jpa.query.Tuple in project crnk-framework by crnk-project.

the class JpaRepositoryBase method fillResourceList.

protected ResourceList<T> fillResourceList(List<Tuple> tuples, ResourceList<T> resources) {
    for (Tuple tuple : tuples) {
        JpaMapper<Object, T> mapper = repositoryConfig.getMapper();
        resources.add(mapper.map(tuple));
    }
    return resources;
}
Also used : Tuple(io.crnk.jpa.query.Tuple)

Example 2 with Tuple

use of io.crnk.jpa.query.Tuple in project crnk-framework by crnk-project.

the class JpaRelationshipRepository method mapTuples.

@SuppressWarnings("unchecked")
private MultivaluedMap<I, T> mapTuples(List<Tuple> tuples) {
    MultivaluedMap<I, T> map = new MultivaluedMap<I, T>() {

        @Override
        protected List<T> newList() {
            return repositoryConfig.newResultList();
        }
    };
    for (Tuple tuple : tuples) {
        I sourceId = (I) tuple.get(0, Object.class);
        tuple.reduce(1);
        JpaMapper<Object, T> mapper = repositoryConfig.getMapper();
        map.add(sourceId, mapper.map(tuple));
    }
    return map;
}
Also used : MultivaluedMap(io.crnk.core.engine.internal.utils.MultivaluedMap) Tuple(io.crnk.jpa.query.Tuple)

Example 3 with Tuple

use of io.crnk.jpa.query.Tuple 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

Tuple (io.crnk.jpa.query.Tuple)3 MultivaluedMap (io.crnk.core.engine.internal.utils.MultivaluedMap)1 QuerySpec (io.crnk.core.queryspec.QuerySpec)1 HasMoreResourcesMetaInformation (io.crnk.core.resource.meta.HasMoreResourcesMetaInformation)1 MetaInformation (io.crnk.core.resource.meta.MetaInformation)1 PagedMetaInformation (io.crnk.core.resource.meta.PagedMetaInformation)1 JpaRequestContext (io.crnk.jpa.internal.JpaRequestContext)1 ComputedAttributeRegistry (io.crnk.jpa.query.ComputedAttributeRegistry)1 JpaQueryFactory (io.crnk.jpa.query.JpaQueryFactory)1 ArrayList (java.util.ArrayList)1