Esempio n. 1
0
        /// <summary>
        /// Render dynamic Navigation
        /// </summary>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public override string Render(string[] parameters)
        {
            //Initialize parameter
            ParseParams(parameters);

            #region Try get template cache

            /*
             * Check storing Navigation result in cache
             * If all the Navigations are not updated then get the cache result
             * If not rebuild the data and recache
             */
            var cacheName       = string.Join("_", parameters);
            var cacheNavigation = StateManager.GetApplication <CacheNavigation>(cacheName);

            if (cacheNavigation != null)
            {
                var template = _widgetTemplateService.GetTemplate(Template) ??
                               _widgetTemplateService.GetTemplate(GetSetup().DefaultTemplate);
                if (template == null)
                {
                    throw new Exception(string.Format("Template {0} is not found", Template));
                }

                var lastCreated   = _clientNavigationService.GetAll().Max(t => t.Created);
                var lastUpdated   = _clientNavigationService.GetAll().Max(t => t.LastUpdate) ?? DateTime.MinValue;
                var dataCacheTime = lastCreated > lastUpdated ? lastCreated : lastUpdated;

                lastCreated = template.Created;
                lastUpdated = template.LastUpdate ?? DateTime.MinValue;
                var templateCacheTime = lastCreated > lastUpdated ? lastCreated : lastUpdated;

                if (cacheNavigation.DataCacheTime == dataCacheTime && cacheNavigation.TemplateCacheTime == templateCacheTime)
                {
                    return(cacheNavigation.Content);
                }
            }

            #endregion

            var renderTemplate = _widgetTemplateService.GetTemplateByName(Template) ??
                                 _widgetTemplateService.GetTemplateByName(GetSetup().DefaultTemplate);

            //Get Navigations
            var data = _clientNavigationService.GenerateNavigations(ParentId, ShowParentNavigation);

            /*
             * Create cache version of this widget and store in Application
             */
            cacheNavigation = new CacheNavigation
            {
                Content           = _widgetTemplateService.ParseTemplate(renderTemplate, data),
                DataCacheTime     = DateTime.UtcNow,
                TemplateCacheTime = DateTime.UtcNow
            };
            HttpContext.Current.Application[cacheName] = cacheNavigation;

            return(cacheNavigation.Content);
        }