Search in sources :

Example 1 with InternalServerErrorException

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

the class PersistentResource method addInverseRelation.

/**
 * If a bidirectional relationship exists, attempts to add itself to the inverse
 * relationship. Given A to B as the relationship, A corresponds to this and B is the inverse.
 *
 * @param relationName The name of the relationship on this (A) object.
 * @param inverseObj   The value (B) which has been added to this object.
 */
protected void addInverseRelation(String relationName, Object inverseObj) {
    String inverseName = dictionary.getRelationInverse(type, relationName);
    if (!"".equals(inverseName)) {
        Type<?> inverseType = dictionary.getType(inverseObj, inverseName);
        String uuid = requestScope.getUUIDFor(inverseObj);
        PersistentResource inverseResource = new PersistentResource(inverseObj, this, relationName, uuid, requestScope);
        Object inverseRelation = inverseResource.getValueUnchecked(inverseName);
        if (COLLECTION_TYPE.isAssignableFrom(inverseType)) {
            if (inverseRelation != null) {
                inverseResource.modifyCollection((Collection) inverseRelation, inverseName, Set.of(this.getObject()), Collections.emptySet(), false);
            } else {
                inverseResource.setValueChecked(inverseName, Collections.singleton(this.getObject()));
            }
        } else if (inverseType.isAssignableFrom(this.getResourceType())) {
            inverseResource.setValueChecked(inverseName, this.getObject());
        } else {
            throw new InternalServerErrorException("Relationship type mismatch");
        }
        inverseResource.markDirty();
        RelationshipType inverseRelationType = inverseResource.getRelationshipType(inverseName);
        if (inverseRelationType.isToOne()) {
            // hook for updateToOneRelation
            transaction.updateToOneRelation(transaction, inverseObj, inverseName, obj, requestScope);
        } else {
            // hook for updateToManyRelation
            assert (inverseRelation == null || inverseRelation instanceof Collection) : inverseName + " not a collection";
            transaction.updateToManyRelation(transaction, inverseObj, inverseName, Sets.newHashSet(obj), new LinkedHashSet<>(), requestScope);
        }
    }
}
Also used : RelationshipType(com.yahoo.elide.core.dictionary.RelationshipType) InternalServerErrorException(com.yahoo.elide.core.exceptions.InternalServerErrorException) Collection(java.util.Collection)

Example 2 with InternalServerErrorException

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

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

the class PersistentResource method deleteInverseRelation.

/**
 * If a bidirectional relationship exists, attempts to delete itself from the inverse
 * relationship. Given A to B as the relationship, A corresponds to this and B is the inverse.
 *
 * @param relationName  The name of the relationship on this (A) object.
 * @param inverseEntity The value (B) which has been deleted from this object.
 */
protected void deleteInverseRelation(String relationName, Object inverseEntity) {
    String inverseField = getInverseRelationField(relationName);
    if (!"".equals(inverseField)) {
        Type<?> inverseType = dictionary.getType(inverseEntity, inverseField);
        String uuid = requestScope.getUUIDFor(inverseEntity);
        PersistentResource inverseResource = new PersistentResource(inverseEntity, this, relationName, uuid, requestScope);
        Object inverseRelation = inverseResource.getValueUnchecked(inverseField);
        if (inverseRelation == null) {
            return;
        }
        if (inverseRelation instanceof Collection) {
            inverseResource.modifyCollection((Collection) inverseRelation, inverseField, Collections.emptySet(), Set.of(this.getObject()), false);
        } else if (inverseType.isAssignableFrom(this.getResourceType())) {
            inverseResource.nullValue(inverseField, this);
        } else {
            throw new InternalServerErrorException("Relationship type mismatch");
        }
        inverseResource.markDirty();
        RelationshipType inverseRelationType = inverseResource.getRelationshipType(inverseField);
        if (inverseRelationType.isToOne()) {
            // hook for updateToOneRelation
            transaction.updateToOneRelation(transaction, inverseEntity, inverseField, null, requestScope);
        } else {
            // hook for updateToManyRelation
            assert (inverseRelation instanceof Collection) : inverseField + " not a collection";
            transaction.updateToManyRelation(transaction, inverseEntity, inverseField, new LinkedHashSet<>(), Sets.newHashSet(obj), requestScope);
        }
    }
}
Also used : RelationshipType(com.yahoo.elide.core.dictionary.RelationshipType) Collection(java.util.Collection) InternalServerErrorException(com.yahoo.elide.core.exceptions.InternalServerErrorException)

Aggregations

InternalServerErrorException (com.yahoo.elide.core.exceptions.InternalServerErrorException)3 RelationshipType (com.yahoo.elide.core.dictionary.RelationshipType)2 Collection (java.util.Collection)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 PersistentResource (com.yahoo.elide.core.PersistentResource)1 InvalidEntityBodyException (com.yahoo.elide.core.exceptions.InvalidEntityBodyException)1 InvalidValueException (com.yahoo.elide.core.exceptions.InvalidValueException)1 UnknownEntityException (com.yahoo.elide.core.exceptions.UnknownEntityException)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