Refactor IContentTypeBase API to use property group aliases

This commit is contained in:
Ronald Barendse
2021-09-07 20:48:45 +02:00
parent fc9c69d1fa
commit e2497d66a8
8 changed files with 65 additions and 101 deletions

View File

@@ -298,23 +298,15 @@ namespace Umbraco.Cms.Core.Models
/// <summary>
/// Checks whether a PropertyType with a given alias already exists
/// </summary>
/// <param name="propertyTypeAlias">Alias of the PropertyType</param>
/// <param name="alias">Alias of the PropertyType</param>
/// <returns>Returns <c>True</c> if a PropertyType with the passed in alias exists, otherwise <c>False</c></returns>
public abstract bool PropertyTypeExists(string propertyTypeAlias);
public abstract bool PropertyTypeExists(string alias);
/// <inheritdoc />
[Obsolete("Use AddPropertyGroup(name, alias) instead to explicitly set the alias.")]
public virtual bool AddPropertyGroup(string groupName) => AddPropertyGroup(groupName, groupName.ToSafeAlias(_shortStringHelper, true));
public abstract bool AddPropertyGroup(string alias, string name);
/// <inheritdoc />
public abstract bool AddPropertyGroup(string name, string alias);
/// <inheritdoc />
[Obsolete("Use AddPropertyType(propertyType, groupAlias, groupName) instead to explicitly set the alias of the group (note the slighty different parameter order).")]
public virtual bool AddPropertyType(IPropertyType propertyType, string propertyGroupName) => AddPropertyType(propertyType, propertyGroupName.ToSafeAlias(_shortStringHelper, true), propertyGroupName);
/// <inheritdoc />
public abstract bool AddPropertyType(IPropertyType propertyType, string groupAlias, string groupName);
public abstract bool AddPropertyType(IPropertyType propertyType, string propertyGroupAlias, string propertyGroupName = null);
/// <summary>
/// Adds a PropertyType, which does not belong to a PropertyGroup.
@@ -336,11 +328,11 @@ namespace Umbraco.Cms.Core.Models
/// Moves a PropertyType to a specified PropertyGroup
/// </summary>
/// <param name="propertyTypeAlias">Alias of the PropertyType to move</param>
/// <param name="propertyGroupName">Name of the PropertyGroup to move the PropertyType to</param>
/// <param name="propertyGroupAlias">Alias of the PropertyGroup to move the PropertyType to</param>
/// <returns></returns>
/// <remarks>If <paramref name="propertyGroupName"/> is null then the property is moved back to
/// <remarks>If <paramref name="propertyGroupAlias"/> is null then the property is moved back to
/// "generic properties" ie does not have a tab anymore.</remarks>
public bool MovePropertyType(string propertyTypeAlias, string propertyGroupName)
public bool MovePropertyType(string propertyTypeAlias, string propertyGroupAlias)
{
// get property, ensure it exists
var propertyType = PropertyTypes.FirstOrDefault(x => x.Alias == propertyTypeAlias);
@@ -348,9 +340,9 @@ namespace Umbraco.Cms.Core.Models
// get new group, if required, and ensure it exists
PropertyGroup newPropertyGroup = null;
if (propertyGroupName != null)
if (propertyGroupAlias != null)
{
var index = PropertyGroups.IndexOfKey(propertyGroupName);
var index = PropertyGroups.IndexOfKey(propertyGroupAlias);
if (index == -1) return false;
newPropertyGroup = PropertyGroups[index];
@@ -373,13 +365,13 @@ namespace Umbraco.Cms.Core.Models
/// <summary>
/// Removes a PropertyType from the current ContentType
/// </summary>
/// <param name="propertyTypeAlias">Alias of the <see cref="IPropertyType"/> to remove</param>
public void RemovePropertyType(string propertyTypeAlias)
/// <param name="alias">Alias of the <see cref="IPropertyType"/> to remove</param>
public void RemovePropertyType(string alias)
{
//check through each property group to see if we can remove the property type by alias from it
foreach (var propertyGroup in PropertyGroups)
{
if (propertyGroup.PropertyTypes.RemoveItem(propertyTypeAlias))
if (propertyGroup.PropertyTypes.RemoveItem(alias))
{
if (!HasPropertyTypeBeenRemoved)
{
@@ -391,7 +383,7 @@ namespace Umbraco.Cms.Core.Models
}
//check through each local property type collection (not assigned to a tab)
if (_noGroupPropertyTypes.RemoveItem(propertyTypeAlias))
if (_noGroupPropertyTypes.RemoveItem(alias))
{
if (!HasPropertyTypeBeenRemoved)
{
@@ -404,11 +396,11 @@ namespace Umbraco.Cms.Core.Models
/// <summary>
/// Removes a PropertyGroup from the current ContentType
/// </summary>
/// <param name="propertyGroupName">Name of the <see cref="PropertyGroup"/> to remove</param>
public void RemovePropertyGroup(string propertyGroupName)
/// <param name="alias">Alias of the <see cref="PropertyGroup"/> to remove</param>
public void RemovePropertyGroup(string alias)
{
// if no group exists with that name, do nothing
var index = PropertyGroups.IndexOfKey(propertyGroupName);
// if no group exists with that alias, do nothing
var index = PropertyGroups.IndexOfKey(alias);
if (index == -1) return;
var group = PropertyGroups[index];

View File

@@ -17,7 +17,8 @@ namespace Umbraco.Cms.Core.Models
private List<IContentTypeComposition> _contentTypeComposition = new List<IContentTypeComposition>();
private List<int> _removedContentTypeKeyTracker = new List<int>();
protected ContentTypeCompositionBase(IShortStringHelper shortStringHelper, int parentId) : base(shortStringHelper, parentId)
protected ContentTypeCompositionBase(IShortStringHelper shortStringHelper, int parentId)
: base(shortStringHelper, parentId)
{ }
protected ContentTypeCompositionBase(IShortStringHelper shortStringHelper,IContentTypeComposition parent)
@@ -61,7 +62,7 @@ namespace Umbraco.Cms.Core.Models
void AcquireProperty(IPropertyType propertyType)
{
propertyType.Variations = propertyType.Variations & Variations;
propertyType.Variations &= Variations;
propertyType.ResetDirtyProperties(false);
}
@@ -90,7 +91,7 @@ namespace Umbraco.Cms.Core.Models
IPropertyType AcquireProperty(IPropertyType propertyType)
{
propertyType = (IPropertyType) propertyType.DeepClone();
propertyType.Variations = propertyType.Variations & Variations;
propertyType.Variations &= Variations;
propertyType.ResetDirtyProperties(false);
return propertyType;
}
@@ -132,8 +133,8 @@ namespace Umbraco.Cms.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.
// 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))
@@ -143,9 +144,12 @@ namespace Umbraco.Cms.Core.Models
throw new InvalidCompositionException(Alias, contentType.Alias, conflictingPropertyTypeAliases.ToArray());
_contentTypeComposition.Add(contentType);
OnPropertyChanged(nameof(ContentTypeComposition));
return true;
}
return false;
}
@@ -159,19 +163,21 @@ namespace Umbraco.Cms.Core.Models
if (ContentTypeCompositionExists(alias))
{
var contentTypeComposition = ContentTypeComposition.FirstOrDefault(x => x.Alias == alias);
if (contentTypeComposition == null)//You can't remove a composition from another composition
if (contentTypeComposition == null) // You can't remove a composition from another composition
return false;
_removedContentTypeKeyTracker.Add(contentTypeComposition.Id);
//If the ContentType we are removing has Compositions of its own these needs to be removed as well
// If the ContentType we are removing has Compositions of its own these needs to be removed as well
var compositionIdsToRemove = contentTypeComposition.CompositionIds().ToList();
if (compositionIdsToRemove.Any())
_removedContentTypeKeyTracker.AddRange(compositionIdsToRemove);
OnPropertyChanged(nameof(ContentTypeComposition));
return _contentTypeComposition.Remove(contentTypeComposition);
}
return false;
}
@@ -194,20 +200,14 @@ namespace Umbraco.Cms.Core.Models
/// <summary>
/// Checks whether a PropertyType with a given alias already exists
/// </summary>
/// <param name="propertyTypeAlias">Alias of the PropertyType</param>
/// <param name="alias">Alias of the PropertyType</param>
/// <returns>Returns <c>True</c> if a PropertyType with the passed in alias exists, otherwise <c>False</c></returns>
public override bool PropertyTypeExists(string propertyTypeAlias)
{
return CompositionPropertyTypes.Any(x => x.Alias == propertyTypeAlias);
}
public override bool PropertyTypeExists(string alias) => CompositionPropertyTypes.Any(x => x.Alias == alias);
/// <inheritdoc />
public override bool AddPropertyGroup(string name, string alias)
{
return AddAndReturnPropertyGroup(name, alias) != null;
}
public override bool AddPropertyGroup(string alias, string name) => AddAndReturnPropertyGroup(alias, name) != null;
private PropertyGroup AddAndReturnPropertyGroup(string name, string alias)
private PropertyGroup AddAndReturnPropertyGroup(string alias, string name)
{
// Ensure we don't have it already
if (PropertyGroups.Contains(alias))
@@ -216,8 +216,8 @@ namespace Umbraco.Cms.Core.Models
// Add new group
var group = new PropertyGroup(SupportsPublishing)
{
Name = name,
Alias = alias
Alias = alias,
Name = name
};
// check if it is inherited - there might be more than 1 but we want the 1st, to
@@ -244,7 +244,7 @@ namespace Umbraco.Cms.Core.Models
}
/// <inheritdoc />
public override bool AddPropertyType(IPropertyType propertyType, string groupAlias, string groupName)
public override bool AddPropertyType(IPropertyType propertyType, string propertyGroupAlias, string propertyGroupName = null)
{
// ensure no duplicate alias - over all composition properties
if (PropertyTypeExists(propertyType.Alias))
@@ -252,15 +252,16 @@ namespace Umbraco.Cms.Core.Models
// get and ensure a group local to this content type
PropertyGroup group;
var index = PropertyGroups.IndexOfKey(groupAlias);
var index = PropertyGroups.IndexOfKey(propertyGroupAlias);
if (index != -1)
{
group = PropertyGroups[index];
}
else if (!string.IsNullOrEmpty(groupName))
else if (!string.IsNullOrEmpty(propertyGroupName))
{
group = AddAndReturnPropertyGroup(groupName, groupAlias);
if (group == null) return false;
group = AddAndReturnPropertyGroup(propertyGroupAlias, propertyGroupName);
if (group == null)
return false;
}
else
{
@@ -281,11 +282,9 @@ namespace Umbraco.Cms.Core.Models
/// <returns>An enumerable list of string aliases</returns>
/// <remarks>Does not contain the alias of the Current ContentType</remarks>
public IEnumerable<string> CompositionAliases()
{
return ContentTypeComposition
=> ContentTypeComposition
.Select(x => x.Alias)
.Union(ContentTypeComposition.SelectMany(x => x.CompositionAliases()));
}
/// <summary>
/// Gets a list of ContentType Ids from the current composition
@@ -293,11 +292,9 @@ namespace Umbraco.Cms.Core.Models
/// <returns>An enumerable list of integer ids</returns>
/// <remarks>Does not contain the Id of the Current ContentType</remarks>
public IEnumerable<int> CompositionIds()
{
return ContentTypeComposition
=> ContentTypeComposition
.Select(x => x.Id)
.Union(ContentTypeComposition.SelectMany(x => x.CompositionIds()));
}
protected override void PerformDeepClone(object clone)
{
@@ -305,7 +302,7 @@ namespace Umbraco.Cms.Core.Models
var clonedEntity = (ContentTypeCompositionBase)clone;
//need to manually assign since this is an internal field and will not be automatically mapped
// need to manually assign since this is an internal field and will not be automatically mapped
clonedEntity._removedContentTypeKeyTracker = new List<int>();
clonedEntity._contentTypeComposition = ContentTypeComposition.Select(x => (IContentTypeComposition)x.DeepClone()).ToList();
}

View File

@@ -1,4 +1,3 @@
using System;
using System.Collections.Generic;
using Umbraco.Cms.Core.Models.Entities;
@@ -113,43 +112,32 @@ namespace Umbraco.Cms.Core.Models
/// <summary>
/// Removes a PropertyType from the current ContentType
/// </summary>
/// <param name="propertyTypeAlias">Alias of the <see cref="IPropertyType"/> to remove</param>
void RemovePropertyType(string propertyTypeAlias);
/// <param name="alias">Alias of the <see cref="IPropertyType"/> to remove</param>
void RemovePropertyType(string alias);
/// <summary>
/// Removes a property group from the current content type.
/// </summary>
/// <param name="propertyGroupName">Name of the <see cref="PropertyGroup" /> to remove</param>
void RemovePropertyGroup(string propertyGroupName); // TODO Rename to propertyGroupAlias
/// <param name="alias">Alias of the <see cref="PropertyGroup" /> to remove</param>
void RemovePropertyGroup(string alias);
/// <summary>
/// Checks whether a PropertyType with a given alias already exists
/// </summary>
/// <param name="propertyTypeAlias">Alias of the PropertyType</param>
/// <param name="alias">Alias of the PropertyType</param>
/// <returns>Returns <c>True</c> if a PropertyType with the passed in alias exists, otherwise <c>False</c></returns>
bool PropertyTypeExists(string propertyTypeAlias);
/// <summary>
/// Adds the property type to the specified property group (creates a new group if not found).
/// </summary>
/// <param name="propertyType">The property type to add.</param>
/// <param name="propertyGroupName">The name of the property group to add the property type to.</param>
/// <returns>
/// Returns <c>true</c> if the property type was added; otherwise, <c>false</c>.
/// </returns>
[Obsolete("Use AddPropertyType(propertyType, groupAlias, groupName) instead to explicitly set the alias of the group (note the slighty different parameter order).")]
bool AddPropertyType(IPropertyType propertyType, string propertyGroupName);
bool PropertyTypeExists(string alias);
/// <summary>
/// Adds the property type to the specified property group (creates a new group if not found and a name is specified).
/// </summary>
/// <param name="propertyType">The property type to add.</param>
/// <param name="groupAlias">The alias of the property group to add the property type to.</param>
/// <param name="groupName">The name of the property group to create when not found.</param>
/// <param name="propertyGroupAlias">The alias of the property group to add the property type to.</param>
/// <param name="propertyGroupName">The name of the property group to create when not found.</param>
/// <returns>
/// Returns <c>true</c> if the property type was added; otherwise, <c>false</c>.
/// </returns>
bool AddPropertyType(IPropertyType propertyType, string groupAlias, string groupName); // TODO Make groupName optional (add null as default value) after removing obsolete overload
bool AddPropertyType(IPropertyType propertyType, string propertyGroupAlias, string propertyGroupName = null);
/// <summary>
/// Adds a PropertyType, which does not belong to a PropertyGroup.
@@ -158,39 +146,26 @@ namespace Umbraco.Cms.Core.Models
/// <returns>Returns <c>True</c> if PropertyType was added, otherwise <c>False</c></returns>
bool AddPropertyType(IPropertyType propertyType);
/// <summary>
/// Adds a property group with the alias based on the specified <paramref name="groupName" />.
/// </summary>
/// <param name="groupName">Name of the group.</param>
/// <returns>
/// Returns <c>true</c> if a property group with specified <paramref name="groupName" /> was added; otherwise, <c>false</c>.
/// </returns>
/// <remarks>
/// This method will also check if a group already exists with the same alias.
/// </remarks>
[Obsolete("Use AddPropertyGroup(name, alias) instead to explicitly set the alias.")]
bool AddPropertyGroup(string groupName);
/// <summary>
/// Adds a property group with the specified <paramref name="name" /> and <paramref name="alias" />.
/// </summary>
/// <param name="name">Name of the group.</param>
/// <param name="alias">The alias.</param>
/// <param name="name">Name of the group.</param>
/// <returns>
/// Returns <c>true</c> if a property group with specified <paramref name="alias" /> was added; otherwise, <c>false</c>.
/// </returns>
/// <remarks>
/// This method will also check if a group already exists with the same alias.
/// </remarks>
bool AddPropertyGroup(string name, string alias);
bool AddPropertyGroup(string alias, string name);
/// <summary>
/// Moves a PropertyType to a specified PropertyGroup
/// </summary>
/// <param name="propertyTypeAlias">Alias of the PropertyType to move</param>
/// <param name="propertyGroupName">Name of the PropertyGroup to move the PropertyType to</param>
/// <param name="propertyGroupAlias">Alias of the PropertyGroup to move the PropertyType to</param>
/// <returns></returns>
bool MovePropertyType(string propertyTypeAlias, string propertyGroupName); // TODO Rename to propertyGroupAlias
bool MovePropertyType(string propertyTypeAlias, string propertyGroupAlias);
/// <summary>
/// Gets an <see cref="ISimpleContentType"/> corresponding to this content type.

View File

@@ -824,7 +824,7 @@ namespace Umbraco.Cms.Infrastructure.Packaging
alias = name.ToSafeAlias(_shortStringHelper, true);
}
contentType.AddPropertyGroup(name, alias);
contentType.AddPropertyGroup(alias, name);
var propertyGroup = contentType.PropertyGroups[alias];
if (Guid.TryParse(propertyGroupElement.Element("Key")?.Value, out var key))

View File

@@ -255,7 +255,7 @@ namespace Umbraco.Cms.Infrastructure.Persistence.Repositories.Implement
if (contentType is IMemberType memberType)
{
// ensure that the group exists (ok if it already exists)
memberType.AddPropertyGroup(Cms.Core.Constants.Conventions.Member.StandardPropertiesGroupName, Cms.Core.Constants.Conventions.Member.StandardPropertiesGroupAlias);
memberType.AddPropertyGroup(Cms.Core.Constants.Conventions.Member.StandardPropertiesGroupAlias, Cms.Core.Constants.Conventions.Member.StandardPropertiesGroupName);
// ensure that property types exist (ok if they already exist)
foreach (var (alias, propertyType) in builtinProperties)

View File

@@ -146,7 +146,7 @@ namespace Umbraco.Cms.Infrastructure.Persistence.Repositories.Implement
}
//By Convention we add 9 standard PropertyTypes to an Umbraco MemberType
entity.AddPropertyGroup(Cms.Core.Constants.Conventions.Member.StandardPropertiesGroupName, Cms.Core.Constants.Conventions.Member.StandardPropertiesGroupAlias);
entity.AddPropertyGroup(Cms.Core.Constants.Conventions.Member.StandardPropertiesGroupAlias, Cms.Core.Constants.Conventions.Member.StandardPropertiesGroupName);
var standardPropertyTypes = ConventionsHelper.GetStandardPropertyTypeStubs(_shortStringHelper);
foreach (var standardPropertyType in standardPropertyTypes)
{

View File

@@ -269,7 +269,7 @@ namespace Umbraco.TestData
Name = "Umbraco Test Data Content",
Icon = "icon-science color-green"
};
docType.AddPropertyGroup("Content", "content");
docType.AddPropertyGroup("content", "Content");
docType.AddPropertyType(new PropertyType(_shortStringHelper, GetOrCreateRichText(), "review")
{
Name = "Review"

View File

@@ -1251,7 +1251,7 @@ namespace Umbraco.Cms.Tests.Integration.Umbraco.Infrastructure.Services
Assert.AreEqual(2, meta.PropertyTypes.Count());
Assert.AreEqual("Meta Keywords", meta.PropertyTypes.First().Name);
Assert.AreEqual("Meta Description", meta.PropertyTypes.Skip(1).First().Name);
meta.AddPropertyGroup("Content", "content");
meta.AddPropertyGroup("content", "Content");
Assert.AreEqual(2, meta.PropertyTypes.Count());
ContentTypeService.Save(meta);
@@ -1488,8 +1488,8 @@ namespace Umbraco.Cms.Tests.Integration.Umbraco.Infrastructure.Services
public void Can_Remove_PropertyGroup_Without_Removing_Property_Types()
{
var basePage = (IContentType)ContentTypeBuilder.CreateBasicContentType();
basePage.AddPropertyGroup("Content", "content");
basePage.AddPropertyGroup("Meta", "meta");
basePage.AddPropertyGroup("content", "Content");
basePage.AddPropertyGroup("meta", "Meta");
ContentTypeService.Save(basePage);
var authorPropertyType = new PropertyType(ShortStringHelper, Constants.PropertyEditors.Aliases.TextBox, ValueStorageType.Ntext, "author")