Search in sources :

Example 81 with EntityDictionary

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

the class PersistentResourceFetcher method upsertOrUpdateObjects.

/**
 * handle UPSERT or UPDATE operation.
 * @param context Environment encapsulating graphQL's request environment
 * @param updateFunc controls the behavior of how the update (or upsert) is performed.
 * @return Connection object.
 */
private ConnectionContainer upsertOrUpdateObjects(Environment context, Executor<?> updateFunc, RelationshipOp operation) {
    /* sanity check for id and data argument w UPSERT/UPDATE */
    if (context.ids.isPresent()) {
        throw new BadRequestException(operation + " must not include ids");
    }
    if (!context.data.isPresent()) {
        throw new BadRequestException(operation + " must include data argument");
    }
    Type<?> entityClass;
    EntityDictionary dictionary = context.requestScope.getDictionary();
    if (context.isRoot()) {
        entityClass = dictionary.getEntityClass(context.field.getName(), context.requestScope.getApiVersion());
    } else {
        assert context.parentResource != null;
        entityClass = dictionary.getParameterizedType(context.parentResource.getResourceType(), context.field.getName());
    }
    /* form entities */
    Optional<Entity> parentEntity;
    if (!context.isRoot()) {
        assert context.parentResource != null;
        parentEntity = Optional.of(new Entity(Optional.empty(), null, context.parentResource.getResourceType(), context.requestScope));
    } else {
        parentEntity = Optional.empty();
    }
    LinkedHashSet<Entity> entitySet = new LinkedHashSet<>();
    for (Map<String, Object> input : context.data.orElseThrow(IllegalStateException::new)) {
        entitySet.add(new Entity(parentEntity, input, entityClass, context.requestScope));
    }
    /* apply function to upsert/update the object */
    for (Entity entity : entitySet) {
        graphWalker(entity, updateFunc, context);
    }
    /* fixup relationships */
    for (Entity entity : entitySet) {
        graphWalker(entity, this::updateRelationship, context);
        PersistentResource<?> childResource = entity.toPersistentResource();
        if (!context.isRoot()) {
            /* add relation between parent and nested entity */
            assert context.parentResource != null;
            context.parentResource.addRelation(context.field.getName(), childResource);
        }
    }
    String entityName = dictionary.getJsonAliasFor(entityClass);
    Set<PersistentResource> resources = entitySet.stream().map(Entity::toPersistentResource).collect(Collectors.toCollection(LinkedHashSet::new));
    return new ConnectionContainer(resources, Optional.empty(), entityName);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) PersistentResource(com.yahoo.elide.core.PersistentResource) ConnectionContainer(com.yahoo.elide.graphql.containers.ConnectionContainer) BadRequestException(com.yahoo.elide.core.exceptions.BadRequestException) EntityDictionary(com.yahoo.elide.core.dictionary.EntityDictionary)

Example 82 with EntityDictionary

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

the class PersistentResourceFetcher method updateAttributes.

/**
 * Updates an object.
 * @param toUpdate Entities to update
 * @param entity Resource entity
 * @param attributes Set of entity attributes
 * @return Persistence Resource object
 */
private PersistentResource<?> updateAttributes(PersistentResource<?> toUpdate, Entity entity, Set<Entity.Attribute> attributes) {
    EntityDictionary dictionary = entity.getRequestScope().getDictionary();
    Type<?> entityClass = entity.getEntityClass();
    String idFieldName = dictionary.getIdFieldName(entityClass);
    /* iterate through each attribute provided */
    for (Entity.Attribute attribute : attributes) {
        if (dictionary.isAttribute(entityClass, attribute.getName())) {
            Type<?> attributeType = dictionary.getType(entityClass, attribute.getName());
            Object attributeValue;
            if (ClassType.MAP_TYPE.isAssignableFrom(attributeType)) {
                attributeValue = MapEntryContainer.translateFromGraphQLMap(attribute);
            } else {
                attributeValue = attribute.getValue();
            }
            toUpdate.updateAttribute(attribute.getName(), attributeValue);
        } else if (!Objects.equals(attribute.getName(), idFieldName)) {
            throw new IllegalStateException("Unrecognized attribute passed to 'data': " + attribute.getName());
        }
    }
    return toUpdate;
}
Also used : EntityDictionary(com.yahoo.elide.core.dictionary.EntityDictionary)

Example 83 with EntityDictionary

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

the class MultiplexManagerTest method checkLoading.

@Test
public void checkLoading() {
    EntityDictionary entityDictionary = multiplexManager.getDictionary();
    assertNotNull(entityDictionary.getJsonAliasFor(ClassType.of(FirstBean.class)));
    assertNotNull(entityDictionary.getJsonAliasFor(ClassType.of(OtherBean.class)));
    assertNotNull(entityDictionary.getJsonAliasFor(ClassType.of(ComplexAttribute.class)));
}
Also used : EntityDictionary(com.yahoo.elide.core.dictionary.EntityDictionary) Test(org.junit.jupiter.api.Test)

Example 84 with EntityDictionary

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

the class Entity method setAttributes.

/**
 * Extract the attributes of the entity.
 */
private void setAttributes() {
    if (this.data != null) {
        this.attributes = new LinkedHashSet<>();
        EntityDictionary dictionary = this.requestScope.getDictionary();
        String idFieldName = dictionary.getIdFieldName(this.entityClass);
        for (Map.Entry<String, Object> entry : this.data.entrySet()) {
            if (dictionary.isAttribute(this.entityClass, entry.getKey())) {
                this.attributes.add(new Attribute(entry.getKey(), entry.getValue()));
            }
            if (Objects.equals(entry.getKey(), idFieldName)) {
                this.attributes.add(new Attribute(entry.getKey(), entry.getValue()));
            }
        }
    }
}
Also used : EntityDictionary(com.yahoo.elide.core.dictionary.EntityDictionary) Map(java.util.Map)

Example 85 with EntityDictionary

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

the class Entity method getId.

/**
 * Get the id of the entity.
 * @return the optional id
 */
public Optional<String> getId() {
    EntityDictionary dictionary = this.requestScope.getDictionary();
    String idFieldName = dictionary.getIdFieldName(this.entityClass);
    return this.attributes.stream().filter(entry -> idFieldName.equalsIgnoreCase(entry.name)).map(e -> (String) e.value).findFirst();
}
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) 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