Search in sources :

Example 41 with QuerySpecAdapter

use of io.crnk.core.queryspec.internal.QuerySpecAdapter in project crnk-framework by crnk-project.

the class ImplicitOwnerBasedRelationshipRepository method findTargets.

@SuppressWarnings("unchecked")
public MultivaluedMap<I, D> findTargets(Iterable<I> sourceIds, String fieldName, QuerySpec querySpec) {
    RegistryEntry sourceEntry = getSourceEntry();
    ResourceInformation sourceInformation = sourceEntry.getResourceInformation();
    ResourceField field = sourceInformation.findFieldByUnderlyingName(fieldName);
    List sources = (List) sourceEntry.getResourceRepository().findAll(sourceIds, getSaveQueryAdapter(fieldName)).getEntity();
    ResourceInformation targetInformation = getTargetInformation(field);
    List<D> targets;
    if (field.hasIdField()) {
        Set targetIds = new HashSet();
        for (Object source : sources) {
            Object targetId = field.getIdAccessor().getValue(source);
            if (field.isCollection()) {
                targetIds.addAll((Collection) targetId);
            } else {
                targetIds.add(targetId);
            }
        }
        QuerySpec idQuerySpec = new QuerySpec(targetInformation);
        ResourceRepositoryAdapter<D, J> targetAdapter = getTargetEntry(field).getResourceRepository();
        JsonApiResponse response = targetAdapter.findAll(targetIds, new QuerySpecAdapter(idQuerySpec, resourceRegistry));
        targets = (List<D>) response.getEntity();
        return toResult(fieldName, targetInformation, sources, targets);
    } else {
        MultivaluedMap bulkResult = new MultivaluedMap<I, D>() {

            @Override
            protected List<D> newList() {
                return new DefaultResourceList<>();
            }
        };
        for (Object source : sources) {
            Object sourceId = sourceInformation.getId(source);
            Object target = field.getAccessor().getValue(source);
            if (target != null) {
                if (field.isCollection()) {
                    bulkResult.addAll(sourceId, (Collection) target);
                } else {
                    bulkResult.add(sourceId, target);
                }
            }
        }
        return bulkResult;
    }
}
Also used : ResourceInformation(io.crnk.core.engine.information.resource.ResourceInformation) Set(java.util.Set) HashSet(java.util.HashSet) QuerySpecAdapter(io.crnk.core.queryspec.internal.QuerySpecAdapter) RegistryEntry(io.crnk.core.engine.registry.RegistryEntry) ResourceField(io.crnk.core.engine.information.resource.ResourceField) DefaultResourceList(io.crnk.core.resource.list.DefaultResourceList) DefaultResourceList(io.crnk.core.resource.list.DefaultResourceList) List(java.util.List) JsonApiResponse(io.crnk.core.repository.response.JsonApiResponse) QuerySpec(io.crnk.core.queryspec.QuerySpec) MultivaluedMap(io.crnk.core.engine.internal.utils.MultivaluedMap) HashSet(java.util.HashSet)

Example 42 with QuerySpecAdapter

use of io.crnk.core.queryspec.internal.QuerySpecAdapter in project crnk-framework by crnk-project.

the class ForwardingStrategyContext method createSaveQueryAdapter.

protected QueryAdapter createSaveQueryAdapter(String fieldName) {
    QuerySpec querySpec = createSourceQuerySpec();
    querySpec.includeRelation(Arrays.asList(fieldName));
    return new QuerySpecAdapter(querySpec, resourceRegistry);
}
Also used : QuerySpec(io.crnk.core.queryspec.QuerySpec) QuerySpecAdapter(io.crnk.core.queryspec.internal.QuerySpecAdapter)

Example 43 with QuerySpecAdapter

use of io.crnk.core.queryspec.internal.QuerySpecAdapter in project crnk-framework by crnk-project.

the class RelationshipRepositoryBase method getTarget.

@SuppressWarnings("unchecked")
protected D getTarget(RegistryEntry entry, J targetId) {
    if (targetId == null) {
        return null;
    }
    ResourceRepositoryAdapter<D, J> targetAdapter = entry.getResourceRepository();
    QueryAdapter queryAdapter = new QuerySpecAdapter(new QuerySpec(entry.getResourceInformation()), resourceRegistry);
    D target = (D) targetAdapter.findOne(targetId, queryAdapter).getEntity();
    if (target == null) {
        throw new IllegalStateException(targetId + " not found");
    }
    return target;
}
Also used : QueryAdapter(io.crnk.core.engine.query.QueryAdapter) QuerySpecAdapter(io.crnk.core.queryspec.internal.QuerySpecAdapter) QuerySpec(io.crnk.core.queryspec.QuerySpec)

Example 44 with QuerySpecAdapter

use of io.crnk.core.queryspec.internal.QuerySpecAdapter in project crnk-framework by crnk-project.

the class OffsetLimitPagingBehaviorTest method testBuild.

@Test
public void testBuild() {
    PagingBehavior pagingBehavior = new OffsetLimitPagingBehavior();
    OffsetLimitPagingSpec pagingSpec = new OffsetLimitPagingSpec(0L, 10L);
    ModuleRegistry moduleRegistry = new ModuleRegistry();
    ResourceRegistry resourceRegistry = new ResourceRegistryImpl(new DefaultResourceRegistryPart(), moduleRegistry);
    QuerySpec spec = new QuerySpec(Task.class);
    QuerySpecAdapter querySpecAdapter = new QuerySpecAdapter(spec, resourceRegistry);
    querySpecAdapter.setPagingSpec(pagingSpec);
    PagingSpecUrlBuilder urlBuilder = mock(PagingSpecUrlBuilder.class);
    when(urlBuilder.build(any(QuerySpecAdapter.class))).thenReturn("http://some.org");
    DefaultPagedMetaInformation pagedMetaInformation = new DefaultPagedMetaInformation();
    pagedMetaInformation.setTotalResourceCount(30L);
    ResourceList resourceList = new DefaultResourceList(pagedMetaInformation, null);
    for (int i = 0; i < 30; i++) {
        resourceList.add(new Task());
    }
    PagedLinksInformation pagedLinksInformation = new DefaultPagedLinksInformation();
    pagingBehavior.build(pagedLinksInformation, resourceList, querySpecAdapter, urlBuilder);
    assertThat(pagedLinksInformation.getFirst(), equalTo("http://some.org"));
    assertThat(pagedLinksInformation.getNext(), equalTo("http://some.org"));
    assertNull(pagedLinksInformation.getPrev());
    assertThat(pagedLinksInformation.getLast(), equalTo("http://some.org"));
}
Also used : Task(io.crnk.core.mock.models.Task) DefaultPagedLinksInformation(io.crnk.core.resource.links.DefaultPagedLinksInformation) ModuleRegistry(io.crnk.core.module.ModuleRegistry) ResourceRegistryImpl(io.crnk.core.engine.internal.registry.ResourceRegistryImpl) ResourceRegistry(io.crnk.core.engine.registry.ResourceRegistry) QuerySpecAdapter(io.crnk.core.queryspec.internal.QuerySpecAdapter) DefaultPagedMetaInformation(io.crnk.core.resource.meta.DefaultPagedMetaInformation) ResourceList(io.crnk.core.resource.list.ResourceList) DefaultResourceList(io.crnk.core.resource.list.DefaultResourceList) DefaultResourceList(io.crnk.core.resource.list.DefaultResourceList) DefaultPagedLinksInformation(io.crnk.core.resource.links.DefaultPagedLinksInformation) PagedLinksInformation(io.crnk.core.resource.links.PagedLinksInformation) DefaultResourceRegistryPart(io.crnk.core.engine.registry.DefaultResourceRegistryPart) QuerySpec(io.crnk.core.queryspec.QuerySpec) Test(org.junit.Test)

Example 45 with QuerySpecAdapter

use of io.crnk.core.queryspec.internal.QuerySpecAdapter in project crnk-framework by crnk-project.

the class CustomResourceRegistryTest method test.

@Test
public void test() {
    CrnkBoot boot = new CrnkBoot();
    boot.addModule(new CustomRegistryPartModule());
    boot.setServiceDiscovery(new TestServiceDiscovery());
    boot.boot();
    ResourceRegistry resourceRegistry = boot.getResourceRegistry();
    RegistryEntry entry = resourceRegistry.getEntry("somePrefix/custom");
    Assert.assertNotNull(entry);
    ResourceRepositoryAdapter adapter = entry.getResourceRepository();
    QueryAdapter queryAdapter = new QuerySpecAdapter(new QuerySpec("somePrefix/custom"), resourceRegistry);
    JsonApiResponse response = adapter.findAll(queryAdapter);
    Assert.assertNotNull(response.getEntity());
    List<Resource> resources = (List<Resource>) response.getEntity();
    Assert.assertEquals(1, resources.size());
}
Also used : QueryAdapter(io.crnk.core.engine.query.QueryAdapter) Resource(io.crnk.core.engine.document.Resource) QuerySpecAdapter(io.crnk.core.queryspec.internal.QuerySpecAdapter) CrnkBoot(io.crnk.core.boot.CrnkBoot) TestServiceDiscovery(io.crnk.core.module.discovery.TestServiceDiscovery) ResourceRepositoryAdapter(io.crnk.core.engine.internal.repository.ResourceRepositoryAdapter) JsonApiResponse(io.crnk.core.repository.response.JsonApiResponse) List(java.util.List) QuerySpec(io.crnk.core.queryspec.QuerySpec) Test(org.junit.Test)

Aggregations

QuerySpecAdapter (io.crnk.core.queryspec.internal.QuerySpecAdapter)48 QuerySpec (io.crnk.core.queryspec.QuerySpec)32 Test (org.junit.Test)25 QueryAdapter (io.crnk.core.engine.query.QueryAdapter)15 AbstractQuerySpecTest (io.crnk.core.queryspec.AbstractQuerySpecTest)13 Task (io.crnk.core.mock.models.Task)11 JsonApiResponse (io.crnk.core.repository.response.JsonApiResponse)11 PagedLinksInformation (io.crnk.core.resource.links.PagedLinksInformation)11 Document (io.crnk.core.engine.document.Document)10 Resource (io.crnk.core.engine.document.Resource)8 RegistryEntry (io.crnk.core.engine.registry.RegistryEntry)8 ResourceInformation (io.crnk.core.engine.information.resource.ResourceInformation)7 Before (org.junit.Before)7 Relationship (io.crnk.core.engine.document.Relationship)6 ResourceRepositoryAdapter (io.crnk.core.engine.internal.repository.ResourceRepositoryAdapter)6 Project (io.crnk.core.mock.models.Project)6 HasMoreResourcesMetaInformation (io.crnk.core.resource.meta.HasMoreResourcesMetaInformation)5 CrnkBoot (io.crnk.core.boot.CrnkBoot)4 Response (io.crnk.core.engine.dispatcher.Response)4 ResourceField (io.crnk.core.engine.information.resource.ResourceField)4