Search in sources :

Example 6 with InvalidObjectIdentifierException

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

the class DataStoreTransaction method loadObject.

/**
 * Loads an object by ID.  The reason we support both load by ID and load by filter is that
 * some legacy stores are optimized to load by ID.
 *
 * @param entityProjection the collection to load.
 * @param id - the ID of the object to load.
 * @param scope - the current request scope
 * @param <T> The model type being loaded.
 * It is optional for the data store to attempt evaluation.
 * @return the loaded object if it exists AND any provided security filters pass.
 */
default <T> T loadObject(EntityProjection entityProjection, Serializable id, RequestScope scope) {
    Type<?> entityClass = entityProjection.getType();
    FilterExpression filterExpression = entityProjection.getFilterExpression();
    EntityDictionary dictionary = scope.getDictionary();
    Type idType = dictionary.getIdType(entityClass);
    String idField = dictionary.getIdFieldName(entityClass);
    FilterExpression idFilter = new InPredicate(new Path.PathElement(entityClass, idType, idField), id);
    FilterExpression joinedFilterExpression = (filterExpression != null) ? new AndFilterExpression(idFilter, filterExpression) : idFilter;
    Iterable<T> results = loadObjects(entityProjection.copyOf().filterExpression(joinedFilterExpression).build(), scope);
    Iterator<T> it = results == null ? null : results.iterator();
    if (it != null && it.hasNext()) {
        T obj = it.next();
        if (!it.hasNext()) {
            return obj;
        }
        // Multiple objects with the same ID.
        throw new InvalidObjectIdentifierException(id.toString(), dictionary.getJsonAliasFor(entityClass));
    }
    return null;
}
Also used : Path(com.yahoo.elide.core.Path) InPredicate(com.yahoo.elide.core.filter.predicates.InPredicate) Type(com.yahoo.elide.core.type.Type) InvalidObjectIdentifierException(com.yahoo.elide.core.exceptions.InvalidObjectIdentifierException) AndFilterExpression(com.yahoo.elide.core.filter.expression.AndFilterExpression) FilterExpression(com.yahoo.elide.core.filter.expression.FilterExpression) EntityDictionary(com.yahoo.elide.core.dictionary.EntityDictionary) AndFilterExpression(com.yahoo.elide.core.filter.expression.AndFilterExpression)

Example 7 with InvalidObjectIdentifierException

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

InvalidObjectIdentifierException (com.yahoo.elide.core.exceptions.InvalidObjectIdentifierException)7 EntityDictionary (com.yahoo.elide.core.dictionary.EntityDictionary)6 AndFilterExpression (com.yahoo.elide.core.filter.expression.AndFilterExpression)4 FilterExpression (com.yahoo.elide.core.filter.expression.FilterExpression)4 DataStoreTransaction (com.yahoo.elide.core.datastore.DataStoreTransaction)3 InPredicate (com.yahoo.elide.core.filter.predicates.InPredicate)3 JsonIgnore (com.fasterxml.jackson.annotation.JsonIgnore)2 Preconditions (com.google.common.base.Preconditions)2 Predicates (com.google.common.base.Predicates)2 Sets (com.google.common.collect.Sets)2 Audit (com.yahoo.elide.annotation.Audit)2 CreatePermission (com.yahoo.elide.annotation.CreatePermission)2 DeletePermission (com.yahoo.elide.annotation.DeletePermission)2 LifeCycleHookBinding (com.yahoo.elide.annotation.LifeCycleHookBinding)2 CREATE (com.yahoo.elide.annotation.LifeCycleHookBinding.Operation.CREATE)2 DELETE (com.yahoo.elide.annotation.LifeCycleHookBinding.Operation.DELETE)2 UPDATE (com.yahoo.elide.annotation.LifeCycleHookBinding.Operation.UPDATE)2 NonTransferable (com.yahoo.elide.annotation.NonTransferable)2 ReadPermission (com.yahoo.elide.annotation.ReadPermission)2 UpdatePermission (com.yahoo.elide.annotation.UpdatePermission)2