From dd16af0989f7064f3eb90be7d18e0367af38efa0 Mon Sep 17 00:00:00 2001 From: Stephan Date: Wed, 18 Sep 2013 14:20:29 +0200 Subject: [PATCH] PublishedContent - align with v7, refactor PublishedPropertyType --- .../PublishedContent/PublishedPropertyType.cs | 16 ++++++------- .../DatePickerValueConverter.cs | 20 +++++++--------- .../PropertyValueConverterBase.cs | 23 ++++++++----------- .../PropertyEditors/TinyMceValueConverter.cs | 22 +++++++++--------- .../PropertyEditors/YesNoValueConverter.cs | 20 +++++++--------- 5 files changed, 45 insertions(+), 56 deletions(-) diff --git a/src/Umbraco.Core/Models/PublishedContent/PublishedPropertyType.cs b/src/Umbraco.Core/Models/PublishedContent/PublishedPropertyType.cs index 35ff64cd3e..b3e884bf44 100644 --- a/src/Umbraco.Core/Models/PublishedContent/PublishedPropertyType.cs +++ b/src/Umbraco.Core/Models/PublishedContent/PublishedPropertyType.cs @@ -27,20 +27,20 @@ namespace Umbraco.Core.Models.PublishedContent Alias = propertyType.Alias; DataTypeId = propertyType.DataTypeDefinitionId; - EditorGuid = propertyType.DataTypeId; + PropertyEditorGuid = propertyType.DataTypeId; InitializeConverters(); } // for unit tests - internal PublishedPropertyType(string alias, Guid dataTypeGuid, int propertyTypeId, int dataTypeDefinitionId) + internal PublishedPropertyType(string alias, Guid propertyEditorGuid, int propertyTypeId, int dataTypeDefinitionId) { // ContentType to be set by PublishedContentType when creating it Id = propertyTypeId; Alias = alias; DataTypeId = dataTypeDefinitionId; - EditorGuid = dataTypeGuid; + PropertyEditorGuid = propertyEditorGuid; InitializeConverters(); } @@ -59,7 +59,7 @@ namespace Umbraco.Core.Models.PublishedContent public int DataTypeId { get; private set; } - public Guid EditorGuid { get; private set; } + public Guid PropertyEditorGuid { get; private set; } #endregion @@ -124,7 +124,7 @@ namespace Umbraco.Core.Models.PublishedContent // use the converter else use dark (& performance-wise expensive) magic return _sourceConverter != null ? _sourceConverter.ConvertDataToSource(this, source, preview) - : ConvertSourceUsingDarkMagic(source); + : ConvertUsingDarkMagic(source); } // gets the source cache level @@ -168,7 +168,7 @@ namespace Umbraco.Core.Models.PublishedContent // gets the xpath cache level public PropertyCacheLevel XPathCacheLevel { get { return _xpathCacheLevel; } } - private static object ConvertSourceUsingDarkMagic(object source) + internal static object ConvertUsingDarkMagic(object source) { // convert to string var stringSource = source as string; @@ -192,7 +192,7 @@ namespace Umbraco.Core.Models.PublishedContent // try xml - that is expensive, performance-wise XElement elt; if (XmlHelper.TryCreateXElementFromPropertyValue(stringSource, out elt)) - return Attempt.Succeed(new DynamicXml(elt)); // xml => return DynamicXml for compatiblity's sake + return new DynamicXml(elt); // xml => return DynamicXml for compatiblity's sake return source; } @@ -207,7 +207,7 @@ namespace Umbraco.Core.Models.PublishedContent { return PropertyEditorValueConvertersResolver.HasCurrent ? PropertyEditorValueConvertersResolver.Current.Converters - .Where(x => x.IsConverterFor(EditorGuid, ContentType.Alias, Alias)) + .Where(x => x.IsConverterFor(PropertyEditorGuid, ContentType.Alias, Alias)) .Select(x => new CompatConverter(x)) : Enumerable.Empty(); } diff --git a/src/Umbraco.Core/PropertyEditors/DatePickerValueConverter.cs b/src/Umbraco.Core/PropertyEditors/DatePickerValueConverter.cs index 1ce4e74dd0..155137ce21 100644 --- a/src/Umbraco.Core/PropertyEditors/DatePickerValueConverter.cs +++ b/src/Umbraco.Core/PropertyEditors/DatePickerValueConverter.cs @@ -8,7 +8,7 @@ namespace Umbraco.Core.PropertyEditors { [PropertyValueType(typeof(DateTime))] [PropertyValueCache(PropertyCacheValue.All, PropertyCacheLevel.Content)] - internal class DatePickerValueConverter : IPropertyValueConverter + internal class DatePickerValueConverter : PropertyValueConverterBase { private static readonly Guid[] DataTypeGuids = new[] { @@ -16,12 +16,12 @@ namespace Umbraco.Core.PropertyEditors Guid.Parse(Constants.PropertyEditors.Date) }; - public bool IsDataToSourceConverter(PublishedPropertyType propertyType) + public override bool IsDataToSourceConverter(PublishedPropertyType propertyType) { - return DataTypeGuids.Contains(propertyType.EditorGuid); + return DataTypeGuids.Contains(propertyType.PropertyEditorGuid); } - public object ConvertDataToSource(PublishedPropertyType propertyType, object source, bool preview) + public override object ConvertDataToSource(PublishedPropertyType propertyType, object source, bool preview) { if (source == null) return DateTime.MinValue; @@ -42,23 +42,19 @@ namespace Umbraco.Core.PropertyEditors : DateTime.MinValue; } - public bool IsSourceToObjectConverter(PublishedPropertyType propertyType) + public override bool IsSourceToObjectConverter(PublishedPropertyType propertyType) { return IsDataToSourceConverter(propertyType); } - public object ConvertSourceToObject(PublishedPropertyType propertyType, object source, bool preview) - { - // source should come from ConvertSource and be a DateTime already - return source; - } + // default ConvertSourceToObject just returns source ie a DateTime value - public bool IsSourceToXPathConverter(PublishedPropertyType propertyType) + public override bool IsSourceToXPathConverter(PublishedPropertyType propertyType) { return IsDataToSourceConverter(propertyType); } - public object ConvertSourceToXPath(PublishedPropertyType propertyType, object source, bool preview) + public override object ConvertSourceToXPath(PublishedPropertyType propertyType, object source, bool preview) { // source should come from ConvertSource and be a DateTime already return XmlConvert.ToString((DateTime) source, "yyyy-MM-ddTHH:mm:ss"); diff --git a/src/Umbraco.Core/PropertyEditors/PropertyValueConverterBase.cs b/src/Umbraco.Core/PropertyEditors/PropertyValueConverterBase.cs index e807c9de43..5fd91168c0 100644 --- a/src/Umbraco.Core/PropertyEditors/PropertyValueConverterBase.cs +++ b/src/Umbraco.Core/PropertyEditors/PropertyValueConverterBase.cs @@ -1,7 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; +using Umbraco.Core.Models.PublishedContent; namespace Umbraco.Core.PropertyEditors { @@ -10,34 +7,34 @@ namespace Umbraco.Core.PropertyEditors /// class PropertyValueConverterBase : IPropertyValueConverter { - public virtual bool IsSourceToObjectConverter(Models.PublishedContent.PublishedPropertyType propertyType) + public virtual bool IsDataToSourceConverter(PublishedPropertyType propertyType) { return false; } - public virtual object ConvertSourceToObject(Models.PublishedContent.PublishedPropertyType propertyType, object source, bool preview) + public virtual object ConvertDataToSource(PublishedPropertyType propertyType, object source, bool preview) { - return null; + return PublishedPropertyType.ConvertUsingDarkMagic(source); } - public virtual bool IsDataToSourceConverter(Models.PublishedContent.PublishedPropertyType propertyType) + public virtual bool IsSourceToObjectConverter(PublishedPropertyType propertyType) { return false; } - public virtual object ConvertDataToSource(Models.PublishedContent.PublishedPropertyType propertyType, object source, bool preview) + public virtual object ConvertSourceToObject(PublishedPropertyType propertyType, object source, bool preview) { - return null; + return source; } - public virtual bool IsSourceToXPathConverter(Models.PublishedContent.PublishedPropertyType propertyType) + public virtual bool IsSourceToXPathConverter(PublishedPropertyType propertyType) { return false; } - public virtual object ConvertSourceToXPath(Models.PublishedContent.PublishedPropertyType propertyType, object source, bool preview) + public virtual object ConvertSourceToXPath(PublishedPropertyType propertyType, object source, bool preview) { - return null; + return source.ToString(); } } } diff --git a/src/Umbraco.Core/PropertyEditors/TinyMceValueConverter.cs b/src/Umbraco.Core/PropertyEditors/TinyMceValueConverter.cs index 1e054c8733..ae48c60093 100644 --- a/src/Umbraco.Core/PropertyEditors/TinyMceValueConverter.cs +++ b/src/Umbraco.Core/PropertyEditors/TinyMceValueConverter.cs @@ -10,14 +10,14 @@ namespace Umbraco.Core.PropertyEditors // PropertyCacheLevel.Content is ok here because that version of RTE converter does not parse {locallink} nor executes macros [PropertyValueType(typeof(IHtmlString))] [PropertyValueCache(PropertyCacheValue.All, PropertyCacheLevel.Content)] - internal class TinyMceValueConverter : IPropertyValueConverter + internal class TinyMceValueConverter : PropertyValueConverterBase { - public bool IsDataToSourceConverter(PublishedPropertyType propertyType) + public override bool IsDataToSourceConverter(PublishedPropertyType propertyType) { - return Guid.Parse(Constants.PropertyEditors.TinyMCEv3).Equals(propertyType.EditorGuid); + return Guid.Parse(Constants.PropertyEditors.TinyMCEv3).Equals(propertyType.PropertyEditorGuid); } - public virtual object ConvertDataToSource(PublishedPropertyType propertyType, object source, bool preview) + public override object ConvertDataToSource(PublishedPropertyType propertyType, object source, bool preview) { // in xml a string is: string // in the database a string is: string @@ -25,25 +25,25 @@ namespace Umbraco.Core.PropertyEditors return source; } - public bool IsSourceToObjectConverter(PublishedPropertyType propertyType) + public override bool IsSourceToObjectConverter(PublishedPropertyType propertyType) { return IsDataToSourceConverter(propertyType); } - public virtual object ConvertSourceToObject(PublishedPropertyType propertyType, object source, bool preview) + public override object ConvertSourceToObject(PublishedPropertyType propertyType, object source, bool preview) { - // source should come from ConvertSource and be a string already - return new HtmlString((string)source); + // source should come from ConvertSource and be a string (or null) already + return new HtmlString(source == null ? string.Empty : (string)source); } - public bool IsSourceToXPathConverter(PublishedPropertyType propertyType) + public override bool IsSourceToXPathConverter(PublishedPropertyType propertyType) { return IsDataToSourceConverter(propertyType); } - public virtual object ConvertSourceToXPath(PublishedPropertyType propertyType, object source, bool preview) + public override object ConvertSourceToXPath(PublishedPropertyType propertyType, object source, bool preview) { - // source should come from ConvertSource and be a string already + // source should come from ConvertSource and be a string (or null) already return source; } } diff --git a/src/Umbraco.Core/PropertyEditors/YesNoValueConverter.cs b/src/Umbraco.Core/PropertyEditors/YesNoValueConverter.cs index 9d7c35c856..7fc2a96873 100644 --- a/src/Umbraco.Core/PropertyEditors/YesNoValueConverter.cs +++ b/src/Umbraco.Core/PropertyEditors/YesNoValueConverter.cs @@ -5,14 +5,14 @@ namespace Umbraco.Core.PropertyEditors { [PropertyValueType(typeof(bool))] [PropertyValueCache(PropertyCacheValue.All, PropertyCacheLevel.Content)] - class YesNoValueConverter : IPropertyValueConverter + class YesNoValueConverter : PropertyValueConverterBase { - public bool IsDataToSourceConverter(PublishedPropertyType propertyType) + public override bool IsDataToSourceConverter(PublishedPropertyType propertyType) { - return Guid.Parse(Constants.PropertyEditors.TrueFalse).Equals(propertyType.EditorGuid); + return Guid.Parse(Constants.PropertyEditors.TrueFalse).Equals(propertyType.PropertyEditorGuid); } - public object ConvertDataToSource(PublishedPropertyType propertyType, object source, bool preview) + public override object ConvertDataToSource(PublishedPropertyType propertyType, object source, bool preview) { // in xml a boolean is: string // in the database a boolean is: string "1" or "0" or empty @@ -24,23 +24,19 @@ namespace Umbraco.Core.PropertyEditors return sourceString == "1"; } - public bool IsSourceToObjectConverter(PublishedPropertyType propertyType) + public override bool IsSourceToObjectConverter(PublishedPropertyType propertyType) { return IsDataToSourceConverter(propertyType); } - public object ConvertSourceToObject(PublishedPropertyType propertyType, object source, bool preview) - { - // source should come from ConvertSource and be a boolean already - return source; - } + // default ConvertSourceToObject just returns source ie a boolean value - public bool IsSourceToXPathConverter(PublishedPropertyType propertyType) + public override bool IsSourceToXPathConverter(PublishedPropertyType propertyType) { return IsDataToSourceConverter(propertyType); } - public object ConvertSourceToXPath(PublishedPropertyType propertyType, object source, bool preview) + public override object ConvertSourceToXPath(PublishedPropertyType propertyType, object source, bool preview) { // source should come from ConvertSource and be a boolean already return (bool) source ? "1" : "0";