use of io.crnk.core.resource.list.DefaultResourceList in project crnk-framework by crnk-project.
the class QuerySpec method apply.
/**
* Evaluates this querySpec against the provided list in memory. It supports
* sorting, filter and paging.
* <p>
* TODO currently ignores relations and inclusions, has room for
* improvements
*
* @param <T> the type of resources in this Iterable
* @param resources resources
* @return sorted, filtered list.
*/
public <T> DefaultResourceList<T> apply(Iterable<T> resources) {
DefaultResourceList<T> resultList = new DefaultResourceList<>();
resultList.setMeta(new DefaultPagedMetaInformation());
apply(resources, resultList);
return resultList;
}
use of io.crnk.core.resource.list.DefaultResourceList 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.resource.list.DefaultResourceList in project crnk-framework by crnk-project.
the class HasNextPageTestRepository method findAll.
@Override
public ResourceList<Task> findAll(QuerySpec querySpec) {
DefaultResourceList<Task> list = new DefaultResourceList<>();
list.setMeta(new DefaultHasMoreResourcesMetaInformation());
querySpec.apply(tasks, list);
return list;
}
use of io.crnk.core.resource.list.DefaultResourceList in project crnk-framework by crnk-project.
the class HasNextPageTestRepository method findAll.
@Override
public ResourceList<Task> findAll(Iterable<Long> ids, QuerySpec querySpec) {
DefaultResourceList<Task> list = new DefaultResourceList<>();
list.setMeta(new DefaultHasMoreResourcesMetaInformation());
querySpec.apply(tasks, list);
return list;
}
use of io.crnk.core.resource.list.DefaultResourceList in project crnk-framework by crnk-project.
the class UserToTaskRepository method findManyTargets.
@JsonApiFindManyTargets
public ResourceList<Task> findManyTargets(Long sourceId, String fieldName, QuerySpec querySpec) {
ResourceList<Task> projects = new DefaultResourceList<>();
for (Relation<User> relation : THREAD_LOCAL_REPOSITORY.keySet()) {
if (relation.getSource().getId().equals(sourceId) && relation.getFieldName().equals(fieldName)) {
Task project = new Task();
project.setId((Long) relation.getTargetId());
projects.add(project);
}
}
return projects;
}
Aggregations