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());
}
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);
}
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;
}
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);
}
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"));
}
Aggregations