From d21f9de8c0f16c6441dea7b793f091beaa8dc513 Mon Sep 17 00:00:00 2001 From: Dmitry Dzygin Date: Tue, 6 Sep 2016 11:52:44 +0200 Subject: [PATCH] Optimizing "Bulk Publishing" view, refactoring --- .../AddNewPageTypeWorkflow.cs | 3 +- .../EditPageTypeWorkflow.cs | 11 ++-- .../Extensions/IQueryableExtensionMethods.cs | 7 +-- Composite/Data/PageFolderFacade.cs | 38 +++++++++--- Composite/Data/Types/IPageType.cs | 59 ++++++++----------- Composite/Data/Types/PageServices.cs | 23 ++++++-- .../PageElementProvider.cs | 10 ++-- .../Composite/services/Tree/TreeServices.asmx | 7 ++- 8 files changed, 90 insertions(+), 68 deletions(-) diff --git a/Composite.Workflows/Plugins/Elements/ElementProviders/PageTypeElementProvider/AddNewPageTypeWorkflow.cs b/Composite.Workflows/Plugins/Elements/ElementProviders/PageTypeElementProvider/AddNewPageTypeWorkflow.cs index f1eaf3abb1..842586c9c7 100644 --- a/Composite.Workflows/Plugins/Elements/ElementProviders/PageTypeElementProvider/AddNewPageTypeWorkflow.cs +++ b/Composite.Workflows/Plugins/Elements/ElementProviders/PageTypeElementProvider/AddNewPageTypeWorkflow.cs @@ -1,5 +1,4 @@ using System; -using System.Linq; using Composite.Data.Types; using Composite.Data; using System.Workflow.Activities; @@ -24,7 +23,7 @@ private void initializeCodeActivity_UpdateBindings_ExecuteCode(object sender, Ev pageType.Id = Guid.NewGuid(); pageType.Available = true; pageType.PresetMenuTitle = true; - pageType.HomepageRelation = PageTypeHomepageRelation.NoRestriction.ToPageTypeHomepageRelationString(); + pageType.HomepageRelation = nameof(PageTypeHomepageRelation.NoRestriction); pageType.DefaultTemplateId = Guid.Empty; pageType.DefaultChildPageType = Guid.Empty; diff --git a/Composite.Workflows/Plugins/Elements/ElementProviders/PageTypeElementProvider/EditPageTypeWorkflow.cs b/Composite.Workflows/Plugins/Elements/ElementProviders/PageTypeElementProvider/EditPageTypeWorkflow.cs index 6475ab435a..323fd18ec9 100644 --- a/Composite.Workflows/Plugins/Elements/ElementProviders/PageTypeElementProvider/EditPageTypeWorkflow.cs +++ b/Composite.Workflows/Plugins/Elements/ElementProviders/PageTypeElementProvider/EditPageTypeWorkflow.cs @@ -73,7 +73,7 @@ private void initializeCodeActivity_UpdateBindings_ExecuteCode(object sender, Ev Func > getOption = relation => new KeyValuePair( - relation.ToPageTypeHomepageRelationString(), + relation.ToString(), GetText($"PageType.EditPageTypeWorkflow.HomepageRelationKeySelector.{relation}Label")); this.Bindings.Add("HomepageRelationOptions", new List> { @@ -88,14 +88,14 @@ private void initializeCodeActivity_UpdateBindings_ExecuteCode(object sender, Ev OrderBy(f => f.Title). ToList(f => new KeyValuePair(f.Id, f.Title)); - var defaultPageTempatesOptions = new List> + var defaultPageTemplateOptions = new List> { new KeyValuePair(Guid.Empty, Texts.PageType_EditPageTypeWorkflow_DefaultPageTemplateKeySelector_NoneSelectedLabel) }; - defaultPageTempatesOptions.AddRange(pageTemplates); + defaultPageTemplateOptions.AddRange(pageTemplates); - this.Bindings.Add("DefaultTemplateOptions", defaultPageTempatesOptions); + this.Bindings.Add("DefaultTemplateOptions", defaultPageTemplateOptions); this.Bindings.Add("TemplateRestrictionOptions", pageTemplates); @@ -180,7 +180,8 @@ private void editCodeActivity_ValidateBindings(object sender, ConditionalEventAr return; } - if ((pageType.HomepageRelation == PageTypeHomepageRelation.OnlyHomePages.ToPageTypeHomepageRelationString()) && (selectedPageTypeParentRestrictions.Count > 0)) + if (pageType.HomepageRelation == nameof(PageTypeHomepageRelation.OnlyHomePages) + && selectedPageTypeParentRestrictions.Count > 0) { this.ShowFieldMessage("PageType.HomepageRelation", Texts.PageType_EditPageTypeWorkflow_ValidationError_HomepageRelationConflictsWithParentRestrictions); SetSaveStatus(false); diff --git a/Composite/Core/Extensions/IQueryableExtensionMethods.cs b/Composite/Core/Extensions/IQueryableExtensionMethods.cs index 64a18b7853..d0d2831864 100644 --- a/Composite/Core/Extensions/IQueryableExtensionMethods.cs +++ b/Composite/Core/Extensions/IQueryableExtensionMethods.cs @@ -47,12 +47,7 @@ public static bool IsEnumerableQuery(this IQueryable query) return (query as DataFacadeQueryable).IsEnumerableQuery; } - if (query is CachingQueryable) - { - return true; - } - - return query.GetType().GetGenericTypeDefinition() == typeof(EnumerableQuery<>); + return query is CachingQueryable || query is EnumerableQuery; } diff --git a/Composite/Data/PageFolderFacade.cs b/Composite/Data/PageFolderFacade.cs index ca5476967f..46b87445a7 100644 --- a/Composite/Data/PageFolderFacade.cs +++ b/Composite/Data/PageFolderFacade.cs @@ -4,6 +4,7 @@ using System.Linq.Expressions; using System.Reflection; using System.Transactions; +using Composite.Core.Extensions; using Composite.Data.DynamicTypes; using Composite.Data.Foundation; using Composite.Data.Types; @@ -43,9 +44,15 @@ public static IEnumerable GetAllFolderTypes() /// public static bool HasFolderDefinitions(this IPage page) { - Verify.ArgumentNotNull(page, "page"); + Verify.ArgumentNotNull(page, nameof(page)); + + Guid pageId = page.Id; - return DataFacade.GetData().Any(f => f.PageId == page.Id); + var definitions = DataFacade.GetData(); + + return definitions.IsEnumerableQuery() + ? definitions.AsEnumerable().Any(f => f.PageId == pageId) + : definitions.Any(f => f.PageId == pageId); } @@ -57,19 +64,32 @@ public static bool HasFolderDefinitions(this IPage page) /// public static IEnumerable GetDefinedFolderTypes(this IPage page) { - Verify.ArgumentNotNull(page, "page"); + Verify.ArgumentNotNull(page, nameof(page)); - IEnumerable typeIds = - DataFacade.GetData(). - Where(f => f.PageId == page.Id). - Select(f => f.FolderTypeId). - Evaluate(); + var folderDefinitions = DataFacade.GetData(); + + IEnumerable typeIds; + + if (folderDefinitions.IsEnumerableQuery()) + { + typeIds = folderDefinitions + .Evaluate() + .Where(f => f.PageId == page.Id) + .Select(f => f.FolderTypeId); + } + else + { + typeIds = folderDefinitions + .Where(f => f.PageId == page.Id) + .Select(f => f.FolderTypeId) + .Evaluate(); + } foreach (Guid typeId in typeIds) { var dataTypeDescriptor = DynamicTypeManager.GetDataTypeDescriptor(typeId); Verify.IsNotNull(dataTypeDescriptor, "Missing a page data folder type with id '{0}', referenced by a IPageFolderDefinition record", typeId); - + yield return TypeManager.GetType(dataTypeDescriptor.TypeManagerTypeName); } } diff --git a/Composite/Data/Types/IPageType.cs b/Composite/Data/Types/IPageType.cs index 179cee8bdd..3bc4c034df 100644 --- a/Composite/Data/Types/IPageType.cs +++ b/Composite/Data/Types/IPageType.cs @@ -36,12 +36,12 @@ public static class PageTypeHomepageRelationExtensionMethods /// public static PageTypeHomepageRelation GetPageTypeHomepageRelation(this string value) { - if (string.IsNullOrEmpty(value)) throw new ArgumentNullException("value"); + if (string.IsNullOrEmpty(value)) throw new ArgumentNullException(nameof(value)); PageTypeHomepageRelation result; - if (Enum.TryParse(value, out result) == false) + if (!Enum.TryParse(value, out result)) { - throw new ArgumentException(string.Format("The argument is wrongly formattet")); + throw new ArgumentException("The argument is wrongly formatted"); } return result; @@ -50,6 +50,7 @@ public static PageTypeHomepageRelation GetPageTypeHomepageRelation(this string v /// + [Obsolete("Use nameof() keyword instead", true)] public static string ToPageTypeHomepageRelationString(this PageTypeHomepageRelation pageTypeHomepageRelation) { return pageTypeHomepageRelation.ToString(); @@ -59,7 +60,7 @@ public static string ToPageTypeHomepageRelationString(this PageTypeHomepageRelat - /// + /// /// /// [System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)] @@ -68,47 +69,37 @@ public static class PageTypeExtensionMethods /// public static IEnumerable GetChildPageSelectablePageTypes(this IPage parentPage, IPage childPage = null) { + var pageTypes = DataFacade.GetData().AsEnumerable() + .Where(pt => pt.Available); + + pageTypes = pageTypes.OrderBy(f => f.Name); + if (parentPage == null) { - return - DataFacade.GetData(). - Where(f => f.Available && f.HomepageRelation != PageTypeHomepageRelation.OnlySubPages.ToString()). - OrderBy(f => f.Name). - Evaluate(); + return pageTypes + .Where(f => f.HomepageRelation != nameof(PageTypeHomepageRelation.OnlySubPages)) + .Evaluate(); } - IEnumerable pageTypes; - if (childPage == null) - { - pageTypes = - DataFacade.GetData(). - Where(f => f.Available && f.HomepageRelation != PageTypeHomepageRelation.OnlyHomePages.ToString()). - OrderBy(f => f.Name). - Evaluate(); - } - else - { - pageTypes = - DataFacade.GetData(). - Where(f => - f.Available && - (f.HomepageRelation != PageTypeHomepageRelation.OnlyHomePages.ToString() || f.Id == childPage.PageTypeId)). - OrderBy(f => f.Name). - Evaluate(); - } + pageTypes = pageTypes + .Where(f => f.HomepageRelation != nameof(PageTypeHomepageRelation.OnlyHomePages) + && (childPage == null || f.Id == childPage.PageTypeId)) + .Evaluate(); + + ICollection parentRestrictions = null; var result = new List(); foreach (IPageType pageType in pageTypes) { if (childPage != null && pageType.Id == childPage.PageTypeId) { - result.Add(pageType); + result.Add(pageType); continue; } - var parentRestrictions = DataFacade.GetData() - .Where(f => f.PageTypeId == pageType.Id) - .ToList(); + parentRestrictions = parentRestrictions ?? + DataFacade.GetData().AsEnumerable() + .Where(f => f.PageTypeId == pageType.Id).ToList(); if (parentRestrictions.Count == 0 || parentRestrictions.Any(f => f.AllowedParentPageTypeId == parentPage.PageTypeId)) { @@ -122,7 +113,7 @@ public static IEnumerable GetChildPageSelectablePageTypes(this IPage - /// + /// /// /// [System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)] @@ -149,7 +140,7 @@ public interface IPageType : IData /// [StoreFieldType(PhysicalStoreFieldType.LargeString)] - [ImmutableFieldId("{CCAA5F15-63E4-42BF-8CDA-3AD0407520A7}")] + [ImmutableFieldId("{CCAA5F15-63E4-42BF-8CDA-3AD0407520A7}")] string Description { get; set; } diff --git a/Composite/Data/Types/PageServices.cs b/Composite/Data/Types/PageServices.cs index c1304f40cd..4c00679d4c 100644 --- a/Composite/Data/Types/PageServices.cs +++ b/Composite/Data/Types/PageServices.cs @@ -4,6 +4,7 @@ using System.Linq; using Composite.C1Console.Trees; using Composite.C1Console.Users; +using Composite.Core.Extensions; using Composite.Core.Linq; using Composite.Data.ProcessControlled; using Composite.Data.Transactions; @@ -58,11 +59,23 @@ public static IQueryable GetChildren(this IPage page) /// public static IQueryable GetChildren(Guid parentId) { - return from ps in DataFacade.GetData() - join p in DataFacade.GetData() on ps.Id equals p.Id - where ps.ParentId == parentId - orderby ps.LocalOrdering - select p; + var structure = DataFacade.GetData(); + var pages = DataFacade.GetData(); + + if (structure.IsEnumerableQuery() && pages.IsEnumerableQuery()) + { + return (from ps in structure.AsEnumerable() + where ps.ParentId == parentId + join p in pages.AsEnumerable() on ps.Id equals p.Id + orderby ps.LocalOrdering + select p).AsQueryable(); + } + + return from ps in structure + where ps.ParentId == parentId + join p in pages on ps.Id equals p.Id + orderby ps.LocalOrdering + select p; #warning revisit this - we return all versions (by design so far). Any ordering on page versions? - check history for original intent } diff --git a/Composite/Plugins/Elements/ElementProviders/PageElementProvider/PageElementProvider.cs b/Composite/Plugins/Elements/ElementProviders/PageElementProvider/PageElementProvider.cs index 0d43b4bc1c..669d215340 100644 --- a/Composite/Plugins/Elements/ElementProviders/PageElementProvider/PageElementProvider.cs +++ b/Composite/Plugins/Elements/ElementProviders/PageElementProvider/PageElementProvider.cs @@ -128,11 +128,11 @@ public IEnumerable GetRoots(SearchToken searchToken) } }; - var allPageTypes = DataFacade.GetData(); + var allPageTypes = DataFacade.GetData().AsEnumerable(); foreach ( var pageType in - allPageTypes.Where(f => f.HomepageRelation != PageTypeHomepageRelation.OnlySubPages.ToPageTypeHomepageRelationString()) + allPageTypes.Where(f => f.HomepageRelation != nameof(PageTypeHomepageRelation.OnlySubPages)) .OrderByDescending(f=>f.Id)) { element.AddAction( @@ -670,7 +670,7 @@ private List GetElements(List> pag string localizePageLabel = StringResourceSystemFacade.GetString("Composite.Plugins.PageElementProvider", "PageElementProvider.LocalizePage"); string localizePageToolTip = StringResourceSystemFacade.GetString("Composite.Plugins.PageElementProvider", "PageElementProvider.LocalizePageToolTip"); string addNewPageLabel = StringResourceSystemFacade.GetString("Composite.Plugins.PageElementProvider", "PageElementProvider.AddSubPageFormat"); - string addNewPageToolTip = StringResourceSystemFacade.GetString("Composite.Plugins.PageElementProvider", "PageElementProvider.AddSubPageToolTip"); + //string addNewPageToolTip = StringResourceSystemFacade.GetString("Composite.Plugins.PageElementProvider", "PageElementProvider.AddSubPageToolTip"); string deletePageLabel = StringResourceSystemFacade.GetString("Composite.Plugins.PageElementProvider", "PageElementProvider.Delete"); string deletePageToolTip = StringResourceSystemFacade.GetString("Composite.Plugins.PageElementProvider", "PageElementProvider.DeleteToolTip"); @@ -681,7 +681,7 @@ private List GetElements(List> pag } var elements = new Element[pages.Count]; - var allPageTypes = DataFacade.GetData(); + var allPageTypes = DataFacade.GetData().AsEnumerable(); ParallelFacade.For("PageElementProvider. Getting elements", 0, pages.Count, i => { @@ -696,7 +696,7 @@ private List GetElements(List> pag var element = new Element(_context.CreateElementHandle(entityToken), MakeVisualData(page, kvp.Key, urlMappingName, rootPages), dragAndDropInfo); - element.PropertyBag.Add("Uri", "~/page({0})".FormatWith(page.Id)); + element.PropertyBag.Add("Uri", $"~/page({page.Id})"); element.PropertyBag.Add("ElementType", "application/x-composite-page"); element.PropertyBag.Add("DataId", page.Id.ToString()); diff --git a/Website/Composite/services/Tree/TreeServices.asmx b/Website/Composite/services/Tree/TreeServices.asmx index b3334ce854..b42c57a9cd 100644 --- a/Website/Composite/services/Tree/TreeServices.asmx +++ b/Website/Composite/services/Tree/TreeServices.asmx @@ -189,9 +189,12 @@ namespace Composite.Services elementBundles.Add(elementBundle); } - foreach (var element in GetPublishControlledDescendants(child.ElementHandle)) + if (child.VisualData.HasChildren) { - yield return element; + foreach (var element in GetPublishControlledDescendants(child.ElementHandle)) + { + yield return element; + } } } }