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