Example #1
0
        /// <summary>
        /// duplicate a entity
        /// </summary>
        /// <param name="sourceId">the id of the source entity</param>
        /// <param name="parentId">the parent entity id of the new entity, empty id for root</param>
        /// <param name="newName">give it a new name</param>
        /// <param name="isRecuresive">do i duplcate the children</param>
        /// <param name="duplicatePayload">du i dupplcate the pyload in the signal, apply to all child eneity</param>
        /// <returns>return new entity</returns>
        public async Task <JDBCEntity> DuplicateAsync(Guid sourceId, Guid parentId, string newName, bool isRecuresive = false, bool duplicatePayload = false)
        {
            //检查是否有源节点
            JDBCEntity source = await GetOneByIdAsync(sourceId);

            if (source == null)
            {
                throw new Exception(ErrorMessages.ExperimentOrSignalNotFoundError);
            }
            var entityWithSameName = await GetChildByNameAsync(parentId, newName);

            if (entityWithSameName != null)
            {
                //如果父节点下有同名节点,报错
                throw new Exception(ErrorMessages.NameDuplicateError);
            }
            JDBCEntity parent = await GetOneByIdAsync(parentId);

            if (source.EntityType.Equals(JDBCEntityType.Signal))
            {
                if (parentId.Equals(Guid.Empty) || parent == null)
                {
                    //如果parentId==Guid.Empty,源节点又是Signal,则报错
                    //检查是否有指定父节点
                    throw new Exception(ErrorMessages.ParentNotExistError);
                }
                if (!parent.EntityType.Equals(JDBCEntityType.Experiment))
                {
                    throw new Exception(ErrorMessages.NotValidParentError);
                }
                source.Id       = Guid.NewGuid();
                source.Path     = parent.Path.Trim() + "/" + newName;
                source.Name     = newName;
                source.ParentId = parentId;
                await collection.InsertOneAsync(source);

                if (duplicatePayload == true)
                {
                    await myStorageEngine.CopyDataAsync(sourceId, source.Id);
                }
                return(source);
            }
            string path = "";

            if (parentId.Equals(Guid.Empty))
            {
                path = "/" + newName;
            }
            else
            {
                if (parent != null)
                {
                    path = parent.Path.Trim() + "/" + newName;
                }
                else
                {
                    throw new Exception(ErrorMessages.ExperimentOrSignalNotFoundError);
                }
            }
            source.Id       = Guid.NewGuid();
            source.Path     = path;
            source.Name     = newName;
            source.ParentId = parentId;
            await collection.InsertOneAsync(source);

            if (isRecuresive == false && duplicatePayload == true)
            {
                throw new Exception(ErrorMessages.NotValidDuplicateOperatorError);
            }
            if (isRecuresive == true)
            {
                if (duplicatePayload == true)
                {
                    await recursiveCopyPayloadAsync(sourceId, source.Path, source.Id);
                }
                else
                {
                    await recursiveCopyEntityAsync(sourceId, source.Path, source.Id);
                }
            }
            return(source);
        }