Search in sources :

Example 86 with EntityDictionary

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

the class NoopDataStoreTest method testPopulateEntityDictionary.

@Test
public void testPopulateEntityDictionary() throws Exception {
    DataStore store = new NoopDataStore(Arrays.asList(NoopBean.class));
    EntityDictionary dictionary = EntityDictionary.builder().build();
    store.populateEntityDictionary(dictionary);
    assertEquals(ClassType.of(NoopBean.class), dictionary.getEntityClass("theNoopBean", EntityDictionary.NO_VERSION));
}
Also used : NoopBean(com.yahoo.elide.beans.NoopBean) DataStore(com.yahoo.elide.core.datastore.DataStore) EntityDictionary(com.yahoo.elide.core.dictionary.EntityDictionary) Test(org.junit.jupiter.api.Test)

Example 87 with EntityDictionary

use of com.yahoo.elide.core.dictionary.EntityDictionary 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");
}
Also used : Attribute(com.yahoo.elide.core.request.Attribute) NonEntityDictionary(com.yahoo.elide.graphql.NonEntityDictionary) Type(com.yahoo.elide.core.type.Type) Relationship(com.yahoo.elide.core.request.Relationship) DeferredId(com.yahoo.elide.graphql.DeferredId) Collection(java.util.Collection) BadRequestException(com.yahoo.elide.core.exceptions.BadRequestException) NonEntityDictionary(com.yahoo.elide.graphql.NonEntityDictionary) EntityDictionary(com.yahoo.elide.core.dictionary.EntityDictionary) Map(java.util.Map)

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