Search in sources :

Example 1 with ParameterizedModel

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);
}
Also used : ParameterizedModel(com.yahoo.elide.core.type.ParameterizedModel)

Example 2 with ParameterizedModel

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;
}
Also used : Table(com.yahoo.elide.datastores.aggregation.metadata.models.Table) Column(com.yahoo.elide.datastores.aggregation.metadata.models.Column) Attribute(com.yahoo.elide.core.request.Attribute) ValueType(com.yahoo.elide.datastores.aggregation.metadata.enums.ValueType) ParameterizedModel(com.yahoo.elide.core.type.ParameterizedModel) SQLColumnProjection(com.yahoo.elide.datastores.aggregation.queryengines.sql.query.SQLColumnProjection) ColumnProjection(com.yahoo.elide.datastores.aggregation.query.ColumnProjection)

Aggregations

ParameterizedModel (com.yahoo.elide.core.type.ParameterizedModel)2 Attribute (com.yahoo.elide.core.request.Attribute)1 ValueType (com.yahoo.elide.datastores.aggregation.metadata.enums.ValueType)1 Column (com.yahoo.elide.datastores.aggregation.metadata.models.Column)1 Table (com.yahoo.elide.datastores.aggregation.metadata.models.Table)1 ColumnProjection (com.yahoo.elide.datastores.aggregation.query.ColumnProjection)1 SQLColumnProjection (com.yahoo.elide.datastores.aggregation.queryengines.sql.query.SQLColumnProjection)1