Changed method signatures of some of the conversion methods on the PropertyValueEditor to make it more flexible for developers to do what they want.

This commit is contained in:
Shannon
2013-11-19 11:51:01 +11:00
parent ac2009e4ae
commit 2f42fc61bf
14 changed files with 103 additions and 67 deletions

View File

@@ -17,16 +17,17 @@ namespace Umbraco.Core.Models
/// <returns>Xml of the property and its value</returns>
public static XElement ToXml(this Property property)
{
return property.ToXml(ApplicationContext.Current.Services.DataTypeService);
return property.ToXml(property.PropertyType, ApplicationContext.Current.Services.DataTypeService);
}
/// <summary>
/// Creates the xml representation for the <see cref="Property"/> object
/// </summary>
/// <param name="property"><see cref="Property"/> to generate xml for</param>
/// <param name="propertyType"></param>
/// <param name="dataTypeService"></param>
/// <returns>Xml of the property and its value</returns>
internal static XElement ToXml(this Property property, IDataTypeService dataTypeService)
internal static XElement ToXml(this Property property, PropertyType propertyType, IDataTypeService dataTypeService)
{
var nodeName = UmbracoConfig.For.UmbracoSettings().Content.UseLegacyXmlSchema ? "data" : property.Alias.ToSafeAlias();
@@ -43,7 +44,7 @@ namespace Umbraco.Core.Models
var propertyEditor = PropertyEditorResolver.Current.GetByAlias(property.PropertyType.PropertyEditorAlias);
if (propertyEditor != null)
{
var xmlValue = propertyEditor.ValueEditor.ConvertDbToXml(property);
var xmlValue = propertyEditor.ValueEditor.ConvertDbToXml(property, propertyType, dataTypeService);
xElement.Add(xmlValue);
}

View File

@@ -41,7 +41,7 @@ namespace Umbraco.Core.PropertyEditors
Value = Value
};
var xd = new XmlDocument();
var xNode = propertyEditor.ValueEditor.ConvertDbToXml(property);
var xNode = propertyEditor.ValueEditor.ConvertDbToXml(property, property.PropertyType, ApplicationContext.Current.Services.DataTypeService);
//check if this xml fragment can be converted to an XmlNode
var xContainer = xNode as XContainer;

View File

@@ -7,6 +7,7 @@ using Umbraco.Core.Logging;
using Umbraco.Core.Manifest;
using Umbraco.Core.Models;
using Umbraco.Core.Models.Editors;
using Umbraco.Core.Services;
namespace Umbraco.Core.PropertyEditors
{
@@ -235,15 +236,17 @@ namespace Umbraco.Core.PropertyEditors
/// <summary>
/// A method used to format the database value to a value that can be used by the editor
/// </summary>
/// <param name="dbValue"></param>
/// <param name="property"></param>
/// <param name="propertyType"></param>
/// <param name="dataTypeService"></param>
/// <returns></returns>
/// <remarks>
/// The object returned will automatically be serialized into json notation. For most property editors
/// the value returned is probably just a string but in some cases a json structure will be returned.
/// </remarks>
public virtual object ConvertDbToEditor(object dbValue)
public virtual object ConvertDbToEditor(Property property, PropertyType propertyType, IDataTypeService dataTypeService)
{
if (dbValue == null) return string.Empty;
if (property.Value == null) return string.Empty;
switch (GetDatabaseType())
{
@@ -251,7 +254,7 @@ namespace Umbraco.Core.PropertyEditors
case DataTypeDatabaseType.Nvarchar:
//if it is a string type, we will attempt to see if it is json stored data, if it is we'll try to convert
//to a real json object so we can pass the true json object directly to angular!
var asString = dbValue.ToString();
var asString = property.Value.ToString();
if (asString.DetectIsJson())
{
try
@@ -264,12 +267,12 @@ namespace Umbraco.Core.PropertyEditors
//swallow this exception, we thought it was json but it really isn't so continue returning a string
}
}
return dbValue.ToString();
return property.Value.ToString();
case DataTypeDatabaseType.Integer:
//we can just ToString() any of these types
return dbValue.ToString();
case DataTypeDatabaseType.Date:
var date = dbValue.TryConvertTo<DateTime?>();
return property.Value.ToString();
case DataTypeDatabaseType.Date:
var date = property.Value.TryConvertTo<DateTime?>();
if (date.Success == false || date.Result == null)
{
return string.Empty;
@@ -285,6 +288,8 @@ namespace Umbraco.Core.PropertyEditors
/// Converts the property db value to an XML fragment
/// </summary>
/// <param name="property"></param>
/// <param name="propertyType"></param>
/// <param name="dataTypeService"></param>
/// <returns></returns>
/// <remarks>
/// By default this will just return the value of ConvertDbToString but ensure that if the db value type is nvarchar or text
@@ -294,23 +299,23 @@ namespace Umbraco.Core.PropertyEditors
///
/// If the value is empty we will not return as CDATA since that will just take up more space in the file.
/// </remarks>
public virtual XNode ConvertDbToXml(Property property)
public virtual XNode ConvertDbToXml(Property property, PropertyType propertyType, IDataTypeService dataTypeService)
{
//check for null or empty value, we don't want to return CDATA if that is the case
if (property.Value == null || property.Value.ToString().IsNullOrWhiteSpace())
{
return new XText(ConvertDbToString(property));
return new XText(ConvertDbToString(property, propertyType, dataTypeService));
}
switch (GetDatabaseType())
{
case DataTypeDatabaseType.Date:
case DataTypeDatabaseType.Integer:
return new XText(ConvertDbToString(property));
return new XText(ConvertDbToString(property, propertyType, dataTypeService));
case DataTypeDatabaseType.Nvarchar:
case DataTypeDatabaseType.Ntext:
//put text in cdata
return new XCData(ConvertDbToString(property));
return new XCData(ConvertDbToString(property, propertyType, dataTypeService));
default:
throw new ArgumentOutOfRangeException();
}
@@ -320,8 +325,10 @@ namespace Umbraco.Core.PropertyEditors
/// Converts the property value for use in the front-end cache
/// </summary>
/// <param name="property"></param>
/// <param name="propertyType"></param>
/// <param name="dataTypeService"></param>
/// <returns></returns>
public virtual string ConvertDbToString(Property property)
public virtual string ConvertDbToString(Property property, PropertyType propertyType, IDataTypeService dataTypeService)
{
if (property.Value == null)
{

View File

@@ -25,10 +25,11 @@ namespace Umbraco.Tests.PropertyEditors
var dataTypeService = dataTypeServiceMock.Object;
var editor = new PublishValuesMultipleValueEditor(true, dataTypeService, new PropertyValueEditor());
var result = editor.ConvertDbToString(
new Property(1, Guid.NewGuid(),
new PropertyType(new DataTypeDefinition(1, "Test.TestEditor")),
"1234,4567,8910"));
var prop = new Property(1, Guid.NewGuid(),
new PropertyType(new DataTypeDefinition(1, "Test.TestEditor")),
"1234,4567,8910");
var result = editor.ConvertDbToString(prop, prop.PropertyType, new Mock<IDataTypeService>().Object);
Assert.AreEqual("1234,4567,8910", result);
}
@@ -50,10 +51,11 @@ namespace Umbraco.Tests.PropertyEditors
var dataTypeService = dataTypeServiceMock.Object;
var editor = new PublishValuesMultipleValueEditor(false, dataTypeService, new PropertyValueEditor());
var result = editor.ConvertDbToString(
new Property(1, Guid.NewGuid(),
new PropertyType(new DataTypeDefinition(1, "Test.TestEditor")),
"1234,4567,8910"));
var prop = new Property(1, Guid.NewGuid(),
new PropertyType(new DataTypeDefinition(1, "Test.TestEditor")),
"1234,4567,8910");
var result = editor.ConvertDbToString(prop, prop.PropertyType, new Mock<IDataTypeService>().Object);
Assert.AreEqual("Value 1,Value 2,Value 3", result);
}
@@ -74,10 +76,11 @@ namespace Umbraco.Tests.PropertyEditors
var dataTypeService = dataTypeServiceMock.Object;
var editor = new PublishValueValueEditor(dataTypeService, new PropertyValueEditor());
var result = editor.ConvertDbToString(
new Property(1, Guid.NewGuid(),
new PropertyType(new DataTypeDefinition(1, "Test.TestEditor")),
"1234"));
var prop = new Property(1, Guid.NewGuid(),
new PropertyType(new DataTypeDefinition(1, "Test.TestEditor")),
"1234");
var result = editor.ConvertDbToString(prop, prop.PropertyType, new Mock<IDataTypeService>().Object);
Assert.AreEqual("Value 2", result);
}

View File

@@ -1,7 +1,10 @@
using System;
using Moq;
using NUnit.Framework;
using Umbraco.Core;
using Umbraco.Core.Models;
using Umbraco.Core.PropertyEditors;
using Umbraco.Core.Services;
namespace Umbraco.Tests.PropertyEditors
{
@@ -14,12 +17,14 @@ namespace Umbraco.Tests.PropertyEditors
[TestCase("hello world", false)]
public void Value_Editor_Can_Convert_To_Json_Object_For_Editor(string value, bool isOk)
{
var prop = new Property(1, Guid.NewGuid(), new PropertyType("test", DataTypeDatabaseType.Nvarchar), value);
var valueEditor = new PropertyValueEditor
{
ValueType = "STRING"
};
var result = valueEditor.ConvertDbToEditor(value);
var result = valueEditor.ConvertDbToEditor(prop, prop.PropertyType, new Mock<IDataTypeService>().Object);
Assert.AreEqual(isOk, !(result is string));
}
@@ -62,12 +67,14 @@ namespace Umbraco.Tests.PropertyEditors
[TestCase("DATETIME", "", "")] //test empty string for date
public void Value_Editor_Can_Serialize_Value(string valueType, object val, string expected)
{
var prop = new Property(1, Guid.NewGuid(), new PropertyType("test", DataTypeDatabaseType.Nvarchar), val);
var valueEditor = new PropertyValueEditor
{
ValueType = valueType
};
var result = valueEditor.ConvertDbToEditor(val);
var result = valueEditor.ConvertDbToEditor(prop, prop.PropertyType, new Mock<IDataTypeService>().Object);
Assert.AreEqual(expected, result);
}
@@ -80,7 +87,9 @@ namespace Umbraco.Tests.PropertyEditors
ValueType = "DATE"
};
var result = valueEditor.ConvertDbToEditor(now);
var prop = new Property(1, Guid.NewGuid(), new PropertyType("test", DataTypeDatabaseType.Date), now);
var result = valueEditor.ConvertDbToEditor(prop, prop.PropertyType, new Mock<IDataTypeService>().Object);
Assert.AreEqual(now.ToIsoString(), result);
}
}

View File

@@ -4,6 +4,7 @@ using Umbraco.Core;
using Umbraco.Core.Logging;
using Umbraco.Core.Models;
using Umbraco.Core.PropertyEditors;
using Umbraco.Core.Services;
using Umbraco.Web.Models.ContentEditing;
namespace Umbraco.Web.Models.Mapping
@@ -15,6 +16,13 @@ namespace Umbraco.Web.Models.Mapping
internal class ContentPropertyBasicConverter<T> : TypeConverter<Property, T>
where T : ContentPropertyBasic, new()
{
protected Lazy<IDataTypeService> DataTypeService { get; private set; }
public ContentPropertyBasicConverter(Lazy<IDataTypeService> dataTypeService)
{
DataTypeService = dataTypeService;
}
/// <summary>
/// Assigns the PropertyEditor, Id, Alias and Value to the property
/// </summary>
@@ -34,12 +42,11 @@ namespace Umbraco.Web.Models.Mapping
var result = new T
{
Id = property.Id,
Value = editor.ValueEditor.ConvertDbToEditor(property.Value),
Alias = property.Alias
Value = editor.ValueEditor.ConvertDbToEditor(property, property.PropertyType, DataTypeService.Value),
Alias = property.Alias,
PropertyEditor = editor
};
result.PropertyEditor = editor;
return result;
}
}

View File

@@ -13,18 +13,17 @@ namespace Umbraco.Web.Models.Mapping
/// </summary>
internal class ContentPropertyDisplayConverter : ContentPropertyBasicConverter<ContentPropertyDisplay>
{
private readonly Lazy<IDataTypeService> _dataTypeService;
public ContentPropertyDisplayConverter(Lazy<IDataTypeService> dataTypeService)
: base(dataTypeService)
{
_dataTypeService = dataTypeService;
}
protected override ContentPropertyDisplay ConvertCore(Property originalProp)
{
var display = base.ConvertCore(originalProp);
var dataTypeService = _dataTypeService.Value;
var dataTypeService = DataTypeService.Value;
var preVals = dataTypeService.GetPreValuesCollectionByDataTypeId(originalProp.PropertyType.DataTypeDefinitionId);
//configure the editor for display with the pre-values

View File

@@ -12,18 +12,16 @@ namespace Umbraco.Web.Models.Mapping
/// </summary>
internal class ContentPropertyDtoConverter : ContentPropertyBasicConverter<ContentPropertyDto>
{
private readonly Lazy<IDataTypeService> _dataTypeService;
public ContentPropertyDtoConverter(Lazy<IDataTypeService> dataTypeService)
: base(dataTypeService)
{
_dataTypeService = dataTypeService;
}
protected override ContentPropertyDto ConvertCore(Property originalProperty)
{
var propertyDto = base.ConvertCore(originalProperty);
var dataTypeService = _dataTypeService.Value;
var dataTypeService = DataTypeService.Value;
propertyDto.IsRequired = originalProperty.PropertyType.Mandatory;
propertyDto.ValidationRegExp = originalProperty.PropertyType.ValidationRegExp;

View File

@@ -26,7 +26,7 @@ namespace Umbraco.Web.Models.Mapping
//FROM Property TO ContentPropertyBasic
config.CreateMap<Property, ContentPropertyBasic>()
.ConvertUsing<ContentPropertyBasicConverter<ContentPropertyBasic>>();
.ConvertUsing(new ContentPropertyBasicConverter<ContentPropertyBasic>(lazyDataTypeService));
//FROM Property TO ContentPropertyDto
config.CreateMap<Property, ContentPropertyDto>()

View File

@@ -5,6 +5,7 @@ using Newtonsoft.Json.Linq;
using Umbraco.Core;
using Umbraco.Core.Models;
using Umbraco.Core.PropertyEditors;
using Umbraco.Core.Services;
namespace Umbraco.Web.PropertyEditors
{
@@ -47,9 +48,9 @@ namespace Umbraco.Web.PropertyEditors
Validators.Add(new DateTimeValidator());
}
public override object ConvertDbToEditor(object dbValue)
public override object ConvertDbToEditor(Property property, PropertyType propertyType, IDataTypeService dataTypeService)
{
var date = dbValue.TryConvertTo<DateTime?>();
var date = property.Value.TryConvertTo<DateTime?>();
if (date.Success == false || date.Result == null)
{
return string.Empty;

View File

@@ -10,6 +10,7 @@ using Umbraco.Core.Logging;
using Umbraco.Core.Models;
using Umbraco.Core.Models.Editors;
using Umbraco.Core.PropertyEditors;
using Umbraco.Core.Services;
namespace Umbraco.Web.PropertyEditors
{
@@ -160,16 +161,18 @@ namespace Umbraco.Web.PropertyEditors
/// We are actually passing back an array of simple objects instead of an array of strings because in angular a primitive (string) value
/// cannot have 2 way binding, so to get around that each item in the array needs to be an object with a string.
/// </summary>
/// <param name="dbValue"></param>
/// <param name="property"></param>
/// <param name="propertyType"></param>
/// <param name="dataTypeService"></param>
/// <returns></returns>
/// <remarks>
/// The legacy property editor saved this data as new line delimited! strange but we have to maintain that.
/// </remarks>
public override object ConvertDbToEditor(object dbValue)
public override object ConvertDbToEditor(Property property, PropertyType propertyType, IDataTypeService dataTypeService)
{
return dbValue == null
return property.Value == null
? new JObject[] {}
: dbValue.ToString().Split(new[] {Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries)
: property.Value.ToString().Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries)
.Select(x => JObject.FromObject(new {value = x}));

View File

@@ -34,8 +34,10 @@ namespace Umbraco.Web.PropertyEditors
/// Need to lookup the pre-values and put the string version in cache, not the ID (which is what is stored in the db)
/// </summary>
/// <param name="property"></param>
/// <param name="propertyType"></param>
/// <param name="dataTypeService"></param>
/// <returns></returns>
public override string ConvertDbToString(Property property)
public override string ConvertDbToString(Property property, PropertyType propertyType, IDataTypeService dataTypeService)
{
var idAttempt = property.Value.TryConvertTo<int>();
if (idAttempt.Success)
@@ -52,10 +54,8 @@ namespace Umbraco.Web.PropertyEditors
LogHelper.Warn<PublishValueValueEditor>("Could not find a pre value with ID " + preValId + " for property alias " + property.Alias);
}
}
return base.ConvertDbToString(property);
return base.ConvertDbToString(property, propertyType, dataTypeService);
}
protected IDictionary<string, PreValue> GetPreValues(Property property)

View File

@@ -34,18 +34,20 @@ namespace Umbraco.Web.PropertyEditors
/// If publishing ids, we don't need to do anything, otherwise we need to look up the pre-values and get the string values
/// </summary>
/// <param name="property"></param>
/// <param name="propertyType"></param>
/// <param name="dataTypeService"></param>
/// <returns></returns>
public override string ConvertDbToString(Property property)
public override string ConvertDbToString(Property property, PropertyType propertyType, IDataTypeService dataTypeService)
{
if (_publishIds)
{
return base.ConvertDbToString(property);
return base.ConvertDbToString(property, propertyType, dataTypeService);
}
var selectedIds = property.Value.ToString().Split(new[] {','}, StringSplitOptions.RemoveEmptyEntries);
if (selectedIds.Any() == false)
{
return base.ConvertDbToString(property);
return base.ConvertDbToString(property, propertyType, dataTypeService);
}
var preValues = GetPreValues(property);
@@ -56,17 +58,19 @@ namespace Umbraco.Web.PropertyEditors
preValues.Where(x => selectedIds.Contains(x.Value.Id.ToInvariantString())).Select(x => x.Value.Value));
}
return base.ConvertDbToString(property);
return base.ConvertDbToString(property, propertyType, dataTypeService);
}
/// <summary>
/// Override so that we can return a json array to the editor for multi-select values
/// </summary>
/// <param name="dbValue"></param>
/// <param name="property"></param>
/// <param name="propertyType"></param>
/// <param name="dataTypeService"></param>
/// <returns></returns>
public override object ConvertDbToEditor(object dbValue)
public override object ConvertDbToEditor(Property property, PropertyType propertyType, IDataTypeService dataTypeService)
{
var delimited = base.ConvertDbToEditor(dbValue).ToString();
var delimited = base.ConvertDbToEditor(property, propertyType, dataTypeService).ToString();
return delimited.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
}

View File

@@ -1,7 +1,9 @@
using System.Collections.Generic;
using Umbraco.Core;
using Umbraco.Core.Macros;
using Umbraco.Core.Models;
using Umbraco.Core.PropertyEditors;
using Umbraco.Core.Services;
namespace Umbraco.Web.PropertyEditors
{
@@ -54,14 +56,16 @@ namespace Umbraco.Web.PropertyEditors
/// <summary>
/// Format the data for the editor
/// </summary>
/// <param name="dbValue"></param>
/// <param name="property"></param>
/// <param name="propertyType"></param>
/// <param name="dataTypeService"></param>
/// <returns></returns>
public override object ConvertDbToEditor(object dbValue)
public override object ConvertDbToEditor(Property property, PropertyType propertyType, IDataTypeService dataTypeService)
{
if (dbValue == null)
if (property.Value == null)
return null;
var parsed = MacroTagParser.FormatRichTextPersistedDataForEditor(dbValue.ToString(), new Dictionary<string, string>());
var parsed = MacroTagParser.FormatRichTextPersistedDataForEditor(property.Value.ToString(), new Dictionary<string, string>());
return parsed;
}