using System;
using AutoMapper;
using Umbraco.Core;
using Umbraco.Core.Logging;
using Umbraco.Core.Models;
using Umbraco.Core.Services;
using Umbraco.Web.Composing;
using Umbraco.Web.Models.ContentEditing;
namespace Umbraco.Web.Models.Mapping
{
///
/// Creates a base generic ContentPropertyBasic from a Property
///
internal class ContentPropertyBasicConverter : ITypeConverter
where TDestination : ContentPropertyBasic, new()
{
protected Lazy DataTypeService { get; }
public ContentPropertyBasicConverter(Lazy dataTypeService)
{
DataTypeService = dataTypeService;
}
///
/// Assigns the PropertyEditor, Id, Alias and Value to the property
///
///
public virtual TDestination Convert(Property property, TDestination dest, ResolutionContext context)
{
var editor = Current.PropertyEditors[property.PropertyType.PropertyEditorAlias];
if (editor == null)
{
Current.Logger.Error>(
"No property editor found, converting to a Label",
new NullReferenceException("The property editor with alias " + property.PropertyType.PropertyEditorAlias + " does not exist"));
editor = Current.PropertyEditors[Constants.PropertyEditors.NoEditAlias];
}
var result = new TDestination
{
Id = property.Id,
Value = editor.ValueEditor.ConvertDbToEditor(property, property.PropertyType, DataTypeService.Value),
Alias = property.Alias,
PropertyEditor = editor,
Editor = editor.Alias
};
return result;
}
}
}