public async Task <ICollection <EntityDependencySummary> > ExecuteAsync(GetEntityDependencySummaryByRelatedEntityIdRangeQuery query, IExecutionContext executionContext) { var dbQuery = _dbContext .UnstructuredDataDependencies .AsNoTracking() .FilterByRelatedEntity(query.EntityDefinitionCode, query.EntityIds); if (query.ExcludeDeletable) { dbQuery = dbQuery.Where(r => r.RelatedEntityCascadeActionId == (int)RelatedEntityCascadeAction.None); } // Groupby still not suppored in EF 3.1 ¯\_(ツ)_/¯ var queryResult = await dbQuery.ToListAsync(); var dbDependencyGroups = queryResult .GroupBy(r => r.RootEntityDefinitionCode) .ToList(); var allRelatedEntities = new List <EntityDependencySummary>(); foreach (var dbDependencyGroup in dbDependencyGroups) { var definition = _entityDefinitionRepository.GetRequiredByCode(dbDependencyGroup.Key) as IDependableEntityDefinition; EntityNotFoundException.ThrowIfNull(definition, dbDependencyGroup.Key); var 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); // relations for previous versions can be removed even when they are required. var canDelete = dbDependency.RelatedEntityCascadeActionId != (int)RelatedEntityCascadeAction.None || entityMicroSummary.Value.IsPreviousVersion; if (query.ExcludeDeletable && canDelete) { continue; } allRelatedEntities.Add(new EntityDependencySummary() { Entity = entityMicroSummary.Value, CanDelete = canDelete }); } } // filter out duplicates, selecting the more restrictive entity first var results = allRelatedEntities .GroupBy(e => new { e.Entity.EntityDefinitionCode, e.Entity.RootEntityId }, (k, v) => v.OrderBy(e => e.CanDelete).First()) .ToList(); return(results); }
/// <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.GetRequiredByCode(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(); }
public async Task ValidateAsync(string entityDefinitionCode, int entityId, IExecutionContext executionContext) { if (string.IsNullOrWhiteSpace(entityDefinitionCode)) { throw new ArgumentEmptyException(nameof(entityDefinitionCode)); } if (entityId < 1) { throw new ArgumentOutOfRangeException(nameof(entityDefinitionCode), nameof(entityId) + "must be a positive integer."); } if (executionContext == null) { throw new ArgumentEmptyException(nameof(executionContext)); } var entityDefinition = _entityDefinitionRepository.GetRequiredByCode(entityDefinitionCode); var requiredDependencies = await _queryExecutor.ExecuteAsync(new GetEntityDependencySummaryByRelatedEntityIdQuery() { EntityDefinitionCode = entityDefinition.EntityDefinitionCode, EntityId = entityId, ExcludeDeletable = true }, executionContext); RequiredDependencyConstaintViolationException.ThrowIfCannotDelete(entityDefinition, requiredDependencies); }
public IEnumerable <IPermissionApplication> GetPermissions(UpdateUnstructuredDataDependenciesCommand command) { var entityDefinition = _entityDefinitionRepository.GetRequiredByCode(command.RootEntityDefinitionCode); // Try and get a delete permission for the root entity. var permission = _permissionRepository.GetByEntityAndPermissionType(entityDefinition, CommonPermissionTypes.Update("Entity")); if (permission != null) { yield return(permission); } }
private IQueryable <RootEntityMicroSummary> Query(GetDocumentAssetEntityMicroSummariesByIdRangeQuery query) { var definition = _entityDefinitionRepository.GetRequiredByCode(DocumentAssetEntityDefinition.DefinitionCode); var dbQuery = _dbContext .DocumentAssets .AsNoTracking() .FilterByIds(query.DocumentAssetIds) .Select(a => new RootEntityMicroSummary() { RootEntityId = a.DocumentAssetId, RootEntityTitle = a.Title, EntityDefinitionCode = definition.EntityDefinitionCode, EntityDefinitionName = definition.Name }); return(dbQuery); }
public async Task <IDictionary <int, RootEntityMicroSummary> > ExecuteAsync(GetPageDirectoryEntityMicroSummariesByIdRangeQuery query, IExecutionContext executionContext) { var definition = _entityDefinitionRepository.GetRequiredByCode(PageDirectoryEntityDefinition.DefinitionCode); var results = await _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 }) .ToDictionaryAsync(e => e.RootEntityId); return(results); }
public async Task ExecuteAsync(EnsureEntityDefinitionExistsCommand command, IExecutionContext executionContext) { var entityDefinition = _entityDefinitionRepository.GetRequiredByCode(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.GetRequiredByCode(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); }
private IQueryable <ChildEntityMicroSummary> Query(GetPageVersionBlockEntityMicroSummariesByIdRangeQuery query) { var definition = _entityDefinitionRepository.GetRequiredByCode(PageEntityDefinition.DefinitionCode); var dbQuery = _dbContext .PageVersionBlocks .AsNoTracking() .FilterActive() .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); }
public async Task <IDictionary <int, RootEntityMicroSummary> > ExecuteAsync(GetPageEntityMicroSummariesByIdRangeQuery query, IExecutionContext executionContext) { var definition = _entityDefinitionRepository.GetRequiredByCode(PageEntityDefinition.DefinitionCode); var results = await _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 }) .ToDictionaryAsync(e => e.RootEntityId); return(results); }