Search in sources :

Example 6 with QueryParamsAdapter

use of io.crnk.legacy.internal.QueryParamsAdapter 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 7 with QueryParamsAdapter

use of io.crnk.legacy.internal.QueryParamsAdapter in project crnk-framework by crnk-project.

the class QueryParamsAdapterTest method testGetNonExistingResourceClass.

@Test(expected = IllegalStateException.class)
public void testGetNonExistingResourceClass() {
    QueryParams params = new QueryParams();
    QueryParamsAdapter adapter = new QueryParamsAdapter(params);
    adapter.getResourceInformation();
}
Also used : QueryParamsAdapter(io.crnk.legacy.internal.QueryParamsAdapter) Test(org.junit.Test)

Example 8 with QueryParamsAdapter

use of io.crnk.legacy.internal.QueryParamsAdapter in project crnk-framework by crnk-project.

the class QuerySpecRepositoryTest method testCrudWithQueryParamsInput.

@Test
public void testCrudWithQueryParamsInput() {
    Map<String, Set<String>> params = new HashMap<String, Set<String>>();
    addParams(params, "sort[tasks][name]", "asc");
    QueryParams queryParams = queryParamsBuilder.buildQueryParams(params);
    QueryParamsAdapter queryAdapter = new QueryParamsAdapter(resourceRegistry.getEntry(Task.class).getResourceInformation(), queryParams, moduleRegistry);
    checkCrud(queryAdapter);
}
Also used : QueryParams(io.crnk.legacy.queryParams.QueryParams) QueryParamsAdapter(io.crnk.legacy.internal.QueryParamsAdapter) AbstractQuerySpecTest(io.crnk.core.queryspec.AbstractQuerySpecTest) Test(org.junit.Test)

Example 9 with QueryParamsAdapter

use of io.crnk.legacy.internal.QueryParamsAdapter in project crnk-framework by crnk-project.

the class ResourceIdControllerTest method toQueryAdapter.

private QueryAdapter toQueryAdapter(QueryParams requestParams, Class resourceType) {
    ResourceRegistry resourceRegistry = moduleRegistry.getResourceRegistry();
    RegistryEntry entry = resourceRegistry.getEntry(resourceType);
    ResourceInformation resourceInformation = entry.getResourceInformation();
    return new QueryParamsAdapter(resourceInformation, requestParams, moduleRegistry);
}
Also used : ResourceInformation(io.crnk.core.engine.information.resource.ResourceInformation) ResourceRegistry(io.crnk.core.engine.registry.ResourceRegistry) QueryParamsAdapter(io.crnk.legacy.internal.QueryParamsAdapter) RegistryEntry(io.crnk.core.engine.registry.RegistryEntry)

Example 10 with QueryParamsAdapter

use of io.crnk.legacy.internal.QueryParamsAdapter in project crnk-framework by crnk-project.

the class RepositoryAdapterTest method setUp.

@Before
public void setUp() throws Exception {
    queryParams = new QueryParams();
    queryAdapter = new QueryParamsAdapter(queryParams);
    parameterFactory = new ParametersFactory(new NewInstanceRepositoryMethodParameterProvider());
}
Also used : NewInstanceRepositoryMethodParameterProvider(io.crnk.core.engine.repository.mock.NewInstanceRepositoryMethodParameterProvider) ParametersFactory(io.crnk.legacy.internal.ParametersFactory) QueryParams(io.crnk.legacy.queryParams.QueryParams) QueryParamsAdapter(io.crnk.legacy.internal.QueryParamsAdapter) Before(org.junit.Before)

Aggregations

QueryParamsAdapter (io.crnk.legacy.internal.QueryParamsAdapter)17 Test (org.junit.Test)11 QueryParams (io.crnk.legacy.queryParams.QueryParams)10 Resource (io.crnk.core.engine.document.Resource)6 Response (io.crnk.core.engine.dispatcher.Response)5 JsonPath (io.crnk.core.engine.internal.dispatcher.path.JsonPath)5 ResourceInformation (io.crnk.core.engine.information.resource.ResourceInformation)4 RegistryEntry (io.crnk.core.engine.registry.RegistryEntry)4 ResourceRegistry (io.crnk.core.engine.registry.ResourceRegistry)4 Project (io.crnk.core.mock.models.Project)4 Task (io.crnk.core.mock.models.Task)4 DefaultQueryParamsParser (io.crnk.legacy.queryParams.DefaultQueryParamsParser)4 QueryParamsBuilder (io.crnk.legacy.queryParams.QueryParamsBuilder)4 Document (io.crnk.core.engine.document.Document)3 ResourceModificationFilter (io.crnk.core.engine.filter.ResourceModificationFilter)3 ResourceField (io.crnk.core.engine.information.resource.ResourceField)3 BaseControllerTest (io.crnk.core.engine.internal.dispatcher.controller.BaseControllerTest)3 NewInstanceRepositoryMethodParameterProvider (io.crnk.core.engine.repository.mock.NewInstanceRepositoryMethodParameterProvider)3 TaskToProjectRepository (io.crnk.core.mock.repository.TaskToProjectRepository)3 ParametersFactory (io.crnk.legacy.internal.ParametersFactory)3