Ejemplo n.º 1
0
        /// <summary>
        /// Maps a batch of the same type of page module data to a collection
        /// of display models ready for rendering.
        /// </summary>
        /// <param name="typeName">The module type name e.g. 'PlainText', 'RawHtml'.</param>
        /// <param name="versionModule">The version data to get the serialized model from.</param>
        /// <param name="workflowStatus">
        /// The workflow status of the parent page or custom entity
        /// being mapped. This is provided so dependent entities can use
        /// the same workflow status.
        /// </param>
        /// <returns>
        /// Collection of mapped display models, wrapped in an output class that
        /// can be used to identify them.
        /// </returns>
        public List <PageModuleDisplayModelMapperOutput> MapDisplayModel(
            string typeName,
            IEnumerable <IEntityVersionPageModule> versionModules,
            WorkFlowStatusQuery workflowStatus
            )
        {
            // Find the data-provider class for this type of module
            Type modelType = _moduleDataModelTypeFactory.CreateByPageModuleTypeFileName(typeName);

            if (typeof(IPageModuleDisplayModel).IsAssignableFrom(modelType))
            {
                // We can serialize directly to the display model
                var displayModels = new List <PageModuleDisplayModelMapperOutput>();
                foreach (var pageModule in versionModules)
                {
                    var mapperModel = new PageModuleDisplayModelMapperOutput();
                    mapperModel.DisplayModel    = (IPageModuleDisplayModel)_dbUnstructuredDataSerializer.Deserialize(pageModule.SerializedData, modelType);
                    mapperModel.VersionModuleId = pageModule.GetVersionModuleId();
                    displayModels.Add(mapperModel);
                }

                return(displayModels);
            }
            else
            {
                var moduleWorkflowStatus = TranslateWorkFlowStatusForModules(workflowStatus);

                // We have to use a mapping class to do some custom mapping
                var displayModels = (List <PageModuleDisplayModelMapperOutput>)_mapGenericMethod
                                    .MakeGenericMethod(modelType)
                                    .Invoke(this, new object[] { versionModules, moduleWorkflowStatus });

                return(displayModels);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Maps a batch of the same type of page block data to a collection
        /// of display models ready for rendering.
        /// </summary>
        /// <param name="typeName">The block type name e.g. 'PlainText', 'RawHtml'.</param>
        /// <param name="pageBlocks">The version data to get the serialized model from.</param>
        /// <param name="publishStatus">
        /// The publish status of the parent page or custom entity
        /// being mapped. This is provided so dependent entities can use
        /// the same publish status.
        /// </param>
        /// <returns>
        /// Collection of mapped display models, wrapped in an output class that
        /// can be used to identify them.
        /// </returns>
        public async Task <ICollection <PageBlockTypeDisplayModelMapperOutput> > MapDisplayModelAsync(
            string typeName,
            IEnumerable <IEntityVersionPageBlock> pageBlocks,
            PublishStatusQuery publishStatus
            )
        {
            // Find the data-provider class for this block type
            Type modelType = _pageBlockDataModelTypeFactory.CreateByPageBlockTypeFileName(typeName);

            if (typeof(IPageBlockTypeDisplayModel).IsAssignableFrom(modelType))
            {
                // We can serialize directly to the display model
                var displayModels = new List <PageBlockTypeDisplayModelMapperOutput>();
                foreach (var pageBlock in pageBlocks)
                {
                    var mapperModel = new PageBlockTypeDisplayModelMapperOutput();
                    mapperModel.DisplayModel   = (IPageBlockTypeDisplayModel)_dbUnstructuredDataSerializer.Deserialize(pageBlock.SerializedData, modelType);
                    mapperModel.VersionBlockId = pageBlock.GetVersionBlockId();
                    displayModels.Add(mapperModel);
                }

                return(displayModels);
            }
            else
            {
                var blockWorkflowStatus = publishStatus.ToRelatedEntityQueryStatus();

                // We have to use a mapping class to do some custom mapping
                var displayModels = (Task <List <PageBlockTypeDisplayModelMapperOutput> >)_mapGenericMethod
                                    .MakeGenericMethod(modelType)
                                    .Invoke(this, new object[] { pageBlocks, blockWorkflowStatus });

                return(await displayModels);
            }
        }
        public async Task <UpdateCustomEntityDraftVersionCommand> ExecuteAsync(GetPatchableCommandByIdQuery <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);
        }
        private void MapDataModel(GetByIdQuery <CustomEntityDetails> query, CustomEntityVersion dbVersion, CustomEntityVersionDetails version)
        {
            var definition = _queryExecutor.GetById <CustomEntityDefinitionSummary>(dbVersion.CustomEntity.CustomEntityDefinitionCode);

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

            version.Model = (ICustomEntityVersionDataModel)_dbUnstructuredDataSerializer.Deserialize(dbVersion.SerializedData, definition.DataModelType);
        }
        public ICustomEntityDataModel Map(string customEntityDefinitionCode, string serializedData)
        {
            var definition = _customEntityDefinitions.SingleOrDefault(d => d.CustomEntityDefinitionCode == customEntityDefinitionCode);

            EntityNotFoundException.ThrowIfNull(definition, customEntityDefinitionCode);

            var dataModelType = definition.GetDataModelType();

            return((ICustomEntityDataModel)_dbUnstructuredDataSerializer.Deserialize(serializedData, dataModelType));
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Maps a batch of the same type of page block data to a collection
        /// of display models ready for rendering.
        /// </summary>
        /// <param name="typeName">The block type name e.g. 'PlainText', 'RawHtml'.</param>
        /// <param name="entityBlocks">The version data to get the serialized model from.</param>
        /// <param name="publishStatus">
        /// The publish status of the parent page or custom entity
        /// being mapped. This is provided so dependent entities can use
        /// the same publish status.
        /// </param>
        /// <param name="executionContext">
        /// The execution context from the caller which can be used in
        /// any child queries to ensure any elevated permissions are
        /// passed down the chain of execution.
        /// </param>
        /// <returns>
        /// Dictionary of mapped display models, with a key (block version id) that can be
        /// used to identify them.
        /// </returns>
        public virtual async Task <IReadOnlyDictionary <int, IPageBlockTypeDisplayModel> > MapDisplayModelAsync(
            string typeName,
            IEnumerable <IEntityVersionPageBlock> entityBlocks,
            PublishStatusQuery publishStatus,
            IExecutionContext executionContext
            )
        {
            // Find the data-provider class for this block type
            Type modelType = _pageBlockDataModelTypeFactory.CreateByPageBlockTypeFileName(typeName);

            if (typeof(IPageBlockTypeDisplayModel).IsAssignableFrom(modelType))
            {
                // We can serialize directly to the display model
                var displayModels = new Dictionary <int, IPageBlockTypeDisplayModel>();
                foreach (var pageBlock in entityBlocks)
                {
                    var displayModel   = (IPageBlockTypeDisplayModel)_dbUnstructuredDataSerializer.Deserialize(pageBlock.SerializedData, modelType);
                    var versionBlockId = pageBlock.GetVersionBlockId();

                    if (displayModels.ContainsKey(versionBlockId))
                    {
                        throw new Exception($"A block with a version id of {versionBlockId} has already been added to the mapping collection.");
                    }

                    displayModels.Add(versionBlockId, displayModel);
                }

                return(displayModels);
            }
            else
            {
                var blockWorkflowStatus = publishStatus.ToRelatedEntityQueryStatus();

                // We have to use a mapping class to do some custom mapping
                var displayModels = (Task <IReadOnlyDictionary <int, IPageBlockTypeDisplayModel> >)_mapGenericMethod
                                    .MakeGenericMethod(modelType)
                                    .Invoke(this, new object[] { entityBlocks, blockWorkflowStatus, executionContext });

                return(await displayModels);
            }
        }
Ejemplo n.º 7
0
        public async Task <PagedQueryResult <CustomEntitySummary> > ExecuteAsync(SearchCustomEntitySummariesQuery query, IExecutionContext executionContext)
        {
            var definition = _queryExecutor.GetById <CustomEntityDefinitionSummary>(query.CustomEntityDefinitionCode);

            EntityNotFoundException.ThrowIfNull(definition, query.CustomEntityDefinitionCode);

            // Get Query
            var dbQuery = GetQuery(query, definition);

            // Execute Query
            var dbPagedResult = dbQuery.ToPagedResult(query);

            var routingsQuery = new GetPageRoutingInfoByCustomEntityIdRangeQuery(dbPagedResult.Items.Select(e => e.CustomEntityId));
            var routings      = await _queryExecutor.ExecuteAsync(routingsQuery);

            var allLocales = (await _queryExecutor.GetAllAsync <ActiveLocale>()).ToDictionary(l => l.LocaleId);

            // Map Items
            var entities = new List <CustomEntitySummary>(dbPagedResult.Items.Length);

            foreach (var dbVersion in dbPagedResult.Items)
            {
                PageRoutingInfo detailsRouting = null;

                if (routings.ContainsKey(dbVersion.CustomEntityId))
                {
                    detailsRouting = routings[dbVersion.CustomEntityId].FirstOrDefault(r => r.CustomEntityRouteRule != null);
                }

                var entity = Mapper.Map <CustomEntitySummary>(dbVersion);

                if (dbVersion.LocaleId.HasValue)
                {
                    entity.Locale = allLocales.GetOrDefault(dbVersion.LocaleId.Value);
                    EntityNotFoundException.ThrowIfNull(entity.Locale, dbVersion.LocaleId.Value);
                }

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

                entity.Model = (ICustomEntityVersionDataModel)_dbUnstructuredDataSerializer.Deserialize(dbVersion.SerializedData, definition.DataModelType);

                entity.AuditData.UpdateDate = dbVersion.VersionAuditData.CreateDate;
                entity.AuditData.Updater    = dbVersion.VersionAuditData.Creator;
                entities.Add(entity);
            }

            return(dbPagedResult.ChangeType(entities));
        }
Ejemplo n.º 8
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);
        }
Ejemplo n.º 9
0
        public T FindSetting <T>(string key, Dictionary <string, string> allSettings)
        {
            if (!allSettings.ContainsKey(key))
            {
                return(default(T));
            }
            var setting = allSettings[key];

            if (string.IsNullOrWhiteSpace(setting))
            {
                return(default(T));
            }

            var value = _dbUnstructuredDataSerializer.Deserialize <T>(setting);

            return(value);
        }
Ejemplo n.º 10
0
        public async Task <UpdateCustomEntityDraftVersionCommand> ExecuteAsync(GetByIdQuery <UpdateCustomEntityDraftVersionCommand> query, IExecutionContext executionContext)
        {
            var dbResult = await _dbContext
                           .CustomEntityVersions
                           .Include(v => v.CustomEntity)
                           .AsNoTracking()
                           .Where(v => v.CustomEntityId == query.Id && v.WorkFlowStatusId == (int)WorkFlowStatus.Draft)
                           .SingleOrDefaultAsync();

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

            var command    = Mapper.Map <UpdateCustomEntityDraftVersionCommand>(dbResult);
            var definition = _queryExecutor.GetById <CustomEntityDefinitionSummary>(command.CustomEntityDefinitionCode);

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

            return(command);
        }
Ejemplo n.º 11
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);
        }
Ejemplo n.º 12
0
        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.FullUrlPath         = 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);
        }