Exemple #1
0
        private void UpdateTemplates(
            IExecutionContext executionContext,
            PageBlockType existingBlock,
            PageBlockTypeFileDetails fileDetails
            )
        {
            var templatesToDelete = existingBlock
                                    .PageBlockTemplates
                                    .Where(mt => !fileDetails.Templates.Any(t => t.FileName.Equals(mt.FileName, StringComparison.OrdinalIgnoreCase)))
                                    .ToList();

            if (templatesToDelete.Any())
            {
                _dbContext.PageBlockTypeTemplates.RemoveRange(templatesToDelete);
            }

            foreach (var fileTemplate in fileDetails.Templates)
            {
                var existingTemplate = existingBlock
                                       .PageBlockTemplates
                                       .FirstOrDefault(t => t.FileName.Equals(fileTemplate.FileName, StringComparison.OrdinalIgnoreCase));

                if (existingTemplate == null)
                {
                    existingTemplate            = new PageBlockTypeTemplate();
                    existingTemplate.CreateDate = executionContext.ExecutionDate;
                    existingBlock.PageBlockTemplates.Add(existingTemplate);
                }

                existingTemplate.FileName    = fileTemplate.FileName;
                existingTemplate.Name        = fileTemplate.Name;
                existingTemplate.Description = fileTemplate.Description;
            }
        }
        private async Task UpdateBlocksAsync(
            IExecutionContext executionContext,
            Dictionary <string, PageBlockType> dbPageBlockTypes,
            Dictionary <string, IPageBlockTypeDataModel> blockTypeDataModels
            )
        {
            foreach (var model in blockTypeDataModels)
            {
                var  fileName      = model.Key;
                var  existingBlock = dbPageBlockTypes.GetOrDefault(fileName);
                bool isUpdated     = false;

                var fileDetails = await _queryExecutor.ExecuteAsync(new GetPageBlockTypeFileDetailsByFileNameQuery(fileName), executionContext);

                DetectDuplicateTemplateFileNames(fileDetails);

                var name = string.IsNullOrWhiteSpace(fileDetails.Name) ? TextFormatter.PascalCaseToSentence(fileName) : fileDetails.Name;

                if (existingBlock == null)
                {
                    existingBlock            = new PageBlockType();
                    existingBlock.FileName   = fileName;
                    existingBlock.CreateDate = executionContext.ExecutionDate;
                    _dbContext.PageBlockTypes.Add(existingBlock);
                    isUpdated = true;
                }

                if (existingBlock.IsArchived)
                {
                    isUpdated = true;
                    existingBlock.IsArchived = false;
                }

                if (existingBlock.Name != name)
                {
                    isUpdated          = true;
                    existingBlock.Name = name;
                }

                if (existingBlock.Description != fileDetails.Description)
                {
                    isUpdated = true;
                    existingBlock.Description = fileDetails.Description;
                }

                UpdateTemplates(executionContext, existingBlock, fileDetails);

                if (isUpdated)
                {
                    existingBlock.UpdateDate = executionContext.ExecutionDate;
                    ValidateBlockProperties(existingBlock);
                }
            }
        }
        /// <summary>
        /// Some properties of the block type are generated based on the class name or paths and these
        /// should be validated to ensure that they do not exceed the database column sizes.
        /// </summary>
        private static void ValidateBlockProperties(PageBlockType dbPageBlockType)
        {
            if (string.IsNullOrWhiteSpace(dbPageBlockType.Name))
            {
                throw new PageBlockTypeRegistrationException($"Page block type name cannot be null. FileName: {dbPageBlockType.FileName}");
            }

            if (dbPageBlockType.Name.Length > 50)
            {
                throw new PageBlockTypeRegistrationException($"Page block type name exceeds the maximum length of 50 characters: {dbPageBlockType.Name}");
            }

            if (dbPageBlockType.FileName.Length > 50)
            {
                throw new PageBlockTypeRegistrationException($"Page block type file nameexceeds the maximum length of 50 characters: {dbPageBlockType.FileName}");
            }
        }
        /// <summary>
        /// Maps an EF PageBlockType record from the db into an PageBlockTypeSummary
        /// object. If the db record is null then null is returned.
        /// </summary>
        /// <param name="dbPageBlockType">PageBlockType record from the database.</param>
        public PageBlockTypeSummary Map(PageBlockType dbPageBlockType)
        {
            var result = new PageBlockTypeSummary()
            {
                Description     = dbPageBlockType.Description,
                FileName        = dbPageBlockType.FileName,
                Name            = dbPageBlockType.Name,
                PageBlockTypeId = dbPageBlockType.PageBlockTypeId
            };

            result.Templates = dbPageBlockType
                               .PageBlockTemplates
                               .Select(Map)
                               .ToList();

            return(result);
        }
Exemple #5
0
 public static PageBlockTypeModel GetPageBlockTypeModel(PageBlockType type)
 {
     return type != null ? new PageBlockTypeModel {Id = type.Id, Name = type.Name} : null;
 }