made static, however this has been refactored will need extra sanity checking to ensure nothing has been broken

This commit is contained in:
Ismail Mayat
2018-06-29 14:30:20 +01:00
parent 058033527d
commit 8f31ea7b3d
2 changed files with 156 additions and 172 deletions

View File

@@ -1,162 +1,148 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Umbraco.Core.Models;
using Umbraco.Core.Persistence.Dtos;
namespace Umbraco.Core.Persistence.Factories
{
internal class PropertyGroupFactory
{
private readonly int _contentTypeId;
private readonly DateTime _createDate;
private readonly DateTime _updateDate;
//a callback to create a property type which can be injected via a contructor
private readonly Func<string, ValueStorageType, string, PropertyType> _propertyTypeCtor;
public PropertyGroupFactory(int contentTypeId)
{
_contentTypeId = contentTypeId;
_propertyTypeCtor = (propertyEditorAlias, dbType, alias) => new PropertyType(propertyEditorAlias, dbType);
}
public PropertyGroupFactory(int contentTypeId, DateTime createDate, DateTime updateDate, Func<string, ValueStorageType, string, PropertyType> propertyTypeCtor)
{
_contentTypeId = contentTypeId;
_createDate = createDate;
_updateDate = updateDate;
_propertyTypeCtor = propertyTypeCtor;
}
#region Implementation of IEntityFactory<IEnumerable<PropertyGroup>,IEnumerable<TabDto>>
public IEnumerable<PropertyGroup> BuildEntity(IEnumerable<PropertyTypeGroupDto> groupDtos, bool isPublishing)
{
// groupDtos contains all the groups, those that are defined on the current
// content type, and those that are inherited from composition content types
var propertyGroups = new PropertyGroupCollection();
foreach (var groupDto in groupDtos)
{
var group = new PropertyGroup(isPublishing);
try
{
group.DisableChangeTracking();
// if the group is defined on the current content type,
// assign its identifier, else it will be zero
if (groupDto.ContentTypeNodeId == _contentTypeId)
group.Id = groupDto.Id;
group.Name = groupDto.Text;
group.SortOrder = groupDto.SortOrder;
group.PropertyTypes = new PropertyTypeCollection(isPublishing);
group.Key = groupDto.UniqueId;
//Because we are likely to have a group with no PropertyTypes we need to ensure that these are excluded
var typeDtos = groupDto.PropertyTypeDtos.Where(x => x.Id > 0);
foreach (var typeDto in typeDtos)
{
var tempGroupDto = groupDto;
var propertyType = _propertyTypeCtor(typeDto.DataTypeDto.EditorAlias,
typeDto.DataTypeDto.DbType.EnumParse<ValueStorageType>(true),
typeDto.Alias);
try
{
propertyType.DisableChangeTracking();
propertyType.Alias = typeDto.Alias;
propertyType.DataTypeId = typeDto.DataTypeId;
propertyType.Description = typeDto.Description;
propertyType.Id = typeDto.Id;
propertyType.Key = typeDto.UniqueId;
propertyType.Name = typeDto.Name;
propertyType.Mandatory = typeDto.Mandatory;
propertyType.SortOrder = typeDto.SortOrder;
propertyType.ValidationRegExp = typeDto.ValidationRegExp;
propertyType.PropertyGroupId = new Lazy<int>(() => tempGroupDto.Id);
propertyType.CreateDate = _createDate;
propertyType.UpdateDate = _updateDate;
propertyType.Variations = (ContentVariation) typeDto.Variations;
// reset dirty initial properties (U4-1946)
propertyType.ResetDirtyProperties(false);
group.PropertyTypes.Add(propertyType);
}
finally
{
propertyType.EnableChangeTracking();
}
}
// reset dirty initial properties (U4-1946)
group.ResetDirtyProperties(false);
propertyGroups.Add(group);
}
finally
{
group.EnableChangeTracking();
}
}
return propertyGroups;
}
public IEnumerable<PropertyTypeGroupDto> BuildDto(IEnumerable<PropertyGroup> entity)
{
return entity.Select(BuildGroupDto).ToList();
}
#endregion
internal PropertyTypeGroupDto BuildGroupDto(PropertyGroup propertyGroup)
{
var dto = new PropertyTypeGroupDto
{
ContentTypeNodeId = _contentTypeId,
SortOrder = propertyGroup.SortOrder,
Text = propertyGroup.Name,
UniqueId = propertyGroup.Key
};
if (propertyGroup.HasIdentity)
dto.Id = propertyGroup.Id;
dto.PropertyTypeDtos = propertyGroup.PropertyTypes.Select(propertyType => BuildPropertyTypeDto(propertyGroup.Id, propertyType)).ToList();
return dto;
}
internal PropertyTypeDto BuildPropertyTypeDto(int tabId, PropertyType propertyType)
{
var propertyTypeDto = new PropertyTypeDto
{
Alias = propertyType.Alias,
ContentTypeId = _contentTypeId,
DataTypeId = propertyType.DataTypeId,
Description = propertyType.Description,
Mandatory = propertyType.Mandatory,
Name = propertyType.Name,
SortOrder = propertyType.SortOrder,
ValidationRegExp = propertyType.ValidationRegExp,
UniqueId = propertyType.Key,
Variations = (byte) propertyType.Variations
};
if (tabId != default)
{
propertyTypeDto.PropertyTypeGroupId = tabId;
}
else
{
propertyTypeDto.PropertyTypeGroupId = null;
}
if (propertyType.HasIdentity)
propertyTypeDto.Id = propertyType.Id;
return propertyTypeDto;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using Umbraco.Core.Models;
using Umbraco.Core.Persistence.Dtos;
namespace Umbraco.Core.Persistence.Factories
{
internal static class PropertyGroupFactory
{
#region Implementation of IEntityFactory<IEnumerable<PropertyGroup>,IEnumerable<TabDto>>
public static IEnumerable<PropertyGroup> BuildEntity(IEnumerable<PropertyTypeGroupDto> groupDtos,
bool isPublishing,
int contentTypeId,
DateTime createDate,
DateTime updateDate,
Func<string, ValueStorageType, string, PropertyType> propertyTypeCtor)
{
// groupDtos contains all the groups, those that are defined on the current
// content type, and those that are inherited from composition content types
var propertyGroups = new PropertyGroupCollection();
foreach (var groupDto in groupDtos)
{
var group = new PropertyGroup(isPublishing);
try
{
group.DisableChangeTracking();
// if the group is defined on the current content type,
// assign its identifier, else it will be zero
if (groupDto.ContentTypeNodeId == contentTypeId)
group.Id = groupDto.Id;
group.Name = groupDto.Text;
group.SortOrder = groupDto.SortOrder;
group.PropertyTypes = new PropertyTypeCollection(isPublishing);
group.Key = groupDto.UniqueId;
//Because we are likely to have a group with no PropertyTypes we need to ensure that these are excluded
var typeDtos = groupDto.PropertyTypeDtos.Where(x => x.Id > 0);
foreach (var typeDto in typeDtos)
{
var tempGroupDto = groupDto;
var propertyType = propertyTypeCtor(typeDto.DataTypeDto.EditorAlias,
typeDto.DataTypeDto.DbType.EnumParse<ValueStorageType>(true),
typeDto.Alias);
try
{
propertyType.DisableChangeTracking();
propertyType.Alias = typeDto.Alias;
propertyType.DataTypeId = typeDto.DataTypeId;
propertyType.Description = typeDto.Description;
propertyType.Id = typeDto.Id;
propertyType.Key = typeDto.UniqueId;
propertyType.Name = typeDto.Name;
propertyType.Mandatory = typeDto.Mandatory;
propertyType.SortOrder = typeDto.SortOrder;
propertyType.ValidationRegExp = typeDto.ValidationRegExp;
propertyType.PropertyGroupId = new Lazy<int>(() => tempGroupDto.Id);
propertyType.CreateDate = createDate;
propertyType.UpdateDate = updateDate;
propertyType.Variations = (ContentVariation)typeDto.Variations;
// reset dirty initial properties (U4-1946)
propertyType.ResetDirtyProperties(false);
group.PropertyTypes.Add(propertyType);
}
finally
{
propertyType.EnableChangeTracking();
}
}
// reset dirty initial properties (U4-1946)
group.ResetDirtyProperties(false);
propertyGroups.Add(group);
}
finally
{
group.EnableChangeTracking();
}
}
return propertyGroups;
}
public static IEnumerable<PropertyTypeGroupDto> BuildDto(IEnumerable<PropertyGroup> entity)
{
return entity.Select(BuildGroupDto).ToList();
}
#endregion
internal static PropertyTypeGroupDto BuildGroupDto(PropertyGroup propertyGroup, int contentTypeId)
{
var dto = new PropertyTypeGroupDto
{
ContentTypeNodeId = contentTypeId,
SortOrder = propertyGroup.SortOrder,
Text = propertyGroup.Name,
UniqueId = propertyGroup.Key
};
if (propertyGroup.HasIdentity)
dto.Id = propertyGroup.Id;
dto.PropertyTypeDtos = propertyGroup.PropertyTypes.Select(propertyType => BuildPropertyTypeDto(propertyGroup.Id, propertyType, contentTypeId)).ToList();
return dto;
}
internal static PropertyTypeDto BuildPropertyTypeDto(int tabId, PropertyType propertyType, int contentTypeId)
{
var propertyTypeDto = new PropertyTypeDto
{
Alias = propertyType.Alias,
ContentTypeId = contentTypeId,
DataTypeId = propertyType.DataTypeId,
Description = propertyType.Description,
Mandatory = propertyType.Mandatory,
Name = propertyType.Name,
SortOrder = propertyType.SortOrder,
ValidationRegExp = propertyType.ValidationRegExp,
UniqueId = propertyType.Key,
Variations = (byte)propertyType.Variations
};
if (tabId != default)
{
propertyTypeDto.PropertyTypeGroupId = tabId;
}
else
{
propertyTypeDto.PropertyTypeGroupId = null;
}
if (propertyType.HasIdentity)
propertyTypeDto.Id = propertyType.Id;
return propertyTypeDto;
}
}
}

View File

@@ -191,13 +191,12 @@ AND umbracoNode.nodeObjectType = @objectType",
SortOrder = allowedContentType.SortOrder
});
}
var propertyFactory = new PropertyGroupFactory(nodeDto.NodeId);
//Insert Tabs
foreach (var propertyGroup in entity.PropertyGroups)
{
var tabDto = propertyFactory.BuildGroupDto(propertyGroup);
var tabDto = PropertyGroupFactory.BuildGroupDto(propertyGroup, nodeDto.NodeId);
var primaryKey = Convert.ToInt32(Database.Insert(tabDto));
propertyGroup.Id = primaryKey;//Set Id on PropertyGroup
@@ -222,7 +221,7 @@ AND umbracoNode.nodeObjectType = @objectType",
{
AssignDataTypeFromPropertyEditor(propertyType);
}
var propertyTypeDto = propertyFactory.BuildPropertyTypeDto(tabId, propertyType);
var propertyTypeDto = PropertyGroupFactory.BuildPropertyTypeDto(tabId, propertyType, nodeDto.NodeId);
int typePrimaryKey = Convert.ToInt32(Database.Insert(propertyTypeDto));
propertyType.Id = typePrimaryKey; //Set Id on new PropertyType
@@ -384,13 +383,12 @@ AND umbracoNode.id <> @id",
Database.Delete<PropertyTypeGroupDto>("WHERE id IN (@ids)", new { ids = groupsToDelete });
}
}
var propertyGroupFactory = new PropertyGroupFactory(entity.Id);
// insert or update groups, assign properties
foreach (var propertyGroup in entity.PropertyGroups)
{
// insert or update group
var groupDto = propertyGroupFactory.BuildGroupDto(propertyGroup);
var groupDto = PropertyGroupFactory.BuildGroupDto(propertyGroup,entity.Id);
var groupId = propertyGroup.HasIdentity
? Database.Update(groupDto)
: Convert.ToInt32(Database.Insert(groupDto));
@@ -419,7 +417,7 @@ AND umbracoNode.id <> @id",
ValidateAlias(propertyType);
// insert or update property
var propertyTypeDto = propertyGroupFactory.BuildPropertyTypeDto(groupId, propertyType);
var propertyTypeDto = PropertyGroupFactory.BuildPropertyTypeDto(groupId, propertyType, entity.Id);
var typeId = propertyType.HasIdentity
? Database.Update(propertyTypeDto)
: Convert.ToInt32(Database.Insert(propertyTypeDto));
@@ -480,8 +478,8 @@ AND umbracoNode.id <> @id",
var dtos = Database
.Fetch<PropertyTypeGroupDto>(sql);
var propertyGroupFactory = new PropertyGroupFactory(id, createDate, updateDate, CreatePropertyType);
var propertyGroups = propertyGroupFactory.BuildEntity(dtos, IsPublishing);
var propertyGroups = PropertyGroupFactory.BuildEntity(dtos, IsPublishing, id, createDate, updateDate,CreatePropertyType);
return new PropertyGroupCollection(propertyGroups);
}