Example #1
0
        private async Task MoveTypeCategoryAsync(Authentication authentication, ITypeCategory sourceCategory, string newPath)
        {
            var destPath   = new DataBasePath(newPath);
            var destObject = await this.GetObjectAsync(authentication, destPath.Path);

            var dataBase = sourceCategory.GetService(typeof(IDataBase)) as IDataBase;
            var types    = sourceCategory.GetService(typeof(ITypeCollection)) as ITypeCollection;

            if (destPath.DataBaseName != dataBase.Name)
            {
                throw new InvalidOperationException($"cannot move to : {destPath}");
            }
            if (destPath.Context != CremaSchema.TypeDirectory)
            {
                throw new InvalidOperationException($"cannot move to : {destPath}");
            }
            if (destObject is IType)
            {
                throw new InvalidOperationException($"cannot move to : {destPath}");
            }

            using (await UsingDataBase.SetAsync(dataBase, authentication))
            {
                if (destObject is ITypeCategory destCategory)
                {
                    if (sourceCategory.Parent != destCategory)
                    {
                        await sourceCategory.MoveAsync(authentication, destCategory.Path);
                    }
                }
                else
                {
                    if (NameValidator.VerifyCategoryPath(destPath.ItemPath) == true)
                    {
                        throw new InvalidOperationException($"cannot move to : {destPath}");
                    }
                    var itemName   = new ItemName(destPath.ItemPath);
                    var categories = sourceCategory.GetService(typeof(ITypeCategoryCollection)) as ITypeCategoryCollection;
                    if (await categories.ContainsAsync(itemName.CategoryPath) == false)
                    {
                        throw new InvalidOperationException($"cannot move to : {destPath}");
                    }
                    if (sourceCategory.Name != itemName.Name && await types.ContainsAsync(itemName.Name) == true)
                    {
                        throw new InvalidOperationException($"cannot move to : {destPath}");
                    }
                    if (sourceCategory.Parent.Path != itemName.CategoryPath)
                    {
                        await sourceCategory.MoveAsync(authentication, itemName.CategoryPath);
                    }
                    if (sourceCategory.Name != itemName.Name)
                    {
                        await sourceCategory.RenameAsync(authentication, itemName.Name);
                    }
                }
            }
        }
        public async Task MoveAsync(ITypeCategory category, TaskContext context)
        {
            var authentication = context.Authentication;
            var categories     = category.GetService(typeof(ITypeCategoryCollection)) as ITypeCategoryCollection;
            var categoryPath   = await categories.Dispatcher.InvokeAsync(() => categories.Random().Path);

            if (context.AllowException == false)
            {
                if (await VerifyAsync() == false)
                {
                    return;
                }
            }
            await category.MoveAsync(authentication, categoryPath);

            async Task <bool> VerifyAsync()
            {
                if ((await category.GetAllTypesAsync(item => item.TypeState != TypeState.None)).Any() == true)
                {
                    return(false);
                }

                if ((await category.GetAllUsingTablesAsync(item => item.TableState != TableState.None)).Any() == true)
                {
                    return(false);
                }

                return(await category.Dispatcher.InvokeAsync(() =>
                {
                    if (category.Parent == null)
                    {
                        return false;
                    }
                    if (category.Parent.Path == categoryPath)
                    {
                        return false;
                    }
                    if (category.Path == categoryPath)
                    {
                        return false;
                    }
                    if (categoryPath.StartsWith(category.Path) == true)
                    {
                        return false;
                    }
                    if (category.VerifyAccessType(authentication, AccessType.Master) == false)
                    {
                        return false;
                    }
                    return true;
                }));
            }
        }