DataType refactoring - in progress

This commit is contained in:
Stephan
2018-01-24 13:37:14 +01:00
parent c3e6886ac0
commit f7f42f893b
57 changed files with 367 additions and 545 deletions
@@ -1,33 +1,24 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Collections.Generic;
using Umbraco.Core;
using Umbraco.Core.Logging;
using Umbraco.Core.Models;
using Umbraco.Core.PropertyEditors;
using Umbraco.Core.PropertyEditors.Validators;
namespace Umbraco.Web.PropertyEditors
{
/// <summary>
/// A custom pre-value editor class to deal with the legacy way that the pre-value data is stored.
/// Represents the configuration editor for a multiple testring value editor.
/// </summary>
internal class MultipleTextStringConfigurationEditor : ConfigurationEditor
internal class MultipleTextStringConfigurationEditor : ConfigurationEditor<MultipleTestStringConfiguration>
{
private readonly ILogger _logger;
public MultipleTextStringConfigurationEditor(ILogger logger)
public MultipleTextStringConfigurationEditor()
{
_logger = logger;
Fields.Add(new ConfigurationField(new IntegerValidator())
{
Description = "Enter the minimum amount of text boxes to be displayed",
Key = "min",
View = "requiredfield",
Name = "Minimum"
Name = "Minimum",
PropertyName = nameof(MultipleTestStringConfiguration.Minimum)
});
Fields.Add(new ConfigurationField(new IntegerValidator())
@@ -35,62 +26,35 @@ namespace Umbraco.Web.PropertyEditors
Description = "Enter the maximum amount of text boxes to be displayed, enter 0 for unlimited",
Key = "max",
View = "requiredfield",
Name = "Maximum"
Name = "Maximum",
PropertyName = nameof(MultipleTestStringConfiguration.Maximum)
});
}
/// <summary>
/// Need to change how we persist the values so they are compatible with the legacy way we store values
/// </summary>
/// <param name="editorValue"></param>
/// <param name="currentValue"></param>
/// <returns></returns>
public override IDictionary<string, PreValue> ConvertEditorToDb(IDictionary<string, object> editorValue, PreValueCollection currentValue)
/// <inheritdoc />
public override MultipleTestStringConfiguration FromEditor(Dictionary<string, object> editorValue, MultipleTestStringConfiguration configuration)
{
// fixme this isn't pretty
//the values from the editor will be min/max fieds and we need to format to json in one field
// is the editor sending strings or ints or?!
var min = (editorValue.ContainsKey("min") ? editorValue["min"].ToString() : "0").TryConvertTo<int>();
var max = (editorValue.ContainsKey("max") ? editorValue["max"].ToString() : "0").TryConvertTo<int>();
var json = JObject.FromObject(new {Minimum = min.Success ? min.Result : 0, Maximum = max.Success ? max.Result : 0});
return new Dictionary<string, PreValue> { { "0", new PreValue(json.ToString(Formatting.None)) } };
return new MultipleTestStringConfiguration
{
Minimum = min ? min.Result : 0,
Maximum = max ? max.Result : 0
};
}
/// <summary>
/// Need to deal with the legacy way of storing pre-values and turn them into nice values for the editor
/// </summary>
/// <param name="defaultPreVals"></param>
/// <param name="persistedPreVals"></param>
/// <returns></returns>
public override IDictionary<string, object> ConvertDbToEditor(IDictionary<string, object> defaultPreVals, PreValueCollection persistedPreVals)
/// <inheritdoc />
public override Dictionary<string, object> ToEditor(MultipleTestStringConfiguration defaultConfiguration, MultipleTestStringConfiguration configuration)
{
var preVals = persistedPreVals.FormatAsDictionary();
var stringVal = preVals.Any() ? preVals.First().Value.Value : "";
var returnVal = new Dictionary<string, object> { { "min", 0 }, { "max", 0 } };
if (stringVal.IsNullOrWhiteSpace() == false)
return new Dictionary<string, object>
{
try
{
var json = JsonConvert.DeserializeObject<JObject>(stringVal);
if (json["Minimum"] != null)
{
//by default pre-values are sent out with an id/value pair
returnVal["min"] = json["Minimum"].Value<int>();
}
if (json["Maximum"] != null)
{
returnVal["max"] = json["Maximum"].Value<int>();
}
}
catch (Exception e)
{
// this shouldn't happen unless there's already a bad formatted pre-value
_logger.Warn<MultipleTextStringConfigurationEditor>(e, "Could not deserialize value to json " + stringVal);
return returnVal;
}
}
return returnVal;
{ "min", configuration.Minimum },
{ "max", configuration.Maximum }
};
}
}
}