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);
}
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());
}
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());
}
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();
}
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));
});
}
}
Aggregations