use of io.crnk.core.engine.internal.utils.MultivaluedMap in project crnk-framework by crnk-project.
the class RelationshipRepositoryBase method findTargets.
@SuppressWarnings("unchecked")
public MultivaluedMap<I, D> findTargets(Iterable<I> sourceIds, String fieldName, QuerySpec querySpec) {
RegistryEntry sourceEntry = resourceRegistry.findEntry(sourceResourceClass);
ResourceInformation sourceInformation = sourceEntry.getResourceInformation();
ResourceField field = sourceInformation.findFieldByUnderlyingName(fieldName);
RegistryEntry targetEntry = getTargetEntry(field);
String oppositeName = getOppositeName(fieldName);
QuerySpec idQuerySpec = querySpec.duplicate();
idQuerySpec.addFilter(new FilterSpec(Arrays.asList(oppositeName, sourceInformation.getIdField().getUnderlyingName()), FilterOperator.EQ, sourceIds));
idQuerySpec.includeRelation(Arrays.asList(oppositeName));
ResourceRepositoryAdapter<D, J> targetAdapter = targetEntry.getResourceRepository();
JsonApiResponse response = targetAdapter.findAll(new QuerySpecAdapter(idQuerySpec, resourceRegistry));
List<D> results = (List<D>) response.getEntity();
MultivaluedMap<I, D> bulkResult = new MultivaluedMap<I, D>() {
@Override
protected List<D> newList() {
return new DefaultResourceList<>();
}
};
Set<I> sourceIdSet = new HashSet<>();
for (I sourceId : sourceIds) {
sourceIdSet.add(sourceId);
}
for (D result : results) {
handleTarget(bulkResult, result, sourceIdSet, oppositeName, sourceInformation);
}
return bulkResult;
}
use of io.crnk.core.engine.internal.utils.MultivaluedMap in project crnk-framework by crnk-project.
the class GetFromOwnerStrategy method findTargets.
@SuppressWarnings("unchecked")
public MultivaluedMap<I, D> findTargets(Iterable<I> sourceIds, String fieldName, QuerySpec querySpec) {
RegistryEntry sourceEntry = context.getSourceEntry();
ResourceInformation sourceInformation = sourceEntry.getResourceInformation();
ResourceField field = sourceInformation.findFieldByUnderlyingName(fieldName);
RegistryEntry targetEntry = context.getTargetEntry(field);
List sources = (List) sourceEntry.getResourceRepository().findAll(sourceIds, context.createSaveQueryAdapter(fieldName)).getEntity();
ResourceInformation targetInformation = targetEntry.getResourceInformation();
List<D> targets;
if (field.hasIdField()) {
Set targetIds = new HashSet();
for (Object source : sources) {
Object targetId = field.getIdAccessor().getValue(source);
if (field.isCollection()) {
targetIds.addAll((Collection) targetId);
} else {
targetIds.add(targetId);
}
}
QuerySpec idQuerySpec = new QuerySpec(targetInformation);
ResourceRepositoryAdapter<D, J> targetAdapter = targetEntry.getResourceRepository();
JsonApiResponse response = targetAdapter.findAll(targetIds, context.createQueryAdapter(idQuerySpec));
targets = (List<D>) response.getEntity();
return toResult(field, targetInformation, sources, targets);
} else {
MultivaluedMap bulkResult = new MultivaluedMap<I, D>() {
@Override
protected List<D> newList() {
return new DefaultResourceList<>();
}
};
for (Object source : sources) {
Object sourceId = sourceInformation.getId(source);
Object target = field.getAccessor().getValue(source);
if (target != null) {
if (field.isCollection()) {
bulkResult.addAll(sourceId, (Collection) target);
} else {
bulkResult.add(sourceId, target);
}
}
}
return bulkResult;
}
}
use of io.crnk.core.engine.internal.utils.MultivaluedMap in project crnk-framework by crnk-project.
the class ImplicitOwnerBasedRelationshipRepositoryTest 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 OwnerFowardingRelationshipRepositoryTest 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 OwnerFowardingRelationshipRepositoryTest 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());
}
Aggregations