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);
}
}
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);
}
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);
}
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"));
}
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());
}
Aggregations