Search in sources :

Example 1 with Inclusion

use of io.crnk.legacy.queryParams.include.Inclusion in project crnk-framework by crnk-project.

the class QuerySpecAdapter method addRelations.

private void addRelations(Map<String, IncludedRelationsParams> params, QuerySpec spec) {
    if (!spec.getIncludedRelations().isEmpty()) {
        Set<Inclusion> set = new HashSet<>();
        for (IncludeRelationSpec relation : spec.getIncludedRelations()) {
            set.add(new Inclusion(StringUtils.join(".", relation.getAttributePath())));
        }
        params.put(getResourceType(spec), new IncludedRelationsParams(set));
    }
}
Also used : Inclusion(io.crnk.legacy.queryParams.include.Inclusion) IncludeRelationSpec(io.crnk.core.queryspec.IncludeRelationSpec) IncludedRelationsParams(io.crnk.legacy.queryParams.params.IncludedRelationsParams) HashSet(java.util.HashSet)

Example 2 with Inclusion

use of io.crnk.legacy.queryParams.include.Inclusion in project crnk-framework by crnk-project.

the class DefaultQueryParamsConverter method applyRelatedFields.

protected void applyRelatedFields(QuerySpec spec, QueryParams queryParams) {
    List<IncludeRelationSpec> includedRelations = spec.getIncludedRelations();
    Map<String, IncludedRelationsParams> decodedSparseMap = new LinkedHashMap<>();
    if (includedRelations != null && !includedRelations.isEmpty()) {
        String resourceType = spec.getResourceType() != null ? spec.getResourceType() : getResourceType(spec.getResourceClass());
        Set<Inclusion> inclusions = new LinkedHashSet<>();
        for (IncludeRelationSpec relationSpec : includedRelations) {
            for (String attrPath : relationSpec.getAttributePath()) {
                Inclusion inclusion = new Inclusion(attrPath);
                inclusions.add(inclusion);
            }
        }
        IncludedRelationsParams includedRelationsParams = new IncludedRelationsParams(Collections.unmodifiableSet(inclusions));
        decodedSparseMap.put(resourceType, includedRelationsParams);
    }
    queryParams.setIncludedRelations(new TypedParams<>(Collections.unmodifiableMap(decodedSparseMap)));
}
Also used : LinkedHashSet(java.util.LinkedHashSet) IncludeRelationSpec(io.crnk.core.queryspec.IncludeRelationSpec) Inclusion(io.crnk.legacy.queryParams.include.Inclusion) IncludedRelationsParams(io.crnk.legacy.queryParams.params.IncludedRelationsParams) LinkedHashMap(java.util.LinkedHashMap)

Example 3 with Inclusion

use of io.crnk.legacy.queryParams.include.Inclusion in project crnk-framework by crnk-project.

the class DefaultQueryParamsSerializer method serializeIncludedRelations.

@Override
public Map<String, Set<String>> serializeIncludedRelations(QueryParams queryParams) {
    Map<String, Set<String>> map = new HashMap<>();
    TypedParams<IncludedRelationsParams> includedRelations = queryParams.getIncludedRelations();
    if (includedRelations != null) {
        Map<String, IncludedRelationsParams> includedRelationsParamsMap = includedRelations.getParams();
        for (Entry<String, IncludedRelationsParams> entry : includedRelationsParamsMap.entrySet()) {
            String type = entry.getKey();
            Set<Inclusion> inclusions = entry.getValue().getParams();
            Set<String> strInclusions = new HashSet<>();
            for (Inclusion inclusion : inclusions) {
                strInclusions.add(inclusion.getPath());
            }
            String paramName = RestrictedQueryParamsMembers.include.name() + "[" + type + "]";
            map.put(paramName, strInclusions);
        }
    }
    return map;
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) Inclusion(io.crnk.legacy.queryParams.include.Inclusion) HashMap(java.util.HashMap) HashSet(java.util.HashSet)

Example 4 with Inclusion

use of io.crnk.legacy.queryParams.include.Inclusion in project crnk-framework by crnk-project.

the class QueryParamsBuilderTest method onGivenIncludedRelationsBuilderShouldReturnRequestParamsWithIncludedRelations.

@Test
public void onGivenIncludedRelationsBuilderShouldReturnRequestParamsWithIncludedRelations() throws ParametersDeserializationException {
    // GIVEN
    queryParams.put("include[special-users]", new LinkedHashSet<>(Arrays.asList("friends", "foes")));
    // WHEN
    QueryParams result = sut.buildQueryParams(queryParams);
    // THEN
    assertThat(result.getIncludedRelations().getParams().get("special-users")).isNotNull();
    assertThat(result.getIncludedRelations().getParams().get("special-users").getParams()).containsExactly(new Inclusion("friends"), new Inclusion("foes"));
}
Also used : Inclusion(io.crnk.legacy.queryParams.include.Inclusion) Test(org.junit.Test)

Example 5 with Inclusion

use of io.crnk.legacy.queryParams.include.Inclusion in project crnk-framework by crnk-project.

the class DefaultQueryParamsParserTest method onGivenIncludedRelationsParserShouldReturnOnlyRequestParamsWithIncludedRelations.

@Test
public void onGivenIncludedRelationsParserShouldReturnOnlyRequestParamsWithIncludedRelations() {
    // GIVEN
    queryParams.put("include[user]", Collections.singleton("name"));
    queryParams.put("random[user]", Collections.singleton("surname"));
    // WHEN
    QueryParams result = parseQueryParams();
    // THEN
    assertThat(result.getIncludedRelations().getParams().size()).isEqualTo(1);
    assertThat(result.getIncludedRelations().getParams().get("user").getParams()).containsOnly(new Inclusion("name"));
}
Also used : Inclusion(io.crnk.legacy.queryParams.include.Inclusion) Test(org.junit.Test)

Aggregations

Inclusion (io.crnk.legacy.queryParams.include.Inclusion)10 IncludedRelationsParams (io.crnk.legacy.queryParams.params.IncludedRelationsParams)4 Test (org.junit.Test)3 IncludeRelationSpec (io.crnk.core.queryspec.IncludeRelationSpec)2 HashSet (java.util.HashSet)2 ResourceField (io.crnk.core.engine.information.resource.ResourceField)1 ParametersDeserializationException (io.crnk.core.exception.ParametersDeserializationException)1 TypedParams (io.crnk.legacy.queryParams.params.TypedParams)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 LinkedHashSet (java.util.LinkedHashSet)1 Set (java.util.Set)1