private async Task MoveTableCategoryAsync(Authentication authentication, ITableCategory 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 tables = sourceCategory.GetService(typeof(ITableCollection)) as ITableCollection; if (destPath.DataBaseName != dataBase.Name) { throw new InvalidOperationException($"cannot move to : {destPath}"); } if (destPath.Context != CremaSchema.TableDirectory) { throw new InvalidOperationException($"cannot move to : {destPath}"); } if (destObject is ITable) { throw new InvalidOperationException($"cannot move to : {destPath}"); } using (await UsingDataBase.SetAsync(dataBase, authentication)) { if (destObject is ITableCategory 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(ITableCategoryCollection)) as ITableCategoryCollection; if (await categories.ContainsAsync(itemName.CategoryPath) == false) { throw new InvalidOperationException($"cannot move to : {destPath}"); } if (sourceCategory.Name != itemName.Name && await tables.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(ITableCategory category, TaskContext context) { var authentication = context.Authentication; var categories = category.GetService(typeof(ITableCategoryCollection)) as ITableCategoryCollection; 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.GetAllTablesAsync(item => item.TableState != TableState.None)).Any() == true) { return(false); } return(await category.Dispatcher.InvokeAsync(() => { if (category.Parent == null) { return false; } if (categoryPath.StartsWith(category.Path) == true) { return false; } if (category.Parent.Path == categoryPath) { return false; } if (categoryPath.StartsWith(category.Path) == true) { return false; } return true; })); } }