This repository has been archived by the owner on Dec 13, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
TupleValueConverter.cs
167 lines (141 loc) · 7 KB
/
TupleValueConverter.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
using System.Xml.XPath;
using Newtonsoft.Json;
using Our.Umbraco.Tuple.Models;
using Our.Umbraco.Tuple.PropertyEditors;
using Umbraco.Core;
using Umbraco.Core.Cache;
using Umbraco.Core.Models;
using Umbraco.Core.Models.PublishedContent;
using Umbraco.Core.PropertyEditors;
namespace Our.Umbraco.Tuple.ValueConverters
{
public class TupleValueConverter : PropertyValueConverterBase, IPropertyValueConverterMeta
{
public override bool IsConverter(PublishedPropertyType propertyType)
{
return propertyType.PropertyEditorAlias.Equals(TuplePropertyEditor.PropertyEditorAlias);
}
public PropertyCacheLevel GetPropertyCacheLevel(PublishedPropertyType propertyType, PropertyCacheValue cacheValue)
{
return PropertyCacheLevel.Content;
}
public Type GetPropertyValueType(PublishedPropertyType propertyType)
{
var innerPropertyTypes = this.GetInnerPublishedPropertyTypes(propertyType);
var clrTypes = innerPropertyTypes.Select(x => x.ClrType).ToArray();
var tuple = Type.GetType($"System.Tuple`{clrTypes.Length}");
return tuple.MakeGenericType(clrTypes);
}
public override object ConvertDataToSource(PublishedPropertyType propertyType, object source, bool preview)
{
var data = source?.ToString();
if (string.IsNullOrWhiteSpace(data))
return base.ConvertDataToSource(propertyType, source, preview);
var innerPropertyTypes = this.GetInnerPublishedPropertyTypes(propertyType);
if (innerPropertyTypes == null || innerPropertyTypes.Count == 0)
return base.ConvertDataToSource(propertyType, source, preview);
var model = new TupleValueItems();
// Detect whether the value is in JSON or XML format
//
// NOTE: We can't be sure which format the data is in.
// With "nested property-editors", (e.g. Nested Content, Stacked Content),
// they don't convert the call `ConvertDbToXml`.
if (data.DetectIsJson())
{
model.AddRange(JsonConvert.DeserializeObject<TupleValueItems>(data));
}
else
{
// otherwise we assume it's XML
var elements = XElement.Parse(data);
if (elements != null && elements.HasElements)
{
model.AddRange(elements.XPathSelectElements("value").Select(x => new TupleValueItem { Value = x.Value }));
}
}
if (model == null && model.Count == 0)
return base.ConvertDataToSource(propertyType, source, preview);
for (int i = 0; i < model.Count; i++)
{
var item = model[i];
var innerPropertyType = innerPropertyTypes[i];
var itemSource = innerPropertyType.ConvertDataToSource(item.Value, preview);
item.Value = itemSource;
}
return model;
}
public override object ConvertSourceToObject(PublishedPropertyType propertyType, object source, bool preview)
{
if (source is TupleValueItems model)
{
var innerPropertyTypes = this.GetInnerPublishedPropertyTypes(propertyType);
if (innerPropertyTypes == null || innerPropertyTypes.Count == 0)
return base.ConvertSourceToObject(propertyType, source, preview);
var objects = new object[model.Count];
for (int i = 0; i < model.Count; i++)
{
var innerPropertyType = innerPropertyTypes[i];
var itemObject = innerPropertyType.ConvertSourceToObject(model[i].Value, preview);
var attempt = itemObject.TryConvertTo(innerPropertyType.ClrType);
if (attempt.Success)
objects[i] = attempt.Result;
}
return Activator.CreateInstance(propertyType.ClrType, objects);
}
return base.ConvertSourceToObject(propertyType, source, preview);
}
public override object ConvertSourceToXPath(PublishedPropertyType propertyType, object source, bool preview)
{
if (source is TupleValueItems model)
{
var innerPropertyTypes = this.GetInnerPublishedPropertyTypes(propertyType);
if (innerPropertyTypes == null || innerPropertyTypes.Count == 0)
return base.ConvertSourceToXPath(propertyType, source, preview);
var elements = new XElement[model.Count];
for (int i = 0; i < model.Count; i++)
{
var itemXPath = innerPropertyTypes[i].ConvertSourceToXPath(model[i].Value, preview);
elements[i] = new XElement($"item{i}", itemXPath);
}
return new XElement("items", elements).CreateNavigator();
}
return base.ConvertSourceToXPath(propertyType, source, preview);
}
private List<PublishedPropertyType> GetInnerPublishedPropertyTypes(PublishedPropertyType propertyType)
{
var cacheKey = string.Format(
"Our.Umbraco.Tuple.PropertyListValueConverter.GetInnerPublishedPropertyTypes_{0}_{1}",
propertyType.DataTypeId,
propertyType.ContentType.Id);
return ApplicationContext.Current.ApplicationCache.RuntimeCache.GetCacheItem<List<PublishedPropertyType>>(
cacheKey,
() =>
{
var list = new List<PublishedPropertyType>();
var dataTypeService = ApplicationContext.Current.Services.DataTypeService;
var prevalues = dataTypeService.GetPreValuesCollectionByDataTypeId(propertyType.DataTypeId);
var dict = prevalues.PreValuesAsDictionary;
if (dict.ContainsKey("dataTypes"))
{
var dtdPreValue = dict["dataTypes"];
var items = JsonConvert.DeserializeObject<IEnumerable<TupleValueItem>>(dtdPreValue.Value);
foreach (var item in items)
{
var dtd = dataTypeService.GetDataTypeDefinitionById(item.DataTypeGuid);
list.Add(new PublishedPropertyType(propertyType.ContentType, new PropertyType(dtd, propertyType.PropertyTypeAlias)));
}
}
return list;
});
}
internal static void ClearDataTypeCache(int dataTypeId)
{
ApplicationContext.Current.ApplicationCache.RuntimeCache.ClearCacheByKeySearch(
string.Concat("Our.Umbraco.Tuple.PropertyListValueConverter.GetInnerPublishedPropertyTypes_", dataTypeId));
}
}
}