public DeleteCore DeleteEntity(Type clazz, int id, IUnitOfWork uow = null, Action beforeDeletion = null) { var deletion = new DeleteCore(configuration, uow); #region Подготовка удаления using (var cancellation = new CancellationTokenSource()) { var preparePage = navigation.OpenViewModel <PrepareDeletionViewModel, DeleteCore, CancellationTokenSource>(null, deletion, cancellation); deletion.PrepareDeletion(clazz, id, cancellation.Token); if (cancellation.IsCancellationRequested) { return(deletion); } navigation.ForceClosePage(preparePage, CloseSource.External); } #endregion #region Диалог удаления if (deletion.TotalLinks > 0) { var deletionPage = navigation.OpenViewModel <DeletionViewModel, DeleteCore>(null, deletion); deletionPage.ViewModel.DeletionAccepted = () => RunDeletion(deletion); } else if (interactive.Question($"Удалить {deletion.RootEntity.Title}?")) { RunDeletion(deletion); } else { deletion.DeletionExecuted = false; } #endregion return(deletion); }
public DeleteCore DeleteEntity(Type clazz, int id, IUnitOfWork uow = null, Action beforeDeletion = null) { var deletion = new DeleteCore(configuration, uow); deletion.BeforeDeletion = beforeDeletion; #region Подготовка удаления IPage <PrepareDeletionViewModel> preparePage = null; using (var cancellation = new CancellationTokenSource()) { try { preparePage = navigation.OpenViewModel <PrepareDeletionViewModel, DeleteCore, CancellationTokenSource>(null, deletion, cancellation); deletion.PrepareDeletion(clazz, id, cancellation.Token); if (cancellation.IsCancellationRequested) { deletion.Close(); return(deletion); } } finally { if (preparePage != null) { navigation.ForceClosePage(preparePage, CloseSource.External); } } } #endregion #region Диалог удаления if (deletion.TotalLinks > 0) { var deletionPage = navigation.OpenViewModel <DeletionViewModel, DeleteCore>(null, deletion); deletionPage.ViewModel.DeletionAccepted = () => RunDeletion(deletion); deletionPage.PageClosed += delegate(object sender, PageClosedEventArgs e) { if (e.CloseSource != CloseSource.Self) { deletion.Close(); } }; } else if (interactive.Question($"Удалить {deletion.RootEntity.Title}?")) { RunDeletion(deletion); } else { deletion.Close(); deletion.DeletionExecuted = false; } #endregion return(deletion); }
public void HandleDelete_CanDeleteEmployeeTest() { var ask = Substitute.For <IInteractiveQuestion>(); ask.Question(string.Empty).ReturnsForAnyArgs(true); using (var uow = UnitOfWorkFactory.CreateWithoutRoot("Тест на обработку удаления сотрудника")) { BuisnessLogicGlobalEventHandler.Init(ask, UnitOfWorkFactory); var nomenclatureType = new ItemsType(); nomenclatureType.Name = "Тестовый тип номенклатуры"; uow.Save(nomenclatureType); var nomenclature = new Nomenclature(); nomenclature.Type = nomenclatureType; uow.Save(nomenclature); var protectionTools = new ProtectionTools(); protectionTools.Name = "СИЗ для тестирования"; protectionTools.AddNomeclature(nomenclature); uow.Save(protectionTools); var norm = new Norm(); var normItem = norm.AddItem(protectionTools); normItem.Amount = 1; normItem.NormPeriod = NormPeriodType.Month; normItem.PeriodCount = 2; uow.Save(norm); var employee = new EmployeeCard(); uow.Save(employee); var warehouseOperation = new WarehouseOperation(); var expenseOp = new EmployeeIssueOperation(); expenseOp.OperationTime = warehouseOperation.OperationTime = new DateTime(2019, 1, 1); expenseOp.ExpiryByNorm = new DateTime(2019, 4, 1); expenseOp.ProtectionTools = protectionTools; expenseOp.Employee = employee; expenseOp.Nomenclature = warehouseOperation.Nomenclature = nomenclature; expenseOp.NormItem = normItem; warehouseOperation.Amount = expenseOp.Issued = 1; expenseOp.WarehouseOperation = warehouseOperation; uow.Save(nomenclature); uow.Save(normItem); uow.Save(warehouseOperation); uow.Save(expenseOp); uow.Commit(); //FIXME Временно чтобы переделака не вызвала конфликт мержа в 2.4 Configure.ConfigureDeletion(); var deletion = new DeleteCore(DeleteConfig.Main, uow); deletion.PrepareDeletion(typeof(EmployeeCard), employee.Id, CancellationToken.None); deletion.RunDeletion(CancellationToken.None); } }
public static List <DeletionObject> GetDeletionObjects(Type objectClass, int id, IUnitOfWork uow = null) { var result = new List <DeletionObject>(); var delete = new DeleteCore(DeleteConfig.Main, uow); delete.PrepareDeletion(objectClass, id, new System.Threading.CancellationTokenSource().Token); foreach (var item in delete.DeletedItems) { result.Add(new DeletionObject() { Id = item.Id, Type = item.ClassType }); } return(result); }
public void Delete_NotFailWhenTryCleanPropertyOfDeletedInstanceTest() { //Тут очень важна последовательность конфига, окуратнее исправляйте тест. //Смысл теста в том, что через каскадное удаление объект в котором предполагалась очистка ссылки, удаляется перед, этим. // А в момент очистки ссылки он заново записывется, обычно с тем же ID, что в последствии вызывает различные проблемы, // например не возможно удалить объект, на который имеется ссылка из этого, или не удается этот записать, так как удален // тот на который мы ссылаемся. Повторюсь, мы не должны были его записывать. var delConfig = new DeleteConfiguration(); delConfig.AddHibernateDeleteInfo <DependDeleteItem>() .AddDeleteCascadeDependence(x => x.CleanLink); delConfig.AddHibernateDeleteInfo <AlsoDeleteItem>() .AddClearDependence <DependDeleteItem>(x => x.CleanLink); delConfig.AddHibernateDeleteInfo <RootDeleteItem>() .AddDeleteDependence <DependDeleteItem>(x => x.DeleteLink) .AddDeleteDependence <AlsoDeleteItem>(x => x.Root); using (var uow = UnitOfWorkFactory.CreateWithoutRoot()) { var root = new RootDeleteItem(); var item1 = new AlsoDeleteItem() { Root = root }; var item2 = new DependDeleteItem() { DeleteLink = root, CleanLink = item1 }; uow.Save(root); uow.Save(item1); uow.Save(item2); uow.Commit(); var deletion = new DeleteCore(delConfig, uow); var cancellation = new System.Threading.CancellationToken(); deletion.PrepareDeletion(typeof(RootDeleteItem), root.Id, cancellation); Assert.That(deletion.ItemsToDelete, Is.EqualTo(3)); deletion.RunDeletion(cancellation); Assert.That(deletion.DeletionExecuted, Is.EqualTo(true)); var dependDeleteItems = uow.GetAll <DependDeleteItem>().ToList(); Assert.That(dependDeleteItems.Count, Is.EqualTo(0)); } }
public void Deletion_ExpenseEmployeeDocumentTest() { NewSessionWithSameDB(); var ask = Substitute.For <IInteractiveQuestion>(); ask.Question(string.Empty).ReturnsForAnyArgs(true); var navigation = Substitute.For <INavigationManager>(); using (var uow = UnitOfWorkFactory.CreateWithoutRoot()) { var warehouse = new Warehouse(); uow.Save(warehouse); var nomenclatureType = new ItemsType { Name = "Тестовый тип номенклатуры" }; uow.Save(nomenclatureType); var nomenclature = new Nomenclature { Type = nomenclatureType }; uow.Save(nomenclature); var position1 = new StockPosition(nomenclature, 0, null, null); var nomenclature2 = new Nomenclature { Type = nomenclatureType }; uow.Save(nomenclature2); var position2 = new StockPosition(nomenclature2, 0, null, null); var protectionTools = new ProtectionTools { Name = "СИЗ для тестирования" }; protectionTools.AddNomeclature(nomenclature); uow.Save(protectionTools); var protectionTools2 = new ProtectionTools { Name = "СИЗ для тестирования 2" }; protectionTools2.AddNomeclature(nomenclature2); uow.Save(protectionTools2); var norm = new Norm(); var normItem = norm.AddItem(protectionTools); normItem.Amount = 1; normItem.NormPeriod = NormPeriodType.Year; normItem.PeriodCount = 1; var normItem2 = norm.AddItem(protectionTools2); normItem2.Amount = 1; normItem2.NormPeriod = NormPeriodType.Month; normItem2.PeriodCount = 1; uow.Save(norm); var employee = new EmployeeCard(); employee.AddUsedNorm(norm); uow.Save(employee); uow.Commit(); var income = new Income { Warehouse = warehouse, Date = new DateTime(2017, 1, 1), Operation = IncomeOperations.Enter }; var incomeItem1 = income.AddItem(nomenclature); incomeItem1.Amount = 10; var incomeItem2 = income.AddItem(nomenclature2); incomeItem2.Amount = 5; income.UpdateOperations(uow, ask); uow.Save(income); var expense = new Expense { Operation = ExpenseOperations.Employee, Warehouse = warehouse, Employee = employee, Date = new DateTime(2018, 10, 22) }; expense.AddItem(position1, 1); expense.AddItem(position2, 1); var baseParameters = Substitute.For <BaseParameters>(); baseParameters.ColDayAheadOfShedule.Returns(0); expense.CreateIssuanceSheet(null); //Обновление операций expense.UpdateOperations(uow, baseParameters, ask); uow.Save(expense); uow.Save(expense.IssuanceSheet); uow.Commit(); expense.UpdateEmployeeWearItems(); uow.Commit(); var docs = uow.GetAll <Expense>().ToList(); Assert.That(docs.Count, Is.EqualTo(1)); //Непосредственно удаление документа var cancel = new CancellationTokenSource(); using (var uowDel = UnitOfWorkFactory.CreateWithoutRoot()) { var deletionService = new DeleteCore(DeleteConfig.Main, uowDel); deletionService.PrepareDeletion(typeof(Expense), expense.Id, cancel.Token); Assert.That(deletionService.TotalLinks, Is.GreaterThan(0)); deletionService.RunDeletion(cancel.Token); } //Проверяем удаление var expenseId = expense.Id; using (var uowCheck = UnitOfWorkFactory.CreateWithoutRoot()) { //Проверяем что удалили документ. docs = uow.GetAll <Expense>().ToList(); Assert.That(docs.Count, Is.Zero); //Проверяем что случайно не удалили СИЗ и номеклатуру. var protections = uow.GetAll <ProtectionTools>().ToList(); Assert.That(protections.Count, Is.EqualTo(2)); var nomenclatures = uow.GetAll <Nomenclature>().ToList(); Assert.That(nomenclatures.Count, Is.EqualTo(2)); } } }