/// <summary> /// Deserialized a page block data model to a stongly typed model. /// </summary> /// <param name="typeName">The block type name e.g. 'PlainText', 'RawHtml'.</param> /// <param name="pageBlock">The version data to get the serialized model from.</param> /// <returns>Strongly typed data model including deserialized data.</returns> public IPageBlockTypeDataModel MapDataModel(string typeName, IEntityVersionPageBlock pageBlock) { Type modelType = _pageBlockDataModelTypeFactory.CreateByPageBlockTypeFileName(typeName); var model = (IPageBlockTypeDataModel)_dbUnstructuredDataSerializer.Deserialize(pageBlock.SerializedData, modelType); return(model); }
public async Task UpdateModelAsync( IPageVersionBlockDataModelCommand command, IEntityVersionPageBlock dbBlock ) { if (command.PageBlockTypeId != dbBlock.PageBlockTypeId) { var pageTemplateRegionId = dbBlock.PageTemplateRegion != null ? dbBlock.PageTemplateRegion.PageTemplateRegionId : dbBlock.PageTemplateRegionId; var pageBlockType = await _dbContext .PageBlockTypes .Where(m => m.PageBlockTypeId == command.PageBlockTypeId) .SingleOrDefaultAsync(); EntityNotFoundException.ThrowIfNull(pageBlockType, command.PageBlockTypeId); dbBlock.PageBlockType = pageBlockType; } dbBlock.SerializedData = _dbUnstructuredDataSerializer.Serialize(command.DataModel); if (command.PageBlockTypeTemplateId != dbBlock.PageBlockTypeTemplateId && command.PageBlockTypeTemplateId.HasValue) { dbBlock.PageBlockTypeTemplate = await _dbContext .PageBlockTypeTemplates .SingleOrDefaultAsync(m => m.PageBlockTypeId == command.PageBlockTypeId && m.PageBlockTypeTemplateId == command.PageBlockTypeTemplateId); EntityNotFoundException.ThrowIfNull(dbBlock.PageBlockTypeTemplate, command.PageBlockTypeTemplateId); } else if (command.PageBlockTypeTemplateId != dbBlock.PageBlockTypeTemplateId) { dbBlock.PageBlockTypeTemplate = null; } }
/// <summary> /// Locates and returns the correct templates for a block if it a custom template /// assigned, otherwise null is returned. /// </summary> /// <param name="pageBlock">An unmapped database block to locate the template for.</param> /// <param name="blockType">The block type associated with the block in which to look for the template.</param> public PageBlockTypeTemplateSummary GetCustomTemplate(IEntityVersionPageBlock pageBlock, PageBlockTypeSummary blockType) { if (pageBlock == null) { throw new ArgumentNullException(nameof(pageBlock)); } if (blockType == null) { throw new ArgumentNullException(nameof(blockType)); } if (!pageBlock.PageBlockTypeTemplateId.HasValue) { return(null); } var template = blockType .Templates .FirstOrDefault(t => t.PageBlockTypeTemplateId == pageBlock.PageBlockTypeTemplateId); if (template == null) { _logger.LogDebug("The block template with id {PageBlockTypeTemplateId} could not be found for {PageBlockType} {VersionBlockId}", pageBlock.PageBlockTypeTemplateId, pageBlock.GetType().Name, pageBlock.GetVersionBlockId()); } return(template); }
/// <summary> /// Maps a single page block data model to a concrete /// display model. /// </summary> /// <param name="typeName">The block type name e.g. 'PlainText', 'RawHtml'.</param> /// <param name="pageBlock">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>Mapped display model.</returns> public async Task <IPageBlockTypeDisplayModel> MapDisplayModelAsync( string typeName, IEntityVersionPageBlock pageBlock, PublishStatusQuery publishStatus ) { var mapped = await MapDisplayModelAsync(typeName, new IEntityVersionPageBlock[] { pageBlock }, publishStatus); return(mapped .Select(m => m.DisplayModel) .SingleOrDefault()); }
/// <summary> /// Maps a single page block data model to a concrete /// display model. /// </summary> /// <param name="typeName">The block type name e.g. 'PlainText', 'RawHtml'.</param> /// <param name="pageBlock">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>Mapped display model.</returns> public virtual async Task <IPageBlockTypeDisplayModel> MapDisplayModelAsync( string typeName, IEntityVersionPageBlock pageBlock, PublishStatusQuery publishStatus, IExecutionContext executionContext ) { var mapped = await MapDisplayModelAsync( typeName, new IEntityVersionPageBlock[] { pageBlock }, publishStatus, executionContext ); var id = pageBlock.GetVersionBlockId(); if (mapped.ContainsKey(id)) { return(mapped[id]); } return(null); }
/// <summary> /// Locates and returns the correct templates for a block if it a custom template /// assigned, otherwise null is returned. /// </summary> /// <param name="pageBlock">An unmapped database block to locate the template for.</param> /// <param name="blockType">The block type associated with the block in which to look for the template.</param> public PageBlockTypeTemplateSummary GetCustomTemplate(IEntityVersionPageBlock pageBlock, PageBlockTypeSummary blockType) { if (pageBlock == null) { throw new ArgumentNullException(nameof(pageBlock)); } if (blockType == null) { throw new ArgumentNullException(nameof(blockType)); } if (!pageBlock.PageBlockTypeTemplateId.HasValue) { return(null); } var template = blockType .Templates .FirstOrDefault(t => t.PageBlockTypeTemplateId == pageBlock.PageBlockTypeTemplateId); Debug.Assert(template != null, string.Format("The block template with id {0} could not be found for {1} {2}", pageBlock.PageBlockTypeTemplateId, pageBlock.GetType().Name, pageBlock.GetVersionBlockId())); return(template); }