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