Search in sources :

Example 6 with DefaultResourceList

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;
}
Also used : DefaultPagedMetaInformation(io.crnk.core.resource.meta.DefaultPagedMetaInformation) DefaultResourceList(io.crnk.core.resource.list.DefaultResourceList)

Example 7 with DefaultResourceList

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;
    }
}
Also used : ResourceInformation(io.crnk.core.engine.information.resource.ResourceInformation) Set(java.util.Set) HashSet(java.util.HashSet) RegistryEntry(io.crnk.core.engine.registry.RegistryEntry) ResourceField(io.crnk.core.engine.information.resource.ResourceField) DefaultResourceList(io.crnk.core.resource.list.DefaultResourceList) DefaultResourceList(io.crnk.core.resource.list.DefaultResourceList) List(java.util.List) JsonApiResponse(io.crnk.core.repository.response.JsonApiResponse) QuerySpec(io.crnk.core.queryspec.QuerySpec) MultivaluedMap(io.crnk.core.engine.internal.utils.MultivaluedMap) HashSet(java.util.HashSet)

Example 8 with DefaultResourceList

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;
}
Also used : Task(io.crnk.core.mock.models.Task) DefaultHasMoreResourcesMetaInformation(io.crnk.core.resource.meta.DefaultHasMoreResourcesMetaInformation) DefaultResourceList(io.crnk.core.resource.list.DefaultResourceList)

Example 9 with DefaultResourceList

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;
}
Also used : Task(io.crnk.core.mock.models.Task) DefaultHasMoreResourcesMetaInformation(io.crnk.core.resource.meta.DefaultHasMoreResourcesMetaInformation) DefaultResourceList(io.crnk.core.resource.list.DefaultResourceList)

Example 10 with DefaultResourceList

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;
}
Also used : Task(io.crnk.core.mock.models.Task) User(io.crnk.core.mock.models.User) DefaultResourceList(io.crnk.core.resource.list.DefaultResourceList) JsonApiFindManyTargets(io.crnk.legacy.repository.annotations.JsonApiFindManyTargets)

Aggregations

DefaultResourceList (io.crnk.core.resource.list.DefaultResourceList)26 Task (io.crnk.core.mock.models.Task)7 ResourceInformation (io.crnk.core.engine.information.resource.ResourceInformation)6 MultivaluedMap (io.crnk.core.engine.internal.utils.MultivaluedMap)6 ResourceField (io.crnk.core.engine.information.resource.ResourceField)5 RegistryEntry (io.crnk.core.engine.registry.RegistryEntry)5 QuerySpec (io.crnk.core.queryspec.QuerySpec)5 JsonApiResponse (io.crnk.core.repository.response.JsonApiResponse)4 DefaultHasMoreResourcesMetaInformation (io.crnk.core.resource.meta.DefaultHasMoreResourcesMetaInformation)4 Collection (java.util.Collection)4 HashSet (java.util.HashSet)4 List (java.util.List)4 Test (org.junit.Test)4 QuerySpecAdapter (io.crnk.core.queryspec.internal.QuerySpecAdapter)3 ResourceList (io.crnk.core.resource.list.ResourceList)3 JsonLinksInformation (io.crnk.client.response.JsonLinksInformation)2 JsonMetaInformation (io.crnk.client.response.JsonMetaInformation)2 Resource (io.crnk.core.engine.document.Resource)2 ModuleRegistry (io.crnk.core.module.ModuleRegistry)2 FilterSpec (io.crnk.core.queryspec.FilterSpec)2