public async Task <UpdateCustomEntityDraftVersionCommand> ExecuteAsync(GetUpdateCommandByIdQuery <UpdateCustomEntityDraftVersionCommand> query, IExecutionContext executionContext)
        {
            var dbResult = await _dbContext
                           .CustomEntityVersions
                           .Include(v => v.CustomEntity)
                           .AsNoTracking()
                           .FilterActive()
                           .FilterByCustomEntityId(query.Id)
                           .Where(v => v.WorkFlowStatusId == (int)WorkFlowStatus.Draft)
                           .SingleOrDefaultAsync();

            if (dbResult == null)
            {
                return(null);
            }
            _permissionValidationService.EnforceCustomEntityPermission <CustomEntityReadPermission>(dbResult.CustomEntity.CustomEntityDefinitionCode, executionContext.UserContext);

            var command = new UpdateCustomEntityDraftVersionCommand()
            {
                CustomEntityDefinitionCode = dbResult.CustomEntity.CustomEntityDefinitionCode,
                CustomEntityId             = dbResult.CustomEntityId,
                Title = dbResult.Title
            };

            var definitionQuery = new GetCustomEntityDefinitionSummaryByCodeQuery(command.CustomEntityDefinitionCode);
            var definition      = await _queryExecutor.ExecuteAsync(definitionQuery, executionContext);

            EntityNotFoundException.ThrowIfNull(definition, command.CustomEntityDefinitionCode);
            command.Model = (ICustomEntityDataModel)_dbUnstructuredDataSerializer.Deserialize(dbResult.SerializedData, definition.DataModelType);

            return(command);
        }
Exemplo n.º 2
0
        public async Task ExecuteAsync(UpdateCustomEntityUrlCommand command, IExecutionContext executionContext)
        {
            var entity = await _dbContext
                         .CustomEntities
                         .Where(e => e.CustomEntityId == command.CustomEntityId)
                         .SingleOrDefaultAsync();

            EntityNotFoundException.ThrowIfNull(entity, command.CustomEntityId);
            _permissionValidationService.EnforceCustomEntityPermission <CustomEntityUpdateUrlPermission>(entity.CustomEntityDefinitionCode, executionContext.UserContext);

            var definitionQuery = new GetCustomEntityDefinitionSummaryByCodeQuery(entity.CustomEntityDefinitionCode);
            var definition      = await _queryExecutor.ExecuteAsync(definitionQuery, executionContext);

            EntityNotFoundException.ThrowIfNull(definition, entity.CustomEntityDefinitionCode);

            await ValidateIsUniqueAsync(command, definition, executionContext);

            Map(command, entity, definition);

            await _dbContext.SaveChangesAsync();

            _customEntityCache.Clear(entity.CustomEntityDefinitionCode, command.CustomEntityId);

            await _messageAggregator.PublishAsync(new CustomEntityUrlChangedMessage()
            {
                CustomEntityId             = command.CustomEntityId,
                CustomEntityDefinitionCode = entity.CustomEntityDefinitionCode,
                HasPublishedVersionChanged = entity.PublishStatusCode == PublishStatusCode.Published
            });
        }
Exemplo n.º 3
0
        private async Task <CustomEntityDefinitionSummary> GetDefinitionAsync(
            IsCustomEntityUrlSlugUniqueQuery query,
            IExecutionContext executionContext
            )
        {
            var definitionQuery = new GetCustomEntityDefinitionSummaryByCodeQuery(query.CustomEntityDefinitionCode);
            var definition      = await _queryExecutor.ExecuteAsync(definitionQuery, executionContext);

            EntityNotFoundException.ThrowIfNull(definition, query.CustomEntityDefinitionCode);

            return(definition);
        }
Exemplo n.º 4
0
        private async Task MapDataModelAsync(
            GetCustomEntityDetailsByIdQuery query,
            CustomEntityVersion dbVersion,
            CustomEntityVersionDetails version,
            IExecutionContext executionContext
            )
        {
            var definitionQuery = new GetCustomEntityDefinitionSummaryByCodeQuery(dbVersion.CustomEntity.CustomEntityDefinitionCode);
            var definition      = await _queryExecutor.ExecuteAsync(definitionQuery, executionContext);

            EntityNotFoundException.ThrowIfNull(definition, dbVersion.CustomEntity.CustomEntityDefinitionCode);

            version.Model = (ICustomEntityDataModel)_dbUnstructuredDataSerializer.Deserialize(dbVersion.SerializedData, definition.DataModelType);
        }
Exemplo n.º 5
0
        public async Task <PagedQueryResult <CustomEntitySummary> > ExecuteAsync(SearchCustomEntitySummariesQuery query, IExecutionContext executionContext)
        {
            var definitionQuery = new GetCustomEntityDefinitionSummaryByCodeQuery(query.CustomEntityDefinitionCode);
            var definition      = await _queryExecutor.ExecuteAsync(definitionQuery);

            EntityNotFoundException.ThrowIfNull(definition, query.CustomEntityDefinitionCode);

            // Get Main Query
            var dbPagedResult = await RunQueryAsync(query, definition, executionContext);

            var mappedResult = await _customEntitySummaryMapper.MapAsync(dbPagedResult.Items, executionContext);

            return(dbPagedResult.ChangeType(mappedResult));
        }
Exemplo n.º 6
0
        public async Task <CustomEntityDataModelSchema> ExecuteAsync(GetCustomEntityDataModelSchemaDetailsByDefinitionCodeQuery query, IExecutionContext executionContext)
        {
            var definitionQuery = new GetCustomEntityDefinitionSummaryByCodeQuery(query.CustomEntityDefinitionCode);
            var definition      = await _queryExecutor.ExecuteAsync(definitionQuery);

            if (definition == null)
            {
                return(null);
            }

            var result = new CustomEntityDataModelSchema();

            _dynamicDataModelTypeMapper.Map(result, definition.DataModelType);

            return(result);
        }
Exemplo n.º 7
0
        public async Task ExecuteAsync(AddCustomEntityCommand command, IExecutionContext executionContext)
        {
            var definitionQuery = new GetCustomEntityDefinitionSummaryByCodeQuery(command.CustomEntityDefinitionCode);
            var definition      = await _queryExecutor.ExecuteAsync(definitionQuery, executionContext);

            EntityNotFoundException.ThrowIfNull(definition, command.CustomEntityDefinitionCode);

            await _commandExecutor.ExecuteAsync(new EnsureCustomEntityDefinitionExistsCommand(definition.CustomEntityDefinitionCode));

            // Custom Validation
            ValidateCommand(command, definition);
            await ValidateIsUniqueAsync(command, definition);

            var entity = MapEntity(command, definition, executionContext);

            _dbContext.CustomEntities.Add(entity);

            using (var scope = _transactionScopeFactory.Create(_dbContext))
            {
                await _dbContext.SaveChangesAsync();

                var dependencyCommand = new UpdateUnstructuredDataDependenciesCommand(
                    CustomEntityVersionEntityDefinition.DefinitionCode,
                    entity.CustomEntityVersions.First().CustomEntityVersionId,
                    command.Model);

                await _commandExecutor.ExecuteAsync(dependencyCommand);

                await _customEntityStoredProcedures.UpdatePublishStatusQueryLookupAsync(entity.CustomEntityId);

                scope.Complete();
            }

            _customEntityCache.ClearRoutes(definition.CustomEntityDefinitionCode);

            // Set Ouput
            command.OutputCustomEntityId = entity.CustomEntityId;

            await _messageAggregator.PublishAsync(new CustomEntityAddedMessage()
            {
                CustomEntityId             = entity.CustomEntityId,
                CustomEntityDefinitionCode = definition.CustomEntityDefinitionCode,
                HasPublishedVersionChanged = command.Publish
            });
        }
Exemplo n.º 8
0
        public async Task ExecuteAsync(UpdateCustomEntityUrlCommand command, IExecutionContext executionContext)
        {
            var entity = await _dbContext
                         .CustomEntities
                         .Where(e => e.CustomEntityId == command.CustomEntityId)
                         .SingleOrDefaultAsync();

            EntityNotFoundException.ThrowIfNull(entity, command.CustomEntityId);
            _permissionValidationService.EnforceCustomEntityPermission <CustomEntityUpdateUrlPermission>(entity.CustomEntityDefinitionCode, executionContext.UserContext);

            var definitionQuery = new GetCustomEntityDefinitionSummaryByCodeQuery(entity.CustomEntityDefinitionCode);
            var definition      = await _queryExecutor.ExecuteAsync(definitionQuery, executionContext);

            EntityNotFoundException.ThrowIfNull(definition, entity.CustomEntityDefinitionCode);

            await ValidateIsUniqueAsync(command, definition, executionContext);

            Map(command, entity, definition);

            await _dbContext.SaveChangesAsync();

            await _transactionScopeFactory.QueueCompletionTaskAsync(_dbContext, () => OnTransactionComplete(entity));
        }
Exemplo n.º 9
0
        /// <summary>
        /// Maps a collection of EF CustomEntityVersion records from the db into CustomEntitySummary
        /// objects.
        /// </summary>
        /// <param name="dbStatusQueries">Collection of versions to map.</param>
        public async Task <List <CustomEntitySummary> > MapAsync(ICollection <CustomEntityPublishStatusQuery> dbStatusQueries, IExecutionContext executionContext)
        {
            var entities      = new List <CustomEntitySummary>(dbStatusQueries.Count);
            var routingsQuery = new GetPageRoutingInfoByCustomEntityIdRangeQuery(dbStatusQueries.Select(e => e.CustomEntityId));
            var routings      = await _queryExecutor.ExecuteAsync(routingsQuery, executionContext);

            Dictionary <int, ActiveLocale> allLocales = null;
            Dictionary <string, CustomEntityDefinitionSummary> customEntityDefinitions = new Dictionary <string, CustomEntityDefinitionSummary>();
            var hasCheckedQueryValid = false;

            foreach (var dbStatusQuery in dbStatusQueries)
            {
                // Validate the input data
                if (!hasCheckedQueryValid)
                {
                    ValidateQuery(dbStatusQuery);
                }
                hasCheckedQueryValid = true;

                // Easy mappings
                var entity = new CustomEntitySummary()
                {
                    AuditData = _auditDataMapper.MapUpdateAuditDataCreatorData(dbStatusQuery.CustomEntity),
                    CustomEntityDefinitionCode = dbStatusQuery.CustomEntity.CustomEntityDefinitionCode,
                    CustomEntityId             = dbStatusQuery.CustomEntityId,
                    HasDraft      = dbStatusQuery.CustomEntityVersion.WorkFlowStatusId == (int)WorkFlowStatus.Draft,
                    PublishStatus = PublishStatusMapper.FromCode(dbStatusQuery.CustomEntity.PublishStatusCode),
                    PublishDate   = DbDateTimeMapper.AsUtc(dbStatusQuery.CustomEntity.PublishDate),
                    Ordering      = dbStatusQuery.CustomEntity.Ordering,
                    Title         = dbStatusQuery.CustomEntityVersion.Title,
                    UrlSlug       = dbStatusQuery.CustomEntity.UrlSlug
                };

                entity.IsPublished = entity.PublishStatus == PublishStatus.Published && entity.PublishDate <= executionContext.ExecutionDate;
                _auditDataMapper.MapUpdateAuditDataUpdaterData(entity.AuditData, dbStatusQuery.CustomEntityVersion);


                // Routing data (if any)

                PageRoutingInfo detailsRouting = null;
                if (routings.ContainsKey(dbStatusQuery.CustomEntityId))
                {
                    detailsRouting  = routings[dbStatusQuery.CustomEntityId].FirstOrDefault(r => r.CustomEntityRouteRule != null);
                    entity.FullPath = detailsRouting?.CustomEntityRouteRule?.MakeUrl(detailsRouting.PageRoute, detailsRouting.CustomEntityRoute);
                }

                // Locale data

                var localeId = dbStatusQuery.CustomEntity.LocaleId;
                if (localeId.HasValue && detailsRouting != null)
                {
                    entity.Locale = detailsRouting.PageRoute.Locale;
                    EntityNotFoundException.ThrowIfNull(entity.Locale, localeId.Value);
                }
                else if (localeId.HasValue)
                {
                    // Lazy load locales, since they aren't always used
                    if (allLocales == null)
                    {
                        allLocales = await GetLocalesAsync(executionContext);
                    }

                    entity.Locale = allLocales.GetOrDefault(localeId.Value);
                    EntityNotFoundException.ThrowIfNull(entity.Locale, localeId.Value);
                }

                // Parse model data
                var definition = customEntityDefinitions.GetOrDefault(dbStatusQuery.CustomEntity.CustomEntityDefinitionCode);
                if (definition == null)
                {
                    // Load and cache definitions
                    var definitionQuery = new GetCustomEntityDefinitionSummaryByCodeQuery(dbStatusQuery.CustomEntity.CustomEntityDefinitionCode);
                    definition = await _queryExecutor.ExecuteAsync(definitionQuery, executionContext);

                    EntityNotFoundException.ThrowIfNull(definition, definition.CustomEntityDefinitionCode);
                    customEntityDefinitions.Add(dbStatusQuery.CustomEntity.CustomEntityDefinitionCode, definition);
                }

                entity.Model = (ICustomEntityDataModel)_dbUnstructuredDataSerializer.Deserialize(dbStatusQuery.CustomEntityVersion.SerializedData, definition.DataModelType);

                entities.Add(entity);
            }

            return(entities);
        }
Exemplo n.º 10
0
        private async Task <PagedQueryResult <CustomEntityVersion> > GetQueryAsync(SearchCustomEntityRenderSummariesQuery query, IExecutionContext executionContext)
        {
            var definitionQuery = new GetCustomEntityDefinitionSummaryByCodeQuery(query.CustomEntityDefinitionCode);
            var definition      = await _queryExecutor.ExecuteAsync(definitionQuery, executionContext);

            EntityNotFoundException.ThrowIfNull(definition, query.CustomEntityDefinitionCode);

            var dbQuery = _dbContext
                          .CustomEntityPublishStatusQueries
                          .AsNoTracking()
                          .Include(e => e.CustomEntityVersion)
                          .ThenInclude(e => e.CustomEntity)
                          .FilterByCustomEntityDefinitionCode(query.CustomEntityDefinitionCode)
                          .FilterByActive()
                          .FilterByStatus(query.PublishStatus, executionContext.ExecutionDate);

            // Filter by locale
            if (query.LocaleId > 0)
            {
                dbQuery = dbQuery.Where(p => p.CustomEntity.LocaleId == query.LocaleId);
            }
            else
            {
                dbQuery = dbQuery.Where(p => !p.CustomEntity.LocaleId.HasValue);
            }

            switch (query.SortBy)
            {
            case CustomEntityQuerySortType.Default:
            case CustomEntityQuerySortType.Natural:
                if (definition.Ordering != CustomEntityOrdering.None)
                {
                    dbQuery = dbQuery
                              .OrderByWithSortDirection(e => !e.CustomEntity.Ordering.HasValue, query.SortDirection)
                              .ThenByWithSortDirection(e => e.CustomEntity.Ordering, query.SortDirection)
                              .ThenByDescendingWithSortDirection(e => e.CustomEntity.CreateDate, query.SortDirection);
                }
                else
                {
                    dbQuery = dbQuery
                              .OrderByDescendingWithSortDirection(e => e.CustomEntity.CreateDate, query.SortDirection);
                }
                break;

            case CustomEntityQuerySortType.Title:
                dbQuery = dbQuery
                          .OrderByWithSortDirection(e => e.CustomEntityVersion.Title, query.SortDirection);
                break;

            case CustomEntityQuerySortType.CreateDate:
                dbQuery = dbQuery
                          .OrderByDescendingWithSortDirection(e => e.CustomEntity.CreateDate, query.SortDirection);
                break;

            case CustomEntityQuerySortType.PublishDate:
                dbQuery = dbQuery
                          .OrderByDescendingWithSortDirection(e => e.CustomEntity.PublishDate.HasValue, query.SortDirection)
                          .ThenByDescendingWithSortDirection(e => e.CustomEntity.PublishDate, query.SortDirection)
                          .ThenByDescendingWithSortDirection(e => e.CustomEntity.CreateDate, query.SortDirection)
                ;
                break;
            }

            var dbPagedResult = await dbQuery.ToPagedResultAsync(query);

            // EF doesnt support includes after select so need to run this post execution
            var results = dbPagedResult
                          .Items
                          .Select(p => p.CustomEntityVersion);

            return(dbPagedResult.ChangeType(results));
        }
        public IContentRepositoryQueryContext <CustomEntityDefinitionSummary> AsSummary()
        {
            var query = new GetCustomEntityDefinitionSummaryByCodeQuery(_customEntityDefinitionCode);

            return(ContentRepositoryQueryContextFactory.Create(query, ExtendableContentRepository));
        }
        /// <summary>
        /// Maps a collection of EF CustomEntityPublishStatusQuery records from the db
        /// into CustomEntitySummary objects. The records must include data for the the
        /// CustomEntity, CustomEntityVersion, CustomEntity.Creator and CustomEntityVersion.Creator
        /// properties.
        /// </summary>
        /// <param name="dbCustomEntities">Collection of CustomEntityPublishStatusQuery records to map.</param>
        /// <param name="executionContext">Execution context to pass down when executing child queries.</param>
        public async Task <List <CustomEntitySummary> > MapAsync(ICollection <CustomEntityPublishStatusQuery> dbCustomEntities, IExecutionContext executionContext)
        {
            var entities      = new List <CustomEntitySummary>(dbCustomEntities.Count);
            var routingsQuery = new GetPageRoutingInfoByCustomEntityIdRangeQuery(dbCustomEntities.Select(e => e.CustomEntityId));
            var routings      = await _queryExecutor.ExecuteAsync(routingsQuery, executionContext);

            Dictionary <int, ActiveLocale> allLocales = null;
            var customEntityDefinitions = new Dictionary <string, CustomEntityDefinitionSummary>();
            var hasCheckedQueryValid    = false;

            foreach (var dbCustomEntity in dbCustomEntities)
            {
                // Validate the input data
                if (!hasCheckedQueryValid)
                {
                    ValidateQuery(dbCustomEntity);
                }
                hasCheckedQueryValid = true;

                // Easy mappings
                var entity = MapBasicProperties(dbCustomEntity);

                // Routing data (if any)
                var detailsRouting = FindRoutingData(routings, dbCustomEntity);

                if (detailsRouting != null)
                {
                    entity.FullPath            = detailsRouting.CustomEntityRouteRule.MakeUrl(detailsRouting.PageRoute, detailsRouting.CustomEntityRoute);
                    entity.HasPublishedVersion = detailsRouting.CustomEntityRoute.HasPublishedVersion;
                }

                // Locale data

                var localeId = dbCustomEntity.CustomEntity.LocaleId;
                if (localeId.HasValue && detailsRouting != null)
                {
                    entity.Locale = detailsRouting.PageRoute.Locale;
                    EntityNotFoundException.ThrowIfNull(entity.Locale, localeId.Value);
                }
                else if (localeId.HasValue)
                {
                    // Lazy load locales, since they aren't always used
                    if (allLocales == null)
                    {
                        allLocales = await GetLocalesAsync(executionContext);
                    }

                    entity.Locale = allLocales.GetOrDefault(localeId.Value);
                    EntityNotFoundException.ThrowIfNull(entity.Locale, localeId.Value);
                }

                // Parse model data
                var definition = customEntityDefinitions.GetOrDefault(dbCustomEntity.CustomEntity.CustomEntityDefinitionCode);
                if (definition == null)
                {
                    // Load and cache definitions
                    var definitionQuery = new GetCustomEntityDefinitionSummaryByCodeQuery(dbCustomEntity.CustomEntity.CustomEntityDefinitionCode);
                    definition = await _queryExecutor.ExecuteAsync(definitionQuery, executionContext);

                    EntityNotFoundException.ThrowIfNull(definition, definition.CustomEntityDefinitionCode);
                    customEntityDefinitions.Add(dbCustomEntity.CustomEntity.CustomEntityDefinitionCode, definition);
                }

                entity.Model = (ICustomEntityDataModel)_dbUnstructuredDataSerializer.Deserialize(dbCustomEntity.CustomEntityVersion.SerializedData, definition.DataModelType);

                entities.Add(entity);
            }

            await EnsureHasPublishedVersionSet(entities);

            return(entities);
        }
        public Task <CustomEntityDefinitionSummary> AsSummaryAsync()
        {
            var query = new GetCustomEntityDefinitionSummaryByCodeQuery(_customEntityDefinitionCode);

            return(ExtendableContentRepository.ExecuteQueryAsync(query));
        }