Skip to content

Instantly share code, notes, and snippets.

@jaandrews
Last active June 1, 2020 23:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jaandrews/0db6ed9d88a41246053bf8a95ad0384e to your computer and use it in GitHub Desktop.
Save jaandrews/0db6ed9d88a41246053bf8a95ad0384e to your computer and use it in GitHub Desktop.
Umbraco 8 LeBlender Value Mapper
using Umbraco.Core;
using Umbraco.Core.Logging;
using Jumoo.TranslationManager.Core.Models;
using Jumoo.TranslationManager.Core.ValueMappers;
using System;
using System.Collections.Generic;
using System.Linq;
using Umbraco.Core.Services;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace Umbraco.TranslationValueMappers {
public class LeBlenderValueMapper : BaseValueMapper, IValueMapper {
public string Name => "Leblender Value Mapper";
public override string[] Editors => new[] { "Umbraco.Grid.leblenderEditor" };
public LeBlenderValueMapper(IContentService contentService, IDataTypeService dataTypeService, IContentTypeService contentTypeService, ILogger logger) : base(contentService, dataTypeService, contentTypeService, logger) {
}
public TranslationValue GetSourceValue(string displayName, string propertyTypeAlias, object value, CultureInfoView culture) {
var attempt = value.TryConvertTo<string>();
if (!attempt || string.IsNullOrWhiteSpace(attempt.Result)) return null;
logger.Debug<LeBlenderValueMapper>($"{propertyTypeAlias} {value}");
var jsonValue = JsonConvert.DeserializeObject<IEnumerable<Dictionary<string, LeBlenderProperties>>>(attempt.Result);
if (jsonValue == null || !jsonValue.Any()) return null;
var translationValue = new TranslationValue(displayName, propertyTypeAlias);
var count = 0;
foreach (var item in jsonValue) {
count++;
var itemValue = new TranslationValue(displayName, propertyTypeAlias, count);
var position = 0;
foreach (var property in item) {
position++;
logger.Debug<LeBlenderValueMapper>($"Property: [{property.Key}] {property.Value.Alias} {property.Value.Value} {property.Value.DataTypeGuid}");
var editorAlias = GetEditorAlias(property.Value.DataTypeGuid);
if (editorAlias == string.Empty)
continue;
string propertyDisplayName = string.Format("{0} {1}", displayName, property.Value.Name);
var innerValue = ValueMapperFactory.GetMapperSource(editorAlias, propertyDisplayName, property.Value.Value, culture);
if (innerValue != null) {
innerValue.SortOrder = position;
itemValue.InnerValues.Add(property.Value.Alias, innerValue);
}
}
if (itemValue.HasChildValues()) {
translationValue.InnerValues.Add(count.ToString(), itemValue);
}
}
if (translationValue.HasChildValues())
return translationValue;
return null;
}
public object GetTargetValue(string propertyTypeAlias, object sourceValue, TranslationValue values, CultureInfoView sourceCulture, CultureInfoView targetCulture) {
logger.Debug<LeBlenderValueMapper>($"{propertyTypeAlias}");
var attempt = sourceValue.TryConvertTo<string>();
if (!attempt) return null;
var jsonValue = JsonConvert.DeserializeObject<IEnumerable<Dictionary<string, LeBlenderProperties>>>(attempt.Result);
if (jsonValue == null || !jsonValue.Any()) return null;
logger.Debug<LeBlenderValueMapper>($"Found {jsonValue.Count()} items");
var count = 0;
foreach (var item in jsonValue) {
logger.Debug<LeBlenderValueMapper>($"Getting values for {item.ToString()}");
count++;
var itemValue = values.InnerValues[count.ToString()];
if (itemValue == null)
continue;
foreach (var property in item) {
var propertyValue = itemValue.InnerValues[property.Value.Alias];
if (propertyValue == null)
continue;
var editorAlias = GetEditorAlias(property.Value.DataTypeGuid);
if (editorAlias == string.Empty)
continue;
var value = ValueMapperFactory.GetMapperTarget(editorAlias, property.Value.Value, propertyValue, sourceCulture, targetCulture);
var valueText = value.TryConvertTo<string>();
if (!valueText.Success || valueText.Result == null)
continue;
if (valueText.Result.DetectIsJson()) {
property.Value.Value = JToken.Parse(valueText.Result);
}
else {
property.Value.Value = valueText.Result;
}
}
}
logger.Debug<LeBlenderValueMapper>($"LeBlender Value: {JsonConvert.SerializeObject(jsonValue)}");
return JsonConvert.SerializeObject(jsonValue);
}
private string GetEditorAlias(string dtdGuidString) {
if (string.IsNullOrWhiteSpace(dtdGuidString))
return string.Empty;
var propEditor = string.Empty;
if (Guid.TryParse(dtdGuidString, out Guid dtdGuid)) {
var dtd = dataTypeService.GetDataType(dtdGuid);
if (dtd != null) {
return dtd.EditorAlias;
}
}
return string.Empty;
}
}
public class LeBlenderProperties {
[JsonProperty("dataTypeGuid")]
public String DataTypeGuid { get; set; }
[JsonProperty("editorName")]
public String Name { get; set; }
[JsonProperty("editorAlias")]
public String Alias { get; set; }
[JsonProperty("value")]
public object Value { get; set; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment