protected override CategoryFascicle BeforeDelete(CategoryFascicle entity, CategoryFascicle entityTransformed)
        {
            if (CurrentDeleteActionType != Common.Infrastructures.DeleteActionType.DeleteCategoryFascicle)
            {
                throw new DSWException(EXCEPTION_MESSAGE, null, DSWExceptionCode.SS_NotAllowedOperation);
            }

            if (entityTransformed.FascicleType == FascicleType.Procedure)
            {
                ICollection <CategoryFascicleTableValuedModel> subCategoryFascicles = _unitOfWork.Repository <CategoryFascicle>().GetCategorySubFascicles(entityTransformed.Category.EntityShortId);
                CategoryFascicle categoryFascicle;
                foreach (CategoryFascicleTableValuedModel toDelete in subCategoryFascicles)
                {
                    categoryFascicle = _unitOfWork.Repository <CategoryFascicle>().FindIncludeRights(toDelete.IdCategoryFascicle).Single();
                    _unitOfWork.Repository <CategoryFascicleRight>().DeleteRange(categoryFascicle.CategoryFascicleRights.ToList());
                    _unitOfWork.Repository <CategoryFascicle>().Delete(categoryFascicle);
                }

                ICollection <CategoryFascicle> periodicCategoryFascicleReferences = _unitOfWork.Repository <CategoryFascicle>().GetAllPeriodicByCategory(entityTransformed.Category.EntityShortId).ToList();
                CategoryFascicleRight          specialRight;
                foreach (CategoryFascicle periodicCategoryFascicleReference in periodicCategoryFascicleReferences)
                {
                    _unitOfWork.Repository <CategoryFascicleRight>().DeleteRange(periodicCategoryFascicleReference.CategoryFascicleRights.ToList());
                    if (_parameterEnvService.FascicleContainerEnabled)
                    {
                        specialRight = new CategoryFascicleRight()
                        {
                            CategoryFascicle = periodicCategoryFascicleReference,
                            Role             = _unitOfWork.Repository <Role>().GetByUniqueId(Guid.Empty).Single()
                        };
                        _unitOfWork.Repository <CategoryFascicleRight>().Insert(specialRight);
                    }
                }
            }

            if (entityTransformed.CategoryFascicleRights != null && entityTransformed.CategoryFascicleRights.Count > 0)
            {
                _unitOfWork.Repository <CategoryFascicleRight>().DeleteRange(entityTransformed.CategoryFascicleRights.ToList());
            }

            TableLog tableLog = TableLogService.CreateLog(entity.UniqueId, null, TableLogEvent.DELETE, $"Eliminato piano di fascicolazione di tipo {entityTransformed.FascicleType.ToString()} con environment {entityTransformed.DSWEnvironment}", nameof(CategoryFascicle), CurrentDomainUser.Account);

            _unitOfWork.Repository <TableLog>().Insert(tableLog);

            return(base.BeforeDelete(entity, entityTransformed));
        }
        protected override CategoryFascicle BeforeCreate(CategoryFascicle entity)
        {
            if (entity.FasciclePeriod != null)
            {
                entity.FasciclePeriod = _unitOfWork.Repository <FasciclePeriod>().Find(entity.FasciclePeriod.UniqueId);
            }

            if (entity.Category != null)
            {
                entity.Category = _unitOfWork.Repository <Category>().Find(entity.Category.EntityShortId);
            }

            if (entity.Manager != null)
            {
                entity.Manager = _unitOfWork.Repository <Contact>().Find(entity.Manager.EntityId);
            }

            if (entity.FascicleType == FascicleType.Period)
            {
                entity.CategoryFascicleRights = _unitOfWork.Repository <CategoryFascicleRight>().GetByProcedureCategoryId(entity.Category.EntityShortId, true).ToList();
            }

            if (entity.FascicleType == FascicleType.Period || entity.FascicleType == FascicleType.Procedure)
            {
                HashSet <CategoryFascicleRight> categoryFascicleRights = new HashSet <CategoryFascicleRight>();
                if (entity.CategoryFascicleRights?.Count > 0)
                {
                    CategoryFascicleRight tmpFascicleRight;
                    foreach (CategoryFascicleRight categoryFascicleRight in entity.CategoryFascicleRights)
                    {
                        tmpFascicleRight = new CategoryFascicleRight()
                        {
                            Role = _unitOfWork.Repository <Role>().Find(categoryFascicleRight.Role.EntityShortId)
                        };
                        categoryFascicleRights.Add(tmpFascicleRight);
                    }
                }

                if ((entity.CategoryFascicleRights == null || entity.CategoryFascicleRights.Count == 0) && _parameterEnvService.FascicleContainerEnabled)
                {
                    entity.CategoryFascicleRights = new List <CategoryFascicleRight>();
                    CategoryFascicleRight specialRight = new CategoryFascicleRight()
                    {
                        CategoryFascicle = entity,
                        Role             = _unitOfWork.Repository <Role>().GetByUniqueId(Guid.Empty).Single()
                    };
                    _unitOfWork.Repository <CategoryFascicleRight>().Insert(specialRight);
                    categoryFascicleRights.Add(specialRight);
                }
                entity.CategoryFascicleRights = categoryFascicleRights.ToList();
                _unitOfWork.Repository <CategoryFascicleRight>().InsertRange(entity.CategoryFascicleRights);

                if (entity.CategoryFascicleRights?.Count > 0 && entity.FascicleType == FascicleType.Procedure)
                {
                    ICollection <CategoryFascicle> periodicCategoryFascicleReferences = _unitOfWork.Repository <CategoryFascicle>().GetAllPeriodicByCategory(entity.Category.EntityShortId).ToList();
                    CategoryFascicleRight          tmpFascicleRight;
                    foreach (CategoryFascicle periodicCategoryFascicle in periodicCategoryFascicleReferences)
                    {
                        foreach (CategoryFascicleRight fascicleRight in entity.CategoryFascicleRights.Where(x => !periodicCategoryFascicle.CategoryFascicleRights.Any(xx => x.Role.EntityShortId == xx.Role.EntityShortId)))
                        {
                            tmpFascicleRight = new CategoryFascicleRight()
                            {
                                CategoryFascicle = periodicCategoryFascicle,
                                Role             = _unitOfWork.Repository <Role>().Find(fascicleRight.Role.EntityShortId)
                            };
                            _unitOfWork.Repository <CategoryFascicleRight>().Insert(tmpFascicleRight);
                        }
                    }
                }
            }

            return(base.BeforeCreate(entity));
        }