Search in sources :

Example 1 with BulkRelationshipRepositoryV2

use of io.crnk.core.repository.BulkRelationshipRepositoryV2 in project crnk-framework by crnk-project.

the class RelationshipRepositoryAdapter method findBulkOneTargets.

@SuppressWarnings("rawtypes")
public Map<I, JsonApiResponse> findBulkOneTargets(List<I> sourceIds, ResourceField field, QueryAdapter queryAdapter) {
    if (relationshipRepository instanceof BulkRelationshipRepositoryV2) {
        RepositoryBulkRequestFilterChainImpl<I> chain = new RepositoryBulkRequestFilterChainImpl<I>() {

            @Override
            protected Map<I, JsonApiResponse> invoke(RepositoryFilterContext context) {
                RepositoryRequestSpec request = context.getRequest();
                Iterable<?> sourceIds = request.getIds();
                ResourceField field = request.getRelationshipField();
                QueryAdapter queryAdapter = request.getQueryAdapter();
                BulkRelationshipRepositoryV2 bulkRepository = (BulkRelationshipRepositoryV2) relationshipRepository;
                ResourceInformation targetResourceInformation = moduleRegistry.getResourceRegistry().getEntry(field.getOppositeResourceType()).getResourceInformation();
                MultivaluedMap<I, D> targetsMap = bulkRepository.findTargets(sourceIds, field.getUnderlyingName(), request.getQuerySpec(targetResourceInformation));
                return toResponses(targetsMap, false, queryAdapter, field, HttpMethod.GET);
            }
        };
        RepositoryRequestSpec requestSpec = RepositoryRequestSpecImpl.forFindTarget(moduleRegistry, queryAdapter, sourceIds, field);
        return chain.doFilter(newRepositoryFilterContext(requestSpec));
    } else {
        // fallback to non-bulk operation
        Map<I, JsonApiResponse> responseMap = new HashMap<>();
        for (I sourceId : sourceIds) {
            JsonApiResponse response = findOneTarget(sourceId, field, queryAdapter);
            responseMap.put(sourceId, response);
        }
        return responseMap;
    }
}
Also used : ResourceInformation(io.crnk.core.engine.information.resource.ResourceInformation) BulkRelationshipRepositoryV2(io.crnk.core.repository.BulkRelationshipRepositoryV2) RepositoryRequestSpec(io.crnk.core.engine.dispatcher.RepositoryRequestSpec) HashMap(java.util.HashMap) QueryAdapter(io.crnk.core.engine.query.QueryAdapter) RepositoryFilterContext(io.crnk.core.engine.filter.RepositoryFilterContext) ResourceField(io.crnk.core.engine.information.resource.ResourceField) JsonApiResponse(io.crnk.core.repository.response.JsonApiResponse)

Example 2 with BulkRelationshipRepositoryV2

use of io.crnk.core.repository.BulkRelationshipRepositoryV2 in project crnk-framework by crnk-project.

the class RelationshipRepositoryAdapter method findBulkManyTargets.

@SuppressWarnings("rawtypes")
public Map<I, JsonApiResponse> findBulkManyTargets(List<I> sourceIds, ResourceField field, QueryAdapter queryAdapter) {
    if (relationshipRepository instanceof BulkRelationshipRepositoryV2) {
        RepositoryBulkRequestFilterChainImpl<I> chain = new RepositoryBulkRequestFilterChainImpl<I>() {

            @Override
            protected Map<I, JsonApiResponse> invoke(RepositoryFilterContext context) {
                RepositoryRequestSpec request = context.getRequest();
                Iterable<I> sourceIds = request.getIds();
                ResourceField field = request.getRelationshipField();
                QueryAdapter queryAdapter = request.getQueryAdapter();
                BulkRelationshipRepositoryV2 bulkRepository = (BulkRelationshipRepositoryV2) relationshipRepository;
                ResourceInformation targetResourceInformation = moduleRegistry.getResourceRegistry().getEntry(field.getOppositeResourceType()).getResourceInformation();
                QuerySpec querySpec = request.getQuerySpec(targetResourceInformation);
                MultivaluedMap targetsMap = bulkRepository.findTargets(sourceIds, field.getUnderlyingName(), querySpec);
                return toResponses(targetsMap, true, queryAdapter, field, HttpMethod.GET);
            }
        };
        RepositoryRequestSpec requestSpec = RepositoryRequestSpecImpl.forFindTarget(moduleRegistry, queryAdapter, sourceIds, field);
        return chain.doFilter(newRepositoryFilterContext(requestSpec));
    } else {
        // fallback to non-bulk operation
        Map<I, JsonApiResponse> responseMap = new HashMap<>();
        for (I sourceId : sourceIds) {
            JsonApiResponse response = findManyTargets(sourceId, field, queryAdapter);
            responseMap.put(sourceId, response);
        }
        return responseMap;
    }
}
Also used : ResourceInformation(io.crnk.core.engine.information.resource.ResourceInformation) BulkRelationshipRepositoryV2(io.crnk.core.repository.BulkRelationshipRepositoryV2) RepositoryRequestSpec(io.crnk.core.engine.dispatcher.RepositoryRequestSpec) HashMap(java.util.HashMap) QueryAdapter(io.crnk.core.engine.query.QueryAdapter) RepositoryFilterContext(io.crnk.core.engine.filter.RepositoryFilterContext) ResourceField(io.crnk.core.engine.information.resource.ResourceField) JsonApiResponse(io.crnk.core.repository.response.JsonApiResponse) QuerySpec(io.crnk.core.queryspec.QuerySpec) MultivaluedMap(io.crnk.core.engine.internal.utils.MultivaluedMap)

Aggregations

RepositoryRequestSpec (io.crnk.core.engine.dispatcher.RepositoryRequestSpec)2 RepositoryFilterContext (io.crnk.core.engine.filter.RepositoryFilterContext)2 ResourceField (io.crnk.core.engine.information.resource.ResourceField)2 ResourceInformation (io.crnk.core.engine.information.resource.ResourceInformation)2 QueryAdapter (io.crnk.core.engine.query.QueryAdapter)2 BulkRelationshipRepositoryV2 (io.crnk.core.repository.BulkRelationshipRepositoryV2)2 JsonApiResponse (io.crnk.core.repository.response.JsonApiResponse)2 HashMap (java.util.HashMap)2 MultivaluedMap (io.crnk.core.engine.internal.utils.MultivaluedMap)1 QuerySpec (io.crnk.core.queryspec.QuerySpec)1