Search in sources :

Example 11 with QueryAdapter

use of io.crnk.core.engine.query.QueryAdapter 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 12 with QueryAdapter

use of io.crnk.core.engine.query.QueryAdapter in project crnk-framework by crnk-project.

the class IncludeLookupSetter method setIncludedElements.

public void setIncludedElements(Document document, Object entity, QueryAdapter queryAdapter, DocumentMappingConfig mappingConfig) {
    QueryAdapter inclusionQueryAdapter = queryAdapter;
    if (!allowPagination && !(queryAdapter instanceof QueryParamsAdapter) && queryAdapter != null) {
        // offset/limit cannot properly work for nested inclusions if becomes cyclic
        inclusionQueryAdapter = queryAdapter.duplicate();
        if (queryAdapter.getResourceInformation().getPagingBehavior() != null) {
            inclusionQueryAdapter.setPagingSpec(queryAdapter.getResourceInformation().getPagingBehavior().createEmptyPagingSpec());
        }
    }
    List<Object> entityList = DocumentMapperUtil.toList(entity);
    List<Resource> dataList = DocumentMapperUtil.toList(document.getData().get());
    Map<ResourceIdentifier, Resource> dataMap = new HashMap<>();
    Map<ResourceIdentifier, Object> entityMap = new HashMap<>();
    for (int i = 0; i < dataList.size(); i++) {
        Resource dataElement = dataList.get(i);
        ResourceIdentifier id = dataElement.toIdentifier();
        entityMap.put(id, entityList.get(i));
        dataMap.put(id, dataElement);
    }
    Map<ResourceIdentifier, Resource> resourceMap = new HashMap<>();
    resourceMap.putAll(dataMap);
    Set<ResourceIdentifier> inclusions = new HashSet<>();
    PopulatedCache populatedCache = new PopulatedCache();
    RepositoryMethodParameterProvider parameterProvider = mappingConfig.getParameterProvider();
    Set<String> fieldsWithEnforcedIdSerialization = mappingConfig.getFieldsWithEnforcedIdSerialization();
    ResourceMappingConfig resourceMappingConfig = mappingConfig.getResourceMapping();
    ArrayList<ResourceField> stack = new ArrayList<>();
    populate(dataList, inclusions, resourceMap, entityMap, stack, inclusionQueryAdapter, parameterProvider, fieldsWithEnforcedIdSerialization, populatedCache, resourceMappingConfig);
    // no need to include resources included in the data section
    inclusions.removeAll(dataMap.keySet());
    // setup included section
    ArrayList<Resource> included = new ArrayList<>();
    for (ResourceIdentifier inclusionId : inclusions) {
        Resource includedResource = resourceMap.get(inclusionId);
        PreconditionUtil.assertNotNull("resource not found", includedResource);
        included.add(includedResource);
    }
    Collections.sort(included);
    LOGGER.debug("Extracted included resources {}", included.toString());
    document.setIncluded(included);
}
Also used : HashMap(java.util.HashMap) QueryAdapter(io.crnk.core.engine.query.QueryAdapter) Resource(io.crnk.core.engine.document.Resource) ArrayList(java.util.ArrayList) ResourceIdentifier(io.crnk.core.engine.document.ResourceIdentifier) ResourceField(io.crnk.core.engine.information.resource.ResourceField) QueryParamsAdapter(io.crnk.legacy.internal.QueryParamsAdapter) RepositoryMethodParameterProvider(io.crnk.legacy.internal.RepositoryMethodParameterProvider) HashSet(java.util.HashSet)

Example 13 with QueryAdapter

use of io.crnk.core.engine.query.QueryAdapter in project crnk-framework by crnk-project.

the class HttpRequestProcessorImpl method dispatchRequest.

/**
 * Dispatch the request from a client
 *
 * @param path              built represents the URI sent in the request
 * @param method            type of the request e.g. POST, GET, PATCH
 * @param parameterProvider repository method legacy provider
 * @param requestBody       deserialized body of the client request
 * @return the response form the Crnk
 */
@Override
public Response dispatchRequest(String path, String method, Map<String, Set<String>> parameters, RepositoryMethodParameterProvider parameterProvider, Document requestBody) {
    JsonPath jsonPath = new PathBuilder(moduleRegistry.getResourceRegistry()).build(path);
    try {
        BaseController controller = controllerRegistry.getController(jsonPath, method);
        ResourceInformation resourceInformation = getRequestedResource(jsonPath);
        QueryAdapter queryAdapter = queryAdapterBuilder.build(resourceInformation, parameters);
        DefaultFilterRequestContext context = new DefaultFilterRequestContext(jsonPath, queryAdapter, parameterProvider, requestBody, method);
        DefaultFilterChain chain = new DefaultFilterChain(controller);
        return chain.doFilter(context);
    } catch (Exception e) {
        Optional<JsonApiExceptionMapper> exceptionMapper = exceptionMapperRegistry.findMapperFor(e.getClass());
        if (exceptionMapper.isPresent()) {
            // noinspection unchecked
            logger.debug("dispatching exception to mapper", e);
            return exceptionMapper.get().toErrorResponse(e).toResponse();
        } else {
            logger.error("failed to process request", e);
            throw e;
        }
    }
}
Also used : PathBuilder(io.crnk.core.engine.internal.dispatcher.path.PathBuilder) ResourceInformation(io.crnk.core.engine.information.resource.ResourceInformation) Optional(io.crnk.core.utils.Optional) BaseController(io.crnk.core.engine.internal.dispatcher.controller.BaseController) QueryAdapter(io.crnk.core.engine.query.QueryAdapter) JsonPath(io.crnk.core.engine.internal.dispatcher.path.JsonPath) ResourceFieldNotFoundException(io.crnk.core.exception.ResourceFieldNotFoundException) IOException(java.io.IOException)

Example 14 with QueryAdapter

use of io.crnk.core.engine.query.QueryAdapter in project crnk-framework by crnk-project.

the class TotalBasedPagedLinksInformationTest method setup.

@Before
public void setup() {
    TotalResourceCountTestRepository.clear();
    super.setup();
    RegistryEntry registryEntry = resourceRegistry.findEntry(Task.class);
    TotalResourceCountTestRepository repo = (TotalResourceCountTestRepository) registryEntry.getResourceRepository(null).getResourceRepository();
    adapter = registryEntry.getResourceRepository(null);
    QueryAdapter queryAdapter = new QuerySpecAdapter(querySpec(), resourceRegistry);
    for (long i = 0; i < 5; i++) {
        Task task = new Task();
        task.setId(i);
        task.setName("myTask");
        adapter.create(task, queryAdapter);
    }
}
Also used : Task(io.crnk.core.mock.models.Task) QueryAdapter(io.crnk.core.engine.query.QueryAdapter) QuerySpecAdapter(io.crnk.core.queryspec.internal.QuerySpecAdapter) RegistryEntry(io.crnk.core.engine.registry.RegistryEntry) Before(org.junit.Before)

Example 15 with QueryAdapter

use of io.crnk.core.engine.query.QueryAdapter in project crnk-framework by crnk-project.

the class QuerySpecRepositoryTest method testCrudWithQuerySpec.

@Test
public void testCrudWithQuerySpec() {
    QuerySpec querySpec = new QuerySpec(Task.class);
    QueryAdapter adapter = new QuerySpecAdapter(querySpec, resourceRegistry);
    checkCrud(adapter);
}
Also used : QueryAdapter(io.crnk.core.engine.query.QueryAdapter) QuerySpec(io.crnk.core.queryspec.QuerySpec) QuerySpecAdapter(io.crnk.core.queryspec.internal.QuerySpecAdapter) AbstractQuerySpecTest(io.crnk.core.queryspec.AbstractQuerySpecTest) Test(org.junit.Test)

Aggregations

QueryAdapter (io.crnk.core.engine.query.QueryAdapter)35 QuerySpecAdapter (io.crnk.core.queryspec.internal.QuerySpecAdapter)15 QuerySpec (io.crnk.core.queryspec.QuerySpec)14 ResourceField (io.crnk.core.engine.information.resource.ResourceField)13 RepositoryRequestSpec (io.crnk.core.engine.dispatcher.RepositoryRequestSpec)12 RepositoryFilterContext (io.crnk.core.engine.filter.RepositoryFilterContext)12 JsonApiResponse (io.crnk.core.repository.response.JsonApiResponse)10 ResourceInformation (io.crnk.core.engine.information.resource.ResourceInformation)9 BulkRelationshipRepositoryV2 (io.crnk.core.repository.BulkRelationshipRepositoryV2)8 ResourceRepositoryAdapter (io.crnk.core.engine.internal.repository.ResourceRepositoryAdapter)7 RelationshipRepositoryV2 (io.crnk.core.repository.RelationshipRepositoryV2)6 Resource (io.crnk.core.engine.document.Resource)5 Task (io.crnk.core.mock.models.Task)5 Serializable (java.io.Serializable)5 ResourceRepositoryV2 (io.crnk.core.repository.ResourceRepositoryV2)4 AnnotatedRelationshipRepositoryAdapter (io.crnk.legacy.internal.AnnotatedRelationshipRepositoryAdapter)4 Before (org.junit.Before)4 Test (org.junit.Test)4 RelationshipRepositoryAdapter (io.crnk.core.engine.internal.repository.RelationshipRepositoryAdapter)3 RegistryEntry (io.crnk.core.engine.registry.RegistryEntry)3