Search in sources :

Example 11 with EntityDictionary

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

the class RecordState method handle.

@Override
public void handle(StateContext state, SubCollectionReadCollectionContext ctx) {
    String subCollection = ctx.term().getText();
    EntityDictionary dictionary = state.getRequestScope().getDictionary();
    Type<?> entityClass;
    String entityName;
    RelationshipType type = dictionary.getRelationshipType(resource.getObject(), subCollection);
    Type<?> paramType = dictionary.getParameterizedType(resource.getObject(), subCollection);
    entityName = dictionary.getJsonAliasFor(paramType);
    entityClass = dictionary.getEntityClass(entityName, state.getRequestScope().getApiVersion());
    if (entityClass == null) {
        throw new IllegalArgumentException("Unknown type " + entityName);
    }
    final BaseState nextState;
    final CollectionTerminalState collectionTerminalState = new CollectionTerminalState(entityClass, Optional.of(resource), Optional.of(subCollection), projection);
    Observable<PersistentResource> collection = null;
    if (type.isToOne()) {
        collection = resource.getRelationCheckedFiltered(projection.getRelationship(subCollection).orElseThrow(IllegalStateException::new));
        PersistentResource record = PersistentResource.firstOrNullIfEmpty(collection);
        nextState = new RecordTerminalState(record, collectionTerminalState);
    } else {
        nextState = collectionTerminalState;
    }
    state.setState(nextState);
}
Also used : PersistentResource(com.yahoo.elide.core.PersistentResource) RelationshipType(com.yahoo.elide.core.dictionary.RelationshipType) EntityDictionary(com.yahoo.elide.core.dictionary.EntityDictionary)

Example 12 with EntityDictionary

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

the class StartState method entityRecord.

private PersistentResource<?> entityRecord(StateContext state, EntityContext entity) {
    String id = entity.id().getText();
    String entityName = entity.term().getText();
    EntityDictionary dictionary = state.getRequestScope().getDictionary();
    Type<?> entityClass = dictionary.getEntityClass(entityName, state.getRequestScope().getApiVersion());
    if (entityClass == null || !dictionary.isRoot(entityClass)) {
        throw new InvalidCollectionException(entityName);
    }
    return PersistentResource.loadRecord(state.getRequestScope().getEntityProjection(), id, state.getRequestScope());
}
Also used : InvalidCollectionException(com.yahoo.elide.core.exceptions.InvalidCollectionException) EntityDictionary(com.yahoo.elide.core.dictionary.EntityDictionary)

Example 13 with EntityDictionary

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

the class FilteredIteratorTest method testFilteredResult.

@Test
public void testFilteredResult() throws Exception {
    EntityDictionary dictionary = EntityDictionary.builder().build();
    dictionary.bindEntity(Book.class);
    Book book1 = new Book();
    book1.setTitle("foo");
    Book book2 = new Book();
    book2.setTitle("bar");
    Book book3 = new Book();
    book3.setTitle("foobar");
    List<Book> books = List.of(book1, book2, book3);
    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());
    assertTrue(bookIterator.hasNext());
    assertEquals("bar", bookIterator.next().getTitle());
    assertTrue(bookIterator.hasNext());
    assertEquals("foobar", bookIterator.next().getTitle());
    assertFalse(bookIterator.hasNext());
}
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 14 with EntityDictionary

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

the class RSQLFilterDialectTest method init.

@BeforeAll
public static void init() {
    EntityDictionary dictionary = EntityDictionary.builder().build();
    dictionary.bindEntity(Author.class);
    dictionary.bindEntity(Book.class);
    dictionary.bindEntity(Publisher.class);
    dictionary.bindEntity(StringId.class);
    dictionary.bindEntity(Job.class);
    dictionary.bindEntity(PrimitiveId.class);
    dialect = RSQLFilterDialect.builder().dictionary(dictionary).build();
}
Also used : EntityDictionary(com.yahoo.elide.core.dictionary.EntityDictionary) BeforeAll(org.junit.jupiter.api.BeforeAll)

Example 15 with EntityDictionary

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

the class Entity method setRelationships.

/**
 * Extract the relationships of the entity.
 */
private void setRelationships() {
    if (this.data != null) {
        this.relationships = new LinkedHashSet<>();
        EntityDictionary dictionary = this.requestScope.getDictionary();
        this.data.entrySet().stream().filter(entry -> dictionary.isRelation(this.entityClass, entry.getKey())).forEach(entry -> {
            String relationshipName = entry.getKey();
            Type<?> relationshipClass = dictionary.getParameterizedType(this.entityClass, relationshipName);
            Set<Entity> relationshipEntities = new LinkedHashSet<>();
            // if the relationship is ToOne, entry.getValue() should be a single map
            if (dictionary.getRelationshipType(this.entityClass, relationshipName).isToOne()) {
                relationshipEntities.add(new Entity(Optional.of(this), ((Map<String, Object>) entry.getValue()), relationshipClass, this.requestScope));
            } else {
                for (Map<String, Object> row : (List<Map<String, Object>>) entry.getValue()) {
                    relationshipEntities.add(new Entity(Optional.of(this), row, relationshipClass, this.requestScope));
                }
            }
            this.relationships.add(new Relationship(relationshipName, relationshipEntities));
        });
    }
}
Also used : Getter(lombok.Getter) Iterator(java.util.Iterator) Set(java.util.Set) EntityProjection(com.yahoo.elide.core.request.EntityProjection) UUID(java.util.UUID) EntityDictionary(com.yahoo.elide.core.dictionary.EntityDictionary) Objects(java.util.Objects) List(java.util.List) Type(com.yahoo.elide.core.type.Type) Map(java.util.Map) PersistentResource(com.yahoo.elide.core.PersistentResource) Optional(java.util.Optional) AllArgsConstructor(lombok.AllArgsConstructor) LinkedHashSet(java.util.LinkedHashSet) RequestScope(com.yahoo.elide.core.RequestScope) LinkedHashSet(java.util.LinkedHashSet) List(java.util.List) EntityDictionary(com.yahoo.elide.core.dictionary.EntityDictionary)

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