Search in sources :

Example 31 with PersistentResource

use of com.yahoo.elide.core.PersistentResource in project elide by yahoo.

the class CollectionTerminalState method getData.

private Data getData(Set<PersistentResource> collection, EntityDictionary dictionary) {
    Preconditions.checkNotNull(collection);
    List<Resource> resources = collection.stream().map(PersistentResource::toResource).collect(Collectors.toList());
    if (parent.isPresent()) {
        Type<?> parentClass = parent.get().getResourceType();
        String relationshipName = relationName.orElseThrow(IllegalStateException::new);
        RelationshipType type = dictionary.getRelationshipType(parentClass, relationshipName);
        return new Data<>(resources, type);
    }
    return new Data<>(resources);
}
Also used : Resource(com.yahoo.elide.jsonapi.models.Resource) PersistentResource(com.yahoo.elide.core.PersistentResource) RelationshipType(com.yahoo.elide.core.dictionary.RelationshipType) Data(com.yahoo.elide.jsonapi.models.Data) ToString(lombok.ToString)

Example 32 with PersistentResource

use of com.yahoo.elide.core.PersistentResource 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 33 with PersistentResource

use of com.yahoo.elide.core.PersistentResource in project elide by yahoo.

the class RecordState method handle.

@Override
public void handle(StateContext state, SubCollectionRelationshipContext ctx) {
    String id = ctx.entity().id().getText();
    String subCollection = ctx.entity().term().getText();
    String relationName = ctx.relationship().term().getText();
    PersistentResource childRecord;
    Relationship childRelationship = projection.getRelationship(subCollection).orElseThrow(IllegalStateException::new);
    childRecord = resource.getRelation(childRelationship, id);
    state.setState(new RelationshipTerminalState(childRecord, relationName, childRelationship.getProjection()));
}
Also used : PersistentResource(com.yahoo.elide.core.PersistentResource) Relationship(com.yahoo.elide.core.request.Relationship)

Example 34 with PersistentResource

use of com.yahoo.elide.core.PersistentResource in project elide by yahoo.

the class RecordState method handle.

@Override
public void handle(StateContext state, SubCollectionReadCollectionContext ctx) {
    String subCollection = ctx.term().getText();
    EntityDictionary dictionary = state.getRequestScope().getDictionary();
    Type<?> entityClass;
    String entityName;
    RelationshipType type = dictionary.getRelationshipType(resource.getObject(), subCollection);
    Type<?> paramType = dictionary.getParameterizedType(resource.getObject(), subCollection);
    entityName = dictionary.getJsonAliasFor(paramType);
    entityClass = dictionary.getEntityClass(entityName, state.getRequestScope().getApiVersion());
    if (entityClass == null) {
        throw new IllegalArgumentException("Unknown type " + entityName);
    }
    final BaseState nextState;
    final CollectionTerminalState collectionTerminalState = new CollectionTerminalState(entityClass, Optional.of(resource), Optional.of(subCollection), projection);
    Observable<PersistentResource> collection = null;
    if (type.isToOne()) {
        collection = resource.getRelationCheckedFiltered(projection.getRelationship(subCollection).orElseThrow(IllegalStateException::new));
        PersistentResource record = PersistentResource.firstOrNullIfEmpty(collection);
        nextState = new RecordTerminalState(record, collectionTerminalState);
    } else {
        nextState = collectionTerminalState;
    }
    state.setState(nextState);
}
Also used : PersistentResource(com.yahoo.elide.core.PersistentResource) RelationshipType(com.yahoo.elide.core.dictionary.RelationshipType) EntityDictionary(com.yahoo.elide.core.dictionary.EntityDictionary)

Example 35 with PersistentResource

use of com.yahoo.elide.core.PersistentResource in project elide by yahoo.

the class RecordTerminalState method patch.

private boolean patch(Resource resource, RequestScope requestScope) {
    boolean isUpdated = false;
    // Update attributes first
    Map<String, Object> attributes = resource.getAttributes();
    if (attributes != null) {
        for (Map.Entry<String, Object> entry : attributes.entrySet()) {
            String fieldName = entry.getKey();
            Object newVal = entry.getValue();
            isUpdated |= record.updateAttribute(fieldName, newVal);
        }
    }
    // Relations next
    Map<String, Relationship> relationships = resource.getRelationships();
    if (relationships != null) {
        for (Map.Entry<String, Relationship> entry : relationships.entrySet()) {
            String fieldName = entry.getKey();
            Relationship relationship = entry.getValue();
            Set<PersistentResource> resources = (relationship == null) ? null : relationship.toPersistentResources(requestScope);
            isUpdated |= record.updateRelation(fieldName, resources);
        }
    }
    return isUpdated;
}
Also used : PersistentResource(com.yahoo.elide.core.PersistentResource) Relationship(com.yahoo.elide.jsonapi.models.Relationship) ToString(lombok.ToString) Map(java.util.Map)

Aggregations

PersistentResource (com.yahoo.elide.core.PersistentResource)100 Test (org.junit.jupiter.api.Test)71 RequestScope (com.yahoo.elide.core.RequestScope)60 ReadPermission (com.yahoo.elide.annotation.ReadPermission)18 UpdatePermission (com.yahoo.elide.annotation.UpdatePermission)18 DataStoreTransaction (com.yahoo.elide.core.datastore.DataStoreTransaction)17 Include (com.yahoo.elide.annotation.Include)16 Entity (javax.persistence.Entity)16 Resource (com.yahoo.elide.jsonapi.models.Resource)13 AndFilterExpression (com.yahoo.elide.core.filter.expression.AndFilterExpression)10 NotFilterExpression (com.yahoo.elide.core.filter.expression.NotFilterExpression)10 OrFilterExpression (com.yahoo.elide.core.filter.expression.OrFilterExpression)10 PermissionExecutor (com.yahoo.elide.core.security.PermissionExecutor)10 JsonApiDocument (com.yahoo.elide.jsonapi.models.JsonApiDocument)10 Book (example.Book)10 LinkedHashSet (java.util.LinkedHashSet)9 EntityDictionary (com.yahoo.elide.core.dictionary.EntityDictionary)8 BadRequestException (com.yahoo.elide.core.exceptions.BadRequestException)8 FilterExpression (com.yahoo.elide.core.filter.expression.FilterExpression)8 RSQLFilterDialect (com.yahoo.elide.core.filter.dialect.RSQLFilterDialect)7