Example #1
0
        /// <summary>
        /// Reset cache name for all hierarchies templates of page
        /// If null then reset all the application
        /// </summary>
        /// <param name="pageTemplateId"></param>
        public void Initialize(int?pageTemplateId = null)
        {
            var currentPageTemplate = GetById(pageTemplateId);

            // Get all templates
            List <PageTemplate> pageTemplates;

            // If has current template, then only refresh the Template tree
            if (currentPageTemplate != null)
            {
                pageTemplates = _pageTemplateRepository.GetHierarchies(currentPageTemplate).ToList();
            }
            // If no current template then reload all trees
            else
            {
                pageTemplates = GetAll().OrderBy(t => t.Hierarchy).ToList();
            }

            // Reset all effected templates
            foreach (var pageTemplate in pageTemplates)
            {
                var template       = pageTemplate.Content.ParseProperties(typeof(PageRenderModel));
                var parentTemplate = GetById(pageTemplate.ParentId);

                //If parent template cache name is null, then it has parsing errors and all children templates is not valid
                if (parentTemplate != null && string.IsNullOrEmpty(parentTemplate.CacheName))
                {
                    pageTemplate.IsValid        = false;
                    pageTemplate.CacheName      = string.Empty;
                    pageTemplate.CompileMessage = parentTemplate.CompileMessage;
                }
                //Generate child template with parent as master
                else if (parentTemplate != null)
                {
                    var parentCacheName = parentTemplate.CacheName;
                    template = template.InsertMasterPage(parentCacheName);

                    // Generate new template cache name base on parent Template and content
                    pageTemplate.CacheName = pageTemplate.Name.GetTemplateCacheName(template);
                }
                else
                {
                    pageTemplate.CacheName = pageTemplate.Name.GetTemplateCacheName(template);
                }

                if (!string.IsNullOrEmpty(pageTemplate.CacheName))
                {
                    var compileMessage = string.Empty;
                    try
                    {
                        var templateKey = EzRazorEngineHelper.TryCompileAndAddTemplate(template, pageTemplate.CacheName,
                                                                                       typeof(PageRenderModel), ResolveType.Layout);

                        pageTemplate.IsValid        = true;
                        pageTemplate.CompileMessage = string.Empty;
                        pageTemplate.CacheName      = templateKey.Name;
                    }
                    catch (TemplateParsingException exception)
                    {
                        compileMessage = exception.Message;
                    }
                    catch (TemplateCompilationException exception)
                    {
                        compileMessage = string.Join("\n", exception.CompilerErrors.Select(e => e.ErrorText));
                    }
                    catch (Exception exception)
                    {
                        compileMessage = exception.Message;
                    }
                    finally
                    {
                        if (!string.IsNullOrEmpty(compileMessage))
                        {
                            pageTemplate.IsValid        = false;
                            pageTemplate.CompileMessage = TFormat("PageTemplate_Message_InvalidPageTemplate",
                                                                  pageTemplate.Name, compileMessage);
                            pageTemplate.CacheName = string.Empty;
                        }
                    }
                }

                Update(pageTemplate);
            }
        }