2013-08-12 15:06:12 +02:00
using System ;
2018-04-09 15:43:17 +02:00
using System.Collections.Generic ;
2018-04-17 08:36:36 +02:00
using System.Linq ;
2013-08-12 15:06:12 +02:00
using AutoMapper ;
using Umbraco.Core ;
2013-09-25 21:35:24 +10:00
using Umbraco.Core.Logging ;
2013-08-12 15:06:12 +02:00
using Umbraco.Core.Models ;
2018-04-04 01:59:51 +10:00
using Umbraco.Core.PropertyEditors ;
2013-11-19 11:51:01 +11:00
using Umbraco.Core.Services ;
2017-05-30 18:13:11 +02:00
using Umbraco.Web.Composing ;
2013-08-12 15:06:12 +02:00
using Umbraco.Web.Models.ContentEditing ;
2018-04-04 01:59:51 +10:00
using ContentVariation = Umbraco . Core . Models . ContentVariation ;
2013-08-12 15:06:12 +02:00
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 ( )
2013-08-12 15:06:12 +02:00
{
2018-04-04 01:59:51 +10:00
private readonly ILogger _logger ;
private readonly PropertyEditorCollection _propertyEditors ;
2018-03-27 10:04:07 +02:00
protected IDataTypeService DataTypeService { get ; }
2018-03-16 09:06:44 +01:00
2018-04-04 01:59:51 +10:00
public ContentPropertyBasicConverter ( IDataTypeService dataTypeService , ILogger logger , PropertyEditorCollection propertyEditors )
2013-11-19 11:51:01 +11:00
{
2018-04-04 01:59:51 +10:00
_logger = logger ;
_propertyEditors = propertyEditors ;
2018-03-27 10:04:07 +02:00
DataTypeService = dataTypeService ;
2013-11-19 11:51:01 +11:00
}
2013-08-14 19:24:20 +10:00
/// <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 )
2013-08-12 15:06:12 +02:00
{
2018-04-04 01:59:51 +10:00
var editor = _propertyEditors [ property . PropertyType . PropertyEditorAlias ] ;
2013-08-12 15:06:12 +02:00
if ( editor = = null )
{
2018-04-04 01:59:51 +10:00
_logger . Error < ContentPropertyBasicConverter < TDestination > > (
2013-09-25 21:35:24 +10:00
"No property editor found, converting to a Label" ,
new NullReferenceException ( "The property editor with alias " + property . PropertyType . PropertyEditorAlias + " does not exist" ) ) ;
2018-04-04 01:59:51 +10:00
editor = _propertyEditors [ Constants . PropertyEditors . Aliases . NoEdit ] ;
2018-05-04 13:44:04 +02:00
}
2018-04-04 01:59:51 +10:00
2017-07-19 13:42:47 +02:00
var result = new TDestination
2013-08-12 15:06:12 +02:00
{
Id = property . Id ,
2017-07-19 13:42:47 +02:00
Alias = property . Alias ,
2014-02-19 00:14:25 +01:00
PropertyEditor = editor ,
Editor = editor . Alias
2013-08-12 15:06:12 +02:00
} ;
2018-04-17 08:36:36 +02:00
// 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.
if ( context . Options . Items . ContainsKey ( "IncludeProperties" ) )
2018-04-09 15:43:17 +02:00
{
2018-04-17 08:36:36 +02:00
var includeProperties = context . Options . Items [ "IncludeProperties" ] as IEnumerable < string > ;
if ( includeProperties ! = null & & includeProperties . Contains ( property . Alias ) = = false )
{
return result ;
}
2018-04-09 15:43:17 +02:00
}
2018-05-09 14:35:23 +10:00
var culture = context . GetCulture ( ) ;
//a culture needs to be in the context for a property type that can vary
if ( culture = = null & & property . PropertyType . Variations . Has ( ContentVariation . CultureNeutral ) )
throw new InvalidOperationException ( $"No languageId found in mapping operation when one is required for the culture neutral property type {property.PropertyType.Alias}" ) ;
//set the culture to null if it's an invariant property type
culture = ! property . PropertyType . Variations . Has ( ContentVariation . CultureNeutral ) ? null : culture ;
2018-04-17 08:36:36 +02:00
// if no 'IncludeProperties' were specified or this property is set to be included - we will map the value and return.
2018-04-21 09:57:28 +02:00
result . Value = editor . GetValueEditor ( ) . ToEditor ( property , DataTypeService , culture ) ;
2013-08-12 15:06:12 +02:00
return result ;
}
}
2017-07-20 11:21:28 +02:00
}