Search in sources :

Example 1 with LinksInformation

use of io.crnk.core.resource.links.LinksInformation in project crnk-framework by crnk-project.

the class DocumentMapperTest method testCompactMode.

@Test
public void testCompactMode() {
    LinksInformation links = new TaskLinks();
    Task task = createTask(2, "sample task");
    task.setLinksInformation(links);
    QuerySpecAdapter queryAdapter = (QuerySpecAdapter) toAdapter(new QuerySpec(Task.class));
    queryAdapter.setCompactMode(true);
    Document document = mapper.toDocument(toResponse(task), queryAdapter);
    Resource resource = document.getSingleData().get();
    Assert.assertEquals("2", resource.getId());
    Assert.assertEquals("tasks", resource.getType());
    Assert.assertNull(resource.getLinks().get("self"));
    Assert.assertNotNull(resource.getLinks().get("someLink"));
    Relationship project = resource.getRelationships().get("project");
    Assert.assertNull(project.getLinks());
}
Also used : LazyTask(io.crnk.core.mock.models.LazyTask) Task(io.crnk.core.mock.models.Task) Relationship(io.crnk.core.engine.document.Relationship) Resource(io.crnk.core.engine.document.Resource) LinksInformation(io.crnk.core.resource.links.LinksInformation) QuerySpecAdapter(io.crnk.core.queryspec.internal.QuerySpecAdapter) QuerySpec(io.crnk.core.queryspec.QuerySpec) Document(io.crnk.core.engine.document.Document) Test(org.junit.Test)

Example 2 with LinksInformation

use of io.crnk.core.resource.links.LinksInformation in project crnk-framework by crnk-project.

the class ResponseRepositoryAdapter method doGetLinksInformation.

@SuppressWarnings({ "unchecked", "rawtypes" })
private LinksInformation doGetLinksInformation(Object repository, Iterable<?> resources, RepositoryRequestSpec requestSpec) {
    if (resources instanceof ResourceList) {
        ResourceList<?> resourceList = (ResourceList<?>) resources;
        boolean createLinksInformation = resourceList instanceof DefaultResourceList;
        LinksInformation newLinksInfo = enrichLinksInformation(resourceList.getLinks(), resources, requestSpec);
        if (createLinksInformation) {
            ((DefaultResourceList) resources).setLinks(newLinksInfo);
        }
        return resourceList.getLinks();
    }
    LinksInformation linksInformation = null;
    if (repository instanceof AnnotatedRepositoryAdapter) {
        if (((AnnotatedRepositoryAdapter) repository).linksRepositoryAvailable()) {
            linksInformation = ((LinksRepository) repository).getLinksInformation(resources, requestSpec.getQueryParams());
        }
    } else if (repository instanceof LinksRepositoryV2) {
        linksInformation = ((LinksRepositoryV2) repository).getLinksInformation(resources, requestSpec.getResponseQuerySpec());
    } else if (repository instanceof LinksRepository) {
        linksInformation = ((LinksRepository) repository).getLinksInformation(resources, requestSpec.getQueryParams());
    }
    // everything deprecated anyway
    return enrichLinksInformation(linksInformation, resources, requestSpec);
}
Also used : ResourceList(io.crnk.core.resource.list.ResourceList) DefaultResourceList(io.crnk.core.resource.list.DefaultResourceList) DefaultResourceList(io.crnk.core.resource.list.DefaultResourceList) DefaultPagedLinksInformation(io.crnk.core.resource.links.DefaultPagedLinksInformation) LinksInformation(io.crnk.core.resource.links.LinksInformation) PagedLinksInformation(io.crnk.core.resource.links.PagedLinksInformation) AnnotatedRepositoryAdapter(io.crnk.legacy.internal.AnnotatedRepositoryAdapter) LinksRepositoryV2(io.crnk.core.repository.LinksRepositoryV2) LinksRepository(io.crnk.legacy.repository.LinksRepository)

Example 3 with LinksInformation

use of io.crnk.core.resource.links.LinksInformation in project crnk-framework by crnk-project.

the class ResponseRepositoryAdapter method enrichLinksInformation.

private LinksInformation enrichLinksInformation(LinksInformation linksInformation, Iterable<?> resources, RepositoryRequestSpec requestSpec) {
    QueryAdapter queryAdapter = requestSpec.getQueryAdapter();
    LinksInformation enrichedLinksInformation = linksInformation;
    if (queryAdapter instanceof QuerySpecAdapter && resources instanceof ResourceList && requestSpec.getResponseResourceInformation().getPagingBehavior() != null && requestSpec.getResponseResourceInformation().getPagingBehavior().isRequired(queryAdapter.getPagingSpec())) {
        enrichedLinksInformation = enrichPageLinksInformation(enrichedLinksInformation, (ResourceList<?>) resources, queryAdapter, requestSpec);
    }
    return enrichedLinksInformation;
}
Also used : ResourceList(io.crnk.core.resource.list.ResourceList) DefaultResourceList(io.crnk.core.resource.list.DefaultResourceList) QueryAdapter(io.crnk.core.engine.query.QueryAdapter) DefaultPagedLinksInformation(io.crnk.core.resource.links.DefaultPagedLinksInformation) LinksInformation(io.crnk.core.resource.links.LinksInformation) PagedLinksInformation(io.crnk.core.resource.links.PagedLinksInformation) QuerySpecAdapter(io.crnk.core.queryspec.internal.QuerySpecAdapter)

Example 4 with LinksInformation

use of io.crnk.core.resource.links.LinksInformation in project crnk-framework by crnk-project.

the class ResponseRepositoryAdapter method getResponse.

protected JsonApiResponse getResponse(Object repository, Object result, RepositoryRequestSpec requestSpec) {
    if (result instanceof JsonApiResponse) {
        return (JsonApiResponse) result;
    }
    Iterable<?> resources;
    boolean isCollection = result instanceof Iterable;
    if (isCollection) {
        resources = (Iterable<?>) result;
    } else {
        resources = Collections.singletonList(result);
    }
    Iterable<?> filteredResult = filterResult(resources, requestSpec);
    MetaInformation metaInformation = getMetaInformation(repository, resources, requestSpec);
    LinksInformation linksInformation = getLinksInformation(repository, resources, requestSpec);
    Object resultEntity;
    if (isCollection) {
        resultEntity = filteredResult;
    } else {
        Iterator<?> iterator = filteredResult.iterator();
        if (iterator.hasNext()) {
            resultEntity = iterator.next();
            PreconditionUtil.assertFalse("expected unique result", iterator.hasNext());
        } else {
            resultEntity = null;
        }
    }
    return new JsonApiResponse().setEntity(resultEntity).setLinksInformation(linksInformation).setMetaInformation(metaInformation);
}
Also used : MetaInformation(io.crnk.core.resource.meta.MetaInformation) DefaultPagedLinksInformation(io.crnk.core.resource.links.DefaultPagedLinksInformation) LinksInformation(io.crnk.core.resource.links.LinksInformation) PagedLinksInformation(io.crnk.core.resource.links.PagedLinksInformation) JsonApiResponse(io.crnk.core.repository.response.JsonApiResponse)

Example 5 with LinksInformation

use of io.crnk.core.resource.links.LinksInformation in project crnk-framework by crnk-project.

the class DocumentMapperTest method testCompactModeWithInclusion.

@Test
public void testCompactModeWithInclusion() {
    Project project = new Project();
    project.setName("someProject");
    project.setId(3L);
    LinksInformation links = new TaskLinks();
    Task task = createTask(2, "sample task");
    task.setLinksInformation(links);
    task.setProject(project);
    QuerySpec querySpec = new QuerySpec(Task.class);
    querySpec.includeRelation(Arrays.asList("project"));
    QuerySpecAdapter queryAdapter = (QuerySpecAdapter) toAdapter(querySpec);
    queryAdapter.setCompactMode(true);
    Document document = mapper.toDocument(toResponse(task), queryAdapter);
    Resource resource = document.getSingleData().get();
    Assert.assertEquals("2", resource.getId());
    Assert.assertEquals("tasks", resource.getType());
    Assert.assertNull(resource.getLinks().get("self"));
    Assert.assertNotNull(resource.getLinks().get("someLink"));
    Relationship projectRel = resource.getRelationships().get("project");
    Assert.assertNull(projectRel.getLinks());
    Assert.assertEquals(1, document.getIncluded().size());
    Resource projectResource = document.getIncluded().get(0);
    Assert.assertNull(projectResource.getRelationships().get("tasks"));
}
Also used : Project(io.crnk.core.mock.models.Project) LazyTask(io.crnk.core.mock.models.LazyTask) Task(io.crnk.core.mock.models.Task) Relationship(io.crnk.core.engine.document.Relationship) Resource(io.crnk.core.engine.document.Resource) LinksInformation(io.crnk.core.resource.links.LinksInformation) QuerySpec(io.crnk.core.queryspec.QuerySpec) QuerySpecAdapter(io.crnk.core.queryspec.internal.QuerySpecAdapter) Document(io.crnk.core.engine.document.Document) Test(org.junit.Test)

Aggregations

LinksInformation (io.crnk.core.resource.links.LinksInformation)5 QuerySpecAdapter (io.crnk.core.queryspec.internal.QuerySpecAdapter)3 DefaultPagedLinksInformation (io.crnk.core.resource.links.DefaultPagedLinksInformation)3 PagedLinksInformation (io.crnk.core.resource.links.PagedLinksInformation)3 Document (io.crnk.core.engine.document.Document)2 Relationship (io.crnk.core.engine.document.Relationship)2 Resource (io.crnk.core.engine.document.Resource)2 LazyTask (io.crnk.core.mock.models.LazyTask)2 Task (io.crnk.core.mock.models.Task)2 QuerySpec (io.crnk.core.queryspec.QuerySpec)2 DefaultResourceList (io.crnk.core.resource.list.DefaultResourceList)2 ResourceList (io.crnk.core.resource.list.ResourceList)2 Test (org.junit.Test)2 QueryAdapter (io.crnk.core.engine.query.QueryAdapter)1 Project (io.crnk.core.mock.models.Project)1 LinksRepositoryV2 (io.crnk.core.repository.LinksRepositoryV2)1 JsonApiResponse (io.crnk.core.repository.response.JsonApiResponse)1 MetaInformation (io.crnk.core.resource.meta.MetaInformation)1 AnnotatedRepositoryAdapter (io.crnk.legacy.internal.AnnotatedRepositoryAdapter)1 LinksRepository (io.crnk.legacy.repository.LinksRepository)1