Search in sources :

Example 21 with JsonApiResponse

use of io.crnk.core.repository.response.JsonApiResponse 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 22 with JsonApiResponse

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

the class RelationshipRepositoryAdapter method toResponses.

private Map<I, JsonApiResponse> toResponses(MultivaluedMap<I, D> targetsMap, boolean isMany, QueryAdapter queryAdapter, ResourceField field, HttpMethod method) {
    Map<I, JsonApiResponse> responseMap = new HashMap<>();
    for (I sourceId : targetsMap.keySet()) {
        Object targets;
        if (isMany) {
            targets = targetsMap.getList(sourceId);
        } else {
            targets = targetsMap.getUnique(sourceId, true);
        }
        RepositoryRequestSpec requestSpec = RepositoryRequestSpecImpl.forRelation(moduleRegistry, method, null, queryAdapter, Collections.singleton(sourceId), field);
        JsonApiResponse response = getResponse(relationshipRepository, targets, requestSpec);
        responseMap.put(sourceId, response);
    }
    return responseMap;
}
Also used : RepositoryRequestSpec(io.crnk.core.engine.dispatcher.RepositoryRequestSpec) HashMap(java.util.HashMap) JsonApiResponse(io.crnk.core.repository.response.JsonApiResponse)

Example 23 with JsonApiResponse

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

the class RelationshipRepositoryAdapter method setRelations.

@SuppressWarnings("rawtypes")
public JsonApiResponse setRelations(T source, Iterable<J> targetIds, ResourceField field, QueryAdapter queryAdapter) {
    RepositoryRequestFilterChainImpl chain = new RepositoryRequestFilterChainImpl() {

        @Override
        protected JsonApiResponse invoke(RepositoryFilterContext context) {
            RepositoryRequestSpec request = context.getRequest();
            Object source = request.getEntity();
            Iterable<?> targetIds = request.getIds();
            ResourceField field = request.getRelationshipField();
            QueryAdapter queryAdapter = request.getQueryAdapter();
            if (isAnnotated) {
                ((AnnotatedRelationshipRepositoryAdapter) relationshipRepository).setRelations(source, targetIds, field.getUnderlyingName(), queryAdapter);
            } else if (relationshipRepository instanceof RelationshipRepositoryV2) {
                ((RelationshipRepositoryV2) relationshipRepository).setRelations(source, targetIds, field.getUnderlyingName());
            } else {
                ((RelationshipRepository) relationshipRepository).setRelations(source, targetIds, field.getUnderlyingName());
            }
            return new JsonApiResponse();
        }
    };
    RepositoryRequestSpec requestSpec = RepositoryRequestSpecImpl.forRelation(moduleRegistry, HttpMethod.PATCH, source, queryAdapter, targetIds, field);
    return chain.doFilter(newRepositoryFilterContext(requestSpec));
}
Also used : ResourceField(io.crnk.core.engine.information.resource.ResourceField) RepositoryRequestSpec(io.crnk.core.engine.dispatcher.RepositoryRequestSpec) AnnotatedRelationshipRepositoryAdapter(io.crnk.legacy.internal.AnnotatedRelationshipRepositoryAdapter) QueryAdapter(io.crnk.core.engine.query.QueryAdapter) JsonApiResponse(io.crnk.core.repository.response.JsonApiResponse) RepositoryFilterContext(io.crnk.core.engine.filter.RepositoryFilterContext) RelationshipRepositoryV2(io.crnk.core.repository.RelationshipRepositoryV2) BulkRelationshipRepositoryV2(io.crnk.core.repository.BulkRelationshipRepositoryV2)

Example 24 with JsonApiResponse

use of io.crnk.core.repository.response.JsonApiResponse 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)

Example 25 with JsonApiResponse

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

the class ResponseRepositoryAdapter method getResponse.

protected JsonApiResponse getResponse(Object repository, Object result, RepositoryRequestSpec requestSpec) {
    if (result instanceof JsonApiResponse) {
        return (JsonApiResponse) result;
    }
    Iterable<?> resources;
    boolean isCollection = result instanceof Iterable;
    if (isCollection) {
        resources = (Iterable<?>) result;
    } else {
        resources = Collections.singletonList(result);
    }
    Iterable<?> filteredResult = filterResult(resources, requestSpec);
    MetaInformation metaInformation = getMetaInformation(repository, resources, requestSpec);
    LinksInformation linksInformation = getLinksInformation(repository, resources, requestSpec);
    Object resultEntity;
    if (isCollection) {
        resultEntity = filteredResult;
    } else {
        Iterator<?> iterator = filteredResult.iterator();
        if (iterator.hasNext()) {
            resultEntity = iterator.next();
            PreconditionUtil.assertFalse("expected unique result", iterator.hasNext());
        } else {
            resultEntity = null;
        }
    }
    return new JsonApiResponse().setEntity(resultEntity).setLinksInformation(linksInformation).setMetaInformation(metaInformation);
}
Also used : MetaInformation(io.crnk.core.resource.meta.MetaInformation) DefaultPagedLinksInformation(io.crnk.core.resource.links.DefaultPagedLinksInformation) LinksInformation(io.crnk.core.resource.links.LinksInformation) PagedLinksInformation(io.crnk.core.resource.links.PagedLinksInformation) JsonApiResponse(io.crnk.core.repository.response.JsonApiResponse)

Aggregations

JsonApiResponse (io.crnk.core.repository.response.JsonApiResponse)49 Document (io.crnk.core.engine.document.Document)16 Test (org.junit.Test)16 ResourceField (io.crnk.core.engine.information.resource.ResourceField)15 RegistryEntry (io.crnk.core.engine.registry.RegistryEntry)14 QuerySpecAdapter (io.crnk.core.queryspec.internal.QuerySpecAdapter)12 QueryAdapter (io.crnk.core.engine.query.QueryAdapter)11 QuerySpec (io.crnk.core.queryspec.QuerySpec)11 RepositoryRequestSpec (io.crnk.core.engine.dispatcher.RepositoryRequestSpec)10 RepositoryFilterContext (io.crnk.core.engine.filter.RepositoryFilterContext)10 ResourceInformation (io.crnk.core.engine.information.resource.ResourceInformation)10 ResourceRepositoryAdapter (io.crnk.core.engine.internal.repository.ResourceRepositoryAdapter)10 Serializable (java.io.Serializable)10 Response (io.crnk.core.engine.dispatcher.Response)9 Resource (io.crnk.core.engine.document.Resource)9 Task (io.crnk.core.mock.models.Task)7 AbstractQuerySpecTest (io.crnk.core.queryspec.AbstractQuerySpecTest)6 BulkRelationshipRepositoryV2 (io.crnk.core.repository.BulkRelationshipRepositoryV2)6 PagedLinksInformation (io.crnk.core.resource.links.PagedLinksInformation)6 HashSet (java.util.HashSet)6