Example #1
0
        /// <summary>
        /// اعتبار سنجی عملیات حذف بخش
        /// </summary>
        /// <param name="dep">بخش مورد نظر</param>
        /// <param name="exception">در صورت عدم معتبر بودن بخش خطا رخ می دهد</param>
        protected override void DeleteValidate(Department dep)
        {
            UIValidationExceptions exception = new UIValidationExceptions();

            PersonRepository personRep = new PersonRepository(false);

            if (departmentRepository.IsRoot(dep.ID))
            {
                exception.Add(ExceptionResourceKeys.DepartmentRootDeleteIllegal, "ریشه قابل حذف نیست", ExceptionSrc);
                throw exception;
            }

            //int count = personRep.GetCountByCriteria(new CriteriaStruct(/*Utility.GetPropertyName(() => new Model.Person().Department)*/"department", dep));
            IList <Person> personList = personRep.GetByCriteria(new CriteriaStruct("department", new Department()
            {
                ID = dep.ID
            }));

            if (personList.Count(p => p.IsDeleted == false) > 0)
            {
                exception.Add(ExceptionResourceKeys.DepUsedByPersons, "این بخش به اشخاص انتساب داده شده است", ExceptionSrc);
                throw exception;
            }
            IList <Department> depList = this.GetDepartmentChildsByParentPath(dep.ID);

            foreach (Department depItem in depList)
            {
                if (depItem.PersonList != null && depItem.PersonList.Count(p => p.IsDeleted == false) > 0)
                {
                    exception.Add(ExceptionResourceKeys.ChildsDepUsedByPersons, "زیر بخش های این بخش به اشخاص انتساب داده شده است", ExceptionSrc);
                    throw exception;
                }
            }
            if (personList.Count > 0)
            {
                Department depRootObj = NHibernateSessionManager.Instance.GetSession().QueryOver <Department>()
                                        .Where(x => x.ParentPath == null).SingleOrDefault();
                foreach (Person item in personList)
                {
                    BPerson personBussiness = new BPerson();
                    item.Department = depRootObj;
                    personBussiness.UpdatePerson(item, UIActionType.EDIT);
                }
            }
            departmentRepository.DelateHierarchicalByParentId(dep.ID);

            if (exception.Count > 0)
            {
                throw exception;
            }
        }