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;
}
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);
}
Aggregations