Search in sources :

Example 1 with IncludedProcessor

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);
}
Also used : PersistentResource(com.yahoo.elide.core.PersistentResource) Meta(com.yahoo.elide.jsonapi.models.Meta) JsonApiDocument(com.yahoo.elide.jsonapi.models.JsonApiDocument) DocumentProcessor(com.yahoo.elide.jsonapi.document.processors.DocumentProcessor) HashMap(java.util.HashMap) JsonNode(com.fasterxml.jackson.databind.JsonNode) ToString(lombok.ToString) RequestScope(com.yahoo.elide.core.RequestScope) Pagination(com.yahoo.elide.core.request.Pagination) IncludedProcessor(com.yahoo.elide.jsonapi.document.processors.IncludedProcessor)

Example 2 with IncludedProcessor

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);
}
Also used : JsonApiDocument(com.yahoo.elide.jsonapi.models.JsonApiDocument) DocumentProcessor(com.yahoo.elide.jsonapi.document.processors.DocumentProcessor) Resource(com.yahoo.elide.jsonapi.models.Resource) PersistentResource(com.yahoo.elide.core.PersistentResource) IncludedProcessor(com.yahoo.elide.jsonapi.document.processors.IncludedProcessor)

Example 3 with IncludedProcessor

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));
}
Also used : JsonApiDocument(com.yahoo.elide.jsonapi.models.JsonApiDocument) DocumentProcessor(com.yahoo.elide.jsonapi.document.processors.DocumentProcessor) Resource(com.yahoo.elide.jsonapi.models.Resource) PersistentResource(com.yahoo.elide.core.PersistentResource) ArrayList(java.util.ArrayList) Data(com.yahoo.elide.jsonapi.models.Data) RequestScope(com.yahoo.elide.core.RequestScope) Relationship(com.yahoo.elide.jsonapi.models.Relationship) JsonApiMapper(com.yahoo.elide.jsonapi.JsonApiMapper) IncludedProcessor(com.yahoo.elide.jsonapi.document.processors.IncludedProcessor)

Aggregations

PersistentResource (com.yahoo.elide.core.PersistentResource)3 DocumentProcessor (com.yahoo.elide.jsonapi.document.processors.DocumentProcessor)3 IncludedProcessor (com.yahoo.elide.jsonapi.document.processors.IncludedProcessor)3 JsonApiDocument (com.yahoo.elide.jsonapi.models.JsonApiDocument)3 RequestScope (com.yahoo.elide.core.RequestScope)2 Resource (com.yahoo.elide.jsonapi.models.Resource)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 Pagination (com.yahoo.elide.core.request.Pagination)1 JsonApiMapper (com.yahoo.elide.jsonapi.JsonApiMapper)1 Data (com.yahoo.elide.jsonapi.models.Data)1 Meta (com.yahoo.elide.jsonapi.models.Meta)1 Relationship (com.yahoo.elide.jsonapi.models.Relationship)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 ToString (lombok.ToString)1