use of com.yahoo.elide.core.type.ParameterizedModel in project elide by yahoo.
the class FieldType method set.
@Override
public void set(Object obj, Object value) throws IllegalArgumentException, IllegalAccessException {
if (!ParameterizedModel.class.isAssignableFrom(obj.getClass())) {
throw new IllegalArgumentException("Class is not a dynamic type: " + obj.getClass());
}
ParameterizedModel model = (ParameterizedModel) obj;
model.addAttributeValue(Attribute.builder().name(name).type(type).alias(name).build(), value);
}
use of com.yahoo.elide.core.type.ParameterizedModel in project elide by yahoo.
the class EntityHydrator method coerceObjectToEntity.
/**
* Coerces results from a {@link Query} into an Object.
*
* @param result a fieldName-value map
* @param counter Monotonically increasing number to generate IDs.
* @return A hydrated entity object.
*/
protected Object coerceObjectToEntity(Map<String, Object> result, MutableInt counter) {
Table table = getBaseTable(query);
Type<?> entityClass = entityDictionary.getEntityClass(table.getName(), table.getVersion());
// Construct the object.
Object entityInstance;
try {
entityInstance = entityClass.newInstance();
} catch (InstantiationException | IllegalAccessException e) {
throw new IllegalStateException(e);
}
result.forEach((fieldName, value) -> {
ColumnProjection columnProjection = query.getColumnProjection(fieldName);
Column column = table.getColumn(Column.class, columnProjection.getName());
Type<?> fieldType = getType(entityClass, columnProjection);
Attribute attribute = projectionToAttribute(columnProjection, fieldType);
ValueType valueType = column.getValueType();
if (entityInstance instanceof ParameterizedModel) {
// This is an ENUM_TEXT or ENUM_ORDINAL type.
if (// Java enums can be coerced directly via CoerceUtil - so skip them.
!fieldType.isEnum() && valueType == ValueType.TEXT && column.getValues() != null && !column.getValues().isEmpty()) {
value = convertToEnumValue(value, column.getValues());
}
((ParameterizedModel) entityInstance).addAttributeValue(attribute, CoerceUtil.coerce(value, fieldType));
} else {
getEntityDictionary().setValue(entityInstance, fieldName, value);
}
});
// Set the ID (it must be coerced from an integer)
getEntityDictionary().setValue(entityInstance, getEntityDictionary().getIdFieldName(entityClass), counter.getAndIncrement());
return entityInstance;
}
Aggregations