Skip to content

Instantly share code, notes, and snippets.

@KevinJump
Created November 1, 2019 14:41
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 KevinJump/91928a31f48a12bdb60d75d4d820a775 to your computer and use it in GitHub Desktop.
Save KevinJump/91928a31f48a12bdb60d75d4d820a775 to your computer and use it in GitHub Desktop.
LeBlender Value mapper for Translation Manager for v2.x
using Jumoo.TranslationManager.Core.Models;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using Umbraco.Core;
using Umbraco.Core.Logging;
namespace Jumoo.TranslationManager.Core.ValueMappers
{
public class LeBlenderValueMapper : BaseValueMapper, IValueMapper
{
public string Name => "Leblender Value Mapper";
public override string[] Editors => new string[] { "Umbraco.Grid.LeBlender" };
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>("{0} {1}", () => 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: [{0}] {1} {2} {3}",
() => property.Key,
() => property.Value.Alias, () => property.Value.Value, () => property.Value.DataTypeGuid);
var editorAlias = GetEdtiorAlias(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>("{0}", () => 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 {0} items", () => jsonValue.Count());
var count = 0;
foreach (var item in jsonValue)
{
_logger.Debug<LeBlenderValueMapper>("Getting values for {0}", () => item.ToString());
count++;
var itemValue = values.GetInnerValue(count.ToString());
if (itemValue == null)
continue;
foreach (var property in item)
{
var propertyValue = itemValue.GetInnerValue(property.Value.Alias);
if (propertyValue == null)
continue;
var editorAlias = GetEdtiorAlias(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: {0}", () => JsonConvert.SerializeObject(jsonValue));
return JsonConvert.SerializeObject(jsonValue);
}
private string GetEdtiorAlias(string dtdGuidString)
{
if (string.IsNullOrWhiteSpace(dtdGuidString))
return string.Empty;
var propEditor = string.Empty;
if (Guid.TryParse(dtdGuidString, out Guid dtdGuid))
{
var dtd = _dataTypeService.GetDataTypeDefinitionById(dtdGuid);
if(dtd != null)
{
return dtd.PropertyEditorAlias;
}
}
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