Search in sources :

Example 46 with PersistentResource

use of com.yahoo.elide.core.PersistentResource in project elide by yahoo.

the class PersistentResourceFetcher method replaceObjects.

/**
 * Replaces a resource, updates given resource and deletes the rest
 * belonging to the the same type/relationship family.
 * @param context Environment encapsulating graphQL's request environment
 * @return set of replaced {@link PersistentResource} object(s)
 */
private ConnectionContainer replaceObjects(Environment context) {
    /* sanity check for id and data argument w REPLACE */
    if (!context.data.isPresent()) {
        throw new BadRequestException("REPLACE must include data argument");
    }
    if (context.ids.isPresent()) {
        throw new BadRequestException("REPLACE must not include ids argument");
    }
    ConnectionContainer existingObjects = (ConnectionContainer) context.container.processFetch(context);
    ConnectionContainer upsertedObjects = upsertObjects(context);
    Set<PersistentResource> toDelete = Sets.difference(existingObjects.getPersistentResources(), upsertedObjects.getPersistentResources());
    if (!context.isRoot()) {
        /* has parent */
        toDelete.forEach(item -> context.parentResource.removeRelation(context.field.getName(), item));
    } else {
        /* is root */
        toDelete.forEach(PersistentResource::deleteResource);
    }
    return upsertedObjects;
}
Also used : PersistentResource(com.yahoo.elide.core.PersistentResource) ConnectionContainer(com.yahoo.elide.graphql.containers.ConnectionContainer) BadRequestException(com.yahoo.elide.core.exceptions.BadRequestException)

Example 47 with PersistentResource

use of com.yahoo.elide.core.PersistentResource in project elide by yahoo.

the class PersistentResourceFetcher method deleteObjects.

/**
 * Deletes a resource.
 * @param context Environment encapsulating graphQL's request environment
 * @return set of deleted {@link PersistentResource} object(s)
 */
private ConnectionContainer deleteObjects(Environment context) {
    /* sanity check for id and data argument w DELETE */
    if (context.data.isPresent()) {
        throw new BadRequestException("DELETE must not include data argument");
    }
    if (!context.ids.isPresent()) {
        throw new BadRequestException("DELETE must include ids argument");
    }
    ConnectionContainer connection = (ConnectionContainer) fetchObjects(context);
    Set<PersistentResource> toDelete = connection.getPersistentResources();
    toDelete.forEach(PersistentResource::deleteResource);
    return new ConnectionContainer(Collections.emptySet(), Optional.empty(), connection.getTypeName());
}
Also used : PersistentResource(com.yahoo.elide.core.PersistentResource) ConnectionContainer(com.yahoo.elide.graphql.containers.ConnectionContainer) BadRequestException(com.yahoo.elide.core.exceptions.BadRequestException)

Example 48 with PersistentResource

use of com.yahoo.elide.core.PersistentResource 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)

Example 49 with PersistentResource

use of com.yahoo.elide.core.PersistentResource in project elide by yahoo.

the class PersistentResourceFetcher method fetchRelationship.

/**
 * Fetches a relationship for a top-level entity.
 *
 * @param parentResource Parent object
 * @param relationship constructed relationship object with entityProjection
 * @param ids List of ids
 * @return persistence resource object(s)
 */
public static ConnectionContainer fetchRelationship(PersistentResource<?> parentResource, @NotNull Relationship relationship, Optional<List<String>> ids) {
    EntityDictionary dictionary = parentResource.getRequestScope().getDictionary();
    Type relationshipClass = dictionary.getParameterizedType(parentResource.getObject(), relationship.getName());
    String relationshipType = dictionary.getJsonAliasFor(relationshipClass);
    Set<PersistentResource> relationResources;
    if (ids.isPresent()) {
        relationResources = parentResource.getRelation(ids.get(), relationship).toList(LinkedHashSet::new).blockingGet();
    } else {
        relationResources = parentResource.getRelationCheckedFiltered(relationship).toList(LinkedHashSet::new).blockingGet();
    }
    return new ConnectionContainer(relationResources, Optional.ofNullable(relationship.getProjection().getPagination()), relationshipType);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) PersistentResource(com.yahoo.elide.core.PersistentResource) ClassType(com.yahoo.elide.core.type.ClassType) Type(com.yahoo.elide.core.type.Type) ConnectionContainer(com.yahoo.elide.graphql.containers.ConnectionContainer) EntityDictionary(com.yahoo.elide.core.dictionary.EntityDictionary)

Example 50 with PersistentResource

use of com.yahoo.elide.core.PersistentResource in project elide by yahoo.

the class CSVExportFormatterTest method testNullResourceToCSV.

@Test
public void testNullResourceToCSV() {
    CSVExportFormatter formatter = new CSVExportFormatter(elide, false);
    PersistentResource persistentResource = null;
    String output = formatter.format(persistentResource, 1);
    assertNull(output);
}
Also used : PersistentResource(com.yahoo.elide.core.PersistentResource) Test(org.junit.jupiter.api.Test)

Aggregations

PersistentResource (com.yahoo.elide.core.PersistentResource)100 Test (org.junit.jupiter.api.Test)71 RequestScope (com.yahoo.elide.core.RequestScope)60 ReadPermission (com.yahoo.elide.annotation.ReadPermission)18 UpdatePermission (com.yahoo.elide.annotation.UpdatePermission)18 DataStoreTransaction (com.yahoo.elide.core.datastore.DataStoreTransaction)17 Include (com.yahoo.elide.annotation.Include)16 Entity (javax.persistence.Entity)16 Resource (com.yahoo.elide.jsonapi.models.Resource)13 AndFilterExpression (com.yahoo.elide.core.filter.expression.AndFilterExpression)10 NotFilterExpression (com.yahoo.elide.core.filter.expression.NotFilterExpression)10 OrFilterExpression (com.yahoo.elide.core.filter.expression.OrFilterExpression)10 PermissionExecutor (com.yahoo.elide.core.security.PermissionExecutor)10 JsonApiDocument (com.yahoo.elide.jsonapi.models.JsonApiDocument)10 Book (example.Book)10 LinkedHashSet (java.util.LinkedHashSet)9 EntityDictionary (com.yahoo.elide.core.dictionary.EntityDictionary)8 BadRequestException (com.yahoo.elide.core.exceptions.BadRequestException)8 FilterExpression (com.yahoo.elide.core.filter.expression.FilterExpression)8 RSQLFilterDialect (com.yahoo.elide.core.filter.dialect.RSQLFilterDialect)7