Search in sources :

Example 1 with UnknownEntityException

use of com.yahoo.elide.core.exceptions.UnknownEntityException 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 2 with UnknownEntityException

use of com.yahoo.elide.core.exceptions.UnknownEntityException in project elide by yahoo.

the class Resource method toPersistentResource.

public PersistentResource<?> toPersistentResource(RequestScope requestScope) throws ForbiddenAccessException, InvalidObjectIdentifierException {
    EntityDictionary dictionary = requestScope.getDictionary();
    Type<?> cls = dictionary.getEntityClass(type, requestScope.getApiVersion());
    if (cls == null) {
        throw new UnknownEntityException(type);
    }
    if (id == null) {
        throw new InvalidObjectIdentifierException(id, type);
    }
    EntityProjection projection = EntityProjection.builder().type(cls).build();
    return PersistentResource.loadRecord(projection, id, requestScope);
}
Also used : EntityProjection(com.yahoo.elide.core.request.EntityProjection) UnknownEntityException(com.yahoo.elide.core.exceptions.UnknownEntityException) InvalidObjectIdentifierException(com.yahoo.elide.core.exceptions.InvalidObjectIdentifierException) EntityDictionary(com.yahoo.elide.core.dictionary.EntityDictionary)

Aggregations

UnknownEntityException (com.yahoo.elide.core.exceptions.UnknownEntityException)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 PersistentResource (com.yahoo.elide.core.PersistentResource)1 EntityDictionary (com.yahoo.elide.core.dictionary.EntityDictionary)1 InternalServerErrorException (com.yahoo.elide.core.exceptions.InternalServerErrorException)1 InvalidEntityBodyException (com.yahoo.elide.core.exceptions.InvalidEntityBodyException)1 InvalidObjectIdentifierException (com.yahoo.elide.core.exceptions.InvalidObjectIdentifierException)1 InvalidValueException (com.yahoo.elide.core.exceptions.InvalidValueException)1 EntityProjection (com.yahoo.elide.core.request.EntityProjection)1 JsonApiMapper (com.yahoo.elide.jsonapi.JsonApiMapper)1 JsonApiDocument (com.yahoo.elide.jsonapi.models.JsonApiDocument)1 Relationship (com.yahoo.elide.jsonapi.models.Relationship)1 Resource (com.yahoo.elide.jsonapi.models.Resource)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 MultivaluedMap (javax.ws.rs.core.MultivaluedMap)1 ToString (lombok.ToString)1