-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLeBlenderLinkMapper.cs
117 lines (93 loc) · 4.17 KB
/
LeBlenderLinkMapper.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
using Jumoo.TranslationManager.Core.Models;
using Jumoo.TranslationManager.LinkUpdater.LinkMappers;
using Jumoo.TranslationManager.LinkUpdater;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Umbraco.Core.Services;
using Umbraco.Core;
namespace Our.TranslationManager.LeBlender
{
public class LeBlenderMapper : LinkMapperBase, ILinkMapper
{
private readonly IDataTypeService dataTypeService;
public LeBlenderMapper(IDataTypeService dataTypeService, LinkResolver linkResolver)
: base(linkResolver)
{
this.dataTypeService = dataTypeService;
}
public string Name => "Leblender Link Mapper";
public string[] Editors => new string[] {
"Umbraco.Grid.LeBlender",
"Umbraco.Grid.LeBlendereditor",
};
public object UpdateLinkValues(TranslationSet set, int targetSiteId, object sourceValue, object targetValue)
{
if (set == null || sourceValue == null || targetValue == null) return targetValue;
var target = targetValue.TryConvertTo<string>();
if (!target.Success) return targetValue;
var source = sourceValue.TryConvertTo<string>();
if (!source.Success) return targetValue;
if (!target.Result.DetectIsJson() || !source.Result.DetectIsJson()) return targetValue;
var sourceJson = LeBlenderExtensions.GetLeblenderModel(source.Result).ToList();
if (sourceJson == null | !sourceJson.Any()) return targetValue;
var targetJson = LeBlenderExtensions.GetLeblenderModel(target.Result).ToList();
if (targetJson == null | !targetJson.Any()) return targetValue;
if (sourceJson.Count() != targetJson.Count()) return targetValue;
for (int x = 0; x < targetJson.Count(); x++)
{
var targetItem = targetJson[x];
var sourceItem = sourceJson[x];
foreach (var targetProperty in targetItem)
{
if (!sourceItem.ContainsKey(targetProperty.Key)) continue;
var sourceProperty = sourceItem[targetProperty.Key];
var editorAlias = GetEdtiorAlias(targetProperty.Value.DataTypeGuid);
if (editorAlias == string.Empty) continue;
var mapper = LinkMapperFactory.GetMapper(editorAlias);
if (mapper != null)
{
var sourceVal = sourceProperty.Value.TryConvertTo<string>();
var targetVal = targetProperty.Value.Value.TryConvertTo<string>();
if (sourceVal.Success && targetVal.Success)
{
var value = mapper.UpdateLinkValues(set, targetSiteId, sourceVal.Result, targetVal.Result);
var text = value.TryConvertTo<string>();
if (text.Success)
{
if (text.Result.DetectIsJson())
{
targetProperty.Value.Value = JToken.Parse(text.Result);
}
else
{
targetProperty.Value.Value = text.Result;
}
}
}
}
}
}
return JsonConvert.SerializeObject(targetJson, Formatting.Indented);
}
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.GetDataType(dtdGuid);
if (dtd != null)
{
return dtd.EditorAlias;
}
}
return string.Empty;
}
}
}