diff --git a/app/Umbraco/Umbraco.Archetype/Archetype.Umbraco.csproj b/app/Umbraco/Umbraco.Archetype/Archetype.Umbraco.csproj index e86b51e..f821384 100644 --- a/app/Umbraco/Umbraco.Archetype/Archetype.Umbraco.csproj +++ b/app/Umbraco/Umbraco.Archetype/Archetype.Umbraco.csproj @@ -231,6 +231,7 @@ + diff --git a/app/Umbraco/Umbraco.Archetype/Extensions/ArchetypeHelper.cs b/app/Umbraco/Umbraco.Archetype/Extensions/ArchetypeHelper.cs new file mode 100644 index 0000000..6d5a0f8 --- /dev/null +++ b/app/Umbraco/Umbraco.Archetype/Extensions/ArchetypeHelper.cs @@ -0,0 +1,106 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net.Mime; +using System.Text; +using System.Threading.Tasks; +using System.Web.Http.ModelBinding; +using Archetype.Umbraco.Models; +using Newtonsoft.Json; +using Umbraco.Core; +using Umbraco.Core.Models; +using Umbraco.Core.Models.PublishedContent; + +namespace Archetype.Umbraco.Extensions +{ + public class ArchetypeHelper + { + + protected JsonSerializerSettings _jsonSettings; + protected ApplicationContext _app; + + public ArchetypeHelper() + { + var dcr = new Newtonsoft.Json.Serialization.DefaultContractResolver(); + dcr.DefaultMembersSearchFlags |= System.Reflection.BindingFlags.NonPublic; + + _jsonSettings = new JsonSerializerSettings { ContractResolver = dcr }; + _app = ApplicationContext.Current; + } + + public Models.Archetype DeserializeJsonToArchetype(string sourceJson, int dataTypeId) + { + try + { + var archetype = JsonConvert.DeserializeObject(sourceJson, _jsonSettings); + + try + { + // Get list of configured properties and their types and map them to the deserialized archetype model + var preValue = GetArchetypePreValueFromDataTypeId(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 = GetDataTypeByGuid(property.DataTypeGuid).Id; + propertyInst.PropertyEditorAlias = property.PropertyEditorAlias; + } + } + } + } + } + catch (Exception ex) + { + } + + return archetype; + } + catch + { + return new Models.Archetype(); + } + } + + private ArchetypePreValue GetArchetypePreValueFromDataTypeId(int dataTypeId) + { + return _app.ApplicationCache.RuntimeCache.GetCacheItem( + Constants.CacheKey_PreValueFromDataTypeId + dataTypeId, + () => + { + var preValues = _app.Services.DataTypeService.GetPreValuesCollectionByDataTypeId(dataTypeId); + + var configJson = preValues.IsDictionaryBased + ? preValues.PreValuesAsDictionary[Constants.PreValueAlias].Value + : preValues.PreValuesAsArray.First().Value; + + var config = JsonConvert.DeserializeObject(configJson, _jsonSettings); + + foreach (var fieldset in config.Fieldsets) + { + foreach (var property in fieldset.Properties) + { + property.PropertyEditorAlias = GetDataTypeByGuid(property.DataTypeGuid).PropertyEditorAlias; + } + } + + return config; + + }) as ArchetypePreValue; + } + + private IDataTypeDefinition GetDataTypeByGuid(Guid guid) + { + return (IDataTypeDefinition) ApplicationContext.Current.ApplicationCache.RuntimeCache.GetCacheItem( + Constants.CacheKey_DataTypeByGuid + guid, + () => _app.Services.DataTypeService.GetDataTypeDefinitionById(guid)); + } + + } +} diff --git a/app/Umbraco/Umbraco.Archetype/PropertyConverters/ArchetypeValueConverter.cs b/app/Umbraco/Umbraco.Archetype/PropertyConverters/ArchetypeValueConverter.cs index 3604acb..2834b36 100644 --- a/app/Umbraco/Umbraco.Archetype/PropertyConverters/ArchetypeValueConverter.cs +++ b/app/Umbraco/Umbraco.Archetype/PropertyConverters/ArchetypeValueConverter.cs @@ -16,15 +16,6 @@ namespace Archetype.Umbraco.PropertyConverters [PropertyValueCache(PropertyCacheValue.All, PropertyCacheLevel.Content)] public class ArchetypeValueConverter : PropertyValueConverterBase { - protected JsonSerializerSettings _jsonSettings; - - public ArchetypeValueConverter() - { - var dcr = new Newtonsoft.Json.Serialization.DefaultContractResolver(); - dcr.DefaultMembersSearchFlags |= System.Reflection.BindingFlags.NonPublic; - - _jsonSettings = new JsonSerializerSettings { ContractResolver = dcr }; - } public ServiceContext Services { @@ -46,79 +37,12 @@ namespace Archetype.Umbraco.PropertyConverters var sourceString = source.ToString(); if (!sourceString.DetectIsJson()) - return defaultValue; + return defaultValue; + + var archetype = new ArchetypeHelper().DeserializeJsonToArchetype(source.ToString(), propertyType.DataTypeId); - try - { - // Deserialize value to archetype model - var archetype = JsonConvert.DeserializeObject(sourceString, _jsonSettings); - - 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 = GetDataTypeByGuid(property.DataTypeGuid).Id; - propertyInst.PropertyEditorAlias = property.PropertyEditorAlias; - } - } - } - } - } - catch (Exception ex) - { - } - - return archetype; - } - catch (Exception ex) - { - } - - return defaultValue; + return archetype; } - private ArchetypePreValue GetArchetypePreValueFromDataTypeId(int dataTypeId) - { - return ApplicationContext.Current.ApplicationCache.RuntimeCache.GetCacheItem( - Constants.CacheKey_PreValueFromDataTypeId + dataTypeId, - () => - { - var preValues = Services.DataTypeService.GetPreValuesCollectionByDataTypeId(dataTypeId); - - var configJson = preValues.IsDictionaryBased - ? preValues.PreValuesAsDictionary[Constants.PreValueAlias].Value - : preValues.PreValuesAsArray.First().Value; - - var config = JsonConvert.DeserializeObject(configJson, _jsonSettings); - - foreach (var fieldset in config.Fieldsets) - { - foreach (var property in fieldset.Properties) - { - property.PropertyEditorAlias = GetDataTypeByGuid(property.DataTypeGuid).PropertyEditorAlias; - } - } - - return config; - - }) as ArchetypePreValue; - } - - private IDataTypeDefinition GetDataTypeByGuid(Guid guid) - { - return (IDataTypeDefinition) ApplicationContext.Current.ApplicationCache.RuntimeCache.GetCacheItem( - Constants.CacheKey_DataTypeByGuid + guid, - () => Services.DataTypeService.GetDataTypeDefinitionById(guid)); - } } }