private bool OnSaveInternal(bool canSave)
        {
            if (!canSave)
            {
                return(true);
            }

            if (!ConnectionManager.Instance.AllowRequest())
            {
                return(false);
            }

            try
            {
                _isUpdating = true;
                WaitStart();

                var changes = GetChanges(Source, true);

                var relatedChanges = CollectRelatedParams(Source, changes);

                if (!Validation(relatedChanges))
                {
                    return(false);
                }

                var selectedItem = SelectedItems[0];

                if (ShouldUpdateSeparately)
                {
                    var uowFactory = IoC.Instance.Resolve <IUnitOfWorkFactory>();
                    using (var uow = uowFactory.Create(false))
                    {
                        try
                        {
                            uow.BeginChanges();
                            var mng = GetManager();
                            mng.SetUnitOfWork(uow);

                            var sources = Source.OrderByDescending(cpv => cpv.CPVParent, new SpecialComparer());
                            foreach (var cpv in sources)
                            {
                                var key = cpv.GetKey <decimal>();
                                if (key < 0)
                                {
                                    var item = cpv;

                                    if (!string.IsNullOrEmpty(cpv.CPVValue) || //Если были проставлены значения по умолчанию - сохраняем
                                        cpv.Cp.CUSTOMPARAMSAVEMODE || HasChanges(GetChanges(CpvHelper.GetChildsCpvByParentCpv <T>(sources, cpv, false), true)))
                                    {
                                        mng.Insert(ref item);

                                        item.Cp             = cpv.Cp;
                                        item.FormattedValue = CpvHelper.GetFormattedValue(item);
                                        SourceUpdate(item, cpv);
                                        var childs = sources.Where(i => i.CPVParent == key);
                                        foreach (var child in childs)
                                        {
                                            child.CPVParent = item.CPVID;
                                        }
                                    }
                                    else
                                    {
                                        cpv.AcceptChanges();
                                    }
                                }
                                else
                                {
                                    //Удаляем параметры, у которых не установлен CUSTOMPARAMSAVEMODE и CPVValue is null и нет деток
                                    if (string.IsNullOrEmpty(cpv.CPVValue) && !cpv.Cp.CUSTOMPARAMSAVEMODE &&
                                        CpvHelper.GetChildsCpvByParentCpv <T>(sources, cpv, false).Length == 0)
                                    {
                                        mng.Delete(cpv);
                                        cpv.AcceptChanges();
                                    }
                                    else if (cpv.IsDirty)
                                    {
                                        mng.Update(cpv);
                                    }
                                }
                            }

                            uow.CommitChanges();
                        }
                        catch
                        {
                            uow.RollbackChanges();
                            throw;
                        }
                    }
                }

                _hasAddCpv = false;
                if (selectedItem != null)
                {
                    SelectedItem = selectedItem;
                }
                OnSourceUpdated();
            }
            catch (Exception ex)
            {
                if (!ExceptionHandler(ex, ExceptionResources.ItemCantSave))
                {
                    throw;
                }
                return(false);
            }
            finally
            {
                _isUpdating = false;
                WaitStop();
            }

            return(true);
        }
Example #2
0
        private void OnDelete()
        {
            if (!ConnectionManager.Instance.AllowRequest())
            {
                return;
            }

            if (!CanDelete())
            {
                return;
            }

            try
            {
                if (GetViewService().ShowDialog(StringResources.Confirmation
                                                , StringResources.ConfirmationDeleteObject
                                                , MessageBoxButton.YesNo
                                                , MessageBoxImage.Question
                                                , MessageBoxResult.Yes) != MessageBoxResult.Yes)
                {
                    return;
                }

                var selectedItem = (CustomParamValue)CurrentItem;
                var deletes      = CpvHelper.GetChildsCpvByParentCpv <CustomParamValue>(Source.Cast <CustomParamValue>(), selectedItem, true);

                if (ShouldUpdateSeparately)
                {
                    var uowFactory = IoC.Instance.Resolve <IUnitOfWorkFactory>();
                    using (var uow = uowFactory.Create(false))
                    {
                        try
                        {
                            uow.BeginChanges();
                            var mng = GetManager(_itemType);
                            mng.SetUnitOfWork(uow);

                            foreach (var p in deletes)
                            {
                                mng.Delete(p);
                            }
                            mng.Delete(CurrentItem);

                            uow.CommitChanges();
                        }
                        catch
                        {
                            uow.RollbackChanges();
                            throw;
                        }
                    }

                    if (ParentViewModel != null)
                    {
                        ParentViewModel.RefreshData();
                    }
                }
                else
                {
                    foreach (var p in deletes)
                    {
                        Source.Remove(p);
                    }
                    Source.Remove(CurrentItem);
                }
            }
            catch (Exception ex)
            {
                throw new OperationException(ExceptionResources.ItemCantDelete, ex);
            }
        }
        protected override void Delete()
        {
            if (!CanDelete())
            {
                return;
            }

            try
            {
                WaitStart();

                if (!ConnectionManager.Instance.AllowRequest())
                {
                    return;
                }

                var selectedItem = SelectedItems[0];
                var deletes      = CpvHelper.GetChildsCpvByParentCpv <T>(Source, selectedItem, true);

                var message       = StringResources.ConfirmationDeleteCpv;
                var messageFormat = StringResources.CpvChanges;
                if (deletes.Length > 1)
                {
                    deletes       = deletes.OrderByDescending(p => p.CPVParent).ToArray();
                    message       = StringResources.ConfirmationDeleteCpvs;
                    messageFormat = StringResources.CpvsChange;
                }
                if (HasChanges(GetChanges(deletes)))
                {
                    message = string.Format(messageFormat, message);
                }
                if (!DeleteConfirmation(message))
                {
                    return;
                }

                if (ShouldUpdateSeparately)
                {
                    var dbdeletes = deletes.Where(p => p.CPVID >= 0).ToArray();
                    if (dbdeletes.Any())
                    {
                        using (var mng = GetManager())
                        {
                            mng.Delete(dbdeletes);
                        }
                    }
                }

                foreach (var d in deletes)
                {
                    Source.Remove(d);
                }

                OnSourceUpdated();
                if (HasSelectedItems())
                {
                    SelectedItem = SelectedItems[0];
                }
            }
            catch (Exception ex)
            {
                if (!ExceptionHandler(ex, ExceptionResources.ItemsCantDelete))
                {
                    throw;
                }
            }
            finally
            {
                WaitStop();
                OnNeedChangeFocusRow();
            }
        }