Search in sources :

Example 61 with PersistentResource

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

the class CollectionTerminalState method createObject.

private PersistentResource createObject(RequestScope requestScope) throws ForbiddenAccessException, InvalidObjectIdentifierException {
    JsonApiDocument doc = requestScope.getJsonApiDocument();
    JsonApiMapper mapper = requestScope.getMapper();
    if (doc.getData() == null) {
        throw new InvalidEntityBodyException("Invalid JSON-API document: " + doc);
    }
    Data<Resource> data = doc.getData();
    Collection<Resource> resources = data.get();
    Resource resource = (resources.size() == 1) ? IterableUtils.first(resources) : null;
    if (resource == null) {
        try {
            throw new InvalidEntityBodyException(mapper.writeJsonApiDocument(doc));
        } catch (JsonProcessingException e) {
            throw new InternalServerErrorException(e);
        }
    }
    String id = resource.getId();
    Type<?> newObjectClass = requestScope.getDictionary().getEntityClass(resource.getType(), requestScope.getApiVersion());
    if (newObjectClass == null) {
        throw new UnknownEntityException("Entity " + resource.getType() + " not found");
    }
    if (!entityClass.isAssignableFrom(newObjectClass)) {
        throw new InvalidValueException("Cannot assign value of type: " + resource.getType() + " to type: " + entityClass);
    }
    PersistentResource pResource = PersistentResource.createObject(parent.orElse(null), relationName.orElse(null), newObjectClass, requestScope, Optional.ofNullable(id));
    Map<String, Object> attributes = resource.getAttributes();
    if (attributes != null) {
        for (Map.Entry<String, Object> entry : attributes.entrySet()) {
            String fieldName = entry.getKey();
            Object val = entry.getValue();
            pResource.updateAttribute(fieldName, val);
        }
    }
    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> resourceSet = (relationship == null) ? null : relationship.toPersistentResources(requestScope);
            pResource.updateRelation(fieldName, resourceSet);
        }
    }
    return pResource;
}
Also used : PersistentResource(com.yahoo.elide.core.PersistentResource) JsonApiDocument(com.yahoo.elide.jsonapi.models.JsonApiDocument) UnknownEntityException(com.yahoo.elide.core.exceptions.UnknownEntityException) Resource(com.yahoo.elide.jsonapi.models.Resource) PersistentResource(com.yahoo.elide.core.PersistentResource) ToString(lombok.ToString) InvalidValueException(com.yahoo.elide.core.exceptions.InvalidValueException) InvalidEntityBodyException(com.yahoo.elide.core.exceptions.InvalidEntityBodyException) Relationship(com.yahoo.elide.jsonapi.models.Relationship) InternalServerErrorException(com.yahoo.elide.core.exceptions.InternalServerErrorException) JsonApiMapper(com.yahoo.elide.jsonapi.JsonApiMapper) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) HashMap(java.util.HashMap) Map(java.util.Map) MultivaluedMap(javax.ws.rs.core.MultivaluedMap)

Example 62 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, SubCollectionReadEntityContext ctx) {
    String id = ctx.entity().id().getText();
    String subCollection = ctx.entity().term().getText();
    PersistentResource nextRecord = resource.getRelation(projection.getRelationship(subCollection).orElseThrow(IllegalStateException::new), id);
    state.setState(new RecordTerminalState(nextRecord));
}
Also used : PersistentResource(com.yahoo.elide.core.PersistentResource)

Example 63 with PersistentResource

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

the class StartState method handle.

@Override
public void handle(StateContext state, RootCollectionSubCollectionContext ctx) {
    PersistentResource record = entityRecord(state, ctx.entity());
    state.setState(new RecordState(record, state.getRequestScope().getEntityProjection()));
}
Also used : PersistentResource(com.yahoo.elide.core.PersistentResource)

Example 64 with PersistentResource

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

the class IncludedProcessor method addResourcesForPath.

/**
 * Adds all the relation resources for a given relation path to the included block of the
 * JsonApiDocument.
 */
private void addResourcesForPath(JsonApiDocument jsonApiDocument, PersistentResource<?> rec, List<String> relationPath, EntityProjection projection) {
    // Pop off a relation of relation path
    String relation = relationPath.remove(0);
    Set<PersistentResource> collection;
    Relationship relationship = projection.getRelationship(relation).orElseThrow(IllegalStateException::new);
    try {
        collection = rec.getRelationCheckedFiltered(relationship).toList(LinkedHashSet::new).blockingGet();
    } catch (ForbiddenAccessException e) {
        return;
    }
    collection.forEach(resource -> {
        jsonApiDocument.addIncluded(resource.toResource());
        // If more relations left in the path, process a level deeper
        if (!relationPath.isEmpty()) {
            // Use a copy of the relationPath to preserve the path for remaining branches of the relationship tree
            addResourcesForPath(jsonApiDocument, resource, new ArrayList<>(relationPath), relationship.getProjection());
        }
    });
}
Also used : LinkedHashSet(java.util.LinkedHashSet) PersistentResource(com.yahoo.elide.core.PersistentResource) Relationship(com.yahoo.elide.core.request.Relationship) ForbiddenAccessException(com.yahoo.elide.core.exceptions.ForbiddenAccessException)

Example 65 with PersistentResource

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

the class Relationship method toPersistentResources.

public Set<PersistentResource> toPersistentResources(RequestScope requestScope) throws ForbiddenAccessException, InvalidObjectIdentifierException {
    Set<PersistentResource> res = new LinkedHashSet<>();
    if (data == null) {
        return null;
    }
    Collection<Resource> resources = data.get();
    if (resources != null) {
        for (Resource resource : resources) {
            try {
                if (resource == null) {
                    continue;
                }
                res.add(resource.toPersistentResource(requestScope));
            } catch (ForbiddenAccessException e) {
            // skip resource
            }
        }
    }
    return res.isEmpty() ? (data.isToOne() ? null : res) : res;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) PersistentResource(com.yahoo.elide.core.PersistentResource) PersistentResource(com.yahoo.elide.core.PersistentResource) ForbiddenAccessException(com.yahoo.elide.core.exceptions.ForbiddenAccessException)

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