use of com.yahoo.elide.annotation.LifeCycleHookBinding.Operation.CREATE 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;
}
Aggregations