From 863e8b5024739ab3aba7844924f805217f7cede4 Mon Sep 17 00:00:00 2001 From: Shannon Date: Wed, 1 Oct 2014 10:18:51 +1000 Subject: [PATCH] Changes PetaPoco UOW to use a Queue instead of trying to order by a date which even in ticks is sometimes the same. Changes some content ext to accept the service it needs instead of relying on the app ctx singleton and obsoletes others. Fixes ContentBase.ContentTypeId to check if the Id has been set on it's true ContentTypeBase which is required with the new foreign key to insert entities (this was actually a bug without the foreign key). Updates media/content services with paging to use parameters correctly. Fixes tests. --- src/Umbraco.Core/Models/ContentBase.cs | 15 ++++- src/Umbraco.Core/Models/ContentExtensions.cs | 65 +++++++++++++++++-- .../Repositories/VersionableRepositoryBase.cs | 2 +- .../UnitOfWork/PetaPocoUnitOfWork.cs | 52 +++++++-------- src/Umbraco.Core/Services/PackagingService.cs | 8 +-- .../Persistence/BaseTableByTableTest.cs | 7 ++ src/Umbraco.Tests/PluginManagerTests.cs | 2 +- .../Trees/TemplateTreeController.cs | 52 --------------- .../DataServices/UmbracoContentService.cs | 2 +- .../DataServices/UmbracoMediaService.cs | 2 +- 10 files changed, 114 insertions(+), 93 deletions(-) delete mode 100644 src/Umbraco.Web/Trees/TemplateTreeController.cs diff --git a/src/Umbraco.Core/Models/ContentBase.cs b/src/Umbraco.Core/Models/ContentBase.cs index e4cc256d1b..0b29a90201 100644 --- a/src/Umbraco.Core/Models/ContentBase.cs +++ b/src/Umbraco.Core/Models/ContentBase.cs @@ -51,7 +51,7 @@ namespace Umbraco.Core.Models _parentId = new Lazy(() => parentId); _name = name; - _contentTypeId = int.Parse(contentType.Id.ToString(CultureInfo.InvariantCulture)); + _contentTypeId = contentType.Id; _properties = properties; _properties.EnsurePropertyTypes(PropertyTypes); _additionalData = new Dictionary(); @@ -75,7 +75,7 @@ namespace Umbraco.Core.Models _parentId = new Lazy(() => parent.Id); _name = name; - _contentTypeId = int.Parse(contentType.Id.ToString(CultureInfo.InvariantCulture)); + _contentTypeId = contentType.Id; _properties = properties; _properties.EnsurePropertyTypes(PropertyTypes); _additionalData = new Dictionary(); @@ -234,7 +234,16 @@ namespace Umbraco.Core.Models [DataMember] public virtual int ContentTypeId { - get { return _contentTypeId; } + get + { + //There will be cases where this has not been updated to reflect the true content type ID. + //This will occur when inserting new content. + if (_contentTypeId == 0 && ContentTypeBase != null && ContentTypeBase.HasIdentity) + { + _contentTypeId = ContentTypeBase.Id; + } + return _contentTypeId; + } protected set { SetPropertyValueAndDetectChanges(o => diff --git a/src/Umbraco.Core/Models/ContentExtensions.cs b/src/Umbraco.Core/Models/ContentExtensions.cs index 20a1caf9b9..c12d5c34d6 100644 --- a/src/Umbraco.Core/Models/ContentExtensions.cs +++ b/src/Umbraco.Core/Models/ContentExtensions.cs @@ -530,6 +530,7 @@ namespace Umbraco.Core.Models /// /// Gets the for the Creator of this media item. /// + [Obsolete("Use the overload that declares the IUserService to use")] public static IProfile GetCreatorProfile(this IMedia media) { return ApplicationContext.Current.Services.UserService.GetProfileById(media.CreatorId); @@ -546,6 +547,7 @@ namespace Umbraco.Core.Models /// /// Gets the for the Creator of this content item. /// + [Obsolete("Use the overload that declares the IUserService to use")] public static IProfile GetCreatorProfile(this IContentBase content) { return ApplicationContext.Current.Services.UserService.GetProfileById(content.CreatorId); @@ -562,6 +564,7 @@ namespace Umbraco.Core.Models /// /// Gets the for the Writer of this content. /// + [Obsolete("Use the overload that declares the IUserService to use")] public static IProfile GetWriterProfile(this IContent content) { return ApplicationContext.Current.Services.UserService.GetProfileById(content.WriterId); @@ -720,14 +723,16 @@ namespace Umbraco.Core.Models #endregion #region XML methods + /// /// Creates the full xml representation for the object and all of it's descendants /// /// to generate xml for + /// /// Xml representation of the passed in - internal static XElement ToDeepXml(this IContent content) + internal static XElement ToDeepXml(this IContent content, IPackagingService packagingService) { - return ApplicationContext.Current.Services.PackagingService.Export(content, true, raiseEvents: false); + return packagingService.Export(content, true, raiseEvents: false); } /// @@ -735,29 +740,54 @@ namespace Umbraco.Core.Models /// /// to generate xml for /// Xml representation of the passed in + [Obsolete("Use the overload that declares the IPackagingService to use")] public static XElement ToXml(this IContent content) { return ApplicationContext.Current.Services.PackagingService.Export(content, raiseEvents: false); } + /// + /// Creates the xml representation for the object + /// + /// to generate xml for + /// + /// Xml representation of the passed in + public static XElement ToXml(this IContent content, IPackagingService packagingService) + { + return packagingService.Export(content, raiseEvents: false); + } + /// /// Creates the xml representation for the object /// /// to generate xml for /// Xml representation of the passed in + [Obsolete("Use the overload that declares the IPackagingService to use")] public static XElement ToXml(this IMedia media) { return ApplicationContext.Current.Services.PackagingService.Export(media, raiseEvents: false); } + /// + /// Creates the xml representation for the object + /// + /// to generate xml for + /// + /// Xml representation of the passed in + public static XElement ToXml(this IMedia media, IPackagingService packagingService) + { + return packagingService.Export(media, raiseEvents: false); + } + /// /// Creates the full xml representation for the object and all of it's descendants /// /// to generate xml for + /// /// Xml representation of the passed in - internal static XElement ToDeepXml(this IMedia media) + internal static XElement ToDeepXml(this IMedia media, IPackagingService packagingService) { - return ApplicationContext.Current.Services.PackagingService.Export(media, true, raiseEvents: false); + return packagingService.Export(media, true, raiseEvents: false); } /// @@ -766,6 +796,7 @@ namespace Umbraco.Core.Models /// to generate xml for /// Boolean indicating whether the xml should be generated for preview /// Xml representation of the passed in + [Obsolete("Use the overload that declares the IPackagingService to use")] public static XElement ToXml(this IContent content, bool isPreview) { //TODO Do a proper implementation of this @@ -773,15 +804,41 @@ namespace Umbraco.Core.Models return content.ToXml(); } + /// + /// Creates the xml representation for the object + /// + /// to generate xml for + /// + /// Boolean indicating whether the xml should be generated for preview + /// Xml representation of the passed in + public static XElement ToXml(this IContent content, IPackagingService packagingService, bool isPreview) + { + //TODO Do a proper implementation of this + //If current IContent is published we should get latest unpublished version + return content.ToXml(packagingService); + } + /// /// Creates the xml representation for the object /// /// to generate xml for /// Xml representation of the passed in + [Obsolete("Use the overload that declares the IPackagingService to use")] public static XElement ToXml(this IMember member) { return ((PackagingService)(ApplicationContext.Current.Services.PackagingService)).Export(member); } + + /// + /// Creates the xml representation for the object + /// + /// to generate xml for + /// + /// Xml representation of the passed in + public static XElement ToXml(this IMember member, IPackagingService packagingService) + { + return ((PackagingService)(packagingService)).Export(member); + } #endregion } } \ No newline at end of file diff --git a/src/Umbraco.Core/Persistence/Repositories/VersionableRepositoryBase.cs b/src/Umbraco.Core/Persistence/Repositories/VersionableRepositoryBase.cs index 281c679a84..cbd35b7366 100644 --- a/src/Umbraco.Core/Persistence/Repositories/VersionableRepositoryBase.cs +++ b/src/Umbraco.Core/Persistence/Repositories/VersionableRepositoryBase.cs @@ -310,7 +310,7 @@ namespace Umbraco.Core.Persistence.Repositories if (totalRecords > 0) { //Crete the inner paged query that was used above to get the paged result, we'll use that as the inner sub query - var args = sqlNodeIds.Arguments; + var args = sqlNodeIdsWithSort.Arguments; string sqlStringCount, sqlStringPage; Database.BuildPageQueries(pageIndex * pageSize, pageSize, sqlNodeIdsWithSort.SQL, ref args, out sqlStringCount, out sqlStringPage); diff --git a/src/Umbraco.Core/Persistence/UnitOfWork/PetaPocoUnitOfWork.cs b/src/Umbraco.Core/Persistence/UnitOfWork/PetaPocoUnitOfWork.cs index eb070ae4e6..435019e25e 100644 --- a/src/Umbraco.Core/Persistence/UnitOfWork/PetaPocoUnitOfWork.cs +++ b/src/Umbraco.Core/Persistence/UnitOfWork/PetaPocoUnitOfWork.cs @@ -17,7 +17,7 @@ namespace Umbraco.Core.Persistence.UnitOfWork internal Guid InstanceId { get; private set; } private Guid _key; - private readonly List _operations = new List(); + private readonly Queue _operations = new Queue(); /// /// Creates a new unit of work instance @@ -40,14 +40,13 @@ namespace Umbraco.Core.Persistence.UnitOfWork /// The participating in the transaction public void RegisterAdded(IEntity entity, IUnitOfWorkRepository repository) { - _operations.Add( - new Operation - { - Entity = entity, - ProcessDate = DateTime.Now, - Repository = repository, - Type = TransactionType.Insert - }); + _operations.Enqueue(new Operation + { + Entity = entity, + ProcessDate = DateTime.Now, + Repository = repository, + Type = TransactionType.Insert + }); } /// @@ -57,14 +56,14 @@ namespace Umbraco.Core.Persistence.UnitOfWork /// The participating in the transaction public void RegisterChanged(IEntity entity, IUnitOfWorkRepository repository) { - _operations.Add( - new Operation - { - Entity = entity, - ProcessDate = DateTime.Now, - Repository = repository, - Type = TransactionType.Update - }); + _operations.Enqueue( + new Operation + { + Entity = entity, + ProcessDate = DateTime.Now, + Repository = repository, + Type = TransactionType.Update + }); } /// @@ -74,14 +73,14 @@ namespace Umbraco.Core.Persistence.UnitOfWork /// The participating in the transaction public void RegisterRemoved(IEntity entity, IUnitOfWorkRepository repository) { - _operations.Add( - new Operation - { - Entity = entity, - ProcessDate = DateTime.Now, - Repository = repository, - Type = TransactionType.Delete - }); + _operations.Enqueue( + new Operation + { + Entity = entity, + ProcessDate = DateTime.Now, + Repository = repository, + Type = TransactionType.Delete + }); } /// @@ -107,8 +106,9 @@ namespace Umbraco.Core.Persistence.UnitOfWork { using (var transaction = Database.GetTransaction()) { - foreach (var operation in _operations.OrderBy(o => o.ProcessDate)) + while (_operations.Count > 0) { + var operation = _operations.Dequeue(); switch (operation.Type) { case TransactionType.Insert: diff --git a/src/Umbraco.Core/Services/PackagingService.cs b/src/Umbraco.Core/Services/PackagingService.cs index a1ebd53280..406d869fab 100644 --- a/src/Umbraco.Core/Services/PackagingService.cs +++ b/src/Umbraco.Core/Services/PackagingService.cs @@ -37,7 +37,7 @@ namespace Umbraco.Core.Services private readonly IDatabaseUnitOfWorkProvider _uowProvider; private Dictionary _importedContentTypes; private IPackageInstallation _packageInstallation; - private readonly IUserService userService; + private readonly IUserService _userService; public PackagingService(IContentService contentService, @@ -60,7 +60,7 @@ namespace Umbraco.Core.Services _localizationService = localizationService; _repositoryFactory = repositoryFactory; _uowProvider = uowProvider; - + _userService = userService; _importedContentTypes = new Dictionary(); } @@ -84,7 +84,7 @@ namespace Umbraco.Core.Services } var exporter = new EntityXmlSerializer(); - var xml = exporter.Serialize(_contentService, _dataTypeService, userService, content, deep); + var xml = exporter.Serialize(_contentService, _dataTypeService, _userService, content, deep); if(raiseEvents) ExportedContent.RaiseEvent(new ExportEventArgs(content, xml, false), this); @@ -1220,7 +1220,7 @@ namespace Umbraco.Core.Services } var exporter = new EntityXmlSerializer(); - var xml = exporter.Serialize(_mediaService, _dataTypeService, userService, media, deep); + var xml = exporter.Serialize(_mediaService, _dataTypeService, _userService, media, deep); if(raiseEvents) ExportedMedia.RaiseEvent(new ExportEventArgs(media, xml, false), this); diff --git a/src/Umbraco.Tests/Persistence/BaseTableByTableTest.cs b/src/Umbraco.Tests/Persistence/BaseTableByTableTest.cs index bbb5c07731..d69a49d27f 100644 --- a/src/Umbraco.Tests/Persistence/BaseTableByTableTest.cs +++ b/src/Umbraco.Tests/Persistence/BaseTableByTableTest.cs @@ -139,6 +139,7 @@ namespace Umbraco.Tests.Persistence using (Transaction transaction = Database.GetTransaction()) { Database.CreateTable(); + Database.CreateTable(); Database.CreateTable(); Database.CreateTable(); @@ -153,6 +154,7 @@ namespace Umbraco.Tests.Persistence using (Transaction transaction = Database.GetTransaction()) { Database.CreateTable(); + Database.CreateTable(); Database.CreateTable(); Database.CreateTable(); @@ -232,6 +234,7 @@ namespace Umbraco.Tests.Persistence using (Transaction transaction = Database.GetTransaction()) { Database.CreateTable(); + Database.CreateTable(); Database.CreateTable(); Database.CreateTable(); Database.CreateTable(); @@ -311,6 +314,7 @@ namespace Umbraco.Tests.Persistence using (Transaction transaction = Database.GetTransaction()) { Database.CreateTable(); + Database.CreateTable(); Database.CreateTable(); Database.CreateTable(); @@ -325,6 +329,7 @@ namespace Umbraco.Tests.Persistence using (Transaction transaction = Database.GetTransaction()) { Database.CreateTable(); + Database.CreateTable(); Database.CreateTable(); Database.CreateTable(); Database.CreateTable(); @@ -354,6 +359,7 @@ namespace Umbraco.Tests.Persistence using (Transaction transaction = Database.GetTransaction()) { Database.CreateTable(); + Database.CreateTable(); Database.CreateTable(); Database.CreateTable(); Database.CreateTable(); @@ -480,6 +486,7 @@ namespace Umbraco.Tests.Persistence using (Transaction transaction = Database.GetTransaction()) { Database.CreateTable(); + Database.CreateTable(); Database.CreateTable(); Database.CreateTable(); Database.CreateTable(); diff --git a/src/Umbraco.Tests/PluginManagerTests.cs b/src/Umbraco.Tests/PluginManagerTests.cs index e7d2b9d77d..8c510256f3 100644 --- a/src/Umbraco.Tests/PluginManagerTests.cs +++ b/src/Umbraco.Tests/PluginManagerTests.cs @@ -285,7 +285,7 @@ namespace Umbraco.Tests public void Resolves_Attributed_Trees() { var trees = PluginManager.Current.ResolveAttributedTrees(); - Assert.AreEqual(19, trees.Count()); + Assert.AreEqual(18, trees.Count()); } [Test] diff --git a/src/Umbraco.Web/Trees/TemplateTreeController.cs b/src/Umbraco.Web/Trees/TemplateTreeController.cs deleted file mode 100644 index fc063c80c3..0000000000 --- a/src/Umbraco.Web/Trees/TemplateTreeController.cs +++ /dev/null @@ -1,52 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using umbraco.cms.presentation.Trees; -using Constants = Umbraco.Core.Constants; -using Umbraco.Web.WebApi.Filters; -using Umbraco.Web.Mvc; -using umbraco.cms.businesslogic.template; -using Umbraco.Web.Models.Trees; - -//namespace Umbraco.Web.Trees -//{ -// [UmbracoApplicationAuthorize(Constants.Applications.Settings)] -// [Tree(Constants.Applications.Settings, Constants.Trees.Templates, "Templates")] -// [PluginController("UmbracoTrees")] -// [CoreTree] -// public class TemplateTreeController : TreeController -// { -// protected override Models.Trees.MenuItemCollection GetMenuForNode(string id, System.Net.Http.Formatting.FormDataCollection queryStrings) -// { -// return new Models.Trees.MenuItemCollection(); -// } - -// protected override Models.Trees.TreeNodeCollection GetTreeNodes(string id, System.Net.Http.Formatting.FormDataCollection queryStrings) -// { -// IEnumerable templates; -// var nodes = new TreeNodeCollection(); - - -// if (id == "-1") -// templates = Services.EntityService.GetRootEntities(Core.Models.UmbracoObjectTypes.Template); -// else -// templates = Services.EntityService.GetChildren(int.Parse(id), Core.Models.UmbracoObjectTypes.Template); - -// foreach (var t in templates) -// { -// var node = CreateTreeNode(t.Id.ToString(), t.ParentId.ToString(), queryStrings, t.Name); -// node.Icon = "icon-newspaper-alt"; -// node.HasChildren = Services.EntityService.GetChildren(t.Id, Core.Models.UmbracoObjectTypes.Template).Any(); - -// if (node.HasChildren) -// node.Icon = "icon-newspaper"; - -// nodes.Add(node); -// } - -// return nodes; -// } -// } -//} diff --git a/src/UmbracoExamine/DataServices/UmbracoContentService.cs b/src/UmbracoExamine/DataServices/UmbracoContentService.cs index 485ebedd12..3d28783127 100644 --- a/src/UmbracoExamine/DataServices/UmbracoContentService.cs +++ b/src/UmbracoExamine/DataServices/UmbracoContentService.cs @@ -76,7 +76,7 @@ namespace UmbracoExamine.DataServices var xmlContent = XDocument.Parse(""); foreach (var c in _applicationContext.Services.ContentService.GetRootContent()) { - xmlContent.Root.Add(c.ToDeepXml()); + xmlContent.Root.Add(c.ToDeepXml(_applicationContext.Services.PackagingService)); } var result = ((IEnumerable)xmlContent.XPathEvaluate(xpath)).Cast(); return result.ToXDocument(); diff --git a/src/UmbracoExamine/DataServices/UmbracoMediaService.cs b/src/UmbracoExamine/DataServices/UmbracoMediaService.cs index 229169aa3d..bf9f204e35 100644 --- a/src/UmbracoExamine/DataServices/UmbracoMediaService.cs +++ b/src/UmbracoExamine/DataServices/UmbracoMediaService.cs @@ -45,7 +45,7 @@ namespace UmbracoExamine.DataServices var xmlMedia = XDocument.Parse(""); foreach (var m in _services.MediaService.GetRootMedia()) { - xmlMedia.Root.Add(m.ToDeepXml()); + xmlMedia.Root.Add(m.ToDeepXml(_services.PackagingService)); } var result = ((IEnumerable)xmlMedia.XPathEvaluate(xpath)).Cast(); return result.ToXDocument();