2018-06-29 19:52:40 +02:00
using System ;
using System.Collections.Generic ;
using System.Linq ;
using AutoMapper ;
using Umbraco.Core ;
using Umbraco.Core.Logging ;
using Umbraco.Core.Models ;
using Umbraco.Core.PropertyEditors ;
using Umbraco.Core.Services ;
using Umbraco.Web.Composing ;
using Umbraco.Web.Models.ContentEditing ;
using ContentVariation = Umbraco . Core . Models . ContentVariation ;
namespace Umbraco.Web.Models.Mapping
{
/// <summary>
/// Creates a base generic ContentPropertyBasic from a Property
/// </summary>
internal class ContentPropertyBasicConverter < TDestination > : ITypeConverter < Property , TDestination >
where TDestination : ContentPropertyBasic , new ( )
{
private readonly ILogger _logger ;
private readonly PropertyEditorCollection _propertyEditors ;
protected IDataTypeService DataTypeService { get ; }
public ContentPropertyBasicConverter ( IDataTypeService dataTypeService , ILogger logger , PropertyEditorCollection propertyEditors )
{
_logger = logger ;
_propertyEditors = propertyEditors ;
DataTypeService = dataTypeService ;
}
/// <summary>
/// Assigns the PropertyEditor, Id, Alias and Value to the property
/// </summary>
/// <returns></returns>
public virtual TDestination Convert ( Property property , TDestination dest , ResolutionContext context )
{
var editor = _propertyEditors [ property . PropertyType . PropertyEditorAlias ] ;
if ( editor = = null )
{
_logger . Error < ContentPropertyBasicConverter < TDestination > > (
2018-08-17 15:41:58 +01:00
new NullReferenceException ( "The property editor with alias " + property . PropertyType . PropertyEditorAlias + " does not exist" ) ,
"No property editor '{PropertyEditorAlias}' found, converting to a Label" ,
property . PropertyType . PropertyEditorAlias ) ;
2018-06-29 19:52:40 +02:00
2019-02-15 11:42:29 +01:00
editor = _propertyEditors [ Constants . PropertyEditors . Aliases . Label ] ;
2018-06-29 19:52:40 +02:00
}
var result = new TDestination
{
Id = property . Id ,
Alias = property . Alias ,
PropertyEditor = editor ,
Editor = editor . Alias
} ;
// if there's a set of property aliases specified, we will check if the current property's value should be mapped.
// if it isn't one of the ones specified in 'includeProperties', we will just return the result without mapping the Value.
2018-09-17 13:19:24 +02:00
var includedProperties = context . Options . GetIncludedProperties ( ) ;
if ( includedProperties ! = null & & ! includedProperties . Contains ( property . Alias ) )
return result ;
2018-05-09 14:35:23 +10:00
2018-07-19 19:32:07 +10:00
//Get the culture from the context which will be set during the mapping operation for each property
2018-09-17 13:19:24 +02:00
var culture = context . Options . GetCulture ( ) ;
2018-05-09 14:35:23 +10:00
//a culture needs to be in the context for a property type that can vary
2018-06-20 14:18:57 +02:00
if ( culture = = null & & property . PropertyType . VariesByCulture ( ) )
2018-08-02 20:00:35 +10:00
throw new InvalidOperationException ( $"No culture found in mapping operation when one is required for the culture variant property type {property.PropertyType.Alias}" ) ;
2018-05-09 14:35:23 +10:00
//set the culture to null if it's an invariant property type
2018-06-20 14:18:57 +02:00
culture = ! property . PropertyType . VariesByCulture ( ) ? null : culture ;
2018-06-29 19:52:40 +02:00
2018-08-07 17:10:02 +10:00
result . Culture = culture ;
2018-06-29 19:52:40 +02:00
// if no 'IncludeProperties' were specified or this property is set to be included - we will map the value and return.
result . Value = editor . GetValueEditor ( ) . ToEditor ( property , DataTypeService , culture ) ;
return result ;
}
}
}