From e6820ad52ab272d27af6ab6882ae8bdfe28ff08d Mon Sep 17 00:00:00 2001 From: Morten Christensen Date: Mon, 6 Oct 2014 15:02:47 +0200 Subject: [PATCH] Fixes U4-5077 Umbraco 7.2 Alpha - doc types with compositions get moved in doc type tree Return 0 if parent is the root --- .../Exceptions/InvalidCompositionException.cs | 25 ++++++++++ .../Models/ContentTypeCompositionBase.cs | 17 +++++++ src/Umbraco.Core/Umbraco.Core.csproj | 1 + src/umbraco.cms/businesslogic/ContentType.cs | 50 ++++++++++++------- .../businesslogic/media/MediaType.cs | 10 ---- .../businesslogic/web/DocumentType.cs | 11 ---- 6 files changed, 74 insertions(+), 40 deletions(-) create mode 100644 src/Umbraco.Core/Exceptions/InvalidCompositionException.cs diff --git a/src/Umbraco.Core/Exceptions/InvalidCompositionException.cs b/src/Umbraco.Core/Exceptions/InvalidCompositionException.cs new file mode 100644 index 0000000000..fcf7a44677 --- /dev/null +++ b/src/Umbraco.Core/Exceptions/InvalidCompositionException.cs @@ -0,0 +1,25 @@ +using System; + +namespace Umbraco.Core.Exceptions +{ + public class InvalidCompositionException : Exception + { + public string ContentTypeAlias { get; set; } + + public string AddedCompositionAlias { get; set; } + + public string PropertyTypeAlias { get; set; } + + public override string Message + { + get + { + return string.Format( + "InvalidCompositionException - ContentType with alias '{0}' was added as a Compsition to ContentType with alias '{1}', " + + "but there was a conflict on the PropertyType alias '{2}'. " + + "PropertyTypes must have a unique alias across all Compositions in order to compose a valid ContentType Composition.", + AddedCompositionAlias, ContentTypeAlias, PropertyTypeAlias); + } + } + } +} \ No newline at end of file diff --git a/src/Umbraco.Core/Models/ContentTypeCompositionBase.cs b/src/Umbraco.Core/Models/ContentTypeCompositionBase.cs index ec4b464c8a..3bebb59959 100644 --- a/src/Umbraco.Core/Models/ContentTypeCompositionBase.cs +++ b/src/Umbraco.Core/Models/ContentTypeCompositionBase.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Runtime.Serialization; +using Umbraco.Core.Exceptions; namespace Umbraco.Core.Models { @@ -77,6 +78,22 @@ namespace Umbraco.Core.Models if (ContentTypeCompositionExists(contentType.Alias) == false) { + //Before we actually go ahead and add the ContentType as a Composition we ensure that we don't + //end up with duplicate PropertyType aliases - in which case we throw an exception. + var conflictingPropertyTypeAliases = CompositionPropertyTypes.SelectMany( + x => contentType.CompositionPropertyTypes + .Where(y => y.Alias.Equals(x.Alias, StringComparison.InvariantCultureIgnoreCase)) + .Select(p => p.Alias)).ToList(); + + if (conflictingPropertyTypeAliases.Any()) + throw new InvalidCompositionException + { + AddedCompositionAlias = contentType.Alias, + ContentTypeAlias = Alias, + PropertyTypeAlias = + string.Join(", ", conflictingPropertyTypeAliases) + }; + _contentTypeComposition.Add(contentType); OnPropertyChanged(ContentTypeCompositionSelector); return true; diff --git a/src/Umbraco.Core/Umbraco.Core.csproj b/src/Umbraco.Core/Umbraco.Core.csproj index db0f8196f9..afaf5d4a0b 100644 --- a/src/Umbraco.Core/Umbraco.Core.csproj +++ b/src/Umbraco.Core/Umbraco.Core.csproj @@ -310,6 +310,7 @@ + diff --git a/src/umbraco.cms/businesslogic/ContentType.cs b/src/umbraco.cms/businesslogic/ContentType.cs index 822bc79cc0..22e249eefd 100644 --- a/src/umbraco.cms/businesslogic/ContentType.cs +++ b/src/umbraco.cms/businesslogic/ContentType.cs @@ -669,28 +669,33 @@ namespace umbraco.cms.businesslogic { get { - if (MasterContentTypes.Count > 0) - return MasterContentTypes[0]; + if (ContentTypeItem == null) + return 0; - return 0; + return ContentTypeItem.ParentId == -1 ? 0 : ContentTypeItem.ParentId; } set { if (value != MasterContentType) { - //TODO: Add support for multiple masters - /*foreach (var mct in MasterContentTypes) - { - RemoveParentContentType(mct); - }*/ - if (MasterContentTypes.Count > 0) + if (ContentTypeItem == null) { - var masterId = MasterContentTypes[0]; - RemoveParentContentType(masterId); + //Legacy + if (MasterContentTypes.Count > 0) + { + var masterId = MasterContentTypes[0]; + RemoveParentContentType(masterId); + } + + AddParentContentType(value); + } + else + { + ContentTypeItem.ParentId = value; + var newMaster = ApplicationContext.Current.Services.ContentTypeService.GetContentType(value); + var added = ContentTypeItem.AddContentType(newMaster); } - - AddParentContentType(value); } } } @@ -703,13 +708,20 @@ namespace umbraco.cms.businesslogic } else { - SqlHelper.ExecuteNonQuery( - "INSERT INTO [cmsContentType2ContentType] (parentContentTypeId, childContentTypeId) VALUES (@parentContentTypeId, @childContentTypeId)", - SqlHelper.CreateParameter("@parentContentTypeId", parentContentTypeId), - SqlHelper.CreateParameter("@childContentTypeId", Id)); - MasterContentTypes.Add(parentContentTypeId); - + if (ContentTypeItem == null) + { + SqlHelper.ExecuteNonQuery( + "INSERT INTO [cmsContentType2ContentType] (parentContentTypeId, childContentTypeId) VALUES (@parentContentTypeId, @childContentTypeId)", + SqlHelper.CreateParameter("@parentContentTypeId", parentContentTypeId), + SqlHelper.CreateParameter("@childContentTypeId", Id)); + MasterContentTypes.Add(parentContentTypeId); + } + else + { + var newMaster = ApplicationContext.Current.Services.ContentTypeService.GetContentType(parentContentTypeId); + var added = ContentTypeItem.AddContentType(newMaster); + } } } diff --git a/src/umbraco.cms/businesslogic/media/MediaType.cs b/src/umbraco.cms/businesslogic/media/MediaType.cs index aef64cd118..2dec2cd56f 100644 --- a/src/umbraco.cms/businesslogic/media/MediaType.cs +++ b/src/umbraco.cms/businesslogic/media/MediaType.cs @@ -2,7 +2,6 @@ using System; using System.Collections.Generic; using Umbraco.Core; using Umbraco.Core.Models; -using Umbraco.Core.Persistence.Caching; using System.Linq; using umbraco.BusinessLogic; @@ -124,15 +123,6 @@ namespace umbraco.cms.businesslogic.media if (!e.Cancel) { - if (MasterContentType != 0) - MediaTypeItem.ParentId = MasterContentType; - - foreach (var masterContentType in MasterContentTypes) - { - var contentType = ApplicationContext.Current.Services.ContentTypeService.GetMediaType(masterContentType); - MediaTypeItem.AddContentType(contentType); - } - var current = User.GetCurrent(); int userId = current == null ? 0 : current.Id; ApplicationContext.Current.Services.ContentTypeService.Save(MediaTypeItem, userId); diff --git a/src/umbraco.cms/businesslogic/web/DocumentType.cs b/src/umbraco.cms/businesslogic/web/DocumentType.cs index 540ad963e2..27c2c4cb9f 100644 --- a/src/umbraco.cms/businesslogic/web/DocumentType.cs +++ b/src/umbraco.cms/businesslogic/web/DocumentType.cs @@ -6,13 +6,11 @@ using System.Linq; using Umbraco.Core.Configuration; using Umbraco.Core.Logging; using Umbraco.Core.Models; -using Umbraco.Core.Persistence.Caching; using umbraco.BusinessLogic; using Umbraco.Core.Services; using umbraco.DataLayer; using System.Collections.Generic; using Umbraco.Core; -using PropertyType = umbraco.cms.businesslogic.propertytype.PropertyType; namespace umbraco.cms.businesslogic.web { @@ -508,15 +506,6 @@ namespace umbraco.cms.businesslogic.web if (!e.Cancel) { - if (MasterContentType != 0) - ContentType.ParentId = MasterContentType; - - /*foreach (var masterContentType in MasterContentTypes) - { - var contentType = ApplicationContext.Current.Services.ContentTypeService.GetContentType(masterContentType); - ContentType.AddContentType(contentType); - }*/ - var current = User.GetCurrent(); int userId = current == null ? 0 : current.Id; ApplicationContext.Current.Services.ContentTypeService.Save(ContentType, userId);