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 2
/
PropertyListValueConverter.cs
184 lines (157 loc) · 6.96 KB
/
PropertyListValueConverter.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
using System.Xml.XPath;
using Newtonsoft.Json;
using Our.Umbraco.PropertyList.Models;
using Our.Umbraco.PropertyList.PropertyEditors;
using Umbraco.Core;
using Umbraco.Core.Cache;
using Umbraco.Core.Models;
using Umbraco.Core.Models.PublishedContent;
using Umbraco.Core.PropertyEditors;
using static Our.Umbraco.PropertyList.PropertyListConstants;
namespace Our.Umbraco.PropertyList.ValueConverters
{
public class PropertyListValueConverter : PropertyValueConverterBase, IPropertyValueConverterMeta
{
public override bool IsConverter(PublishedPropertyType propertyType)
{
return propertyType.PropertyEditorAlias.InvariantEquals(PropertyEditorKeys.Alias);
}
public override object ConvertDataToSource(PublishedPropertyType propertyType, object source, bool preview)
{
var data = source?.ToString();
if (string.IsNullOrWhiteSpace(data))
{
return null;
}
var innerPropertyType = this.GetInnerPublishedPropertyType(propertyType);
var items = new List<object>();
// 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())
{
var model = JsonConvert.DeserializeObject<PropertyListValue>(data);
if (model != null)
{
items.AddRange(model.Values);
}
}
else
{
// otherwise we assume it's XML
var elements = XElement.Parse(data);
if (elements != null && elements.HasElements)
{
items.AddRange(elements.XPathSelectElements("value").Select(x => x.Value));
}
}
var values = new List<object>();
foreach (var valueData in items)
{
var valueSource = innerPropertyType.ConvertDataToSource(valueData, preview);
values.Add(valueSource);
}
return values;
}
public override object ConvertSourceToObject(PublishedPropertyType propertyType, object source, bool preview)
{
if (source is List<object> items)
{
var innerPropertyType = this.GetInnerPublishedPropertyType(propertyType);
var objects = new List<object>();
foreach (var item in items)
{
if (item != null)
{
objects.Add(innerPropertyType.ConvertSourceToObject(item, preview));
}
}
// Ensure the result is of the correct type
var targetType = innerPropertyType.ClrType;
var result = Array.CreateInstance(targetType, objects.Count);
for (var i = 0; i < objects.Count; i++)
{
var attempt = objects[i].TryConvertTo(targetType);
if (attempt.Success)
{
result.SetValue(attempt.Result, i);
}
else
{
// NOTE: At this point `TryConvertTo` can't convert to the `targetType`.
// This may be a case where the `targetType` is an interface.
// We can attempt to cast it directly, as a last resort.
if (targetType.IsInstanceOfType(objects[i]))
{
result.SetValue(objects[i], i);
}
}
}
return result;
}
return base.ConvertSourceToObject(propertyType, source, preview);
}
public override object ConvertSourceToXPath(PublishedPropertyType propertyType, object source, bool preview)
{
if (source is List<object> items)
{
var innerPropertyType = this.GetInnerPublishedPropertyType(propertyType);
var elements = new List<XElement>();
foreach (var item in items)
{
if (item != null)
{
var xpathValue = innerPropertyType.ConvertSourceToXPath(item, preview);
var element = new XElement("value", xpathValue);
elements.Add(element);
}
}
return new XElement("values", elements).CreateNavigator();
}
return base.ConvertSourceToXPath(propertyType, source, preview);
}
private PublishedPropertyType GetInnerPublishedPropertyType(PublishedPropertyType propertyType)
{
return ApplicationContext.Current.ApplicationCache.RuntimeCache.GetCacheItem<PublishedPropertyType>(
string.Format(ValueConverterKeys.CacheKeyFormat, propertyType.DataTypeId, propertyType.ContentType.Id),
() =>
{
var dataTypeService = ApplicationContext.Current.Services.DataTypeService;
var prevalues = dataTypeService.GetPreValuesCollectionByDataTypeId(propertyType.DataTypeId);
var dict = prevalues.PreValuesAsDictionary;
if (dict.ContainsKey(PreValueKeys.DataType))
{
var dtdPreValue = dict[PreValueKeys.DataType];
if (Guid.TryParse(dtdPreValue.Value, out Guid dtdGuid))
{
var dtd = dataTypeService.GetDataTypeDefinitionById(dtdGuid);
return new PublishedPropertyType(propertyType.ContentType, new PropertyType(dtd, propertyType.PropertyTypeAlias));
}
}
return null;
});
}
public Type GetPropertyValueType(PublishedPropertyType propertyType)
{
var innerPropertyType = GetInnerPublishedPropertyType(propertyType);
return innerPropertyType != null
? typeof(IEnumerable<>).MakeGenericType(innerPropertyType.ClrType)
: typeof(IEnumerable<object>);
}
public PropertyCacheLevel GetPropertyCacheLevel(PublishedPropertyType propertyType, PropertyCacheValue cacheValue)
{
return PropertyCacheLevel.Content;
}
internal static void ClearDataTypeCache(int dataTypeId)
{
ApplicationContext.Current.ApplicationCache.RuntimeCache.ClearCacheByKeySearch(
string.Format(ValueConverterKeys.CacheKeyFormat, dataTypeId, string.Empty));
}
}
}