use of io.crnk.core.queryspec.QuerySpec 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.queryspec.QuerySpec 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);
}
use of io.crnk.core.queryspec.QuerySpec 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);
}
use of io.crnk.core.queryspec.QuerySpec in project crnk-framework by crnk-project.
the class MetaDefaultLimitIntTest method limitShouldApplyToResults.
@Test
public void limitShouldApplyToResults() {
QuerySpec querySpec = new QuerySpec(MetaResource.class);
querySpec.includeRelation(Arrays.asList("attributes"));
ResourceList<MetaResource> list = repository.findAll(querySpec);
Assert.assertEquals(2, list.size());
}
use of io.crnk.core.queryspec.QuerySpec in project crnk-framework by crnk-project.
the class MetaIntTest method testIdPrefix.
@Test
public void testIdPrefix() {
ResourceList<MetaElement> list = repository.findAll(new QuerySpec(MetaElement.class));
Assert.assertFalse(list.isEmpty());
for (MetaElement elem : list) {
if (elem instanceof MetaPrimitiveType) {
Assert.assertTrue(elem.getId(), elem.getId().startsWith("base."));
} else {
Assert.assertTrue(elem.getId(), elem.getId().startsWith("base.") || elem.getId().startsWith("resources.") || elem.getId().startsWith("app.resources.") || elem.getId().startsWith("io.crnk"));
}
}
}
Aggregations