fixes most of the newly failing unit tests and adds better null checking
This commit is contained in:
@@ -94,7 +94,10 @@ namespace Umbraco.Core.Persistence.Factories
|
||||
var propertyType = new PropertyType(typeDto.ControlId,
|
||||
//ensures that any built-in membership properties have their correct dbtype assigned no matter
|
||||
//what the underlying data type is
|
||||
MemberTypeRepository.GetDbTypeForProperty(typeDto.Alias, typeDto.DbType.EnumParse<DataTypeDatabaseType>(true), standardProps))
|
||||
MemberTypeRepository.GetDbTypeForBuiltInProperty(
|
||||
typeDto.Alias,
|
||||
typeDto.DbType.EnumParse<DataTypeDatabaseType>(true),
|
||||
standardProps).Result)
|
||||
{
|
||||
Alias = typeDto.Alias,
|
||||
DataTypeDefinitionId = typeDto.DataTypeId,
|
||||
|
||||
@@ -145,8 +145,7 @@ namespace Umbraco.Core.Persistence.Repositories
|
||||
//If the Id of the DataType is not set, we resolve it from the db by its ControlId
|
||||
if (propertyType.DataTypeDefinitionId == 0 || propertyType.DataTypeDefinitionId == default(int))
|
||||
{
|
||||
var datatype = Database.FirstOrDefault<DataTypeDto>("WHERE controlId = @Id", new { Id = propertyType.DataTypeId });
|
||||
propertyType.DataTypeDefinitionId = datatype.DataTypeId;
|
||||
AssignDataTypeFromPropertyEditor(propertyType);
|
||||
}
|
||||
var propertyTypeDto = propertyFactory.BuildPropertyTypeDto(tabId, propertyType);
|
||||
int typePrimaryKey = Convert.ToInt32(Database.Insert(propertyTypeDto));
|
||||
@@ -299,24 +298,11 @@ namespace Umbraco.Core.Persistence.Repositories
|
||||
foreach (var propertyType in entity.PropertyTypes)
|
||||
{
|
||||
var tabId = propertyType.PropertyGroupId != null ? propertyType.PropertyGroupId.Value : default(int);
|
||||
|
||||
//If the Id of the DataType is not set, we resolve it from the db by its ControlId
|
||||
if (propertyType.DataTypeDefinitionId == 0 || propertyType.DataTypeDefinitionId == default(int))
|
||||
{
|
||||
//we cannot try to assign a data type of it's an empty guid
|
||||
if (propertyType.DataTypeId != Guid.Empty)
|
||||
{
|
||||
var sql = new Sql()
|
||||
.Select("*")
|
||||
.From<DataTypeDto>()
|
||||
.Where("controlId = @Id", new { Id = propertyType.DataTypeId })
|
||||
.OrderBy<DataTypeDto>(typeDto => typeDto.DataTypeId);
|
||||
var datatype = Database.FirstOrDefault<DataTypeDto>(sql);
|
||||
//we cannot assign a data type if one was not found
|
||||
if (datatype != null)
|
||||
{
|
||||
propertyType.DataTypeDefinitionId = datatype.DataTypeId;
|
||||
}
|
||||
}
|
||||
AssignDataTypeFromPropertyEditor(propertyType);
|
||||
}
|
||||
|
||||
//validate the alias!
|
||||
@@ -433,5 +419,32 @@ namespace Umbraco.Core.Persistence.Repositories
|
||||
throw exception;
|
||||
});
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Try to set the data type id based on its ControlId
|
||||
/// </summary>
|
||||
/// <param name="propertyType"></param>
|
||||
private void AssignDataTypeFromPropertyEditor(PropertyType propertyType)
|
||||
{
|
||||
//we cannot try to assign a data type of it's an empty guid
|
||||
if (propertyType.DataTypeId != Guid.Empty)
|
||||
{
|
||||
var sql = new Sql()
|
||||
.Select("*")
|
||||
.From<DataTypeDto>()
|
||||
.Where("controlId = @Id", new { Id = propertyType.DataTypeId })
|
||||
.OrderBy<DataTypeDto>(typeDto => typeDto.DataTypeId);
|
||||
var datatype = Database.FirstOrDefault<DataTypeDto>(sql);
|
||||
//we cannot assign a data type if one was not found
|
||||
if (datatype != null)
|
||||
{
|
||||
propertyType.DataTypeDefinitionId = datatype.DataTypeId;
|
||||
}
|
||||
else
|
||||
{
|
||||
LogHelper.Warn<ContentTypeBaseRepository<TId, TEntity>>("Could not assign a data type for the property type " + propertyType.Alias + " since no data type was found with a property editor " + propertyType.DataTypeId);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -245,10 +245,14 @@ namespace Umbraco.Core.Persistence.Repositories
|
||||
var stdProps = Constants.Conventions.Member.GetStandardPropertyTypeStubs();
|
||||
foreach (var propertyType in memberType.PropertyTypes)
|
||||
{
|
||||
propertyType.DataTypeDatabaseType = GetDbTypeForProperty(propertyType.Alias, propertyType.DataTypeDatabaseType, stdProps);
|
||||
//this reset's it's current data type reference which will be re-assigned based on the property editor assigned on the next line
|
||||
propertyType.DataTypeDefinitionId = 0;
|
||||
propertyType.DataTypeId = GetPropertyEditorForProperty(propertyType.Alias, propertyType.DataTypeId, stdProps);
|
||||
var dbTypeAttempt = GetDbTypeForBuiltInProperty(propertyType.Alias, propertyType.DataTypeDatabaseType, stdProps);
|
||||
if (dbTypeAttempt)
|
||||
{
|
||||
propertyType.DataTypeDatabaseType = dbTypeAttempt.Result;
|
||||
//this reset's it's current data type reference which will be re-assigned based on the property editor assigned on the next line
|
||||
propertyType.DataTypeDefinitionId = 0;
|
||||
propertyType.DataTypeId = GetPropertyEditorForBuiltInProperty(propertyType.Alias, propertyType.DataTypeId, stdProps).Result;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -273,8 +277,10 @@ namespace Umbraco.Core.Persistence.Repositories
|
||||
/// <param name="propAlias"></param>
|
||||
/// <param name="dbType"></param>
|
||||
/// <param name="standardProps"></param>
|
||||
/// <returns></returns>
|
||||
internal static DataTypeDatabaseType GetDbTypeForProperty(
|
||||
/// <returns>
|
||||
/// Successful attempt if it was a built in property
|
||||
/// </returns>
|
||||
internal static Attempt<DataTypeDatabaseType> GetDbTypeForBuiltInProperty(
|
||||
string propAlias,
|
||||
DataTypeDatabaseType dbType,
|
||||
Dictionary<string, PropertyType> standardProps)
|
||||
@@ -285,13 +291,22 @@ namespace Umbraco.Core.Persistence.Repositories
|
||||
if (aliases.Contains(propAlias))
|
||||
{
|
||||
//return the pre-determined db type for this property
|
||||
return standardProps.Single(x => x.Key == propAlias).Value.DataTypeDatabaseType;
|
||||
return Attempt<DataTypeDatabaseType>.Succeed(standardProps.Single(x => x.Key == propAlias).Value.DataTypeDatabaseType);
|
||||
}
|
||||
|
||||
return dbType;
|
||||
return Attempt<DataTypeDatabaseType>.Fail(dbType);
|
||||
}
|
||||
|
||||
internal static Guid GetPropertyEditorForProperty(
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="propAlias"></param>
|
||||
/// <param name="propertyEditor"></param>
|
||||
/// <param name="standardProps"></param>
|
||||
/// <returns>
|
||||
/// Successful attempt if it was a built in property
|
||||
/// </returns>
|
||||
internal static Attempt<Guid> GetPropertyEditorForBuiltInProperty(
|
||||
string propAlias,
|
||||
Guid propertyEditor,
|
||||
Dictionary<string, PropertyType> standardProps)
|
||||
@@ -302,10 +317,10 @@ namespace Umbraco.Core.Persistence.Repositories
|
||||
if (aliases.Contains(propAlias))
|
||||
{
|
||||
//return the pre-determined db type for this property
|
||||
return standardProps.Single(x => x.Key == propAlias).Value.DataTypeId;
|
||||
return Attempt<Guid>.Succeed(standardProps.Single(x => x.Key == propAlias).Value.DataTypeId);
|
||||
}
|
||||
|
||||
return propertyEditor;
|
||||
return Attempt<Guid>.Fail(propertyEditor);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user