Search in sources :

Example 1 with DefaultResourceList

use of io.crnk.core.resource.list.DefaultResourceList in project crnk-framework by crnk-project.

the class ProjectToTaskRepository method findManyTargets.

@Override
public ResourceList<Task> findManyTargets(Long sourceId, String fieldName, QuerySpec queryParams) {
    DefaultResourceList<Task> tasks = new DefaultResourceList<>();
    for (Relation<Project> relation : THREAD_LOCAL_REPOSITORY.keySet()) {
        if (relation.getSource().getId().equals(sourceId) && relation.getFieldName().equals(fieldName)) {
            Task task = taskRepo.findOne((long) relation.getTargetId(), null);
            Assert.assertNotNull(task);
            tasks.add(task);
        }
    }
    return tasks;
}
Also used : Project(io.crnk.test.mock.models.Project) Task(io.crnk.test.mock.models.Task) DefaultResourceList(io.crnk.core.resource.list.DefaultResourceList)

Example 2 with DefaultResourceList

use of io.crnk.core.resource.list.DefaultResourceList in project crnk-framework by crnk-project.

the class CrnkClient method setProxyFactory.

public void setProxyFactory(ClientProxyFactory proxyFactory) {
    proxyFactory.init(new ClientProxyFactoryContext() {

        @Override
        public ModuleRegistry getModuleRegistry() {
            return moduleRegistry;
        }

        @Override
        public <T> DefaultResourceList<T> getCollection(Class<T> resourceClass, String url) {
            RegistryEntry entry = resourceRegistry.findEntry(resourceClass);
            ResourceInformation resourceInformation = entry.getResourceInformation();
            final ResourceRepositoryStubImpl<T, ?> repositoryStub = new ResourceRepositoryStubImpl<>(CrnkClient.this, resourceClass, resourceInformation, urlBuilder);
            return repositoryStub.findAll(url);
        }
    });
    documentMapper.setProxyFactory(proxyFactory);
}
Also used : ResourceInformation(io.crnk.core.engine.information.resource.ResourceInformation) ResourceRepositoryStubImpl(io.crnk.client.internal.ResourceRepositoryStubImpl) DefaultResourceList(io.crnk.core.resource.list.DefaultResourceList) ModuleRegistry(io.crnk.core.module.ModuleRegistry) ClientProxyFactoryContext(io.crnk.client.internal.proxy.ClientProxyFactoryContext)

Example 3 with DefaultResourceList

use of io.crnk.core.resource.list.DefaultResourceList in project crnk-framework by crnk-project.

the class ClientDocumentMapper method fromDocument.

public Object fromDocument(Document document, boolean getList) {
    ClientResourceUpsert upsert = new ClientResourceUpsert(resourceRegistry, propertiesProvider, typeParser, objectMapper, null, proxyFactory);
    PreconditionUtil.assertFalse("document contains json api errors and cannot be processed", document.getErrors() != null && !document.getErrors().isEmpty());
    if (!document.getData().isPresent()) {
        return null;
    }
    List<Resource> included = document.getIncluded();
    List<Resource> data = document.getCollectionData().get();
    List<Object> dataObjects = upsert.allocateResources(data);
    if (included != null) {
        upsert.allocateResources(included);
    }
    upsert.setRelations(data);
    if (included != null) {
        upsert.setRelations(included);
    }
    if (getList) {
        DefaultResourceList<Object> resourceList = new DefaultResourceList();
        resourceList.addAll(dataObjects);
        if (document.getLinks() != null) {
            resourceList.setLinks(new JsonLinksInformation(document.getLinks(), objectMapper));
        }
        if (document.getMeta() != null) {
            resourceList.setMeta(new JsonMetaInformation(document.getMeta(), objectMapper));
        }
        return resourceList;
    } else {
        if (dataObjects.isEmpty()) {
            return null;
        }
        PreconditionUtil.assertFalse("expected unique result", dataObjects.size() > 1);
        return dataObjects.get(0);
    }
}
Also used : JsonLinksInformation(io.crnk.client.response.JsonLinksInformation) DefaultResourceList(io.crnk.core.resource.list.DefaultResourceList) JsonMetaInformation(io.crnk.client.response.JsonMetaInformation) Resource(io.crnk.core.engine.document.Resource)

Example 4 with DefaultResourceList

use of io.crnk.core.resource.list.DefaultResourceList in project crnk-framework by crnk-project.

the class ClientStubInvocationHandler method createTypesafeList.

@SuppressWarnings({ "unchecked", "rawtypes" })
private Object createTypesafeList(Object result, Class<?> returnType) {
    DefaultResourceList defaultList = (DefaultResourceList) result;
    Class<?>[] typeArguments = TypeResolver.resolveRawArguments(ResourceListBase.class, returnType);
    Class<?> metaType = typeArguments[1];
    Class<?> linksType = typeArguments[2];
    ResourceListBase typedList = (ResourceListBase) ClassUtils.newInstance(returnType);
    typedList.addAll(defaultList);
    typedList.setMeta(defaultList.getMeta(metaType));
    typedList.setLinks(defaultList.getLinks(linksType));
    return typedList;
}
Also used : DefaultResourceList(io.crnk.core.resource.list.DefaultResourceList) ResourceListBase(io.crnk.core.resource.list.ResourceListBase)

Example 5 with DefaultResourceList

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

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