Search in sources :

Example 6 with ChangeSpec

use of com.yahoo.elide.core.security.ChangeSpec in project elide by yahoo.

the class PersistentResource method createObject.

/**
 * Create a resource in the database.
 *
 * @param parent             - The immediate ancestor in the lineage or null if this is a root.
 * @param parentRelationship - The name of the parent relationship traversed to create this object.
 * @param entityClass        the entity class
 * @param requestScope       the request scope
 * @param uuid               the (optional) uuid
 * @param <T>                object type
 * @return persistent resource
 */
public static <T> PersistentResource<T> createObject(PersistentResource<?> parent, String parentRelationship, Type<T> entityClass, RequestScope requestScope, Optional<String> uuid) {
    T obj = requestScope.getTransaction().createNewObject(entityClass, requestScope);
    String id = uuid.orElse(null);
    PersistentResource<T> newResource = new PersistentResource<>(obj, parent, parentRelationship, id, requestScope);
    // The ID must be assigned before we add it to the new resources set.  Persistent resource
    // hashcode and equals are only based on the ID/UUID & type.
    assignId(newResource, id);
    // Keep track of new resources for non-transferable resources
    requestScope.getNewPersistentResources().add(newResource);
    checkPermission(CreatePermission.class, newResource);
    newResource.auditClass(Audit.Action.CREATE, new ChangeSpec(newResource, null, null, newResource.getObject()));
    requestScope.publishLifecycleEvent(newResource, CREATE);
    requestScope.setUUIDForObject(newResource.type, id, newResource.getObject());
    // Initialize null ToMany collections
    requestScope.getDictionary().getRelationships(entityClass).stream().filter(relationName -> newResource.getRelationshipType(relationName).isToMany() && newResource.getValueUnchecked(relationName) == null).forEach(relationName -> newResource.setValue(relationName, new LinkedHashSet<>()));
    newResource.markDirty();
    return newResource;
}
Also used : Resource(com.yahoo.elide.jsonapi.models.Resource) Data(com.yahoo.elide.jsonapi.models.Data) StringUtils(org.apache.commons.lang3.StringUtils) UpdatePermission(com.yahoo.elide.annotation.UpdatePermission) ClassType(com.yahoo.elide.core.type.ClassType) DeletePermission(com.yahoo.elide.annotation.DeletePermission) Argument(com.yahoo.elide.core.request.Argument) InvalidSyntaxException(com.yahoo.elide.core.audit.InvalidSyntaxException) Map(java.util.Map) DataStoreIterable(com.yahoo.elide.core.datastore.DataStoreIterable) LifeCycleHookBinding(com.yahoo.elide.annotation.LifeCycleHookBinding) EntityBinding(com.yahoo.elide.core.dictionary.EntityBinding) NonNull(lombok.NonNull) Collection(java.util.Collection) Set(java.util.Set) CoerceUtil(com.yahoo.elide.core.utils.coerce.CoerceUtil) Collectors(java.util.stream.Collectors) EntityDictionary(com.yahoo.elide.core.dictionary.EntityDictionary) Sets(com.google.common.collect.Sets) Serializable(java.io.Serializable) Objects(java.util.Objects) ExpressionResult(com.yahoo.elide.core.security.permissions.ExpressionResult) List(java.util.List) Annotation(java.lang.annotation.Annotation) AndFilterExpression(com.yahoo.elide.core.filter.expression.AndFilterExpression) Optional(java.util.Optional) RelationshipType(com.yahoo.elide.core.dictionary.RelationshipType) Attribute(com.yahoo.elide.core.request.Attribute) InvalidAttributeException(com.yahoo.elide.core.exceptions.InvalidAttributeException) Function(java.util.function.Function) Supplier(java.util.function.Supplier) CollectionUtils(org.apache.commons.collections4.CollectionUtils) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) LinkedHashMap(java.util.LinkedHashMap) IterableUtils(org.apache.commons.collections4.IterableUtils) LogMessageImpl(com.yahoo.elide.core.audit.LogMessageImpl) DELETE(com.yahoo.elide.annotation.LifeCycleHookBinding.Operation.DELETE) EntityDictionary.getType(com.yahoo.elide.core.dictionary.EntityDictionary.getType) JsonIgnore(com.fasterxml.jackson.annotation.JsonIgnore) Predicates(com.google.common.base.Predicates) InternalServerErrorException(com.yahoo.elide.core.exceptions.InternalServerErrorException) CanPaginateVisitor(com.yahoo.elide.core.security.visitors.CanPaginateVisitor) Observable(io.reactivex.Observable) LogMessage(com.yahoo.elide.core.audit.LogMessage) FilterExpression(com.yahoo.elide.core.filter.expression.FilterExpression) LinkedHashSet(java.util.LinkedHashSet) UPDATE(com.yahoo.elide.annotation.LifeCycleHookBinding.Operation.UPDATE) DataStoreTransaction(com.yahoo.elide.core.datastore.DataStoreTransaction) VerifyFieldAccessFilterExpressionVisitor(com.yahoo.elide.core.filter.visitors.VerifyFieldAccessFilterExpressionVisitor) ChangeSpec(com.yahoo.elide.core.security.ChangeSpec) COLLECTION_TYPE(com.yahoo.elide.core.type.ClassType.COLLECTION_TYPE) Sorting(com.yahoo.elide.core.request.Sorting) InvalidEntityBodyException(com.yahoo.elide.core.exceptions.InvalidEntityBodyException) InvalidObjectIdentifierException(com.yahoo.elide.core.exceptions.InvalidObjectIdentifierException) InPredicate(com.yahoo.elide.core.filter.predicates.InPredicate) EntityProjection(com.yahoo.elide.core.request.EntityProjection) Relationship(com.yahoo.elide.jsonapi.models.Relationship) ForbiddenAccessException(com.yahoo.elide.core.exceptions.ForbiddenAccessException) ReadPermission(com.yahoo.elide.annotation.ReadPermission) BadRequestException(com.yahoo.elide.core.exceptions.BadRequestException) Pagination(com.yahoo.elide.core.request.Pagination) ResourceIdentifier(com.yahoo.elide.jsonapi.models.ResourceIdentifier) TreeMap(java.util.TreeMap) CreatePermission(com.yahoo.elide.annotation.CreatePermission) CREATE(com.yahoo.elide.annotation.LifeCycleHookBinding.Operation.CREATE) Type(com.yahoo.elide.core.type.Type) Preconditions(com.google.common.base.Preconditions) Comparator(java.util.Comparator) Collections(java.util.Collections) EMPTY_BINDING(com.yahoo.elide.core.dictionary.EntityBinding.EMPTY_BINDING) Audit(com.yahoo.elide.annotation.Audit) NonTransferable(com.yahoo.elide.annotation.NonTransferable) LinkedHashSet(java.util.LinkedHashSet) ChangeSpec(com.yahoo.elide.core.security.ChangeSpec)

Example 7 with ChangeSpec

use of com.yahoo.elide.core.security.ChangeSpec in project elide by yahoo.

the class PersistentResource method triggerUpdate.

/**
 * Queue the @*Update triggers iff this is not a newly created object (otherwise we run @*Create)
 */
private void triggerUpdate(String fieldName, Object original, Object value) {
    ChangeSpec changeSpec = new ChangeSpec(this, fieldName, original, value);
    LifeCycleHookBinding.Operation action = isNewlyCreated() ? CREATE : UPDATE;
    requestScope.publishLifecycleEvent(this, fieldName, action, Optional.of(changeSpec));
    requestScope.publishLifecycleEvent(this, action);
    auditField(new ChangeSpec(this, fieldName, original, value));
}
Also used : ChangeSpec(com.yahoo.elide.core.security.ChangeSpec) LifeCycleHookBinding(com.yahoo.elide.annotation.LifeCycleHookBinding)

Aggregations

ChangeSpec (com.yahoo.elide.core.security.ChangeSpec)7 Test (org.junit.jupiter.api.Test)4 LogMessage (com.yahoo.elide.core.audit.LogMessage)3 LifeCycleHookBinding (com.yahoo.elide.annotation.LifeCycleHookBinding)2 UpdatePermission (com.yahoo.elide.annotation.UpdatePermission)2 PersistentResource (com.yahoo.elide.core.PersistentResource)2 TestAuditLogger (com.yahoo.elide.core.audit.TestAuditLogger)2 DataStoreTransaction (com.yahoo.elide.core.datastore.DataStoreTransaction)2 PatchRequestScope (com.yahoo.elide.jsonapi.extensions.PatchRequestScope)2 Child (example.Child)2 Parent (example.Parent)2 JsonIgnore (com.fasterxml.jackson.annotation.JsonIgnore)1 Preconditions (com.google.common.base.Preconditions)1 Predicates (com.google.common.base.Predicates)1 Sets (com.google.common.collect.Sets)1 Audit (com.yahoo.elide.annotation.Audit)1 CreatePermission (com.yahoo.elide.annotation.CreatePermission)1 DeletePermission (com.yahoo.elide.annotation.DeletePermission)1 Include (com.yahoo.elide.annotation.Include)1 CREATE (com.yahoo.elide.annotation.LifeCycleHookBinding.Operation.CREATE)1