Search in sources :

Example 1 with EntityBinding

use of com.yahoo.elide.core.dictionary.EntityBinding in project elide by yahoo.

the class MultiplexManager method populateEntityDictionary.

@Override
public void populateEntityDictionary(EntityDictionary dictionary) {
    this.dictionary = dictionary;
    for (DataStore dataStore : dataStores) {
        EntityDictionary subordinateDictionary = new EntityDictionary(dictionary.getCheckMappings(), dictionary.getRoleChecks(), dictionary.getInjector(), dictionary.getSerdeLookup(), dictionary.getEntitiesToExclude(), dictionary.getScanner());
        dataStore.populateEntityDictionary(subordinateDictionary);
        for (EntityBinding binding : subordinateDictionary.getBindings(false)) {
            // route class to this database manager
            this.dataStoreMap.put(binding.entityClass, dataStore);
            // bind to multiplex dictionary
            dictionary.bindEntity(binding);
        }
        for (Map.Entry<Type<?>, Function<RequestScope, PermissionExecutor>> entry : subordinateDictionary.getEntityPermissionExecutor().entrySet()) {
            dictionary.bindPermissionExecutor(entry.getKey(), entry.getValue());
        }
    }
}
Also used : Function(java.util.function.Function) Type(com.yahoo.elide.core.type.Type) DataStore(com.yahoo.elide.core.datastore.DataStore) EntityBinding(com.yahoo.elide.core.dictionary.EntityBinding) EntityDictionary(com.yahoo.elide.core.dictionary.EntityDictionary) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Map(java.util.Map)

Example 2 with EntityBinding

use of com.yahoo.elide.core.dictionary.EntityBinding in project elide by yahoo.

the class PersistentResource method copyComplexAttribute.

/**
 * Copies a complex attribute.  If the attribute fields are complex, recurses to perform a deep copy.
 * @param object The attribute to copy.
 * @return The copy.
 */
private Object copyComplexAttribute(Object object) {
    if (object == null) {
        return null;
    }
    Type<?> type = getType(object);
    EntityBinding binding = dictionary.getEntityBinding(type);
    Preconditions.checkState(!binding.equals(EMPTY_BINDING), "Model not found.");
    Preconditions.checkState(binding.apiRelationships.isEmpty(), "Deep copy of relationships not supported");
    Object copy;
    try {
        copy = type.newInstance();
    } catch (InstantiationException | IllegalAccessException e) {
        throw new IllegalStateException("Cannot perform deep copy of " + type.getName(), e);
    }
    binding.apiAttributes.forEach(attribute -> {
        Object newValue;
        Object oldValue = dictionary.getValue(object, attribute, requestScope);
        if (!dictionary.isComplexAttribute(type, attribute)) {
            newValue = oldValue;
        } else {
            newValue = copyComplexAttribute(oldValue);
        }
        dictionary.setValue(copy, attribute, newValue);
    });
    return copy;
}
Also used : EntityBinding(com.yahoo.elide.core.dictionary.EntityBinding)

Example 3 with EntityBinding

use of com.yahoo.elide.core.dictionary.EntityBinding in project elide by yahoo.

the class Table method constructColumns.

/**
 * Construct all columns of this table.
 *
 * @param cls table class
 * @param dictionary dictionary contains the table class
 * @return all resolved column metadata
 */
private Set<Column> constructColumns(Type<?> cls, EntityDictionary dictionary) {
    EntityBinding binding = dictionary.getEntityBinding(cls);
    Set<Column> columns = new HashSet<>();
    // TODO - refactor entity binding to have classes for attributes & relationships.
    binding.fieldsToValues.forEach((name, field) -> {
        if (EntityBinding.isIdField(field)) {
            return;
        }
        ValueType valueType = getValueType(cls, name, dictionary);
        if (valueType == ValueType.UNKNOWN) {
            return;
        }
        if (isMetricField(dictionary, cls, name)) {
            columns.add(constructMetric(name, dictionary));
        } else if (dictionary.attributeOrRelationAnnotationExists(cls, name, Temporal.class)) {
            columns.add(constructTimeDimension(name, dictionary));
        } else {
            columns.add(constructDimension(name, dictionary));
        }
    });
    // add id field if exists
    if (dictionary.getIdFieldName(cls) != null) {
        String idFieldName = dictionary.getIdFieldName(cls);
        if (AggregationDataStore.isAggregationStoreModel(cls)) {
            columns.add(constructMetric(idFieldName, dictionary));
        } else {
            columns.add(constructDimension(idFieldName, dictionary));
        }
    }
    return columns;
}
Also used : Temporal(com.yahoo.elide.datastores.aggregation.annotation.Temporal) Column.getValueType(com.yahoo.elide.datastores.aggregation.metadata.models.Column.getValueType) ValueType(com.yahoo.elide.datastores.aggregation.metadata.enums.ValueType) EntityBinding(com.yahoo.elide.core.dictionary.EntityBinding) ToString(lombok.ToString) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 4 with EntityBinding

use of com.yahoo.elide.core.dictionary.EntityBinding in project elide by yahoo.

the class NonEntityDictionary method bindEntity.

/**
 * Add given class to dictionary.
 *
 * @param cls Entity bean class
 */
@Override
public void bindEntity(Type<?> cls) {
    String type = WordUtils.uncapitalize(cls.getSimpleName());
    Type<?> duplicate = bindJsonApiToEntity.put(Pair.of(type, NO_VERSION), cls);
    if (duplicate != null && !duplicate.equals(cls)) {
        log.error("Duplicate binding {} for {}, {}", type, cls, duplicate);
        throw new DuplicateMappingException(type + " " + cls.getName() + ":" + duplicate.getName());
    }
    entityBindings.put(cls, new EntityBinding(getInjector(), cls, type));
}
Also used : EntityBinding(com.yahoo.elide.core.dictionary.EntityBinding) DuplicateMappingException(com.yahoo.elide.core.exceptions.DuplicateMappingException)

Aggregations

EntityBinding (com.yahoo.elide.core.dictionary.EntityBinding)4 DataStore (com.yahoo.elide.core.datastore.DataStore)1 EntityDictionary (com.yahoo.elide.core.dictionary.EntityDictionary)1 DuplicateMappingException (com.yahoo.elide.core.exceptions.DuplicateMappingException)1 Type (com.yahoo.elide.core.type.Type)1 Temporal (com.yahoo.elide.datastores.aggregation.annotation.Temporal)1 ValueType (com.yahoo.elide.datastores.aggregation.metadata.enums.ValueType)1 Column.getValueType (com.yahoo.elide.datastores.aggregation.metadata.models.Column.getValueType)1 HashSet (java.util.HashSet)1 LinkedHashSet (java.util.LinkedHashSet)1 Map (java.util.Map)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 Function (java.util.function.Function)1 ToString (lombok.ToString)1