Search in sources :

Example 11 with RepositoryFilterContext

use of io.crnk.core.engine.filter.RepositoryFilterContext in project crnk-framework by crnk-project.

the class ResourceRepositoryAdapter method findOne.

public JsonApiResponse findOne(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();
            Object resource;
            if (isAnnotated) {
                resource = ((AnnotatedResourceRepositoryAdapter) resourceRepository).findOne(id, queryAdapter);
            } else if (resourceRepository instanceof ResourceRepositoryV2) {
                resource = ((ResourceRepositoryV2) resourceRepository).findOne(id, request.getQuerySpec(resourceInformation));
            } else {
                resource = ((ResourceRepository) resourceRepository).findOne(id, request.getQueryParams());
            }
            if (resource == null && return404OnNull) {
                throw new ResourceNotFoundException(resourceInformation.getResourceType());
            }
            return getResponse(resourceRepository, resource, request);
        }
    };
    RepositoryRequestSpec requestSpec = RepositoryRequestSpecImpl.forFindId(moduleRegistry, resourceInformation, queryAdapter, id);
    return chain.doFilter(newRepositoryFilterContext(requestSpec));
}
Also used : Serializable(java.io.Serializable) 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) ResourceNotFoundException(io.crnk.core.exception.ResourceNotFoundException)

Example 12 with RepositoryFilterContext

use of io.crnk.core.engine.filter.RepositoryFilterContext in project crnk-framework by crnk-project.

the class ResourceRepositoryAdapter method save.

private <S extends T> JsonApiResponse save(S entity, QueryAdapter queryAdapter, final HttpMethod method) {
    RepositoryRequestFilterChainImpl chain = new RepositoryRequestFilterChainImpl() {

        @SuppressWarnings("rawtypes")
        @Override
        protected JsonApiResponse invoke(RepositoryFilterContext context) {
            RepositoryRequestSpec request = context.getRequest();
            Object entity = request.getEntity();
            Object resource;
            if (isAnnotated) {
                resource = ((AnnotatedResourceRepositoryAdapter) resourceRepository).save(entity);
            } else if (resourceRepository instanceof ResourceRepositoryV2) {
                if (method == HttpMethod.POST) {
                    resource = ((ResourceRepositoryV2) resourceRepository).create(entity);
                } else {
                    resource = ((ResourceRepositoryV2) resourceRepository).save(entity);
                }
            } else {
                resource = ((ResourceRepository) resourceRepository).save(entity);
            }
            return getResponse(resourceRepository, resource, request);
        }
    };
    RepositoryRequestSpec requestSpec = RepositoryRequestSpecImpl.forSave(moduleRegistry, method, resourceInformation, queryAdapter, entity);
    return chain.doFilter(newRepositoryFilterContext(requestSpec));
}
Also used : RepositoryRequestSpec(io.crnk.core.engine.dispatcher.RepositoryRequestSpec) ResourceRepositoryV2(io.crnk.core.repository.ResourceRepositoryV2) RepositoryFilterContext(io.crnk.core.engine.filter.RepositoryFilterContext) ResourceRepository(io.crnk.legacy.repository.ResourceRepository)

Example 13 with RepositoryFilterContext

use of io.crnk.core.engine.filter.RepositoryFilterContext in project crnk-framework by crnk-project.

the class ResourceRepositoryAdapter method findAll.

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

        @SuppressWarnings("rawtypes")
        @Override
        protected JsonApiResponse invoke(RepositoryFilterContext context) {
            RepositoryRequestSpec request = context.getRequest();
            QueryAdapter queryAdapter = request.getQueryAdapter();
            Object resources;
            if (isAnnotated) {
                resources = ((AnnotatedResourceRepositoryAdapter) resourceRepository).findAll(queryAdapter);
            } else if (resourceRepository instanceof ResourceRepositoryV2) {
                QuerySpec querySpec = request.getQuerySpec(resourceInformation);
                resources = ((ResourceRepositoryV2) resourceRepository).findAll(querySpec);
            } else {
                resources = ((ResourceRepository) resourceRepository).findAll(request.getQueryParams());
            }
            return getResponse(resourceRepository, resources, request);
        }
    };
    RepositoryRequestSpec requestSpec = RepositoryRequestSpecImpl.forFindAll(moduleRegistry, resourceInformation, queryAdapter);
    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) QuerySpec(io.crnk.core.queryspec.QuerySpec) ResourceRepository(io.crnk.legacy.repository.ResourceRepository)

Aggregations

RepositoryRequestSpec (io.crnk.core.engine.dispatcher.RepositoryRequestSpec)13 RepositoryFilterContext (io.crnk.core.engine.filter.RepositoryFilterContext)13 QueryAdapter (io.crnk.core.engine.query.QueryAdapter)12 ResourceField (io.crnk.core.engine.information.resource.ResourceField)8 BulkRelationshipRepositoryV2 (io.crnk.core.repository.BulkRelationshipRepositoryV2)8 JsonApiResponse (io.crnk.core.repository.response.JsonApiResponse)7 RelationshipRepositoryV2 (io.crnk.core.repository.RelationshipRepositoryV2)6 ResourceRepositoryV2 (io.crnk.core.repository.ResourceRepositoryV2)5 Serializable (java.io.Serializable)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)2 RelationshipRepository (io.crnk.legacy.repository.RelationshipRepository)2 HashMap (java.util.HashMap)2 MultivaluedMap (io.crnk.core.engine.internal.utils.MultivaluedMap)1 ResourceNotFoundException (io.crnk.core.exception.ResourceNotFoundException)1 AnnotatedResourceRepositoryAdapter (io.crnk.legacy.internal.AnnotatedResourceRepositoryAdapter)1