Search in sources :

Example 16 with FilterSpec

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

the class ResourceRepositoryBase method findOne.

/**
 * Forwards to {@link #findAll(QuerySpec)}
 *
 * @param id        of the resource
 * @param querySpec for field and relation inclusion
 * @return resource
 */
@Override
public T findOne(I id, QuerySpec querySpec) {
    RegistryEntry entry = resourceRegistry.findEntry(resourceClass);
    String idName = entry.getResourceInformation().getIdField().getUnderlyingName();
    QuerySpec idQuerySpec = querySpec.duplicate();
    idQuerySpec.addFilter(new FilterSpec(Arrays.asList(idName), FilterOperator.EQ, id));
    Iterable<T> iterable = findAll(idQuerySpec);
    Iterator<T> iterator = iterable.iterator();
    if (iterator.hasNext()) {
        T resource = iterator.next();
        PreconditionUtil.assertFalse("expected unique result", iterator.hasNext());
        return resource;
    } else {
        throw new ResourceNotFoundException("resource not found");
    }
}
Also used : QuerySpec(io.crnk.core.queryspec.QuerySpec) FilterSpec(io.crnk.core.queryspec.FilterSpec) ResourceNotFoundException(io.crnk.core.exception.ResourceNotFoundException) RegistryEntry(io.crnk.core.engine.registry.RegistryEntry)

Example 17 with FilterSpec

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

the class ResourceRepositoryBase method findAll.

/**
 * Forwards to {@link #findAll(QuerySpec)}
 *
 * @param ids       of the resources
 * @param querySpec for field and relation inclusion
 * @return resources
 */
@Override
public ResourceList<T> findAll(Iterable<I> ids, QuerySpec querySpec) {
    RegistryEntry entry = resourceRegistry.findEntry(resourceClass);
    String idName = entry.getResourceInformation().getIdField().getUnderlyingName();
    QuerySpec idQuerySpec = querySpec.duplicate();
    idQuerySpec.addFilter(new FilterSpec(Arrays.asList(idName), FilterOperator.EQ, ids));
    return findAll(idQuerySpec);
}
Also used : QuerySpec(io.crnk.core.queryspec.QuerySpec) FilterSpec(io.crnk.core.queryspec.FilterSpec) RegistryEntry(io.crnk.core.engine.registry.RegistryEntry)

Example 18 with FilterSpec

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

the class DefaultQueryParamsConverter method applyFiltering.

protected void applyFiltering(QuerySpec spec, QueryParams queryParams) {
    List<FilterSpec> filters = spec.getFilters();
    Map<String, FilterParams> decodedFiltersMap = new LinkedHashMap<>();
    if (filters != null && !filters.isEmpty()) {
        String resourceType = getResourceType(spec.getResourceClass());
        Map<String, Set<String>> map = new LinkedHashMap<>();
        for (FilterSpec filter : filters) {
            applyFilterSpec(map, filter);
        }
        decodedFiltersMap.put(resourceType, new FilterParams(map));
    }
    queryParams.setFilters(new TypedParams<>(Collections.unmodifiableMap(decodedFiltersMap)));
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Set(java.util.Set) FilterParams(io.crnk.legacy.queryParams.params.FilterParams) FilterSpec(io.crnk.core.queryspec.FilterSpec) LinkedHashMap(java.util.LinkedHashMap)

Example 19 with FilterSpec

use of io.crnk.core.queryspec.FilterSpec 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 20 with FilterSpec

use of io.crnk.core.queryspec.FilterSpec 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)

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