Search in sources :

Example 11 with RepositoryRequestSpec

use of io.crnk.core.engine.dispatcher.RepositoryRequestSpec in project crnk-framework by crnk-project.

the class ResourceRepositoryAdapter method delete.

public JsonApiResponse delete(I id, QueryAdapter queryAdapter) {
    RepositoryRequestFilterChainImpl chain = new RepositoryRequestFilterChainImpl() {

        @SuppressWarnings("rawtypes")
        @Override
        protected JsonApiResponse invoke(RepositoryFilterContext context) {
            RepositoryRequestSpec request = context.getRequest();
            QueryAdapter queryAdapter = request.getQueryAdapter();
            Serializable id = request.getId();
            if (isAnnotated) {
                ((AnnotatedResourceRepositoryAdapter) resourceRepository).delete(id, queryAdapter);
            } else if (resourceRepository instanceof ResourceRepositoryV2) {
                ((ResourceRepositoryV2) resourceRepository).delete(id);
            } else {
                ((ResourceRepository) resourceRepository).delete(id);
            }
            return new JsonApiResponse();
        }
    };
    RepositoryRequestSpec requestSpec = RepositoryRequestSpecImpl.forDelete(moduleRegistry, resourceInformation, queryAdapter, id);
    return chain.doFilter(newRepositoryFilterContext(requestSpec));
}
Also used : Serializable(java.io.Serializable) RepositoryRequestSpec(io.crnk.core.engine.dispatcher.RepositoryRequestSpec) AnnotatedResourceRepositoryAdapter(io.crnk.legacy.internal.AnnotatedResourceRepositoryAdapter) QueryAdapter(io.crnk.core.engine.query.QueryAdapter) ResourceRepositoryV2(io.crnk.core.repository.ResourceRepositoryV2) JsonApiResponse(io.crnk.core.repository.response.JsonApiResponse) RepositoryFilterContext(io.crnk.core.engine.filter.RepositoryFilterContext)

Example 12 with RepositoryRequestSpec

use of io.crnk.core.engine.dispatcher.RepositoryRequestSpec in project crnk-framework by crnk-project.

the class ResourceRepositoryAdapter method findAll.

public JsonApiResponse findAll(Iterable ids, QueryAdapter queryAdapter) {
    RepositoryRequestFilterChainImpl chain = new RepositoryRequestFilterChainImpl() {

        @SuppressWarnings("rawtypes")
        @Override
        protected JsonApiResponse invoke(RepositoryFilterContext context) {
            RepositoryRequestSpec request = context.getRequest();
            QueryAdapter queryAdapter = request.getQueryAdapter();
            Iterable<?> ids = request.getIds();
            Object resources;
            if (isAnnotated) {
                resources = ((AnnotatedResourceRepositoryAdapter) resourceRepository).findAll(ids, queryAdapter);
            } else if (resourceRepository instanceof ResourceRepositoryV2) {
                resources = ((ResourceRepositoryV2) resourceRepository).findAll(ids, request.getQuerySpec(resourceInformation));
            } else {
                resources = ((ResourceRepository) resourceRepository).findAll(ids, request.getQueryParams());
            }
            return getResponse(resourceRepository, resources, request);
        }
    };
    RepositoryRequestSpec requestSpec = RepositoryRequestSpecImpl.forFindIds(moduleRegistry, resourceInformation, queryAdapter, ids);
    return chain.doFilter(newRepositoryFilterContext(requestSpec));
}
Also used : RepositoryRequestSpec(io.crnk.core.engine.dispatcher.RepositoryRequestSpec) QueryAdapter(io.crnk.core.engine.query.QueryAdapter) ResourceRepositoryV2(io.crnk.core.repository.ResourceRepositoryV2) RepositoryFilterContext(io.crnk.core.engine.filter.RepositoryFilterContext) ResourceRepository(io.crnk.legacy.repository.ResourceRepository)

Example 13 with RepositoryRequestSpec

use of io.crnk.core.engine.dispatcher.RepositoryRequestSpec in project crnk-framework by crnk-project.

the class RelationshipRepositoryAdapter method removeRelations.

@SuppressWarnings("rawtypes")
public JsonApiResponse removeRelations(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).removeRelations(source, targetIds, field.getUnderlyingName(), queryAdapter);
            } else if (relationshipRepository instanceof RelationshipRepositoryV2) {
                ((RelationshipRepositoryV2) relationshipRepository).removeRelations(source, targetIds, field.getUnderlyingName());
            } else {
                ((RelationshipRepository) relationshipRepository).removeRelations(source, targetIds, field.getUnderlyingName());
            }
            return new JsonApiResponse();
        }
    };
    RepositoryRequestSpec requestSpec = RepositoryRequestSpecImpl.forRelation(moduleRegistry, HttpMethod.DELETE, 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 14 with RepositoryRequestSpec

use of io.crnk.core.engine.dispatcher.RepositoryRequestSpec 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 15 with RepositoryRequestSpec

use of io.crnk.core.engine.dispatcher.RepositoryRequestSpec 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)

Aggregations

RepositoryRequestSpec (io.crnk.core.engine.dispatcher.RepositoryRequestSpec)33 ResourceRegistryTest (io.crnk.core.resource.registry.ResourceRegistryTest)17 Test (org.junit.Test)17 RepositoryFilterContext (io.crnk.core.engine.filter.RepositoryFilterContext)13 QueryAdapter (io.crnk.core.engine.query.QueryAdapter)12 JsonApiResponse (io.crnk.core.repository.response.JsonApiResponse)10 ResourceField (io.crnk.core.engine.information.resource.ResourceField)8 BulkRelationshipRepositoryV2 (io.crnk.core.repository.BulkRelationshipRepositoryV2)8 RelationshipRepositoryV2 (io.crnk.core.repository.RelationshipRepositoryV2)6 Serializable (java.io.Serializable)6 ResourceRepositoryV2 (io.crnk.core.repository.ResourceRepositoryV2)5 ResourceInformation (io.crnk.core.engine.information.resource.ResourceInformation)4 AnnotatedRelationshipRepositoryAdapter (io.crnk.legacy.internal.AnnotatedRelationshipRepositoryAdapter)4 ResourceRepository (io.crnk.legacy.repository.ResourceRepository)4 QuerySpec (io.crnk.core.queryspec.QuerySpec)3 HashMap (java.util.HashMap)3 User (io.crnk.core.mock.models.User)2 RelationshipRepository (io.crnk.legacy.repository.RelationshipRepository)2 Span (brave.Span)1 Tracer (brave.Tracer)1