Exemple #1
0
        private void UpdateSections(
            PageTemplateFile fileTemplate,
            PageTemplateFileInfo fileTemplateDetails,
            PageTemplate dbPageTemplate,
            IExecutionContext executionContext
            )
        {
            // De-dup section names
            var duplicateSectionName = fileTemplateDetails
                                       .Sections
                                       .GroupBy(s => s.Name.ToLowerInvariant())
                                       .FirstOrDefault(g => g.Count() > 1);

            if (duplicateSectionName != null)
            {
                throw new PageTemplateRegistrationException($"Dulpicate template section '{ duplicateSectionName.First().Name }' in template { fileTemplate.FullPath }");
            }

            // Deletions
            var sectionsToDelete = dbPageTemplate
                                   .PageTemplateSections
                                   .Where(ts => !fileTemplateDetails.Sections.Any(fs => ts.Name.Equals(fs.Name, StringComparison.OrdinalIgnoreCase)))
                                   .ToList();

            foreach (var sectionToDelete in sectionsToDelete)
            {
                _dbContext.PageTemplateSections.Remove(sectionToDelete);
            }

            // Updates/Additions
            foreach (var fileSection in fileTemplateDetails.Sections)
            {
                var existing = dbPageTemplate
                               .PageTemplateSections
                               .FirstOrDefault(s => s.Name.Equals(fileSection.Name, StringComparison.OrdinalIgnoreCase));

                if (existing == null)
                {
                    existing = new PageTemplateSection();
                    existing.PageTemplate = dbPageTemplate;
                    existing.CreateDate   = executionContext.ExecutionDate;
                    _dbContext.PageTemplateSections.Add(existing);
                }

                // casing might have changed
                if (existing.Name != fileSection.Name)
                {
                    existing.Name       = fileSection.Name;
                    existing.UpdateDate = executionContext.ExecutionDate;
                }

                // this will detach section data but there's no migrating that...
                if (existing.IsCustomEntitySection != fileSection.IsCustomEntitySection)
                {
                    existing.IsCustomEntitySection = fileSection.IsCustomEntitySection;
                    existing.UpdateDate            = executionContext.ExecutionDate;
                }
            }
        }
Exemple #2
0
        private async Task <PageTemplate> UpdateTemplate(
            IExecutionContext executionContext,
            PageTemplate dbPageTemplate,
            PageTemplateFile fileTemplate,
            PageTemplateFileInfo fileTemplateDetails
            )
        {
            bool isNew = false;

            if (dbPageTemplate == null)
            {
                isNew = true;

                dbPageTemplate            = new PageTemplate();
                dbPageTemplate.FileName   = fileTemplate.FileName;
                dbPageTemplate.CreateDate = executionContext.ExecutionDate;

                _dbContext.PageTemplates.Add(dbPageTemplate);
            }

            if (!isNew &&
                HasCustomEntityDefinitionChanged(dbPageTemplate, fileTemplateDetails) &&
                await IsTemplateInUse(dbPageTemplate))
            {
                var msg = "Cannot change the custom entity type associated with a page template once it is in use";
                throw new Exception(msg);
            }

            if (fileTemplateDetails.CustomEntityDefinition == null)
            {
                dbPageTemplate.PageTypeId = (int)PageType.Generic;
                dbPageTemplate.CustomEntityDefinitionCode = null;
                dbPageTemplate.CustomEntityModelType      = null;
            }
            else
            {
                dbPageTemplate.PageTypeId                 = (int)PageType.CustomEntityDetails;
                dbPageTemplate.CustomEntityModelType      = fileTemplateDetails.CustomEntityModelType;
                dbPageTemplate.CustomEntityDefinitionCode = fileTemplateDetails.CustomEntityDefinition.CustomEntityDefinitionCode;
            }

            if (dbPageTemplate.IsArchived)
            {
                // Re-add template that was previously deleted
                dbPageTemplate.IsArchived = false;
            }

            dbPageTemplate.Name        = TextFormatter.PascalCaseToSentence(fileTemplate.FileName);
            dbPageTemplate.Description = fileTemplateDetails.Description;
            dbPageTemplate.FullPath    = fileTemplate.VirtualPath;
            dbPageTemplate.UpdateDate  = executionContext.ExecutionDate;

            ValidateTemplateProperties(dbPageTemplate);

            return(dbPageTemplate);
        }
Exemple #3
0
        /// <remarks>
        /// Here we have to check to see if the definition has changed because
        /// changing it would break any pages that are using that template. The
        /// best approach for the developer would be to create a new template under a
        /// different name
        /// </remarks>
        private bool HasCustomEntityDefinitionChanged(PageTemplate pageTemplate, PageTemplateFileInfo fileInfo)
        {
            // If generic page
            if (pageTemplate.CustomEntityDefinitionCode == null)
            {
                return(fileInfo.CustomEntityDefinition != null);
            }

            // If custom entity page: compare details.
            return(pageTemplate.CustomEntityDefinitionCode != fileInfo.CustomEntityDefinition.CustomEntityDefinitionCode);
        }
Exemple #4
0
        /// <summary>
        /// Checks that a custom entity definition exists if it is required by the tempate. This
        /// can cause a DbContext.SaveChanges to run.
        /// </summary>
        private Task EnsureCustomEntityDefinitionExists(
            PageTemplateFileInfo fileTemplateDetails,
            PageTemplate dbPageTemplate
            )
        {
            var definitionCode = fileTemplateDetails.CustomEntityDefinition?.CustomEntityDefinitionCode;

            // Only update/check the definition if it has changed to potentially save a query
            if (!string.IsNullOrEmpty(definitionCode) && (dbPageTemplate == null || definitionCode != dbPageTemplate.CustomEntityDefinitionCode))
            {
                var command = new EnsureCustomEntityDefinitionExistsCommand(fileTemplateDetails.CustomEntityDefinition.CustomEntityDefinitionCode);
                return(_commandExecutor.ExecuteAsync(command));
            }

            return(Task.CompletedTask);
        }