Пример #1
0
        /// <summary>
        /// Creates a new entity returning the map of old to new entity ids.
        /// </summary>
        /// <param name="entityData">The entity data.</param>
        /// <returns>A map of old to new entity ids.</returns>
        /// <exception cref="System.ArgumentNullException">entityData</exception>
        /// <exception cref="System.ArgumentException">entityData.TypeId must be set.</exception>
        public IDictionary <long, IEntity> CreateEntityGetMap(EntityData entityData)
        {
            try
            {
                // Validate arguments
                if (entityData == null)
                {
                    throw new ArgumentNullException("entityData");
                }

                if (entityData.TypeIds == null || entityData.TypeIds.Count < 0)
                {
                    throw new ArgumentException("entityData.TypeId must be set.");
                }

                // Process create
                entityData.DataState = DataState.Create;

                return(CreateUpdateDeleteImpl(entityData, true));
            }
            catch (Exception ex)
            {
                EventLog.Application.WriteError("CreateEntity failed:\n{0}\n\nException:\n{1}", EntityDataHelper.GetDebug(entityData), ex);
                throw;
            }
        }
Пример #2
0
        /// <summary>
        /// Creates a new entity.
        /// </summary>
        public EntityRef CreateEntity(EntityData entityData)
        {
            try
            {
                var     entityMap = CreateEntityGetMap(entityData);
                IEntity entity    = entityMap[entityData.Id.Id];
                long    id        = entity == null ? -1 : entity.Id;

                return(new EntityRef(id));
            }
            catch (CardinalityViolationException cvEx)
            {
                EventLog.Application.WriteWarning("Create Entity failed:\n{0}\n\nException:\n{1}", EntityDataHelper.GetDebug(entityData), cvEx.InnerException);
                throw;
            }
            catch (Exception ex)
            {
                EventLog.Application.WriteError("CreateEntity failed:\n{0}\n\nException:\n{1}", EntityDataHelper.GetDebug(entityData), ex);
                throw;
            }
        }
Пример #3
0
        /// <summary>
        /// Updates an entity.
        /// </summary>
        public void UpdateEntity(EntityData entityData)
        {
            try
            {
                // Validate arguments
                if (entityData == null)
                {
                    throw new ArgumentNullException("entityData");
                }

                // Process update
                entityData.DataState = DataState.Update;

                CreateUpdateDeleteImpl(entityData, true);

                NotifyRootModification(entityData.Id);
            }
            catch (CardinalityViolationException cvEx)
            {
                EventLog.Application.WriteWarning("Update Entity failed:\n{0}\n\nException:\n{1}", EntityDataHelper.GetDebug(entityData), cvEx.InnerException);
                throw;
            }
            catch (Exception ex)
            {
                EventLog.Application.WriteError("UpdateEntity failed:\n{0}\n\nException:\n{1}", EntityDataHelper.GetDebug(entityData), ex);
                throw;
            }
        }
Пример #4
0
        /// <summary>
        /// Decodes entity data.
        /// </summary>
        /// <param name="entityData">The data</param>
        /// <param name="persistChanges">If true, the nugget is intended to be persisted.</param>
        public IEntity DecodeEntity(EntityData entityData, bool persistChanges = false)
        {
            try
            {
                if (entityData == null)
                {
                    return(null);
                }

                var entity = CreateUpdateDeleteImpl(entityData, persistChanges);
                return(entity[entityData.Id.Id]);
            }
            catch (CardinalityViolationException cvEx)
            {
                EventLog.Application.WriteWarning("Decode Entity failed:\n{0}\n\nException:\n{1}", EntityDataHelper.GetDebug(entityData), cvEx.InnerException);
                throw;
            }
            catch (Exception ex)
            {
                EventLog.Application.WriteError("DecodeEntity failed:\n{0}\n\nException:\n{1}", EntityDataHelper.GetDebug(entityData), ex);
                throw;
            }
        }
Пример #5
0
        /// <summary>
        /// Clones the entity at the root of the request then updates it or any cloned or original related entities as needed.
        /// </summary>
        /// <param name="entityData">The entity data.</param>
        /// <returns></returns>
        public IDictionary <long, long> CloneAndUpdateEntity(EntityData entityData)
        {
            try
            {
                // Validate arguments
                if (entityData == null)
                {
                    throw new ArgumentNullException(nameof(entityData));
                }

                // Get the entity to clone
                IEntity entity = Entity.Get(entityData.Id);

                if (entity.IsTemporaryId)
                {
                    throw new InvalidOperationException("Cannot clone a temporary entity.");
                }

                // Clone it
                IEntity clone = entity.Clone(CloneOption.Deep);

                // Do a save and store the mapping of old to new ids
                var clonedIdsMap = clone.Save();

                // Update the cloned ids in the entity graph
                UpdateEntityDataClonedIds(entityData, clonedIdsMap);

                UpdateEntity(entityData);

                if (!string.IsNullOrWhiteSpace(entity.Alias))
                {
                    // Clear alias to prevent duplicates
                    clone.SetField("core:alias", null);
                    clone.Save();
                }

                return(clonedIdsMap);
            }
            catch (CardinalityViolationException cvEx)
            {
                EventLog.Application.WriteWarning("Clone Entity failed:\n{0}\n\nException:\n{1}", EntityDataHelper.GetDebug(entityData), cvEx.InnerException);
                throw;
            }
            catch (Exception ex)
            {
                EventLog.Application.WriteError("CloneEntity failed:\n{0}\n\nException:\n{1}", EntityDataHelper.GetDebug(entityData), ex);
                throw;
            }
        }