using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Umbraco.Core;
using Umbraco.Core.Models;
using Umbraco.Core.Models.PublishedContent;
using Umbraco.Core.PropertyEditors;
using Umbraco.Core.Services;
namespace Umbraco.Web.Models
{
public static class PublishedProperty
{
///
/// Maps a collection of Property to a collection of IPublishedProperty for a specified collection of PublishedPropertyType.
///
/// The published property types.
/// The properties.
/// A mapping function.
/// A collection of IPublishedProperty corresponding to the collection of PublishedPropertyType
/// and taking values from the collection of Property.
/// Ensures that all conversions took place correctly.
internal static IEnumerable MapProperties(
IEnumerable propertyTypes, IEnumerable properties,
Func map)
{
var peResolver = PropertyEditorResolver.Current;
var dtService = ApplicationContext.Current.Services.DataTypeService;
return MapProperties(propertyTypes, properties, peResolver, dtService, map);
}
///
/// Maps a collection of Property to a collection of IPublishedProperty for a specified collection of PublishedPropertyType.
///
/// The published property types.
/// The properties.
/// A mapping function.
/// A PropertyEditorResolver instance.
/// An IDataTypeService instance.
/// A collection of IPublishedProperty corresponding to the collection of PublishedPropertyType
/// and taking values from the collection of Property.
/// Ensures that all conversions took place correctly.
internal static IEnumerable MapProperties(
IEnumerable propertyTypes, IEnumerable properties,
PropertyEditorResolver propertyEditorResolver, IDataTypeService dataTypeService,
Func map)
{
return propertyTypes
.Select(x =>
{
var p = properties.SingleOrDefault(xx => xx.Alias == x.PropertyTypeAlias);
var v = p == null || p.Value == null ? null : p.Value;
if (v != null)
{
var e = propertyEditorResolver.GetByAlias(x.PropertyEditorAlias);
if (e != null)
v = e.ValueEditor.ConvertDbToString(p, p.PropertyType, dataTypeService);
}
// fixme - means that the IPropertyValueConverter will always get a string
// fixme and never an int or DateTime that's in the DB unless the value editor has
// fixme a way to say it's OK to use what's in the DB?
return map(x, p, v);
});
}
}
}