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

53 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.Services;
2017-05-30 18:13:11 +02:00
using Umbraco.Web.Composing;
using Umbraco.Web.Models.ContentEditing;
namespace Umbraco.Web.Models.Mapping
{
/// <summary>
/// Creates a base generic ContentPropertyBasic from a Property
/// </summary>
2017-07-19 13:42:47 +02:00
internal class ContentPropertyBasicConverter<TDestination> : ITypeConverter<Property, TDestination>
where TDestination : ContentPropertyBasic, new()
{
2017-07-19 13:42:47 +02:00
protected Lazy<IDataTypeService> DataTypeService { get; }
public ContentPropertyBasicConverter(Lazy<IDataTypeService> dataTypeService)
{
DataTypeService = dataTypeService;
}
/// <summary>
/// Assigns the PropertyEditor, Id, Alias and Value to the property
/// </summary>
/// <returns></returns>
2017-07-19 13:42:47 +02:00
public virtual TDestination Convert(Property property, TDestination dest, ResolutionContext context)
{
2016-08-07 17:08:57 +02:00
var editor = Current.PropertyEditors[property.PropertyType.PropertyEditorAlias];
if (editor == null)
{
2017-07-19 13:42:47 +02:00
Current.Logger.Error<ContentPropertyBasicConverter<TDestination>>(
"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];
}
2017-07-19 13:42:47 +02:00
var result = new TDestination
{
Id = property.Id,
Value = editor.ValueEditor.ConvertDbToEditor(property, property.PropertyType, DataTypeService.Value),
2017-07-19 13:42:47 +02:00
Alias = property.Alias,
PropertyEditor = editor,
Editor = editor.Alias
};
return result;
}
}
2017-07-20 11:21:28 +02:00
}