Search in sources :

Example 41 with EntityDictionary

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

the class FilteredIteratorTest method testEmptyResult.

@Test
public void testEmptyResult() throws Exception {
    EntityDictionary dictionary = EntityDictionary.builder().build();
    dictionary.bindEntity(Book.class);
    List<Book> books = List.of();
    RSQLFilterDialect filterDialect = RSQLFilterDialect.builder().dictionary(dictionary).build();
    FilterExpression expression = filterDialect.parse(ClassType.of(Book.class), new HashSet<>(), "title==*bar", NO_VERSION);
    RequestScope scope = new TestRequestScope(null, null, dictionary);
    Iterator<Book> bookIterator = new FilteredIterator<>(expression, scope, books.iterator());
    assertFalse(bookIterator.hasNext());
    assertThrows(NoSuchElementException.class, () -> bookIterator.next());
}
Also used : TestRequestScope(com.yahoo.elide.core.TestRequestScope) Book(example.Book) FilterExpression(com.yahoo.elide.core.filter.expression.FilterExpression) EntityDictionary(com.yahoo.elide.core.dictionary.EntityDictionary) RSQLFilterDialect(com.yahoo.elide.core.filter.dialect.RSQLFilterDialect) RequestScope(com.yahoo.elide.core.RequestScope) TestRequestScope(com.yahoo.elide.core.TestRequestScope) Test(org.junit.jupiter.api.Test)

Example 42 with EntityDictionary

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

the class FilterPredicateTest method setup.

@BeforeAll
static void setup() {
    EntityDictionary entityDictionary = EntityDictionary.builder().build();
    entityDictionary.bindEntity(Book.class);
    entityDictionary.bindEntity(Author.class);
    strategy = new DefaultFilterDialect(entityDictionary);
}
Also used : DefaultFilterDialect(com.yahoo.elide.core.filter.dialect.jsonapi.DefaultFilterDialect) EntityDictionary(com.yahoo.elide.core.dictionary.EntityDictionary) BeforeAll(org.junit.jupiter.api.BeforeAll)

Example 43 with EntityDictionary

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

the class DefaultFilterDialectTest method init.

@BeforeAll
public static void init() {
    EntityDictionary dictionary = EntityDictionary.builder().build();
    dictionary.bindEntity(Author.class);
    dictionary.bindEntity(Book.class);
    dialect = new DefaultFilterDialect(dictionary);
}
Also used : DefaultFilterDialect(com.yahoo.elide.core.filter.dialect.jsonapi.DefaultFilterDialect) EntityDictionary(com.yahoo.elide.core.dictionary.EntityDictionary) BeforeAll(org.junit.jupiter.api.BeforeAll)

Example 44 with EntityDictionary

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

the class ElideAutoConfiguration method buildDictionary.

/**
 * Creates the entity dictionary for Elide which contains static metadata about Elide models.
 * Override to load check classes or life cycle hooks.
 * @param beanFactory Injector to inject Elide models.
 * @param dynamicConfig An instance of DynamicConfiguration.
 * @param settings Elide configuration settings.
 * @param entitiesToExclude set of Entities to exclude from binding.
 * @return a newly configured EntityDictionary.
 */
@Bean
@ConditionalOnMissingBean
@Scope(SCOPE_PROTOTYPE)
public EntityDictionary buildDictionary(AutowireCapableBeanFactory beanFactory, ClassScanner scanner, @Autowired(required = false) DynamicConfiguration dynamicConfig, ElideConfigProperties settings, @Qualifier("entitiesToExclude") Set<Type<?>> entitiesToExclude) {
    Map<String, Class<? extends Check>> checks = new HashMap<>();
    if (settings.getDynamicConfig().isConfigApiEnabled()) {
        checks.put(ConfigChecks.CAN_CREATE_CONFIG, ConfigChecks.CanNotCreate.class);
        checks.put(ConfigChecks.CAN_READ_CONFIG, ConfigChecks.CanNotRead.class);
        checks.put(ConfigChecks.CAN_DELETE_CONFIG, ConfigChecks.CanNotDelete.class);
        checks.put(ConfigChecks.CAN_UPDATE_CONFIG, ConfigChecks.CanNotUpdate.class);
    }
    EntityDictionary dictionary = new EntityDictionary(// Checks
    checks, // Role Checks
    new HashMap<>(), new Injector() {

        @Override
        public void inject(Object entity) {
            beanFactory.autowireBean(entity);
        }

        @Override
        public <T> T instantiate(Class<T> cls) {
            return beanFactory.createBean(cls);
        }
    }, // Serde Lookup
    CoerceUtil::lookup, entitiesToExclude, scanner);
    if (isAggregationStoreEnabled(settings) && isDynamicConfigEnabled(settings)) {
        dynamicConfig.getRoles().forEach(role -> {
            dictionary.addRoleCheck(role, new Role.RoleMemberCheck(role));
        });
    }
    return dictionary;
}
Also used : HashMap(java.util.HashMap) Check(com.yahoo.elide.core.security.checks.Check) Role(com.yahoo.elide.core.security.checks.prefab.Role) CoerceUtil(com.yahoo.elide.core.utils.coerce.CoerceUtil) Injector(com.yahoo.elide.core.dictionary.Injector) ConfigChecks(com.yahoo.elide.modelconfig.store.models.ConfigChecks) EntityDictionary(com.yahoo.elide.core.dictionary.EntityDictionary) RefreshScope(org.springframework.cloud.context.config.annotation.RefreshScope) Scope(org.springframework.context.annotation.Scope) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) Bean(org.springframework.context.annotation.Bean)

Example 45 with EntityDictionary

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

the class DataStoreTransaction method loadObject.

/**
 * Loads an object by ID.  The reason we support both load by ID and load by filter is that
 * some legacy stores are optimized to load by ID.
 *
 * @param entityProjection the collection to load.
 * @param id - the ID of the object to load.
 * @param scope - the current request scope
 * @param <T> The model type being loaded.
 * It is optional for the data store to attempt evaluation.
 * @return the loaded object if it exists AND any provided security filters pass.
 */
default <T> T loadObject(EntityProjection entityProjection, Serializable id, RequestScope scope) {
    Type<?> entityClass = entityProjection.getType();
    FilterExpression filterExpression = entityProjection.getFilterExpression();
    EntityDictionary dictionary = scope.getDictionary();
    Type idType = dictionary.getIdType(entityClass);
    String idField = dictionary.getIdFieldName(entityClass);
    FilterExpression idFilter = new InPredicate(new Path.PathElement(entityClass, idType, idField), id);
    FilterExpression joinedFilterExpression = (filterExpression != null) ? new AndFilterExpression(idFilter, filterExpression) : idFilter;
    Iterable<T> results = loadObjects(entityProjection.copyOf().filterExpression(joinedFilterExpression).build(), scope);
    Iterator<T> it = results == null ? null : results.iterator();
    if (it != null && it.hasNext()) {
        T obj = it.next();
        if (!it.hasNext()) {
            return obj;
        }
        // Multiple objects with the same ID.
        throw new InvalidObjectIdentifierException(id.toString(), dictionary.getJsonAliasFor(entityClass));
    }
    return null;
}
Also used : Path(com.yahoo.elide.core.Path) InPredicate(com.yahoo.elide.core.filter.predicates.InPredicate) Type(com.yahoo.elide.core.type.Type) InvalidObjectIdentifierException(com.yahoo.elide.core.exceptions.InvalidObjectIdentifierException) AndFilterExpression(com.yahoo.elide.core.filter.expression.AndFilterExpression) FilterExpression(com.yahoo.elide.core.filter.expression.FilterExpression) EntityDictionary(com.yahoo.elide.core.dictionary.EntityDictionary) AndFilterExpression(com.yahoo.elide.core.filter.expression.AndFilterExpression)

Aggregations

EntityDictionary (com.yahoo.elide.core.dictionary.EntityDictionary)87 Test (org.junit.jupiter.api.Test)31 RequestScope (com.yahoo.elide.core.RequestScope)27 Include (com.yahoo.elide.annotation.Include)17 Entity (javax.persistence.Entity)17 HashSet (java.util.HashSet)16 Type (com.yahoo.elide.core.type.Type)13 FilterExpression (com.yahoo.elide.core.filter.expression.FilterExpression)12 DataStoreTransaction (com.yahoo.elide.core.datastore.DataStoreTransaction)10 Map (java.util.Map)10 BeforeAll (org.junit.jupiter.api.BeforeAll)10 PersistentResource (com.yahoo.elide.core.PersistentResource)9 Set (java.util.Set)9 ReadPermission (com.yahoo.elide.annotation.ReadPermission)8 ClassType (com.yahoo.elide.core.type.ClassType)8 List (java.util.List)8 ElideSettingsBuilder (com.yahoo.elide.ElideSettingsBuilder)7 DataStore (com.yahoo.elide.core.datastore.DataStore)7 InvalidObjectIdentifierException (com.yahoo.elide.core.exceptions.InvalidObjectIdentifierException)7 Check (com.yahoo.elide.core.security.checks.Check)7