Search in sources :

Example 1 with Field

use of com.yahoo.elide.core.type.Field in project elide by yahoo.

the class EntityBinding method getFieldName.

/**
 * Returns name of field whether public member or method.
 *
 * @param fieldOrMethod field or method
 * @return field or method name
 */
public static String getFieldName(AccessibleObject fieldOrMethod) {
    if (fieldOrMethod instanceof Field) {
        return ((Field) fieldOrMethod).getName();
    }
    Method method = (Method) fieldOrMethod;
    String name = method.getName();
    boolean hasValidParameterCount = method.getParameterCount() == 0 || isRequestScopeableMethod(method);
    if (name.startsWith("get") && hasValidParameterCount) {
        return StringUtils.uncapitalize(name.substring("get".length()));
    }
    if (name.startsWith("is") && hasValidParameterCount) {
        return StringUtils.uncapitalize(name.substring("is".length()));
    }
    return null;
}
Also used : Field(com.yahoo.elide.core.type.Field) Method(com.yahoo.elide.core.type.Method)

Example 2 with Field

use of com.yahoo.elide.core.type.Field 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 3 with Field

use of com.yahoo.elide.core.type.Field in project elide by yahoo.

the class EntityDictionary method getId.

/**
 * Gets id.
 *
 * @param value the value
 * @return the id
 */
public String getId(Object value) {
    if (value == null) {
        return null;
    }
    try {
        AccessibleObject idField = null;
        Type<?> valueClass = getType(value);
        for (; idField == null && valueClass != null; valueClass = valueClass.getSuperclass()) {
            try {
                idField = getEntityBinding(valueClass).getIdField();
            } catch (NullPointerException e) {
                log.warn("Class: {} ID Field: {}", valueClass.getSimpleName(), idField);
            }
        }
        Type<?> idClass;
        Object idValue;
        if (idField instanceof Field) {
            idValue = ((Field) idField).get(value);
            idClass = ((Field) idField).getType();
        } else if (idField instanceof Method) {
            idValue = ((Method) idField).invoke(value, (Object[]) null);
            idClass = ((Method) idField).getReturnType();
        } else {
            return null;
        }
        Serde serde = serdeLookup.apply(((ClassType) idClass).getCls());
        if (serde != null) {
            return String.valueOf(serde.serialize(idValue));
        }
        return String.valueOf(idValue);
    } catch (IllegalArgumentException | IllegalAccessException | InvocationTargetException e) {
        return null;
    }
}
Also used : Serde(com.yahoo.elide.core.utils.coerce.converters.Serde) Field(com.yahoo.elide.core.type.Field) AccessibleObject(com.yahoo.elide.core.type.AccessibleObject) AccessibleObject(com.yahoo.elide.core.type.AccessibleObject) Method(com.yahoo.elide.core.type.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 4 with Field

use of com.yahoo.elide.core.type.Field in project elide by yahoo.

the class TableTypeTest method testHiddenDimension.

@Test
void testHiddenDimension() throws Exception {
    Table testTable = Table.builder().table("table1").name("Table").dimension(Dimension.builder().name("dim1").type(Type.BOOLEAN).hidden(true).build()).build();
    TableType testType = new TableType(testTable);
    Field field = testType.getDeclaredField("dim1");
    assertNotNull(field);
    ColumnMeta columnMeta = field.getAnnotation(ColumnMeta.class);
    assertNotNull(columnMeta);
    assertTrue(columnMeta.isHidden());
}
Also used : Field(com.yahoo.elide.core.type.Field) Table(com.yahoo.elide.modelconfig.model.Table) FromTable(com.yahoo.elide.datastores.aggregation.queryengines.sql.annotation.FromTable) ColumnMeta(com.yahoo.elide.datastores.aggregation.annotation.ColumnMeta) Test(org.junit.jupiter.api.Test)

Example 5 with Field

use of com.yahoo.elide.core.type.Field in project elide by yahoo.

the class TableTypeTest method testMeasureAnnotations.

@Test
void testMeasureAnnotations() throws Exception {
    Set<String> tags = new HashSet<>(Arrays.asList("tag1", "tag2"));
    Table testTable = Table.builder().table("table1").name("Table").measure(Measure.builder().type(Type.MONEY).category("category1").definition("SUM{{  price}}").hidden(false).friendlyName("Price").name("price").readAccess("Admin").description("A measure").tags(tags).build()).build();
    TableType testType = new TableType(testTable);
    Field field = testType.getDeclaredField("price");
    assertNotNull(field);
    ReadPermission readPermission = field.getAnnotation(ReadPermission.class);
    assertEquals("Admin", readPermission.expression());
    ColumnMeta columnMeta = field.getAnnotation(ColumnMeta.class);
    assertEquals("A measure", columnMeta.description());
    assertEquals("category1", columnMeta.category());
    assertEquals("Price", columnMeta.friendlyName());
    assertEquals(CardinalitySize.UNKNOWN, columnMeta.size());
    assertEquals(tags, new HashSet<>(Arrays.asList(columnMeta.tags())));
    MetricFormula metricFormula = field.getAnnotation(MetricFormula.class);
    assertEquals("SUM{{price}}", metricFormula.value());
    assertEquals(DefaultMetricProjectionMaker.class, metricFormula.maker());
}
Also used : Field(com.yahoo.elide.core.type.Field) Table(com.yahoo.elide.modelconfig.model.Table) FromTable(com.yahoo.elide.datastores.aggregation.queryengines.sql.annotation.FromTable) ColumnMeta(com.yahoo.elide.datastores.aggregation.annotation.ColumnMeta) MetricFormula(com.yahoo.elide.datastores.aggregation.annotation.MetricFormula) ReadPermission(com.yahoo.elide.annotation.ReadPermission) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Aggregations

Field (com.yahoo.elide.core.type.Field)16 FromTable (com.yahoo.elide.datastores.aggregation.queryengines.sql.annotation.FromTable)12 Table (com.yahoo.elide.modelconfig.model.Table)12 Test (org.junit.jupiter.api.Test)12 ColumnMeta (com.yahoo.elide.datastores.aggregation.annotation.ColumnMeta)6 Method (com.yahoo.elide.core.type.Method)4 HashSet (java.util.HashSet)4 ReadPermission (com.yahoo.elide.annotation.ReadPermission)3 AccessibleObject (com.yahoo.elide.core.type.AccessibleObject)2 DimensionFormula (com.yahoo.elide.datastores.aggregation.annotation.DimensionFormula)2 MetricFormula (com.yahoo.elide.datastores.aggregation.annotation.MetricFormula)2 Temporal (com.yahoo.elide.datastores.aggregation.annotation.Temporal)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 InvalidAttributeException (com.yahoo.elide.core.exceptions.InvalidAttributeException)1 Serde (com.yahoo.elide.core.utils.coerce.converters.Serde)1 Type (com.yahoo.elide.modelconfig.model.Type)1 HashMap (java.util.HashMap)1 EnumType (javax.persistence.EnumType)1 Enumerated (javax.persistence.Enumerated)1 Id (javax.persistence.Id)1