Merge pull request #108 from imulus/pvc-refactor

Extract methods from ValueConverter for reuse
This commit is contained in:
Tom Fulton
2014-04-27 22:55:15 -06:00
3 changed files with 131 additions and 81 deletions
@@ -231,6 +231,7 @@
<Compile Include="Api\ArchetypeDataTypeController.cs" />
<Compile Include="Constants.cs" />
<Compile Include="Events\ExpireCache.cs" />
<Compile Include="Extensions\ArchetypeHelper.cs" />
<Compile Include="Extensions\Extensions.cs" />
<Compile Include="Models\Archetype.cs" />
<Compile Include="Models\ArchetypePreValue.cs" />
@@ -0,0 +1,124 @@
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;
internal ArchetypeHelper()
{
var dcr = new Newtonsoft.Json.Serialization.DefaultContractResolver();
dcr.DefaultMembersSearchFlags |= System.Reflection.BindingFlags.NonPublic;
_jsonSettings = new JsonSerializerSettings { ContractResolver = dcr };
_app = ApplicationContext.Current;
}
internal Models.Archetype DeserializeJsonToArchetype(string sourceJson, int dataTypeId)
{
try
{
var archetype = JsonConvert.DeserializeObject<Models.Archetype>(sourceJson, _jsonSettings);
try
{
// Get list of configured properties and their types and map them to the deserialized archetype model
var preValue = GetArchetypePreValueFromDataTypeId(dataTypeId);
RetrieveAdditionalProperties(ref archetype, preValue);
}
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<ArchetypePreValue>(configJson, _jsonSettings);
RetrieveAdditionalProperties(ref config);
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));
}
/// <summary>
/// Retrieves additional metadata that isn't available on the stored model of an Archetype
/// </summary>
/// <param name="archetype">The Archetype to add the additional metadata to</param>
/// <param name="preValue">The configuration of the Archetype</param>
private void RetrieveAdditionalProperties(ref Models.Archetype archetype, ArchetypePreValue preValue)
{
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;
}
}
}
}
}
/// <summary>
/// Retrieves additional metadata that isn't available on the stored model of an ArchetypePreValue
/// </summary>
/// <param name="archetype">The Archetype to add the additional metadata to</param>
/// <param name="preValue">The configuration of the Archetype</param>
private void RetrieveAdditionalProperties(ref Models.ArchetypePreValue preValue)
{
foreach (var fieldset in preValue.Fieldsets)
{
foreach (var property in fieldset.Properties)
{
property.PropertyEditorAlias = GetDataTypeByGuid(property.DataTypeGuid).PropertyEditorAlias;
}
}
}
}
}
@@ -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,13 @@ namespace Archetype.Umbraco.PropertyConverters
var sourceString = source.ToString();
if (!sourceString.DetectIsJson())
return defaultValue;
try
{
// Deserialize value to archetype model
var archetype = JsonConvert.DeserializeObject<Models.Archetype>(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 defaultValue;
var archetype = new ArchetypeHelper().DeserializeJsonToArchetype(source.ToString(),
(propertyType != null ? propertyType.DataTypeId : -1));
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<ArchetypePreValue>(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));
}
}
}