Search in sources :

Example 16 with EntityDictionary

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

the class Entity method setId.

/**
 * Set the id of the entity if it doesn't have one already.
 */
public void setId() {
    if (!getId().isPresent()) {
        EntityDictionary dictionary = this.requestScope.getDictionary();
        String idFieldName = dictionary.getIdFieldName(this.entityClass);
        String uuid = UUID.randomUUID().toString();
        this.attributes.add(new Attribute(idFieldName, uuid));
    }
}
Also used : EntityDictionary(com.yahoo.elide.core.dictionary.EntityDictionary)

Example 17 with EntityDictionary

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

the class MultiplexManager method populateEntityDictionary.

@Override
public void populateEntityDictionary(EntityDictionary dictionary) {
    this.dictionary = dictionary;
    for (DataStore dataStore : dataStores) {
        EntityDictionary subordinateDictionary = new EntityDictionary(dictionary.getCheckMappings(), dictionary.getRoleChecks(), dictionary.getInjector(), dictionary.getSerdeLookup(), dictionary.getEntitiesToExclude(), dictionary.getScanner());
        dataStore.populateEntityDictionary(subordinateDictionary);
        for (EntityBinding binding : subordinateDictionary.getBindings(false)) {
            // route class to this database manager
            this.dataStoreMap.put(binding.entityClass, dataStore);
            // bind to multiplex dictionary
            dictionary.bindEntity(binding);
        }
        for (Map.Entry<Type<?>, Function<RequestScope, PermissionExecutor>> entry : subordinateDictionary.getEntityPermissionExecutor().entrySet()) {
            dictionary.bindPermissionExecutor(entry.getKey(), entry.getValue());
        }
    }
}
Also used : Function(java.util.function.Function) Type(com.yahoo.elide.core.type.Type) DataStore(com.yahoo.elide.core.datastore.DataStore) EntityBinding(com.yahoo.elide.core.dictionary.EntityBinding) EntityDictionary(com.yahoo.elide.core.dictionary.EntityDictionary) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Map(java.util.Map)

Example 18 with EntityDictionary

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

the class MultiplexTransaction method getRelationTransaction.

protected DataStoreTransaction getRelationTransaction(Object object, String relationName) {
    EntityDictionary dictionary = multiplexManager.getDictionary();
    Type<?> relationClass = dictionary.getParameterizedType(EntityDictionary.getType(object), relationName);
    return getTransaction(relationClass);
}
Also used : EntityDictionary(com.yahoo.elide.core.dictionary.EntityDictionary)

Example 19 with EntityDictionary

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

the class PersistentResourceFetcher method fetchObject.

/**
 * Fetches a root-level entity.
 * @param requestScope Request scope
 * @param projection constructed entityProjection for a class
 * @param ids List of ids (can be NULL)
 * @return {@link PersistentResource} object(s)
 */
public static ConnectionContainer fetchObject(RequestScope requestScope, EntityProjection projection, Optional<List<String>> ids) {
    EntityDictionary dictionary = requestScope.getDictionary();
    String typeName = dictionary.getJsonAliasFor(projection.getType());
    /* fetching a collection */
    Observable<PersistentResource> records = ids.map((idList) -> {
        /* handle empty list of ids */
        if (idList.isEmpty()) {
            throw new BadRequestException("Empty list passed to ids");
        }
        return PersistentResource.loadRecords(projection, idList, requestScope);
    }).orElseGet(() -> PersistentResource.loadRecords(projection, new ArrayList<>(), requestScope));
    return new ConnectionContainer(records.toList(LinkedHashSet::new).blockingGet(), Optional.ofNullable(projection.getPagination()), typeName);
}
Also used : MapEntryContainer(com.yahoo.elide.graphql.containers.MapEntryContainer) OperationDefinition(graphql.language.OperationDefinition) DataFetchingEnvironment(graphql.schema.DataFetchingEnvironment) ConnectionContainer(com.yahoo.elide.graphql.containers.ConnectionContainer) ClassType(com.yahoo.elide.core.type.ClassType) ArrayList(java.util.ArrayList) Map(java.util.Map) PersistentResource(com.yahoo.elide.core.PersistentResource) DataFetcher(graphql.schema.DataFetcher) Relationship(com.yahoo.elide.core.request.Relationship) Observable(io.reactivex.Observable) LinkedHashSet(java.util.LinkedHashSet) RequestScope(com.yahoo.elide.core.RequestScope) ARGUMENT_OPERATION(com.yahoo.elide.graphql.ModelBuilder.ARGUMENT_OPERATION) FETCH(com.yahoo.elide.graphql.RelationshipOp.FETCH) InvalidObjectIdentifierException(com.yahoo.elide.core.exceptions.InvalidObjectIdentifierException) Set(java.util.Set) EntityProjection(com.yahoo.elide.core.request.EntityProjection) NotNull(javax.validation.constraints.NotNull) Collectors(java.util.stream.Collectors) EntityDictionary(com.yahoo.elide.core.dictionary.EntityDictionary) Sets(com.google.common.collect.Sets) Objects(java.util.Objects) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) BadRequestException(com.yahoo.elide.core.exceptions.BadRequestException) InvalidValueException(com.yahoo.elide.core.exceptions.InvalidValueException) Type(com.yahoo.elide.core.type.Type) Optional(java.util.Optional) GraphQLContainer(com.yahoo.elide.graphql.containers.GraphQLContainer) Queue(java.util.Queue) ArrayDeque(java.util.ArrayDeque) Collections(java.util.Collections) LinkedHashSet(java.util.LinkedHashSet) PersistentResource(com.yahoo.elide.core.PersistentResource) ConnectionContainer(com.yahoo.elide.graphql.containers.ConnectionContainer) ArrayList(java.util.ArrayList) BadRequestException(com.yahoo.elide.core.exceptions.BadRequestException) EntityDictionary(com.yahoo.elide.core.dictionary.EntityDictionary)

Example 20 with EntityDictionary

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

the class PersistentResourceFetcher method upsertObject.

/**
 * updates or creates existing/new entities.
 * @param entity Resource entity
 * @param context The request context
 * @return {@link PersistentResource} object
 */
private PersistentResource upsertObject(Entity entity, Environment context) {
    Set<Entity.Attribute> attributes = entity.getAttributes();
    Optional<String> id = entity.getId();
    RequestScope requestScope = entity.getRequestScope();
    PersistentResource upsertedResource;
    EntityDictionary dictionary = requestScope.getDictionary();
    PersistentResource parentResource = entity.getParentResource().map(Entity::toPersistentResource).orElse(null);
    if (!id.isPresent()) {
        // If the ID is generated, it is safe to assign a temporary UUID.  Otherwise the client must provide one.
        if (dictionary.isIdGenerated(entity.getEntityClass())) {
            // Assign a temporary UUID.
            entity.setId();
            id = entity.getId();
        }
        upsertedResource = PersistentResource.createObject(parentResource, context.field.getName(), entity.getEntityClass(), requestScope, id);
    } else {
        try {
            Set<PersistentResource> loadedResource = fetchObject(requestScope, entity.getProjection(), Optional.of(Collections.singletonList(id.get()))).getPersistentResources();
            upsertedResource = loadedResource.iterator().next();
        // The ID doesn't exist yet.  Let's create the object.
        } catch (InvalidObjectIdentifierException | InvalidValueException e) {
            upsertedResource = PersistentResource.createObject(parentResource, context.field.getName(), entity.getEntityClass(), requestScope, id);
        }
    }
    return updateAttributes(upsertedResource, entity, attributes);
}
Also used : InvalidValueException(com.yahoo.elide.core.exceptions.InvalidValueException) PersistentResource(com.yahoo.elide.core.PersistentResource) InvalidObjectIdentifierException(com.yahoo.elide.core.exceptions.InvalidObjectIdentifierException) 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