use of com.yahoo.elide.core.request.Relationship in project elide by yahoo.
the class SubCollectionPageTotalsQueryBuilderTest method testSubCollectionPageTotals.
@Test
public void testSubCollectionPageTotals() {
Author author = new Author();
author.setId(1L);
Book book = new Book();
book.setId(2);
EntityProjection entityProjection = EntityProjection.builder().type(Book.class).build();
Relationship relationshipProjection = Relationship.builder().projection(entityProjection).name(BOOKS).build();
RelationshipImpl relationship = new RelationshipImpl(ClassType.of(Author.class), author, relationshipProjection);
SubCollectionPageTotalsQueryBuilder builder = new SubCollectionPageTotalsQueryBuilder(relationship, dictionary, new TestSessionWrapper());
TestQueryWrapper query = (TestQueryWrapper) builder.build();
String actual = query.getQueryText();
actual = actual.trim().replaceAll(" +", " ");
actual = actual.replaceFirst(":id_\\w+", ":id_XXX");
String expected = "SELECT COUNT(DISTINCT example_Author_books) " + "FROM example.Author AS example_Author " + "JOIN example_Author.books example_Author_books " + "WHERE example_Author.id IN (:id_XXX)";
assertEquals(expected, actual);
}
use of com.yahoo.elide.core.request.Relationship in project elide by yahoo.
the class NodeContainer method processFetch.
@Override
public Object processFetch(Environment context) {
EntityDictionary entityDictionary = context.requestScope.getDictionary();
NonEntityDictionary nonEntityDictionary = context.nonEntityDictionary;
Type parentClass = context.parentResource.getResourceType();
String fieldName = context.field.getName();
String idFieldName = entityDictionary.getIdFieldName(parentClass);
if (entityDictionary.isAttribute(parentClass, fieldName)) {
/* fetch attribute properties */
Attribute requested = context.requestScope.getProjectionInfo().getAttributeMap().getOrDefault(context.field.getSourceLocation(), null);
Object attribute = context.parentResource.getAttribute(requested);
if (attribute != null && nonEntityDictionary.hasBinding(EntityDictionary.getType(attribute))) {
return new NonEntityContainer(attribute);
}
if (attribute instanceof Map) {
return ((Map<Object, Object>) attribute).entrySet().stream().map(MapEntryContainer::new).collect(Collectors.toList());
}
if (attribute instanceof Collection) {
Type<?> innerType = entityDictionary.getParameterizedType(parentClass, fieldName);
if (nonEntityDictionary.hasBinding(innerType)) {
return ((Collection) attribute).stream().map(NonEntityContainer::new).collect(Collectors.toList());
}
}
return attribute;
}
if (entityDictionary.isRelation(parentClass, fieldName)) {
/* fetch relationship properties */
// get the relationship from constructed projections
Relationship relationship = context.requestScope.getProjectionInfo().getRelationshipMap().getOrDefault(context.field.getSourceLocation(), null);
if (relationship == null) {
throw new BadRequestException("Relationship doesn't have projection " + context.parentResource.getTypeName() + "." + fieldName);
}
return fetchRelationship(context, relationship);
}
if (Objects.equals(idFieldName, fieldName)) {
return new DeferredId(context.parentResource);
}
throw new BadRequestException("Unrecognized object: " + fieldName + " for: " + parentClass.getName() + " in node");
}
Aggregations