Files
Umbraco-CMS/src/Umbraco.Web/Models/Mapping/ContentPropertyBasicConverter.cs

54 lines
2.0 KiB
C#
Raw Normal View History

using System;
using AutoMapper;
using Umbraco.Core;
using Umbraco.Core.Logging;
using Umbraco.Core.Models;
using Umbraco.Core.PropertyEditors;
using Umbraco.Core.Services;
using Umbraco.Web.Models.ContentEditing;
namespace Umbraco.Web.Models.Mapping
{
/// <summary>
/// Creates a base generic ContentPropertyBasic from a Property
/// </summary>
/// <typeparam name="T"></typeparam>
internal class ContentPropertyBasicConverter<T> : TypeConverter<Property, T>
where T : ContentPropertyBasic, new()
{
protected Lazy<IDataTypeService> DataTypeService { get; private set; }
public ContentPropertyBasicConverter(Lazy<IDataTypeService> dataTypeService)
{
DataTypeService = dataTypeService;
}
/// <summary>
/// Assigns the PropertyEditor, Id, Alias and Value to the property
/// </summary>
/// <param name="property"></param>
/// <returns></returns>
protected override T ConvertCore(Property property)
{
2016-08-07 17:08:57 +02:00
var editor = Current.PropertyEditors[property.PropertyType.PropertyEditorAlias];
if (editor == null)
{
Current.Logger.Error<ContentPropertyBasicConverter<T>>(
"No property editor found, converting to a Label",
new NullReferenceException("The property editor with alias " + property.PropertyType.PropertyEditorAlias + " does not exist"));
2016-08-07 17:08:57 +02:00
editor = Current.PropertyEditors[Constants.PropertyEditors.NoEditAlias];
}
var result = new T
{
Id = property.Id,
Value = editor.ValueEditor.ConvertDbToEditor(property, property.PropertyType, DataTypeService.Value),
Alias = property.Alias,
PropertyEditor = editor,
Editor = editor.Alias
};
return result;
}
}
}