use of com.yahoo.elide.jsonapi.document.processors.IncludedProcessor in project elide by yahoo.
the class CollectionTerminalState method handleGet.
@Override
public Supplier<Pair<Integer, JsonNode>> handleGet(StateContext state) {
JsonApiDocument jsonApiDocument = new JsonApiDocument();
RequestScope requestScope = state.getRequestScope();
MultivaluedMap<String, String> queryParams = requestScope.getQueryParams();
Set<PersistentResource> collection = getResourceCollection(requestScope).toList(LinkedHashSet::new).blockingGet();
// Set data
jsonApiDocument.setData(getData(collection, requestScope.getDictionary()));
// Run include processor
DocumentProcessor includedProcessor = new IncludedProcessor();
includedProcessor.execute(jsonApiDocument, collection, queryParams);
Pagination pagination = parentProjection.getPagination();
if (parent.isPresent()) {
pagination = parentProjection.getRelationship(relationName.orElseThrow(IllegalStateException::new)).get().getProjection().getPagination();
}
// Add pagination meta data
if (!pagination.isDefaultInstance()) {
Map<String, Number> pageMetaData = new HashMap<>();
pageMetaData.put("number", (pagination.getOffset() / pagination.getLimit()) + 1);
pageMetaData.put("limit", pagination.getLimit());
// Get total records if it has been requested and add to the page meta data
if (pagination.returnPageTotals()) {
Long totalRecords = pagination.getPageTotals();
pageMetaData.put("totalPages", totalRecords / pagination.getLimit() + ((totalRecords % pagination.getLimit()) > 0 ? 1 : 0));
pageMetaData.put("totalRecords", totalRecords);
}
Map<String, Object> allMetaData = new HashMap<>();
allMetaData.put("page", pageMetaData);
Meta meta = new Meta(allMetaData);
jsonApiDocument.setMeta(meta);
}
JsonNode responseBody = requestScope.getMapper().toJsonObject(jsonApiDocument);
return () -> Pair.of(HttpStatus.SC_OK, responseBody);
}
use of com.yahoo.elide.jsonapi.document.processors.IncludedProcessor in project elide by yahoo.
the class BaseState method getResponseBody.
protected static JsonNode getResponseBody(PersistentResource resource, RequestScope requestScope) {
MultivaluedMap<String, String> queryParams = requestScope.getQueryParams();
JsonApiDocument jsonApiDocument = new JsonApiDocument();
// TODO Make this a document processor
Data<Resource> data = resource == null ? null : new Data<>(resource.toResource());
jsonApiDocument.setData(data);
// TODO Iterate over set of document processors
DocumentProcessor includedProcessor = new IncludedProcessor();
includedProcessor.execute(jsonApiDocument, resource, queryParams);
return requestScope.getMapper().toJsonObject(jsonApiDocument);
}
use of com.yahoo.elide.jsonapi.document.processors.IncludedProcessor in project elide by yahoo.
the class RelationshipTerminalState method handleGet.
@Override
public Supplier<Pair<Integer, JsonNode>> handleGet(StateContext state) {
JsonApiDocument doc = new JsonApiDocument();
RequestScope requestScope = state.getRequestScope();
JsonApiMapper mapper = requestScope.getMapper();
MultivaluedMap<String, String> queryParams = requestScope.getQueryParams();
Map<String, Relationship> relationships = record.toResource(parentProjection).getRelationships();
if (relationships != null && relationships.containsKey(relationshipName)) {
Relationship relationship = relationships.get(relationshipName);
// Handle valid relationship
// Set data
Data<Resource> data = relationship.getData();
doc.setData(data);
// Run include processor
DocumentProcessor includedProcessor = new IncludedProcessor();
includedProcessor.execute(doc, record, queryParams);
return () -> Pair.of(HttpStatus.SC_OK, mapper.toJsonObject(doc));
}
// Handle no data for relationship
if (relationshipType.isToMany()) {
doc.setData(new Data<>(new ArrayList<>()));
} else if (relationshipType.isToOne()) {
doc.setData(new Data<>((Resource) null));
} else {
throw new IllegalStateException("Failed to GET a relationship; relationship is neither toMany nor toOne");
}
return () -> Pair.of(HttpStatus.SC_OK, mapper.toJsonObject(doc));
}
Aggregations