Search in sources :

Example 51 with QuerySpec

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

the class GetFromOwnerStrategy method findTargets.

@SuppressWarnings("unchecked")
public MultivaluedMap<I, D> findTargets(Iterable<I> sourceIds, String fieldName, QuerySpec querySpec) {
    RegistryEntry sourceEntry = context.getSourceEntry();
    ResourceInformation sourceInformation = sourceEntry.getResourceInformation();
    ResourceField field = sourceInformation.findFieldByUnderlyingName(fieldName);
    RegistryEntry targetEntry = context.getTargetEntry(field);
    List sources = (List) sourceEntry.getResourceRepository().findAll(sourceIds, context.createSaveQueryAdapter(fieldName)).getEntity();
    ResourceInformation targetInformation = targetEntry.getResourceInformation();
    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 = targetEntry.getResourceRepository();
        JsonApiResponse response = targetAdapter.findAll(targetIds, context.createQueryAdapter(idQuerySpec));
        targets = (List<D>) response.getEntity();
        return toResult(field, 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) 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 52 with QuerySpec

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

the class MetaDefaultLimitIntTest method limitShouldNotAffectRelationshipsWithCustomLimit.

@Test
public void limitShouldNotAffectRelationshipsWithCustomLimit() {
    QuerySpec querySpec = new QuerySpec(MetaResource.class);
    querySpec.includeRelation(Arrays.asList("attributes"));
    querySpec.setLimit(3L);
    querySpec.addFilter(new FilterSpec(Arrays.asList("resourceType"), FilterOperator.EQ, "tasks"));
    ResourceList<MetaResource> list = repository.findAll(querySpec);
    Assert.assertEquals(1, list.size());
    MetaResource taskMeta = list.get(0);
    List<? extends MetaAttribute> attributes = taskMeta.getAttributes();
    Assert.assertTrue(attributes.size() > 5);
}
Also used : MetaResource(io.crnk.meta.model.resource.MetaResource) QuerySpec(io.crnk.core.queryspec.QuerySpec) FilterSpec(io.crnk.core.queryspec.FilterSpec) Test(org.junit.Test)

Example 53 with QuerySpec

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

the class MetaDefaultLimitIntTest method limitShouldNotAffectRelationshipsWithMetaElement.

@Test
public void limitShouldNotAffectRelationshipsWithMetaElement() {
    ResourceRepositoryV2<MetaElement, Serializable> elementRepository = client.getRepositoryForType(MetaElement.class);
    QuerySpec querySpec = new QuerySpec(MetaElement.class);
    querySpec.includeRelation(Arrays.asList("attributes"));
    querySpec.addFilter(new FilterSpec(Arrays.asList("id"), FilterOperator.EQ, "resources.tasks"));
    ResourceList<MetaElement> list = elementRepository.findAll(querySpec);
    Assert.assertEquals(1, list.size());
    MetaResource taskMeta = (MetaResource) list.get(0);
    List<? extends MetaAttribute> attributes = taskMeta.getAttributes();
    Assert.assertTrue(attributes.size() > 5);
}
Also used : Serializable(java.io.Serializable) MetaElement(io.crnk.meta.model.MetaElement) MetaResource(io.crnk.meta.model.resource.MetaResource) QuerySpec(io.crnk.core.queryspec.QuerySpec) FilterSpec(io.crnk.core.queryspec.FilterSpec) Test(org.junit.Test)

Example 54 with QuerySpec

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

the class MetaDefaultLimitIntTest method limitShouldApplyToResults.

@Test
public void limitShouldApplyToResults() {
    QuerySpec querySpec = new QuerySpec(MetaResource.class);
    querySpec.includeRelation(Arrays.asList("attributes"));
    ResourceList<MetaResource> list = repository.findAll(querySpec);
    Assert.assertEquals(2, list.size());
}
Also used : MetaResource(io.crnk.meta.model.resource.MetaResource) QuerySpec(io.crnk.core.queryspec.QuerySpec) Test(org.junit.Test)

Example 55 with QuerySpec

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

the class MetaIntTest method testIdPrefix.

@Test
public void testIdPrefix() {
    ResourceList<MetaElement> list = repository.findAll(new QuerySpec(MetaElement.class));
    Assert.assertFalse(list.isEmpty());
    for (MetaElement elem : list) {
        if (elem instanceof MetaPrimitiveType) {
            Assert.assertTrue(elem.getId(), elem.getId().startsWith("base."));
        } else {
            Assert.assertTrue(elem.getId(), elem.getId().startsWith("base.") || elem.getId().startsWith("resources.") || elem.getId().startsWith("app.resources.") || elem.getId().startsWith("io.crnk"));
        }
    }
}
Also used : QuerySpec(io.crnk.core.queryspec.QuerySpec) Test(org.junit.Test)

Aggregations

QuerySpec (io.crnk.core.queryspec.QuerySpec)306 Test (org.junit.Test)233 FilterSpec (io.crnk.core.queryspec.FilterSpec)51 Document (io.crnk.core.engine.document.Document)45 Resource (io.crnk.core.engine.document.Resource)43 Set (java.util.Set)39 HashMap (java.util.HashMap)37 HashSet (java.util.HashSet)36 AbstractQuerySpecTest (io.crnk.core.queryspec.AbstractQuerySpecTest)34 QuerySpecAdapter (io.crnk.core.queryspec.internal.QuerySpecAdapter)32 AbstractJpaJerseyTest (io.crnk.jpa.AbstractJpaJerseyTest)32 Task (io.crnk.test.mock.models.Task)32 Project (io.crnk.core.mock.models.Project)28 Relationship (io.crnk.core.engine.document.Relationship)26 Task (io.crnk.core.mock.models.Task)26 TestEntity (io.crnk.jpa.model.TestEntity)26 ResourceIdentifier (io.crnk.core.engine.document.ResourceIdentifier)25 Serializable (java.io.Serializable)24 RelatedEntity (io.crnk.jpa.model.RelatedEntity)21 ResourceRegistryTest (io.crnk.core.resource.registry.ResourceRegistryTest)20