Beispiel #1
0
        private async Task <CustomEntityVersionPageBlockRenderDetails> MapAsync(
            CustomEntityVersionPageBlock versionBlock,
            string blockTypeFileName,
            string customEntityDefinitionCode,
            PublishStatusQuery publishStatus,
            IExecutionContext executionContext
            )
        {
            _permissionValidationService.EnforceCustomEntityPermission <CustomEntityReadPermission>(customEntityDefinitionCode, executionContext.UserContext);

            var blockTypeQuery = new GetPageBlockTypeSummaryByIdQuery(versionBlock.PageBlockTypeId);
            var blockType      = await _queryExecutor.ExecuteAsync(blockTypeQuery, executionContext);

            EntityNotFoundException.ThrowIfNull(blockType, versionBlock.PageBlockTypeId);

            var result = new CustomEntityVersionPageBlockRenderDetails();

            result.CustomEntityVersionPageBlockId = versionBlock.CustomEntityVersionPageBlockId;
            result.BlockType    = blockType;
            result.DisplayModel = await _pageVersionBlockModelMapper.MapDisplayModelAsync(
                blockTypeFileName,
                versionBlock,
                publishStatus,
                executionContext
                );

            return(result);
        }
Beispiel #2
0
 private IQueryable <int> GetOrderedBlockIds(CustomEntityVersionPageBlock versionBlock)
 {
     return(_dbContext
            .CustomEntityVersionPageBlocks
            .AsNoTracking()
            .Where(m => m.CustomEntityVersionId == versionBlock.CustomEntityVersionId && m.PageTemplateRegionId == versionBlock.PageTemplateRegionId)
            .OrderBy(m => m.Ordering)
            .Select(m => m.CustomEntityVersionPageBlockId));
 }
Beispiel #3
0
        private Task OnTransactionComplete(CustomEntityVersion customEntityVersion, CustomEntityVersionPageBlock newBlock)
        {
            _customEntityCache.Clear(customEntityVersion.CustomEntity.CustomEntityDefinitionCode, customEntityVersion.CustomEntityId);

            return(_messageAggregator.PublishAsync(new CustomEntityVersionBlockAddedMessage()
            {
                CustomEntityId = customEntityVersion.CustomEntityId,
                CustomEntityVersionPageBlockId = newBlock.CustomEntityVersionPageBlockId,
                CustomEntityDefinitionCode = customEntityVersion.CustomEntity.CustomEntityDefinitionCode
            }));
        }
        private Task OnTransactionComplete(CustomEntityVersionPageBlock dbBlock)
        {
            _customEntityCache.Clear(dbBlock.CustomEntityVersion.CustomEntity.CustomEntityDefinitionCode, dbBlock.CustomEntityVersion.CustomEntity.CustomEntityId);

            return(_messageAggregator.PublishAsync(new CustomEntityVersionBlockUpdatedMessage()
            {
                CustomEntityId = dbBlock.CustomEntityVersion.CustomEntityId,
                CustomEntityDefinitionCode = dbBlock.CustomEntityVersion.CustomEntity.CustomEntityDefinitionCode,
                CustomEntityVersionBlockId = dbBlock.CustomEntityVersionPageBlockId
            }));
        }
Beispiel #5
0
        private CustomEntityVersionPageBlockDetails MapBlock(CustomEntityVersionPageBlock dbBlock, ICollection <PageBlockTypeSummary> allPageBlockTypes)
        {
            var blockType = allPageBlockTypes.SingleOrDefault(t => t.PageBlockTypeId == dbBlock.PageBlockTypeId);

            var block = new CustomEntityVersionPageBlockDetails();

            block.BlockType = blockType;
            block.DataModel = _pageVersionBlockModelMapper.MapDataModel(blockType.FileName, dbBlock);
            block.CustomEntityVersionPageBlockId = dbBlock.CustomEntityVersionPageBlockId;
            block.Template = _entityVersionPageBlockMapper.GetCustomTemplate(dbBlock, blockType);

            return(block);
        }
Beispiel #6
0
        private UpdateCustomEntityVersionPageBlockCommand Map(CustomEntityVersionPageBlock dbPageBlock, string pageBlockTypeFileName)
        {
            var result = new UpdateCustomEntityVersionPageBlockCommand()
            {
                CustomEntityVersionPageBlockId = dbPageBlock.CustomEntityVersionPageBlockId,
                PageBlockTypeId         = dbPageBlock.PageBlockTypeId,
                PageBlockTypeTemplateId = dbPageBlock.PageBlockTypeTemplateId
            };

            result.DataModel = _pageVersionBlockModelMapper.MapDataModel(pageBlockTypeFileName, dbPageBlock);

            return(result);
        }
Beispiel #7
0
        /// <summary>
        /// Gets the custom view template to render the block as if one
        /// is assigned, otherwise null.
        /// </summary>
        public PageBlockTypeTemplateSummary GetCustomTemplate(CustomEntityVersionPageBlock pageBlock, CustomEntityVersionPageBlockRenderDetails blockRenderDetails)
        {
            if (!pageBlock.PageBlockTypeTemplateId.HasValue)
            {
                return(null);
            }

            var template = blockRenderDetails
                           .BlockType
                           .Templates
                           .FirstOrDefault(t => t.PageBlockTypeTemplateId == pageBlock.PageBlockTypeTemplateId);

            Debug.Assert(template != null, string.Format("The page block type template with id {0} could not be found for custom entity block {1}", pageBlock.PageBlockTypeTemplateId, pageBlock.CustomEntityVersionPageBlockId));

            return(template);
        }
Beispiel #8
0
        public async Task ExecuteAsync(AddCustomEntityVersionPageBlockCommand command, IExecutionContext executionContext)
        {
            var customEntityVersion = _dbContext
                                      .CustomEntityVersions
                                      .Include(s => s.CustomEntityVersionPageBlocks)
                                      .Include(s => s.CustomEntity)
                                      .FirstOrDefault(v => v.CustomEntityVersionId == command.CustomEntityVersionId);

            EntityNotFoundException.ThrowIfNull(customEntityVersion, command.CustomEntityVersionId);
            _permissionValidationService.EnforceCustomEntityPermission <CustomEntityUpdatePermission>(customEntityVersion.CustomEntity.CustomEntityDefinitionCode, executionContext.UserContext);

            if (customEntityVersion.WorkFlowStatusId != (int)WorkFlowStatus.Draft)
            {
                throw new NotPermittedException("Page blocks cannot be added unless the entity is in draft status");
            }

            var page = await _dbContext
                       .Pages
                       .FilterActive()
                       .FilterByPageId(command.PageId)
                       .SingleOrDefaultAsync();

            EntityNotFoundException.ThrowIfNull(page, command.PageId);

            var templateRegion = await _dbContext
                                 .PageTemplateRegions
                                 .FirstOrDefaultAsync(l => l.PageTemplateRegionId == command.PageTemplateRegionId);

            EntityNotFoundException.ThrowIfNull(templateRegion, command.PageTemplateRegionId);

            await ValidateTemplateUsedByPage(command, templateRegion);

            var customEntityVersionBlocks = customEntityVersion
                                            .CustomEntityVersionPageBlocks
                                            .Where(m => m.PageTemplateRegionId == templateRegion.PageTemplateRegionId);

            CustomEntityVersionPageBlock adjacentItem = null;

            if (command.AdjacentVersionBlockId.HasValue)
            {
                adjacentItem = customEntityVersionBlocks
                               .SingleOrDefault(m => m.CustomEntityVersionPageBlockId == command.AdjacentVersionBlockId);
                EntityNotFoundException.ThrowIfNull(adjacentItem, command.AdjacentVersionBlockId);

                if (adjacentItem.PageTemplateRegionId != command.PageTemplateRegionId)
                {
                    throw new Exception("Error adding custom entity page block: the block specified in AdjacentVersionBlockId is in a different region to the block being added.");
                }
            }

            var newBlock = new CustomEntityVersionPageBlock();

            newBlock.PageTemplateRegion = templateRegion;
            newBlock.Page = page;
            newBlock.CustomEntityVersion = customEntityVersion;

            await _pageBlockCommandHelper.UpdateModelAsync(command, newBlock);

            _entityOrderableHelper.SetOrderingForInsert(customEntityVersionBlocks, newBlock, command.InsertMode, adjacentItem);

            _dbContext.CustomEntityVersionPageBlocks.Add(newBlock);

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

                var dependencyCommand = new UpdateUnstructuredDataDependenciesCommand(
                    CustomEntityVersionPageBlockEntityDefinition.DefinitionCode,
                    newBlock.CustomEntityVersionPageBlockId,
                    command.DataModel);

                await _commandExecutor.ExecuteAsync(dependencyCommand, executionContext);

                scope.QueueCompletionTask(() => OnTransactionComplete(customEntityVersion, newBlock));

                await scope.CompleteAsync();
            }

            command.OutputCustomEntityVersionPageBlockId = newBlock.CustomEntityVersionPageBlockId;
        }
Beispiel #9
0
        public async Task ExecuteAsync(AddCustomEntityVersionPageBlockCommand command, IExecutionContext executionContext)
        {
            var customEntityVersion = _dbContext
                                      .CustomEntityVersions
                                      .Include(s => s.CustomEntityVersionPageBlocks)
                                      .Include(s => s.CustomEntity)
                                      .FirstOrDefault(v => v.CustomEntityVersionId == command.CustomEntityVersionId);

            EntityNotFoundException.ThrowIfNull(customEntityVersion, command.CustomEntityVersionId);
            await _permissionValidationService.EnforceCustomEntityPermissionAsync <CustomEntityUpdatePermission>(customEntityVersion.CustomEntity.CustomEntityDefinitionCode);

            if (customEntityVersion.WorkFlowStatusId != (int)WorkFlowStatus.Draft)
            {
                throw new NotPermittedException("Page blocks cannot be deleted unless the entity is in draft status");
            }

            var templateRegion = await _dbContext
                                 .PageTemplateRegions
                                 .FirstOrDefaultAsync(l => l.PageTemplateRegionId == command.PageTemplateRegionId);

            EntityNotFoundException.ThrowIfNull(templateRegion, command.PageTemplateRegionId);

            var customEntityVersionBlocks = customEntityVersion
                                            .CustomEntityVersionPageBlocks
                                            .Where(m => m.PageTemplateRegionId == templateRegion.PageTemplateRegionId);

            CustomEntityVersionPageBlock adjacentItem = null;

            if (command.AdjacentVersionBlockId.HasValue)
            {
                adjacentItem = customEntityVersionBlocks
                               .SingleOrDefault(m => m.CustomEntityVersionPageBlockId == command.AdjacentVersionBlockId);
                EntityNotFoundException.ThrowIfNull(adjacentItem, command.AdjacentVersionBlockId);
            }

            var newBlock = new CustomEntityVersionPageBlock();

            newBlock.PageTemplateRegion = templateRegion;

            await _pageBlockCommandHelper.UpdateModelAsync(command, newBlock);

            newBlock.CustomEntityVersion = customEntityVersion;

            _entityOrderableHelper.SetOrderingForInsert(customEntityVersionBlocks, newBlock, command.InsertMode, adjacentItem);

            _dbContext.CustomEntityVersionPageBlocks.Add(newBlock);

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

                var dependencyCommand = new UpdateUnstructuredDataDependenciesCommand(
                    CustomEntityVersionPageBlockEntityDefinition.DefinitionCode,
                    newBlock.CustomEntityVersionPageBlockId,
                    command.DataModel);

                await _commandExecutor.ExecuteAsync(dependencyCommand);

                scope.Complete();
            }
            _customEntityCache.Clear(customEntityVersion.CustomEntity.CustomEntityDefinitionCode, customEntityVersion.CustomEntityId);

            command.OutputCustomEntityVersionPageBlockId = newBlock.CustomEntityVersionPageBlockId;

            await _messageAggregator.PublishAsync(new CustomEntityVersionBlockAddedMessage()
            {
                CustomEntityId = customEntityVersion.CustomEntityId,
                CustomEntityVersionPageBlockId = newBlock.CustomEntityVersionPageBlockId,
                CustomEntityDefinitionCode     = customEntityVersion.CustomEntity.CustomEntityDefinitionCode
            });
        }