using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
using Umbraco.Core.Configuration;
using Umbraco.Core.Dynamics;
using Umbraco.Core.PropertyEditors;
using Umbraco.Core.Services;
namespace Umbraco.Core
{
///
/// Utility class for dealing with data types and value conversions
///
///
/// TODO: The logic for the GetDataType + cache should probably be moved to a service, no ?
///
/// We inherit from ApplicationEventHandler so we can bind to the ContentTypeService events to ensure that our local cache
/// object gets cleared when content types change.
///
internal class PublishedContentHelper : ApplicationEventHandler
{
#region event handlers to ensure that the cache is cleared when content types change
protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
{
ContentTypeService.SavedContentType += ContentTypeServiceSavedContentType;
ContentTypeService.SavedMediaType += ContentTypeServiceSavedMediaType;
ContentTypeService.DeletedContentType += ContentTypeServiceDeletedContentType;
ContentTypeService.DeletedMediaType += ContentTypeServiceDeletedMediaType;
}
static void ContentTypeServiceDeletedMediaType(IContentTypeService sender, Events.DeleteEventArgs e)
{
PropertyTypeCache.Clear();
}
static void ContentTypeServiceDeletedContentType(IContentTypeService sender, Events.DeleteEventArgs e)
{
PropertyTypeCache.Clear();
}
static void ContentTypeServiceSavedMediaType(IContentTypeService sender, Events.SaveEventArgs e)
{
PropertyTypeCache.Clear();
}
static void ContentTypeServiceSavedContentType(IContentTypeService sender, Events.SaveEventArgs e)
{
PropertyTypeCache.Clear();
}
#endregion
///
/// This callback is used only for unit tests which enables us to return any data we want and not rely on having the data in a database
///
internal static Func GetDataTypeCallback = null;
private static readonly ConcurrentDictionary, Guid> PropertyTypeCache = new ConcurrentDictionary, Guid>();
///
/// Return the GUID Id for the data type assigned to the document type with the property alias
///
///
///
///
///
internal static Guid GetDataType(ApplicationContext applicationContext, string docTypeAlias, string propertyAlias)
{
if (GetDataTypeCallback != null)
return GetDataTypeCallback(docTypeAlias, propertyAlias);
var key = new Tuple(docTypeAlias, propertyAlias);
return PropertyTypeCache.GetOrAdd(key, tuple =>
{
var result = applicationContext.Services.ContentTypeService.GetContentType(docTypeAlias);
if (result == null) return Guid.Empty;
var property = result.PropertyTypes.FirstOrDefault(x => x.Alias.InvariantEquals(propertyAlias));
if (property == null) return Guid.Empty;
return property.DataTypeId;
});
}
///
/// Converts the currentValue to a correctly typed value based on known registered converters, then based on known standards.
///
///
///
///
///
///
internal static Attempt