Created
February 3, 2022 23:21
-
-
Save KevinJump/9c935704614a7a69ade0ad673e5a40f3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Newtonsoft.Json; | |
using Newtonsoft.Json.Linq; | |
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using Umbraco.Core; | |
using Umbraco.Core.Logging; | |
using Umbraco.Core.Models; | |
using Umbraco.Core.Services; | |
using uSync8.ContentEdition.Mapping; | |
using uSync8.Core.Dependency; | |
using static Umbraco.Core.Constants; | |
namespace uSync.Community.Leblender | |
{ | |
/// <summary> | |
/// Leblender mapper for Umbraco v8 | |
/// </summary> | |
/// <remarks> | |
/// This mapper is not supported and hasn't actually been tested against a working version of LeBlender for v8 | |
/// | |
/// LeBlender has no offical release for v8 so there are many assumptions here that your version of leblender | |
/// is similar to the orginal v7 version. | |
/// | |
/// this mapper will go through each property within a leblender value, and attempt to map the individual | |
/// values within that to the correct data type. | |
/// | |
/// the call to SyncValueMapperFactory.GetExportValue or SyncValueMapperFactory.GetImportValue | |
/// asctually do the work as they pass the values for each field to the relevant Mapper for that type | |
/// | |
/// the GetDependencies call is only required if you are using uSync.Complete and is used to so | |
/// uSync.Publisher can tell what datatypes also need publishing to make this property work on the other | |
/// side. | |
/// </remarks> | |
public class LeBlenderMapper : SyncValueMapperBase, ISyncMapper | |
{ | |
private IDataTypeService _dataTypeService; | |
private ILogger _logger; | |
public LeBlenderMapper( | |
IEntityService entityService, | |
IDataTypeService dataTypeService, | |
ILogger logger | |
) : base(entityService) | |
{ | |
_dataTypeService = dataTypeService; | |
_logger = logger; | |
} | |
public override string Name => "LeBlenderMapper"; | |
public override string[] Editors => new[] | |
{ | |
"Umbraco.Grid.LeBlender", | |
"Umbraco.Grid.LeBlendereditor" | |
}; | |
public override string GetExportValue(object value, string editorAlias) | |
{ | |
if (value == null) return String.Empty; | |
var items = GetLeblenderObjects(value); | |
_logger.Debug<LeBlenderMapper>("Getting Export value for leblender {value}", value); | |
foreach (var item in items) | |
{ | |
foreach(var val in item.Values()) | |
{ | |
var dtdValue = val.Value<string>("dataTypeGuid"); | |
string propValue = val["value"].ToString(); | |
var dataType = GetDataType(dtdValue); | |
if (dataType != null) continue; | |
_logger.Debug<LeBlenderMapper>("Looking to map {editor} {value}", dataType.Editor, propValue); | |
var result = SyncValueMapperFactory.GetExportValue(propValue, dataType.EditorAlias); | |
if (result != null) | |
{ | |
val["value"] = JToken.Parse(result); | |
} | |
} | |
} | |
var mappedValue = JsonConvert.SerializeObject(items, Formatting.Indented); | |
_logger.Debug<LeBlenderMapper>("Export value: {items}", mappedValue); | |
return mappedValue; | |
} | |
public override string GetImportValue(string value, string editorAlias) | |
{ | |
if (value == null) return value; | |
var items = GetLeblenderObjects(value); | |
_logger.Debug<LeBlenderMapper>("Getting Import value for leblender {value}", value); | |
foreach (var item in items) | |
{ | |
foreach (var val in item.Values()) | |
{ | |
var dtdValue = val.Value<string>("dataTypeGuid"); | |
string propValue = val["value"].ToString(); | |
var dataType = GetDataType(dtdValue); | |
if (dataType != null) continue; | |
_logger.Debug<LeBlenderMapper>("Looking to map {editor} {value}", dataType.Editor, propValue); | |
var result = SyncValueMapperFactory.GetExportValue(propValue, dataType.EditorAlias); | |
if (result != null) | |
{ | |
val["value"] = JToken.Parse(result); | |
} | |
} | |
} | |
var mappedValue = JsonConvert.SerializeObject(items, Formatting.Indented); | |
_logger.Debug<LeBlenderMapper>("Import value: {items}", mappedValue); | |
return mappedValue; | |
} | |
private IEnumerable<JObject> GetLeblenderObjects(object value) | |
{ | |
if (value == null) | |
return Enumerable.Empty<JObject>(); | |
var attempt = value.TryConvertTo<string>(); | |
if (!attempt || string.IsNullOrWhiteSpace(attempt.Result)) | |
return Enumerable.Empty<JObject>(); | |
return GetLeblenderObjects(attempt.Result); | |
} | |
private IEnumerable<JObject> GetLeblenderObjects(string value) | |
{ | |
if (string.IsNullOrWhiteSpace(value)) | |
return Enumerable.Empty<JObject>(); | |
var items = JsonConvert.DeserializeObject<IEnumerable<JObject>>(value); | |
if (items == null) | |
return Enumerable.Empty<JObject>(); | |
return items; | |
} | |
private IDataType GetDataType(string dataTypeGuid) | |
{ | |
if (Guid.TryParse(dataTypeGuid, out Guid id)) | |
{ | |
return _dataTypeService.GetDataType(id); | |
} | |
return null; | |
} | |
public override IEnumerable<uSyncDependency> GetDependencies(object value, string editorAlias, DependencyFlags flags) | |
{ | |
if (!flags.HasFlag(DependencyFlags.IncludeDependencies)) | |
return Enumerable.Empty<uSyncDependency>(); | |
var dependencies = new List<uSyncDependency>(); | |
var items = GetLeblenderObjects(value); | |
foreach (var item in items) | |
{ | |
foreach (var val in item.Values()) | |
{ | |
var dtdValue = val.Value<string>("dataTypeGuid"); | |
var dataType = GetDataType(dtdValue); | |
if (dataType != null) continue; | |
var udi = Udi.Create(UdiEntityType.DataType, dataType.Key); | |
dependencies.Add(CreateDependency(udi as GuidUdi, flags)); | |
} | |
} | |
return dependencies; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment