Search in sources :

Example 1 with InvalidAttributeException

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

the class EntityDictionary method setValue.

/**
 * Invoke the set[fieldName] method on the target object OR set the field with the corresponding name.
 * @param target The object which owns the field to set
 * @param fieldName the field name to set or invoke equivalent set method
 * @param value the value to set
 */
public void setValue(Object target, String fieldName, Object value) {
    Type<?> targetClass = getType(target);
    String targetType = getJsonAliasFor(targetClass);
    String fieldAlias = fieldName;
    try {
        Type<?> fieldClass = getType(targetClass, fieldName);
        String realName = getNameFromAlias(target, fieldName);
        fieldAlias = (realName != null) ? realName : fieldName;
        String setMethod = "set" + StringUtils.capitalize(fieldAlias);
        Method method = EntityDictionary.findMethod(targetClass, setMethod, fieldClass);
        method.invoke(target, coerce(target, value, fieldAlias, fieldClass));
    } catch (IllegalAccessException e) {
        throw new InvalidAttributeException(fieldAlias, targetType, e);
    } catch (InvocationTargetException e) {
        throw handleInvocationTargetException(e);
    } catch (IllegalArgumentException | NoSuchMethodException noMethod) {
        AccessibleObject accessor = getAccessibleObject(target, fieldAlias);
        if (accessor != null && accessor instanceof Field) {
            Field field = (Field) accessor;
            try {
                field.set(target, coerce(target, value, fieldAlias, field.getType()));
            } catch (IllegalAccessException noField) {
                throw new InvalidAttributeException(fieldAlias, targetType, noField);
            }
        } else {
            throw new InvalidAttributeException(fieldAlias, targetType);
        }
    }
}
Also used : Field(com.yahoo.elide.core.type.Field) InvalidAttributeException(com.yahoo.elide.core.exceptions.InvalidAttributeException) AccessibleObject(com.yahoo.elide.core.type.AccessibleObject) Method(com.yahoo.elide.core.type.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 2 with InvalidAttributeException

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

the class PersistentResource method getRelationUnchecked.

/**
 * Retrieve an unchecked set of relations.
 */
private Observable<PersistentResource> getRelationUnchecked(com.yahoo.elide.core.request.Relationship relationship) {
    String relationName = relationship.getName();
    FilterExpression filterExpression = relationship.getProjection().getFilterExpression();
    Pagination pagination = relationship.getProjection().getPagination();
    Sorting sorting = relationship.getProjection().getSorting();
    RelationshipType type = getRelationshipType(relationName);
    final Type<?> relationClass = dictionary.getParameterizedType(obj, relationName);
    if (relationClass == null) {
        throw new InvalidAttributeException(relationName, this.getTypeName());
    }
    // Invoke filterExpressionCheck and then merge with filterExpression.
    Optional<FilterExpression> permissionFilter = getPermissionFilterExpression(relationClass, requestScope, relationship.getProjection().getRequestedFields());
    Optional<FilterExpression> computedFilters = Optional.ofNullable(filterExpression);
    if (permissionFilter.isPresent() && filterExpression != null) {
        FilterExpression mergedExpression = new AndFilterExpression(filterExpression, permissionFilter.get());
        computedFilters = Optional.of(mergedExpression);
    } else if (permissionFilter.isPresent()) {
        computedFilters = permissionFilter;
    }
    com.yahoo.elide.core.request.Relationship modifiedRelationship = relationship.copyOf().projection(relationship.getProjection().copyOf().filterExpression(computedFilters.orElse(null)).sorting(sorting).pagination(pagination).build()).build();
    Observable<PersistentResource> resources;
    if (type.isToMany()) {
        DataStoreIterable val = transaction.getToManyRelation(transaction, obj, modifiedRelationship, requestScope);
        if (val == null) {
            return Observable.empty();
        }
        resources = Observable.fromIterable(new PersistentResourceSet(this, relationName, val, requestScope));
    } else {
        Object val = transaction.getToOneRelation(transaction, obj, modifiedRelationship, requestScope);
        if (val == null) {
            return Observable.empty();
        }
        resources = Observable.fromArray(new PersistentResource(val, this, relationName, requestScope.getUUIDFor(val), requestScope));
    }
    return resources;
}
Also used : InvalidAttributeException(com.yahoo.elide.core.exceptions.InvalidAttributeException) RelationshipType(com.yahoo.elide.core.dictionary.RelationshipType) DataStoreIterable(com.yahoo.elide.core.datastore.DataStoreIterable) Sorting(com.yahoo.elide.core.request.Sorting) Pagination(com.yahoo.elide.core.request.Pagination) AndFilterExpression(com.yahoo.elide.core.filter.expression.AndFilterExpression) FilterExpression(com.yahoo.elide.core.filter.expression.FilterExpression) AndFilterExpression(com.yahoo.elide.core.filter.expression.AndFilterExpression)

Aggregations

InvalidAttributeException (com.yahoo.elide.core.exceptions.InvalidAttributeException)2 DataStoreIterable (com.yahoo.elide.core.datastore.DataStoreIterable)1 RelationshipType (com.yahoo.elide.core.dictionary.RelationshipType)1 AndFilterExpression (com.yahoo.elide.core.filter.expression.AndFilterExpression)1 FilterExpression (com.yahoo.elide.core.filter.expression.FilterExpression)1 Pagination (com.yahoo.elide.core.request.Pagination)1 Sorting (com.yahoo.elide.core.request.Sorting)1 AccessibleObject (com.yahoo.elide.core.type.AccessibleObject)1 Field (com.yahoo.elide.core.type.Field)1 Method (com.yahoo.elide.core.type.Method)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1