Search in sources :

Example 6 with MultivaluedMap

use of io.crnk.core.engine.internal.utils.MultivaluedMap in project crnk-framework by crnk-project.

the class RelationshipRepositoryAdapter method findBulkManyTargets.

@SuppressWarnings("rawtypes")
public Map<I, JsonApiResponse> findBulkManyTargets(List<I> sourceIds, ResourceField field, QueryAdapter queryAdapter) {
    if (relationshipRepository instanceof BulkRelationshipRepositoryV2) {
        RepositoryBulkRequestFilterChainImpl<I> chain = new RepositoryBulkRequestFilterChainImpl<I>() {

            @Override
            protected Map<I, JsonApiResponse> invoke(RepositoryFilterContext context) {
                RepositoryRequestSpec request = context.getRequest();
                Iterable<I> sourceIds = request.getIds();
                ResourceField field = request.getRelationshipField();
                QueryAdapter queryAdapter = request.getQueryAdapter();
                BulkRelationshipRepositoryV2 bulkRepository = (BulkRelationshipRepositoryV2) relationshipRepository;
                ResourceInformation targetResourceInformation = moduleRegistry.getResourceRegistry().getEntry(field.getOppositeResourceType()).getResourceInformation();
                QuerySpec querySpec = request.getQuerySpec(targetResourceInformation);
                MultivaluedMap targetsMap = bulkRepository.findTargets(sourceIds, field.getUnderlyingName(), querySpec);
                return toResponses(targetsMap, true, queryAdapter, field, HttpMethod.GET);
            }
        };
        RepositoryRequestSpec requestSpec = RepositoryRequestSpecImpl.forFindTarget(moduleRegistry, queryAdapter, sourceIds, field);
        return chain.doFilter(newRepositoryFilterContext(requestSpec));
    } else {
        // fallback to non-bulk operation
        Map<I, JsonApiResponse> responseMap = new HashMap<>();
        for (I sourceId : sourceIds) {
            JsonApiResponse response = findManyTargets(sourceId, field, queryAdapter);
            responseMap.put(sourceId, response);
        }
        return responseMap;
    }
}
Also used : ResourceInformation(io.crnk.core.engine.information.resource.ResourceInformation) BulkRelationshipRepositoryV2(io.crnk.core.repository.BulkRelationshipRepositoryV2) RepositoryRequestSpec(io.crnk.core.engine.dispatcher.RepositoryRequestSpec) HashMap(java.util.HashMap) QueryAdapter(io.crnk.core.engine.query.QueryAdapter) RepositoryFilterContext(io.crnk.core.engine.filter.RepositoryFilterContext) ResourceField(io.crnk.core.engine.information.resource.ResourceField) JsonApiResponse(io.crnk.core.repository.response.JsonApiResponse) QuerySpec(io.crnk.core.queryspec.QuerySpec) MultivaluedMap(io.crnk.core.engine.internal.utils.MultivaluedMap)

Example 7 with MultivaluedMap

use of io.crnk.core.engine.internal.utils.MultivaluedMap in project crnk-framework by crnk-project.

the class OwnerFowardingRelationshipRepositoryTest method checkSetRelations.

@Test
public void checkSetRelations() {
    taskProjectRepository.setRelations(task, Arrays.asList(42L), "projects");
    Assert.assertEquals(1, task.getProjects().size());
    Assert.assertEquals(42L, task.getProjects().iterator().next().getId().longValue());
    MultivaluedMap targets = taskProjectRepository.findTargets(Arrays.asList(task.getId()), "projects", new QuerySpec(Task.class));
    Assert.assertEquals(1, targets.keySet().size());
    Assert.assertEquals(task.getId(), targets.keySet().iterator().next());
    Project target = (Project) targets.getUnique(task.getId());
    Assert.assertEquals(42L, target.getId().longValue());
}
Also used : Project(io.crnk.core.mock.models.Project) Task(io.crnk.core.mock.models.Task) MultivaluedMap(io.crnk.core.engine.internal.utils.MultivaluedMap) QuerySpec(io.crnk.core.queryspec.QuerySpec) ResourceRegistryTest(io.crnk.core.resource.registry.ResourceRegistryTest) Test(org.junit.Test)

Example 8 with MultivaluedMap

use of io.crnk.core.engine.internal.utils.MultivaluedMap in project crnk-framework by crnk-project.

the class ImplicitOwnerBasedRelationshipRepositoryTest method checkSetRelationIds.

@Test
public void checkSetRelationIds() {
    relRepository.setRelations(resource, Arrays.asList(3L, 4L), "testMultipleValues");
    Assert.assertEquals(Arrays.asList(3L, 4L), resource.getTestMultipleValueIds());
    List<Schedule> targets = relRepository.findManyTargets(resource.getId(), "testMultipleValues", new QuerySpec(Schedule.class));
    Assert.assertEquals(2, targets.size());
    Assert.assertSame(schedule3, targets.get(0));
    Assert.assertSame(4L, targets.get(1).getId().longValue());
    MultivaluedMap targetsMap = relRepository.findTargets(Arrays.asList(resource.getId()), "testMultipleValues", new QuerySpec(Schedule.class));
    Assert.assertEquals(1, targetsMap.keySet().size());
    targets = targetsMap.getList(resource.getId());
    Assert.assertEquals(2, targets.size());
    Assert.assertSame(3L, targets.get(0).getId().longValue());
    Assert.assertSame(4L, targets.get(1).getId().longValue());
}
Also used : Schedule(io.crnk.core.mock.models.Schedule) QuerySpec(io.crnk.core.queryspec.QuerySpec) MultivaluedMap(io.crnk.core.engine.internal.utils.MultivaluedMap) ResourceRegistryTest(io.crnk.core.resource.registry.ResourceRegistryTest) Test(org.junit.Test)

Example 9 with MultivaluedMap

use of io.crnk.core.engine.internal.utils.MultivaluedMap in project crnk-framework by crnk-project.

the class ImplicitOwnerBasedRelationshipRepositoryTest method checkSetRelationId.

@Test
public void checkSetRelationId() {
    relRepository.setRelation(resource, 3L, "testSerializeEager");
    Assert.assertEquals(3L, resource.getTestSerializeEagerId().longValue());
    Assert.assertNull(resource.getTestSerializeEager());
    Assert.assertSame(schedule3, relRepository.findOneTarget(resource.getId(), "testSerializeEager", new QuerySpec(Schedule.class)));
    MultivaluedMap targets = relRepository.findTargets(Arrays.asList(resource.getId()), "testSerializeEager", new QuerySpec(Schedule.class));
    Assert.assertEquals(1, targets.keySet().size());
    Object target = targets.getUnique(resource.getId());
    Assert.assertEquals(schedule3, target);
    relRepository.setRelation(resource, null, "testSerializeEager");
    Assert.assertNull(resource.getTestSerializeEagerId());
    Assert.assertNull(resource.getTestSerializeEager());
}
Also used : Schedule(io.crnk.core.mock.models.Schedule) QuerySpec(io.crnk.core.queryspec.QuerySpec) MultivaluedMap(io.crnk.core.engine.internal.utils.MultivaluedMap) ResourceRegistryTest(io.crnk.core.resource.registry.ResourceRegistryTest) Test(org.junit.Test)

Example 10 with MultivaluedMap

use of io.crnk.core.engine.internal.utils.MultivaluedMap in project crnk-framework by crnk-project.

the class GetFromOwnerStrategy method toResult.

private MultivaluedMap<I, D> toResult(ResourceField field, ResourceInformation targetInformation, List sources, List<D> targets) {
    MultivaluedMap bulkResult = new MultivaluedMap<I, D>() {

        @Override
        protected List<D> newList() {
            return new DefaultResourceList<>();
        }
    };
    Map targetMap = new HashMap();
    for (D target : targets) {
        Object targetId = targetInformation.getId(target);
        targetMap.put(targetId, target);
    }
    for (Object source : sources) {
        Object sourceId = field.getParentResourceInformation().getId(source);
        Object targetId = field.getIdAccessor().getValue(source);
        if (field.isCollection()) {
            for (Object targetElementId : (Collection) targetId) {
                addResult(bulkResult, field, sourceId, targetElementId, targetMap);
            }
        } else if (targetId != null) {
            addResult(bulkResult, field, sourceId, targetId, targetMap);
        } else {
            bulkResult.add(sourceId, null);
        }
    }
    return bulkResult;
}
Also used : DefaultResourceList(io.crnk.core.resource.list.DefaultResourceList) HashMap(java.util.HashMap) Collection(java.util.Collection) MultivaluedMap(io.crnk.core.engine.internal.utils.MultivaluedMap) HashMap(java.util.HashMap) MultivaluedMap(io.crnk.core.engine.internal.utils.MultivaluedMap) Map(java.util.Map)

Aggregations

MultivaluedMap (io.crnk.core.engine.internal.utils.MultivaluedMap)14 QuerySpec (io.crnk.core.queryspec.QuerySpec)11 ResourceField (io.crnk.core.engine.information.resource.ResourceField)6 ResourceInformation (io.crnk.core.engine.information.resource.ResourceInformation)6 DefaultResourceList (io.crnk.core.resource.list.DefaultResourceList)6 ResourceRegistryTest (io.crnk.core.resource.registry.ResourceRegistryTest)6 Test (org.junit.Test)6 RegistryEntry (io.crnk.core.engine.registry.RegistryEntry)5 JsonApiResponse (io.crnk.core.repository.response.JsonApiResponse)5 Schedule (io.crnk.core.mock.models.Schedule)4 HashSet (java.util.HashSet)4 Collection (java.util.Collection)3 HashMap (java.util.HashMap)3 List (java.util.List)3 Project (io.crnk.core.mock.models.Project)2 Task (io.crnk.core.mock.models.Task)2 FilterSpec (io.crnk.core.queryspec.FilterSpec)2 QuerySpecAdapter (io.crnk.core.queryspec.internal.QuerySpecAdapter)2 Map (java.util.Map)2 Set (java.util.Set)2