Example #1
0
        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);
        }
        public async Task <ICollection <EntityDependencySummary> > ExecuteAsync(GetEntityDependencySummaryByRelatedEntityIdQuery query, IExecutionContext executionContext)
        {
            // Query and permissions are delegated to the GetByIdRange version
            var delegateQuery = new GetEntityDependencySummaryByRelatedEntityIdRangeQuery(query.EntityDefinitionCode, new int[] { query.EntityId })
            {
                ExcludeDeletable = query.ExcludeDeletable
            };

            var results = await _queryExecutor.ExecuteAsync(delegateQuery, executionContext);

            return(results);
        }
Example #3
0
        public IEnumerable <IPermissionApplication> GetPermissions(GetEntityDependencySummaryByRelatedEntityIdRangeQuery query)
        {
            var entityDefinition = _entityDefinitionRepository.GetRequiredByCode(query.EntityDefinitionCode);

            if (entityDefinition == null)
            {
                yield break;
            }

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

            if (permission != null)
            {
                yield return(permission);
            }
        }