Beispiel #1
0
        private IDeleteInfo CalculateSecondInfo(IDeleteInfo info, EntityDTO entity)
        {
            var hinfo = info as IDeleteInfoHibernate;

            if (hinfo == null)
            {
                return(null);
            }

            if (hinfo.IsSubclass)
            {
                var root = hinfo.GetRootDeleteInfo();
                if (root != null)
                {
                    return(root);
                }
            }

            if (hinfo.IsRootForSubclasses)
            {
                var subclass = NHibernate.NHibernateUtil.GetClass(entity.Entity);

                if (subclass != null)
                {
                    return(DeleteConfig.GetDeleteInfo(subclass));
                }
            }

            return(null);
        }
Beispiel #2
0
        IDeleteInfo IDeleteInfoHibernate.GetRootDeleteInfo()
        {
            if (!IsSubclass)
            {
                return(null);
            }

            var hmap = OrmConfig.NhConfig.GetClassMapping(ObjectClass) as NHibernate.Mapping.Subclass;

            return(DeleteConfig.GetDeleteInfo(hmap.RootClazz.MappedClass));
        }
Beispiel #3
0
        public static int CalculateTotalLinks <TEntity>(IUnitOfWork uow, TEntity fromE)
            where TEntity : IDomainObject
        {
            logger.Info("Подсчет ссылок...");
            int totalLinks = 0;

            if (fromE == null)
            {
                throw new ArgumentNullException("fromE");
            }
            if (fromE.Id == 0)
            {
                throw new ArgumentException("Сущьность должна уже иметь ID", nameof(fromE));
            }

            var delConfig = DeleteConfig.GetDeleteInfo <TEntity>();

            if (delConfig == null)
            {
                throw new InvalidOperationException($"Конфигурация удаления для типа {typeof(TEntity)} не найдена.");
            }

            if (!(delConfig is IDeleteInfoHibernate))
            {
                throw new NotSupportedException($"Поддерживаются только конфигурации удаляения Hibernate.");
            }

            foreach (var depend in delConfig.DeleteItems)
            {
                if (String.IsNullOrEmpty(depend.PropertyName))
                {
                    continue;
                }
                var propInfo = depend.ObjectClass.GetProperty(depend.PropertyName);
                totalLinks += GetLinkedEntities(uow, depend, fromE).Count;
            }

            foreach (var depend in delConfig.ClearItems)
            {
                var propInfo = depend.ObjectClass.GetProperty(depend.PropertyName);
                totalLinks += GetLinkedEntities(uow, depend, fromE).Count;
            }

            foreach (var depend in delConfig.RemoveFromItems)
            {
                var collPropInfo = depend.ObjectClass.GetProperty(depend.CollectionName);
                totalLinks += GetLinkedEntities(uow, depend, fromE).Count;
            }

            logger.Info("Найдено {0} ссылок.", totalLinks);
            return(totalLinks);
        }
Beispiel #4
0
        public static int ReplaceEverywhere <TEntity>(IUnitOfWork uow, TEntity fromE, TEntity toE)
            where TEntity : IDomainObject
        {
            int replacedLinks = 0;

            if (fromE == null || toE == null)
            {
                throw new ArgumentNullException("fromE || toE");
            }
            if (fromE.Id == 0)
            {
                throw new ArgumentException("Сущьность должна уже иметь ID", nameof(fromE));
            }
            if (toE.Id == 0)
            {
                throw new ArgumentException("Сущьность должна уже иметь ID", nameof(toE));
            }

            var delConfig = DeleteConfig.GetDeleteInfo <TEntity>();

            if (delConfig == null)
            {
                throw new InvalidOperationException($"Конфигурация удаления для типа {typeof(TEntity)} не найдена.");
            }

            if (!(delConfig is IDeleteInfoHibernate))
            {
                throw new NotSupportedException($"Поддерживаются только конфигурации удаляения Hibernate.");
            }

            foreach (var depend in delConfig.DeleteItems)
            {
                if (String.IsNullOrEmpty(depend.PropertyName))
                {
                    continue;
                }
                var propInfo = depend.ObjectClass.GetProperty(depend.PropertyName);
                foreach (var item in GetLinkedEntities(uow, depend, fromE))
                {
                    propInfo.SetValue(item, toE, null);
                    uow.TrySave(item);
                    replacedLinks++;
                }
            }

            foreach (var depend in delConfig.ClearItems)
            {
                var propInfo = depend.ObjectClass.GetProperty(depend.PropertyName);
                foreach (var item in GetLinkedEntities(uow, depend, fromE))
                {
                    propInfo.SetValue(item, toE, null);
                    uow.TrySave(item);
                    replacedLinks++;
                }
            }

            foreach (var depend in delConfig.RemoveFromItems)
            {
                var collPropInfo = depend.ObjectClass.GetProperty(depend.CollectionName);
                foreach (var item in GetLinkedEntities(uow, depend, fromE))
                {
                    var coll     = (collPropInfo as IList);
                    var replaced = coll.Cast <IDomainObject>().First(x => x.Id == fromE.Id);
                    var ix       = coll.IndexOf(replaced);
                    collPropInfo.SetValue(item, toE, new object[] { ix });
                    uow.TrySave(item);
                    replacedLinks++;
                }
            }

            return(replacedLinks);
        }
Beispiel #5
0
        private bool Run(IDeleteInfo info, uint id)
        {
            try {
                CheckDlg = new CheckOperationDlg();
                CheckDlg.Show();

                var self = info.GetSelfEntity(this, id);
                PreparedOperation = info.CreateDeleteOperation(self);

                DeletedItems.Add(new DeletedItem {
                    ItemClass = info.ObjectClass,
                    ItemId    = id,
                    Title     = self.Title
                });

                countReferenceItems = FillChildOperation(info, PreparedOperation, new TreeIter(), self);
                bool isCanceled = CheckDlg.IsCanceled;
                CheckDlg.Destroy();

                if (isCanceled)
                {
                    return(false);
                }
            } catch (Exception ex) {
                CheckDlg.Destroy();
                QSMain.ErrorMessageWithLog("Ошибка в разборе зависимостей удаляемого объекта.", logger, ex);
                return(false);
            }

            bool userAccept = DeleteDlg.RunDialog(this);

            if (userAccept)
            {
                ExcuteDlg = new DeleteOperationDlg();
                ExcuteDlg.SetOperationsCount(PreparedOperation.GetOperationsCount() + 2);
                ExcuteDlg.Show();
                BeforeDeletion?.Invoke();

                try {
                    IsHibernateMode = HasHibernateOperations(PreparedOperation);
                    PreparedOperation.Execute(this);
                    ExcuteDlg.AddExcuteOperation("Операции с журналом изменений");
                    DeleteConfig.OnAfterDeletion(sqlTransaction, DeletedItems);
                    ExcuteDlg.AddExcuteOperation("Завершение транзакции");
                    if (sqlTransaction != null)
                    {
                        sqlTransaction.Commit();
                    }
                    if (uow != null)
                    {
                        uow.Commit();
                    }
                    return(true);
                } catch (Exception ex) {
                    if (SqlTransaction != null)
                    {
                        sqlTransaction.Rollback();
                    }
                    QSMain.ErrorMessageWithLog("Ошибка при удалении", logger, ex);
                }
                finally
                {
                    ExcuteDlg.Destroy();
                }
            }
            return(false);
        }