Search in sources :

Example 51 with FilterSpec

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

the class QueryBuilder method applyFilterSpec.

protected void applyFilterSpec() {
    QueryFilterBuilder<P, F> predicateBuilder = new QueryFilterBuilder<>(backend, attributeFinder);
    MetaDataObject meta = query.getMeta();
    List<FilterSpec> filters = query.getFilterSpecs();
    List<P> predicates = predicateBuilder.filterSpecListToPredicateArray(meta, backend.getRoot(), filters);
    if (predicates != null && !predicates.isEmpty()) {
        backend.addPredicate(backend.and(predicates));
    }
    MetaAttribute parentAttr = query.getParentAttr();
    if (parentAttr != null) {
        MetaDataObject parentMeta = query.getParentMeta();
        MetaKey primaryKey = parentMeta.getPrimaryKey();
        if (primaryKey == null) {
            throw new IllegalStateException("primary key not found for " + parentAttr.getId());
        }
        MetaAttribute primaryKeyAttr = primaryKey.getUniqueElement();
        backend.addParentPredicate(primaryKeyAttr);
    }
}
Also used : MetaKey(io.crnk.meta.model.MetaKey) MetaDataObject(io.crnk.meta.model.MetaDataObject) MetaAttribute(io.crnk.meta.model.MetaAttribute) FilterSpec(io.crnk.core.queryspec.FilterSpec)

Example 52 with FilterSpec

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

the class MetaDefaultLimitIntTest method limitShouldNotAffectRelationships.

@Test
public void limitShouldNotAffectRelationships() {
    QuerySpec querySpec = new QuerySpec(MetaResource.class);
    querySpec.includeRelation(Arrays.asList("attributes"));
    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 FilterSpec

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

the class MetaDefaultLimitIntTest method limitShouldNotAffectRelationshipsWithSpecOnRelationship.

@Test
public void limitShouldNotAffectRelationshipsWithSpecOnRelationship() {
    QuerySpec querySpec = new QuerySpec(MetaResource.class);
    querySpec.includeRelation(Arrays.asList("attributes"));
    querySpec.addFilter(new FilterSpec(Arrays.asList("resourceType"), FilterOperator.EQ, "tasks"));
    querySpec.getOrCreateQuerySpec(MetaAttribute.class).addSort(new SortSpec(Arrays.asList("id"), Direction.ASC));
    querySpec.getOrCreateQuerySpec(MetaElement.class).addSort(new SortSpec(Arrays.asList("id"), Direction.ASC));
    querySpec.getOrCreateQuerySpec(MetaResourceField.class).addSort(new SortSpec(Arrays.asList("id"), Direction.ASC));
    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 : MetaResourceField(io.crnk.meta.model.resource.MetaResourceField) MetaElement(io.crnk.meta.model.MetaElement) MetaResource(io.crnk.meta.model.resource.MetaResource) MetaAttribute(io.crnk.meta.model.MetaAttribute) QuerySpec(io.crnk.core.queryspec.QuerySpec) FilterSpec(io.crnk.core.queryspec.FilterSpec) SortSpec(io.crnk.core.queryspec.SortSpec) Test(org.junit.Test)

Example 54 with FilterSpec

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

the class MetaFilteringTest method checkFilterRelationship.

@Test
public void checkFilterRelationship() throws IOException {
    RegistryEntry projectEntry = boot.getResourceRegistry().getEntry(Project.class);
    ResourceInformation projectResourceInformation = projectEntry.getResourceInformation();
    Mockito.when(filter.filterResource(Mockito.eq(projectResourceInformation), Mockito.any(HttpMethod.class))).thenReturn(FilterBehavior.FORBIDDEN);
    QuerySpec querySpec = new QuerySpec(MetaResource.class);
    querySpec.addFilter(new FilterSpec(Arrays.asList("name"), FilterOperator.EQ, "Tasks"));
    ResourceList<MetaResource> list = repository.findAll(querySpec);
    Assert.assertEquals(1, list.size());
    MetaResource taskMeta = list.get(0);
    // project attribute not available as opposite project type not available
    Assert.assertFalse(taskMeta.hasAttribute("project"));
}
Also used : ResourceInformation(io.crnk.core.engine.information.resource.ResourceInformation) MetaResource(io.crnk.meta.model.resource.MetaResource) QuerySpec(io.crnk.core.queryspec.QuerySpec) FilterSpec(io.crnk.core.queryspec.FilterSpec) RegistryEntry(io.crnk.core.engine.registry.RegistryEntry) HttpMethod(io.crnk.core.engine.http.HttpMethod) Test(org.junit.Test)

Example 55 with FilterSpec

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

the class MetaFilteringTest method checkInsertableAttribute.

@Test
public void checkInsertableAttribute() throws IOException {
    RegistryEntry entry = boot.getResourceRegistry().getEntry(Task.class);
    ResourceInformation resourceInformation = entry.getResourceInformation();
    ResourceField projectField = resourceInformation.findFieldByUnderlyingName("project");
    Mockito.when(filter.filterField(Mockito.eq(projectField), Mockito.eq(HttpMethod.POST))).thenReturn(FilterBehavior.NONE);
    Mockito.when(filter.filterField(Mockito.eq(projectField), Mockito.eq(HttpMethod.PATCH))).thenReturn(FilterBehavior.FORBIDDEN);
    QuerySpec querySpec = new QuerySpec(MetaResource.class);
    querySpec.addFilter(new FilterSpec(Arrays.asList("name"), FilterOperator.EQ, "Tasks"));
    ResourceList<MetaResource> list = repository.findAll(querySpec);
    Assert.assertEquals(1, list.size());
    MetaResource taskMeta = list.get(0);
    Assert.assertTrue(taskMeta.hasAttribute("project"));
    MetaAttribute projectAttr = taskMeta.getAttribute("project");
    Assert.assertTrue(projectAttr.isInsertable());
    Assert.assertFalse(projectAttr.isUpdatable());
}
Also used : ResourceField(io.crnk.core.engine.information.resource.ResourceField) ResourceInformation(io.crnk.core.engine.information.resource.ResourceInformation) MetaResource(io.crnk.meta.model.resource.MetaResource) MetaAttribute(io.crnk.meta.model.MetaAttribute) QuerySpec(io.crnk.core.queryspec.QuerySpec) FilterSpec(io.crnk.core.queryspec.FilterSpec) RegistryEntry(io.crnk.core.engine.registry.RegistryEntry) Test(org.junit.Test)

Aggregations

FilterSpec (io.crnk.core.queryspec.FilterSpec)56 QuerySpec (io.crnk.core.queryspec.QuerySpec)51 Test (org.junit.Test)40 HashSet (java.util.HashSet)14 Set (java.util.Set)13 AbstractQuerySpecTest (io.crnk.core.queryspec.AbstractQuerySpecTest)12 HashMap (java.util.HashMap)12 RegistryEntry (io.crnk.core.engine.registry.RegistryEntry)9 TaskResource (io.crnk.activiti.resource.TaskResource)8 MetaResource (io.crnk.meta.model.resource.MetaResource)8 ScheduleApprovalProcessInstance (io.crnk.activiti.example.model.ScheduleApprovalProcessInstance)5 ResourceInformation (io.crnk.core.engine.information.resource.ResourceInformation)5 MetaAttribute (io.crnk.meta.model.MetaAttribute)5 ResourceField (io.crnk.core.engine.information.resource.ResourceField)4 ResourceList (io.crnk.core.resource.list.ResourceList)3 RelatedEntity (io.crnk.jpa.model.RelatedEntity)3 ArrayList (java.util.ArrayList)3 MultivaluedMap (io.crnk.core.engine.internal.utils.MultivaluedMap)2 ResourceRepositoryV2 (io.crnk.core.repository.ResourceRepositoryV2)2 JsonApiResponse (io.crnk.core.repository.response.JsonApiResponse)2