From 220a034d2a22653868d96dcfdd5575eed7939308 Mon Sep 17 00:00:00 2001 From: leekelleher Date: Thu, 13 Feb 2014 14:16:42 +0000 Subject: [PATCH 1/8] Changed the `Property.Value` setter to be `public`. --- app/Umbraco/Umbraco.Archetype/Models/Property.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/Umbraco/Umbraco.Archetype/Models/Property.cs b/app/Umbraco/Umbraco.Archetype/Models/Property.cs index b6ba09f..b531be6 100644 --- a/app/Umbraco/Umbraco.Archetype/Models/Property.cs +++ b/app/Umbraco/Umbraco.Archetype/Models/Property.cs @@ -9,7 +9,7 @@ namespace Archetype.Umbraco.Models public class Property { public string Alias { get; internal set; } - public object Value { get; internal set; } + public object Value { get; set; } public string PropertyEditorAlias { get; internal set; } internal int DataTypeId { get; set; } From aacbc06992c2f46b359a51ce1d11df845ad23a2f Mon Sep 17 00:00:00 2001 From: leekelleher Date: Thu, 13 Feb 2014 16:27:29 +0000 Subject: [PATCH 2/8] Changed the `Property.DataTypeId` property to be `public`, with an internal setter. (We need to reference it within Courier's DataResolver) --- app/Umbraco/Umbraco.Archetype/Models/Property.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/Umbraco/Umbraco.Archetype/Models/Property.cs b/app/Umbraco/Umbraco.Archetype/Models/Property.cs index b531be6..c1fda48 100644 --- a/app/Umbraco/Umbraco.Archetype/Models/Property.cs +++ b/app/Umbraco/Umbraco.Archetype/Models/Property.cs @@ -12,7 +12,7 @@ namespace Archetype.Umbraco.Models public object Value { get; set; } public string PropertyEditorAlias { get; internal set; } - internal int DataTypeId { get; set; } + public int DataTypeId { get; internal set; } public T GetValue() { From 3c5e2013a08143916d99c5e0b042181b071ff03b Mon Sep 17 00:00:00 2001 From: leekelleher Date: Thu, 13 Feb 2014 16:32:03 +0000 Subject: [PATCH 3/8] Added `JsonProperty` attributes to the properties. (So that we can serialize/deserialize from the Courier DataResolver) --- app/Umbraco/Umbraco.Archetype/Models/Archetype.cs | 4 +++- app/Umbraco/Umbraco.Archetype/Models/Fieldset.cs | 5 ++++- app/Umbraco/Umbraco.Archetype/Models/Property.cs | 11 +++++++++-- 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/app/Umbraco/Umbraco.Archetype/Models/Archetype.cs b/app/Umbraco/Umbraco.Archetype/Models/Archetype.cs index 93c344c..1c920bf 100644 --- a/app/Umbraco/Umbraco.Archetype/Models/Archetype.cs +++ b/app/Umbraco/Umbraco.Archetype/Models/Archetype.cs @@ -1,10 +1,12 @@ using System.Collections.Generic; +using Newtonsoft.Json; namespace Archetype.Umbraco.Models { public class Archetype { - public IEnumerable
Fieldsets { get; set; } + [JsonProperty("fieldsets")] + public IEnumerable
Fieldsets { get; set; } public Archetype() { diff --git a/app/Umbraco/Umbraco.Archetype/Models/Fieldset.cs b/app/Umbraco/Umbraco.Archetype/Models/Fieldset.cs index d3f0f90..297d9ad 100644 --- a/app/Umbraco/Umbraco.Archetype/Models/Fieldset.cs +++ b/app/Umbraco/Umbraco.Archetype/Models/Fieldset.cs @@ -1,14 +1,17 @@ using System.Collections.Generic; using System.Linq; +using Newtonsoft.Json; using Umbraco.Core; namespace Archetype.Umbraco.Models { public class Fieldset { + [JsonProperty("alias")] public string Alias { get; set; } - public IEnumerable Properties; + [JsonProperty("properties")] + public IEnumerable Properties; public Fieldset() { diff --git a/app/Umbraco/Umbraco.Archetype/Models/Property.cs b/app/Umbraco/Umbraco.Archetype/Models/Property.cs index c1fda48..452f980 100644 --- a/app/Umbraco/Umbraco.Archetype/Models/Property.cs +++ b/app/Umbraco/Umbraco.Archetype/Models/Property.cs @@ -1,4 +1,5 @@ using System.Linq; +using Newtonsoft.Json; using Umbraco.Core; using Umbraco.Core.Models; using Umbraco.Core.Models.PublishedContent; @@ -8,10 +9,16 @@ namespace Archetype.Umbraco.Models { public class Property { - public string Alias { get; internal set; } - public object Value { get; set; } + [JsonProperty("alias")] + public string Alias { get; internal set; } + + [JsonProperty("value")] + public object Value { get; set; } + + [JsonProperty("propertyEditorAlias")] public string PropertyEditorAlias { get; internal set; } + [JsonProperty("dataTypeId")] public int DataTypeId { get; internal set; } public T GetValue() From 9296388abe0e59d6d28ed336c15f88297eb2b657 Mon Sep 17 00:00:00 2001 From: leekelleher Date: Thu, 13 Feb 2014 16:51:31 +0000 Subject: [PATCH 4/8] Added a constant for the PropertyEditor alias. (So that we can reference it from the Courier DataResolver, without fear of typos) --- app/Umbraco/Umbraco.Archetype/Archetype.Umbraco.csproj | 1 + app/Umbraco/Umbraco.Archetype/Constants.cs | 7 +++++++ app/Umbraco/Umbraco.Archetype/Extensions/Extensions.cs | 2 +- .../PropertyConverters/ArchetypeValueConverter.cs | 2 +- 4 files changed, 10 insertions(+), 2 deletions(-) create mode 100644 app/Umbraco/Umbraco.Archetype/Constants.cs diff --git a/app/Umbraco/Umbraco.Archetype/Archetype.Umbraco.csproj b/app/Umbraco/Umbraco.Archetype/Archetype.Umbraco.csproj index 0be938f..9034fd3 100644 --- a/app/Umbraco/Umbraco.Archetype/Archetype.Umbraco.csproj +++ b/app/Umbraco/Umbraco.Archetype/Archetype.Umbraco.csproj @@ -184,6 +184,7 @@ + diff --git a/app/Umbraco/Umbraco.Archetype/Constants.cs b/app/Umbraco/Umbraco.Archetype/Constants.cs new file mode 100644 index 0000000..4c677e1 --- /dev/null +++ b/app/Umbraco/Umbraco.Archetype/Constants.cs @@ -0,0 +1,7 @@ +namespace Archetype.Umbraco +{ + public static class Constants + { + public const string PropertyEditorAlias = "Imulus.Archetype"; + } +} diff --git a/app/Umbraco/Umbraco.Archetype/Extensions/Extensions.cs b/app/Umbraco/Umbraco.Archetype/Extensions/Extensions.cs index 6eed552..2232aa5 100644 --- a/app/Umbraco/Umbraco.Archetype/Extensions/Extensions.cs +++ b/app/Umbraco/Umbraco.Archetype/Extensions/Extensions.cs @@ -15,7 +15,7 @@ namespace Archetype.Umbraco.Extensions public static bool IsArchetype(this Property prop) { - return prop.PropertyEditorAlias.InvariantEquals("Imulus.Archetype"); + return prop.PropertyEditorAlias.InvariantEquals(Constants.PropertyEditorAlias); } } } diff --git a/app/Umbraco/Umbraco.Archetype/PropertyConverters/ArchetypeValueConverter.cs b/app/Umbraco/Umbraco.Archetype/PropertyConverters/ArchetypeValueConverter.cs index acc5feb..96eeb1e 100644 --- a/app/Umbraco/Umbraco.Archetype/PropertyConverters/ArchetypeValueConverter.cs +++ b/app/Umbraco/Umbraco.Archetype/PropertyConverters/ArchetypeValueConverter.cs @@ -32,7 +32,7 @@ namespace Archetype.Umbraco.PropertyConverters public override bool IsConverter(PublishedPropertyType propertyType) { - return propertyType.PropertyEditorAlias.Equals("Imulus.Archetype"); + return propertyType.PropertyEditorAlias.Equals(Constants.PropertyEditorAlias); } public override object ConvertDataToSource(PublishedPropertyType propertyType, object source, bool preview) From 4c8bbd5d8fbb41d4092f7ffffaa1055478f3bbad Mon Sep 17 00:00:00 2001 From: leekelleher Date: Thu, 13 Feb 2014 16:53:38 +0000 Subject: [PATCH 5/8] Added a nested try/catch. For if the `PublishedPropertyType` value is null, we can still get an `Archetype` model returned) --- .../ArchetypeValueConverter.cs | 48 +++++++++++-------- 1 file changed, 27 insertions(+), 21 deletions(-) diff --git a/app/Umbraco/Umbraco.Archetype/PropertyConverters/ArchetypeValueConverter.cs b/app/Umbraco/Umbraco.Archetype/PropertyConverters/ArchetypeValueConverter.cs index 96eeb1e..de2d8fe 100644 --- a/app/Umbraco/Umbraco.Archetype/PropertyConverters/ArchetypeValueConverter.cs +++ b/app/Umbraco/Umbraco.Archetype/PropertyConverters/ArchetypeValueConverter.cs @@ -45,28 +45,34 @@ namespace Archetype.Umbraco.PropertyConverters { try { - // Deserialize value to archetype model - var archetype = JsonConvert.DeserializeObject(sourceString, _jsonSettings); + // Deserialize value to archetype model + var archetype = JsonConvert.DeserializeObject(sourceString, _jsonSettings); - // Get list of configured properties and their types - // and map them to the deserialized archetype model - var preValue = GetArchetypePreValueFromDataTypeId(propertyType.DataTypeId); - foreach (var fieldset in preValue.Fieldsets) - { - var fieldsetAlias = fieldset.Alias; - foreach (var fieldsetInst in archetype.Fieldsets.Where(x => x.Alias == fieldsetAlias)) - { - foreach (var property in fieldset.Properties) - { - var propertyAlias = property.Alias; - foreach (var propertyInst in fieldsetInst.Properties.Where(x => x.Alias == propertyAlias)) - { - propertyInst.DataTypeId = property.DataTypeId; - propertyInst.PropertyEditorAlias = property.PropertyEditorAlias; - } - } - } - } + try + { + // Get list of configured properties and their types + // and map them to the deserialized archetype model + var preValue = GetArchetypePreValueFromDataTypeId(propertyType.DataTypeId); + foreach (var fieldset in preValue.Fieldsets) + { + var fieldsetAlias = fieldset.Alias; + foreach (var fieldsetInst in archetype.Fieldsets.Where(x => x.Alias == fieldsetAlias)) + { + foreach (var property in fieldset.Properties) + { + var propertyAlias = property.Alias; + foreach (var propertyInst in fieldsetInst.Properties.Where(x => x.Alias == propertyAlias)) + { + propertyInst.DataTypeId = property.DataTypeId; + propertyInst.PropertyEditorAlias = property.PropertyEditorAlias; + } + } + } + } + } + catch (Exception ex) + { + } return archetype; } From 9a712acd6a4ccb8e7d8fd05944193f8a741afddc Mon Sep 17 00:00:00 2001 From: leekelleher Date: Thu, 13 Feb 2014 17:30:28 +0000 Subject: [PATCH 6/8] Added constant for the prevalue alias. (As we reference to this in the Courier DataResolver) --- app/Umbraco/Umbraco.Archetype/Constants.cs | 2 ++ .../PropertyConverters/ArchetypeValueConverter.cs | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/app/Umbraco/Umbraco.Archetype/Constants.cs b/app/Umbraco/Umbraco.Archetype/Constants.cs index 4c677e1..9bc7f0d 100644 --- a/app/Umbraco/Umbraco.Archetype/Constants.cs +++ b/app/Umbraco/Umbraco.Archetype/Constants.cs @@ -3,5 +3,7 @@ public static class Constants { public const string PropertyEditorAlias = "Imulus.Archetype"; + + public const string PreValueAlias = "archetypeConfig"; } } diff --git a/app/Umbraco/Umbraco.Archetype/PropertyConverters/ArchetypeValueConverter.cs b/app/Umbraco/Umbraco.Archetype/PropertyConverters/ArchetypeValueConverter.cs index de2d8fe..d9a1a23 100644 --- a/app/Umbraco/Umbraco.Archetype/PropertyConverters/ArchetypeValueConverter.cs +++ b/app/Umbraco/Umbraco.Archetype/PropertyConverters/ArchetypeValueConverter.cs @@ -90,7 +90,7 @@ namespace Archetype.Umbraco.PropertyConverters var preValues = Services.DataTypeService.GetPreValuesCollectionByDataTypeId(dataTypeId); var configJson = preValues.IsDictionaryBased - ? preValues.PreValuesAsDictionary["archetypeConfig"].Value + ? preValues.PreValuesAsDictionary[Constants.PreValueAlias].Value : preValues.PreValuesAsArray.First().Value; var config = JsonConvert.DeserializeObject(configJson, _jsonSettings); From 42743b1394c8819e4420456f15f445a45cc8e767 Mon Sep 17 00:00:00 2001 From: leekelleher Date: Thu, 13 Feb 2014 17:43:52 +0000 Subject: [PATCH 7/8] Fleshed out the `ArchetypePreValue` models. (When Courier packages up the DataTypeDefinition - we need to serialize/deserialize the config options/JSON) --- .../Models/ArchetypePreValue.cs | 43 +++++++++++++++++-- .../Models/ArchetypePreValueFieldset.cs | 31 ++++++++++--- .../Models/ArchetypePreValueProperty.cs | 42 +++++++++++++++--- 3 files changed, 99 insertions(+), 17 deletions(-) diff --git a/app/Umbraco/Umbraco.Archetype/Models/ArchetypePreValue.cs b/app/Umbraco/Umbraco.Archetype/Models/ArchetypePreValue.cs index da14659..69ce09d 100644 --- a/app/Umbraco/Umbraco.Archetype/Models/ArchetypePreValue.cs +++ b/app/Umbraco/Umbraco.Archetype/Models/ArchetypePreValue.cs @@ -1,9 +1,44 @@ using System.Collections.Generic; +using Newtonsoft.Json; namespace Archetype.Umbraco.Models { - internal class ArchetypePreValue - { - internal IEnumerable Fieldsets { get; set; } - } + public class ArchetypePreValue + { + [JsonProperty("showAdvancedOptions")] + public bool ShowAdvancedOptions { get; set; } + + [JsonProperty("hideFieldsetToolbar")] + public bool HideFieldsetToolbar { get; set; } + + [JsonProperty("enableMultipleFieldsets")] + public bool EnableMultipleFieldsets { get; set; } + + [JsonProperty("hideFieldsetControls")] + public bool HideFieldsetControls { get; set; } + + [JsonProperty("hidePropertyLabel")] + public bool HidePropertyLabel { get; set; } + + [JsonProperty("maxFieldsets", NullValueHandling = NullValueHandling.Ignore)] + public int MaxFieldsets { get; set; } + + [JsonProperty("fieldsets")] + public IEnumerable Fieldsets { get; set; } + + [JsonProperty("hidePropertyLabels")] + public bool HidePropertyLabels { get; set; } + + [JsonProperty("customCssClass")] + public string CustomCssClass { get; set; } + + [JsonProperty("customCssPath")] + public string CustomCssPath { get; set; } + + [JsonProperty("customJsPath")] + public string CustomJsPath { get; set; } + + [JsonProperty("developerMode")] + public bool DeveloperMode { get; set; } + } } diff --git a/app/Umbraco/Umbraco.Archetype/Models/ArchetypePreValueFieldset.cs b/app/Umbraco/Umbraco.Archetype/Models/ArchetypePreValueFieldset.cs index 11db031..251203a 100644 --- a/app/Umbraco/Umbraco.Archetype/Models/ArchetypePreValueFieldset.cs +++ b/app/Umbraco/Umbraco.Archetype/Models/ArchetypePreValueFieldset.cs @@ -1,10 +1,29 @@ using System.Collections.Generic; +using Newtonsoft.Json; namespace Archetype.Umbraco.Models { - internal class ArchetypePreValueFieldset - { - internal string Alias { get; set; } - internal IEnumerable Properties { get; set; } - } -} \ No newline at end of file + public class ArchetypePreValueFieldset + { + [JsonProperty("alias")] + public string Alias { get; set; } + + [JsonProperty("remove")] + public bool Remove { get; set; } + + [JsonProperty("collapse")] + public bool Collapse { get; set; } + + [JsonProperty("labelTemplate")] + public string LabelTemplate { get; set; } + + [JsonProperty("icon")] + public string Icon { get; set; } + + [JsonProperty("label")] + public string Label { get; set; } + + [JsonProperty("properties")] + public IEnumerable Properties { get; set; } + } +} diff --git a/app/Umbraco/Umbraco.Archetype/Models/ArchetypePreValueProperty.cs b/app/Umbraco/Umbraco.Archetype/Models/ArchetypePreValueProperty.cs index ddc6c70..c8c13e4 100644 --- a/app/Umbraco/Umbraco.Archetype/Models/ArchetypePreValueProperty.cs +++ b/app/Umbraco/Umbraco.Archetype/Models/ArchetypePreValueProperty.cs @@ -1,9 +1,37 @@ -namespace Archetype.Umbraco.Models +using Newtonsoft.Json; + +namespace Archetype.Umbraco.Models { - internal class ArchetypePreValueProperty - { - internal string Alias { get; set; } - internal int DataTypeId { get; set; } - internal string PropertyEditorAlias { get; set; } - } + public class ArchetypePreValueProperty + { + [JsonProperty("alias")] + public string Alias { get; set; } + + [JsonProperty("remove")] + public bool Remove { get; set; } + + [JsonProperty("collapse")] + public bool Collapse { get; set; } + + [JsonProperty("label")] + public string Label { get; set; } + + [JsonProperty("helpText")] + public string HelpText { get; set; } + + [JsonProperty("dataTypeId")] + public int DataTypeId { get; set; } + + [JsonProperty("dataTypeGuid")] + public string DataTypeGuid { get; set; } + + [JsonProperty("propertyEditorAlias")] + public string PropertyEditorAlias { get; set; } + + [JsonProperty("value")] + public string Value { get; set; } + + [JsonProperty("required")] + public bool Required { get; set; } + } } \ No newline at end of file From bbe3506747223362bc9af6ceb07f4dd094b36b33 Mon Sep 17 00:00:00 2001 From: leekelleher Date: Thu, 13 Feb 2014 17:50:07 +0000 Subject: [PATCH 8/8] Housekeeping: Fixed up the whitespace :bowtie: There was a mix of tabs & spaces... ... normalized to use spaces (which was painful for me as I'm a "tabs kinda guy" :metal: --- .../Umbraco.Archetype/Models/Fieldset.cs | 14 +-- .../Umbraco.Archetype/Models/Property.cs | 94 +++++++++---------- .../ArchetypeValueConverter.cs | 79 ++++++++-------- 3 files changed, 94 insertions(+), 93 deletions(-) diff --git a/app/Umbraco/Umbraco.Archetype/Models/Fieldset.cs b/app/Umbraco/Umbraco.Archetype/Models/Fieldset.cs index 297d9ad..934a51d 100644 --- a/app/Umbraco/Umbraco.Archetype/Models/Fieldset.cs +++ b/app/Umbraco/Umbraco.Archetype/Models/Fieldset.cs @@ -18,9 +18,9 @@ namespace Archetype.Umbraco.Models Properties = new List(); } - #region Helper Methods + #region Helper Methods - public string GetValue(string propertyAlias) + public string GetValue(string propertyAlias) { return GetValue(propertyAlias); } @@ -29,18 +29,18 @@ namespace Archetype.Umbraco.Models { var property = GetProperty(propertyAlias); - if (property == null || string.IsNullOrEmpty(property.Value.ToString())) - return default(T); + if (property == null || string.IsNullOrEmpty(property.Value.ToString())) + return default(T); - return property.GetValue(); + return property.GetValue(); } private Property GetProperty(string propertyAlias) { return Properties.FirstOrDefault(p => p.Alias.InvariantEquals(propertyAlias)); - } + } - #endregion + #endregion } } diff --git a/app/Umbraco/Umbraco.Archetype/Models/Property.cs b/app/Umbraco/Umbraco.Archetype/Models/Property.cs index 452f980..924cd71 100644 --- a/app/Umbraco/Umbraco.Archetype/Models/Property.cs +++ b/app/Umbraco/Umbraco.Archetype/Models/Property.cs @@ -7,7 +7,7 @@ using Umbraco.Core.PropertyEditors; namespace Archetype.Umbraco.Models { - public class Property + public class Property { [JsonProperty("alias")] public string Alias { get; internal set; } @@ -21,58 +21,58 @@ namespace Archetype.Umbraco.Models [JsonProperty("dataTypeId")] public int DataTypeId { get; internal set; } - public T GetValue() - { - // If the value is of type T, just return it - if (Value is T) - return (T)Value; + public T GetValue() + { + // If the value is of type T, just return it + if (Value is T) + return (T)Value; - // Umbraco has the concept of a IPropertyEditorValueConverter which it - // also queries for property resolvers. However I'm not sure what these - // are for, nor can I find any implementations in core, so am currently - // just ignoring these when looking up converters. - // NB: IPropertyEditorValueConverter not to be confused with - // IPropertyValueConverter which are the ones most people are creating - var properyType = CreateDummyPropertyType(DataTypeId, PropertyEditorAlias); - var converters = PropertyValueConvertersResolver.Current.Converters.ToArray(); + // Umbraco has the concept of a IPropertyEditorValueConverter which it + // also queries for property resolvers. However I'm not sure what these + // are for, nor can I find any implementations in core, so am currently + // just ignoring these when looking up converters. + // NB: IPropertyEditorValueConverter not to be confused with + // IPropertyValueConverter which are the ones most people are creating + var properyType = CreateDummyPropertyType(DataTypeId, PropertyEditorAlias); + var converters = PropertyValueConvertersResolver.Current.Converters.ToArray(); - // In umbraco, there are default value converters that try to convert the - // value if all else fails. The problem is, they are also in the list of - // converters, and the means for filtering these out is internal, so - // we currently have to try ALL converters to see if they can convert - // rather than just finding the most appropreate. If the ability to filter - // out default value converters becomes public, the following logic could - // and probably should be changed. - foreach (var converter in converters.Where(x => x.IsConverter(properyType))) - { - // Convert the type using a found value converter - var value2 = converter.ConvertDataToSource(properyType, Value, false); + // In umbraco, there are default value converters that try to convert the + // value if all else fails. The problem is, they are also in the list of + // converters, and the means for filtering these out is internal, so + // we currently have to try ALL converters to see if they can convert + // rather than just finding the most appropreate. If the ability to filter + // out default value converters becomes public, the following logic could + // and probably should be changed. + foreach (var converter in converters.Where(x => x.IsConverter(properyType))) + { + // Convert the type using a found value converter + var value2 = converter.ConvertDataToSource(properyType, Value, false); - // If the value is of type T, just return it - if (value2 is T) - return (T)value2; + // If the value is of type T, just return it + if (value2 is T) + return (T)value2; - // Value is not final value type, so try a regular type conversion aswell - var convertAttempt = value2.TryConvertTo(); - if (convertAttempt.Success) - return convertAttempt.Result; - } + // Value is not final value type, so try a regular type conversion aswell + var convertAttempt = value2.TryConvertTo(); + if (convertAttempt.Success) + return convertAttempt.Result; + } - // Value is not final value type, so try a regular type conversion - var convertAttempt2 = Value.TryConvertTo(); - if (convertAttempt2.Success) - return convertAttempt2.Result; + // Value is not final value type, so try a regular type conversion + var convertAttempt2 = Value.TryConvertTo(); + if (convertAttempt2.Success) + return convertAttempt2.Result; - return default(T); - } + return default(T); + } - private PublishedPropertyType CreateDummyPropertyType(int dataTypeId, string propertyEditorAlias) - { - return new PublishedPropertyType(null, - new PropertyType(new DataTypeDefinition(-1, propertyEditorAlias) - { - Id = dataTypeId - })); - } + private PublishedPropertyType CreateDummyPropertyType(int dataTypeId, string propertyEditorAlias) + { + return new PublishedPropertyType(null, + new PropertyType(new DataTypeDefinition(-1, propertyEditorAlias) + { + Id = dataTypeId + })); + } } } diff --git a/app/Umbraco/Umbraco.Archetype/PropertyConverters/ArchetypeValueConverter.cs b/app/Umbraco/Umbraco.Archetype/PropertyConverters/ArchetypeValueConverter.cs index d9a1a23..949f9dd 100644 --- a/app/Umbraco/Umbraco.Archetype/PropertyConverters/ArchetypeValueConverter.cs +++ b/app/Umbraco/Umbraco.Archetype/PropertyConverters/ArchetypeValueConverter.cs @@ -1,11 +1,11 @@ using System; using System.Linq; +using Archetype.Umbraco.Extensions; +using Archetype.Umbraco.Models; using Newtonsoft.Json; using Umbraco.Core; -using Umbraco.Core.PropertyEditors; using Umbraco.Core.Models.PublishedContent; -using Archetype.Umbraco.Models; -using Archetype.Umbraco.Extensions; +using Umbraco.Core.PropertyEditors; using Umbraco.Core.Services; namespace Archetype.Umbraco.PropertyConverters @@ -15,20 +15,20 @@ namespace Archetype.Umbraco.PropertyConverters [PropertyValueCache(PropertyCacheValue.All, PropertyCacheLevel.Content)] public class ArchetypeValueConverter : PropertyValueConverterBase { - protected JsonSerializerSettings _jsonSettings; + protected JsonSerializerSettings _jsonSettings; - public ArchetypeValueConverter() - { - var dcr = new Newtonsoft.Json.Serialization.DefaultContractResolver(); - dcr.DefaultMembersSearchFlags |= System.Reflection.BindingFlags.NonPublic; + public ArchetypeValueConverter() + { + var dcr = new Newtonsoft.Json.Serialization.DefaultContractResolver(); + dcr.DefaultMembersSearchFlags |= System.Reflection.BindingFlags.NonPublic; - _jsonSettings = new JsonSerializerSettings { ContractResolver = dcr }; - } + _jsonSettings = new JsonSerializerSettings { ContractResolver = dcr }; + } - public ServiceContext Services - { - get { return ApplicationContext.Current.Services; } - } + public ServiceContext Services + { + get { return ApplicationContext.Current.Services; } + } public override bool IsConverter(PublishedPropertyType propertyType) { @@ -37,9 +37,10 @@ namespace Archetype.Umbraco.PropertyConverters public override object ConvertDataToSource(PublishedPropertyType propertyType, object source, bool preview) { - if (source == null) return null; - - var sourceString = source.ToString(); + if (source == null) + return null; + + var sourceString = source.ToString(); if (sourceString.DetectIsJson()) { @@ -85,32 +86,32 @@ namespace Archetype.Umbraco.PropertyConverters return sourceString; } - internal ArchetypePreValue GetArchetypePreValueFromDataTypeId(int dataTypeId) - { - var preValues = Services.DataTypeService.GetPreValuesCollectionByDataTypeId(dataTypeId); + internal ArchetypePreValue GetArchetypePreValueFromDataTypeId(int dataTypeId) + { + var preValues = Services.DataTypeService.GetPreValuesCollectionByDataTypeId(dataTypeId); - var configJson = preValues.IsDictionaryBased - ? preValues.PreValuesAsDictionary[Constants.PreValueAlias].Value - : preValues.PreValuesAsArray.First().Value; + var configJson = preValues.IsDictionaryBased + ? preValues.PreValuesAsDictionary[Constants.PreValueAlias].Value + : preValues.PreValuesAsArray.First().Value; - var config = JsonConvert.DeserializeObject(configJson, _jsonSettings); + var config = JsonConvert.DeserializeObject(configJson, _jsonSettings); - foreach (var fieldset in config.Fieldsets) - { - foreach (var property in fieldset.Properties) - { - // Lookup the properties property editor alias - // (See if we've already looked it up first though to save a database hit) - var propertyWithSameDataType = config.Fieldsets.SelectMany(x => x.Properties) - .FirstOrDefault(x => x.DataTypeId == property.DataTypeId && !string.IsNullOrWhiteSpace(x.PropertyEditorAlias)); + foreach (var fieldset in config.Fieldsets) + { + foreach (var property in fieldset.Properties) + { + // Lookup the properties property editor alias + // (See if we've already looked it up first though to save a database hit) + var propertyWithSameDataType = config.Fieldsets.SelectMany(x => x.Properties) + .FirstOrDefault(x => x.DataTypeId == property.DataTypeId && !string.IsNullOrWhiteSpace(x.PropertyEditorAlias)); - property.PropertyEditorAlias = propertyWithSameDataType != null - ? propertyWithSameDataType.PropertyEditorAlias - : Services.DataTypeService.GetDataTypeDefinitionById(property.DataTypeId).PropertyEditorAlias; - } - } + property.PropertyEditorAlias = propertyWithSameDataType != null + ? propertyWithSameDataType.PropertyEditorAlias + : Services.DataTypeService.GetDataTypeDefinitionById(property.DataTypeId).PropertyEditorAlias; + } + } - return config; - } + return config; + } } }