Ejemplo n.º 1
0
        public async Task ExecuteAsync(DeleteUnstructuredDataDependenciesCommand command, IExecutionContext executionContext)
        {
            string entityName;

            var entityDefinition = _entityDefinitionRepository.GetByCode(command.RootEntityDefinitionCode);

            EntityNotFoundException.ThrowIfNull(entityDefinition, command.RootEntityDefinitionCode);
            entityName = entityDefinition.Name;

            var query        = new GetEntityDependencySummaryByRelatedEntityQuery(command.RootEntityDefinitionCode, command.RootEntityId);
            var dependencies = await _queryExecutor.ExecuteAsync(query, executionContext);

            var requiredDependency = dependencies.FirstOrDefault(d => !d.CanDelete);

            if (requiredDependency != null)
            {
                throw new ValidationException(
                          string.Format("Cannot delete this {0} because {1} '{2}' has a dependency on it.",
                                        entityName,
                                        requiredDependency.Entity.EntityDefinitionName.ToLower(),
                                        requiredDependency.Entity.RootEntityTitle));
            }

            await _entityFrameworkSqlExecutor
            .ExecuteCommandAsync(_dbContext,
                                 "Cofoundry.UnstructuredDataDependency_Delete",
                                 new SqlParameter("EntityDefinitionCode", command.RootEntityDefinitionCode),
                                 new SqlParameter("EntityId", command.RootEntityId)
                                 );
        }
        /// <summary>
        /// Entity definitions db records are created on the fly so we need to make sure
        /// any new ones exist before we add permissions to them.
        ///
        /// Typically we'd use EnsureEntityDefinitionExistsCommand to create the entity
        /// definition, but since this command also creates permissions we need to do this
        /// manually.
        /// </summary>
        private async Task EnsureAllEntityDefinitionsExistAsync(
            Dictionary <string, EntityDefinition> dbDefinitions
            )
        {
            var codeDefinitions = _entityDefinitionRepository.GetAll();

            var newEntityCodes = codeDefinitions
                                 .Select(d => d.EntityDefinitionCode)
                                 .Where(d => !dbDefinitions.ContainsKey(d));

            if (!newEntityCodes.Any())
            {
                return;
            }

            foreach (var definitionCode in newEntityCodes)
            {
                // get the entity definition class
                var entityDefinition = _entityDefinitionRepository.GetByCode(definitionCode);

                // create a matching db record
                var dbDefinition = new EntityDefinition()
                {
                    EntityDefinitionCode = entityDefinition.EntityDefinitionCode,
                    Name = entityDefinition.Name
                };

                _dbContext.EntityDefinitions.Add(dbDefinition);
                dbDefinitions.Add(dbDefinition.EntityDefinitionCode, dbDefinition);
            }

            await _dbContext.SaveChangesAsync();
        }
Ejemplo n.º 3
0
        public async Task <ICollection <EntityDependencySummary> > ExecuteAsync(GetEntityDependencySummaryByRelatedEntityQuery query, IExecutionContext executionContext)
        {
            // Where there are duplicates, prioritise relationships that cannot be deleted
            var dbDependencyPreResult = await _dbContext
                                        .UnstructuredDataDependencies
                                        .AsNoTracking()
                                        .Where(r => r.RelatedEntityDefinitionCode == query.EntityDefinitionCode && r.RelatedEntityId == query.EntityId)
                                        .ToListAsync();

            // Query is split because EF core cannot translate groupings yet
            var dbDependencyGroups = dbDependencyPreResult
                                     .GroupBy(r => new { r.RootEntityDefinitionCode, r.RootEntityId }, (d, e) => e.OrderByDescending(x => x.RelatedEntityCascadeActionId == (int)RelatedEntityCascadeAction.None).FirstOrDefault())
                                     .GroupBy(r => r.RootEntityDefinitionCode)
                                     .ToList();

            var allRelatedEntities = new List <EntityDependencySummary>();

            foreach (var dbDependencyGroup in dbDependencyGroups)
            {
                var definition = _entityDefinitionRepository.GetByCode(dbDependencyGroup.Key) as IDependableEntityDefinition;
                IQuery <IDictionary <int, RootEntityMicroSummary> > getEntitiesQuery;

                EntityNotFoundException.ThrowIfNull(definition, dbDependencyGroup.Key);
                getEntitiesQuery = definition.CreateGetEntityMicroSummariesByIdRangeQuery(dbDependencyGroup.Select(e => e.RootEntityId));

                var entityMicroSummaries = await _queryExecutor.ExecuteAsync(getEntitiesQuery, executionContext);

                foreach (var entityMicroSummary in entityMicroSummaries.OrderBy(e => e.Value.RootEntityTitle))
                {
                    var dbDependency = dbDependencyGroup.SingleOrDefault(e => e.RootEntityId == entityMicroSummary.Key);

                    var entityDependencySummary = new EntityDependencySummary();
                    entityDependencySummary.Entity = entityMicroSummary.Value;

                    // relations for previous versions can be removed even when they are required.
                    entityDependencySummary.CanDelete =
                        dbDependency.RelatedEntityCascadeActionId != (int)RelatedEntityCascadeAction.None ||
                        entityDependencySummary.Entity.IsPreviousVersion;

                    allRelatedEntities.Add(entityDependencySummary);
                }
            }

            return(allRelatedEntities);
        }
Ejemplo n.º 4
0
        public IEnumerable <IPermissionApplication> GetPermissions(DeleteUnstructuredDataDependenciesCommand command)
        {
            var entityDefinition = _entityDefinitionRepository.GetByCode(command.RootEntityDefinitionCode);

            EntityNotFoundException.ThrowIfNull(entityDefinition, command.RootEntityDefinitionCode);

            var permission = _permissionRepository.GetByEntityAndPermissionType(entityDefinition, CommonPermissionTypes.Delete("Entity"));

            if (permission != null)
            {
                yield return(permission);
            }
        }
Ejemplo n.º 5
0
        private IQueryable <RootEntityMicroSummary> Query(GetImageAssetEntityMicroSummariesByIdRangeQuery query)
        {
            var definition = _entityDefinitionRepository.GetByCode(ImageAssetEntityDefinition.DefinitionCode);

            var dbQuery = _dbContext
                          .ImageAssets
                          .AsNoTracking()
                          .FilterByIds(query.ImageAssetIds)
                          .Select(v => new RootEntityMicroSummary()
            {
                RootEntityId         = v.ImageAssetId,
                RootEntityTitle      = v.FileName,
                EntityDefinitionCode = definition.EntityDefinitionCode,
                EntityDefinitionName = definition.Name
            });

            return(dbQuery);
        }
Ejemplo n.º 6
0
        private IQueryable <RootEntityMicroSummary> Query(GetPageDirectoryEntityMicroSummariesByIdRangeQuery query)
        {
            var definition = _entityDefinitionRepository.GetByCode(PageDirectoryEntityDefinition.DefinitionCode);

            var dbQuery = _dbContext
                          .PageDirectories
                          .AsNoTracking()
                          .Where(d => query.PageDirectoryIds.Contains(d.PageDirectoryId))
                          .Select(d => new RootEntityMicroSummary()
            {
                RootEntityId         = d.PageDirectoryId,
                RootEntityTitle      = d.Name,
                EntityDefinitionName = definition.Name,
                EntityDefinitionCode = definition.EntityDefinitionCode
            });

            return(dbQuery);
        }
Ejemplo n.º 7
0
        private IQueryable <RootEntityMicroSummary> Query(GetPageEntityMicroSummariesByIdRangeQuery query)
        {
            var definition = _entityDefinitionRepository.GetByCode(PageEntityDefinition.DefinitionCode);

            var dbQuery = _dbContext
                          .PageVersions
                          .AsNoTracking()
                          .FilterByWorkFlowStatusQuery(WorkFlowStatusQuery.Latest)
                          .Where(v => query.PageIds.Contains(v.PageId))
                          .Select(v => new RootEntityMicroSummary()
            {
                RootEntityId         = v.PageId,
                RootEntityTitle      = v.Title,
                EntityDefinitionName = definition.Name,
                EntityDefinitionCode = definition.EntityDefinitionCode
            });

            return(dbQuery);
        }
Ejemplo n.º 8
0
        public async Task ExecuteAsync(EnsureEntityDefinitionExistsCommand command, IExecutionContext executionContext)
        {
            var entityDefinition = _entityDefinitionRepository.GetByCode(command.EntityDefinitionCode);

            var dbDefinition = await _dbContext
                               .EntityDefinitions
                               .SingleOrDefaultAsync(e => e.EntityDefinitionCode == command.EntityDefinitionCode);

            if (dbDefinition == null)
            {
                dbDefinition = new EntityDefinition()
                {
                    EntityDefinitionCode = entityDefinition.EntityDefinitionCode,
                    Name = entityDefinition.Name
                };

                _dbContext.EntityDefinitions.Add(dbDefinition);
                await _dbContext.SaveChangesAsync();
            }
        }
        private IQueryable <ChildEntityMicroSummary> Query(GetPageVersionEntityMicroSummariesByIdRangeQuery query)
        {
            var definition = _entityDefinitionRepository.GetByCode(PageEntityDefinition.DefinitionCode);

            var dbQuery = _dbContext
                          .PageVersions
                          .AsNoTracking()
                          .Where(v => query.PageVersionIds.Contains(v.PageVersionId))
                          .Select(v => new ChildEntityMicroSummary()
            {
                ChildEntityId        = v.PageVersionId,
                RootEntityId         = v.PageId,
                RootEntityTitle      = v.Title,
                EntityDefinitionCode = definition.EntityDefinitionCode,
                EntityDefinitionName = definition.Name,
                IsPreviousVersion    = !v.PagePublishStatusQueries.Any()  // not draft or latest published version
            });

            return(dbQuery);
        }
Ejemplo n.º 10
0
        private IQueryable <ChildEntityMicroSummary> Query(GetPageVersionBlockEntityMicroSummariesByIdRangeQuery query)
        {
            var definition = _entityDefinitionRepository.GetByCode(PageEntityDefinition.DefinitionCode);

            var dbQuery = _dbContext
                          .PageVersionBlocks
                          .AsNoTracking()
                          .Where(m => query.PageVersionBlockIds.Contains(m.PageVersionBlockId))
                          .Select(m => new ChildEntityMicroSummary()
            {
                ChildEntityId        = m.PageVersionBlockId,
                RootEntityId         = m.PageVersion.PageId,
                RootEntityTitle      = m.PageVersion.Title,
                EntityDefinitionCode = definition.EntityDefinitionCode,
                EntityDefinitionName = definition.Name,
                IsPreviousVersion    = !m.PageVersion.PagePublishStatusQueries.Any()
            });

            return(dbQuery);
        }
        private IQueryable <ChildEntityMicroSummary> Query(GetPageVersionModuleEntityMicroSummariesByIdRangeQuery query)
        {
            var definition = _entityDefinitionRepository.GetByCode(PageEntityDefinition.DefinitionCode);

            var dbQuery = _dbContext
                          .PageVersionModules
                          .AsNoTracking()
                          .Where(m => query.PageVersionModuleIds.Contains(m.PageVersionModuleId))
                          .Select(m => new ChildEntityMicroSummary()
            {
                ChildEntityId        = m.PageVersionModuleId,
                RootEntityId         = m.PageVersion.PageId,
                RootEntityTitle      = m.PageVersion.Title,
                EntityDefinitionCode = definition.EntityDefinitionCode,
                EntityDefinitionName = definition.Name,
                IsPreviousVersion    = m.PageVersion.WorkFlowStatusId != (int)WorkFlowStatus.Published || m.PageVersion.WorkFlowStatusId != (int)WorkFlowStatus.Draft
            });

            return(dbQuery);
        }
Ejemplo n.º 12
0
        private IQueryable <RootEntityMicroSummary> Query(GetPageEntityMicroSummariesByIdRangeQuery query, IExecutionContext executionContext)
        {
            var definition = _entityDefinitionRepository.GetByCode(PageEntityDefinition.DefinitionCode);

            var dbQuery = _dbContext
                          .PagePublishStatusQueries
                          .AsNoTracking()
                          .FilterActive()
                          .FilterByStatus(PublishStatusQuery.Latest, executionContext.ExecutionDate)
                          .Where(q => query.PageIds.Contains(q.PageId))
                          .Select(q => q.PageVersion)
                          .Select(v => new RootEntityMicroSummary()
            {
                RootEntityId         = v.PageId,
                RootEntityTitle      = v.Title,
                EntityDefinitionName = definition.Name,
                EntityDefinitionCode = definition.EntityDefinitionCode
            });

            return(dbQuery);
        }