This repository was archived by the owner on Feb 26, 2022. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathNodeMapper.cs
More file actions
350 lines (311 loc) · 12.2 KB
/
Copy pathNodeMapper.cs
File metadata and controls
350 lines (311 loc) · 12.2 KB
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using umbraco.cms.businesslogic.web;
using uComponents.Mapping.Property;
namespace uComponents.Mapping
{
/// <summary>
/// Mapper which maps from Umbraco Node properties to strongly typed model properties
/// </summary>
public class NodeMapper
{
/// <summary>
/// Gets the engine.
/// </summary>
/// <value>
/// The engine.
/// </value>
public NodeMappingEngine Engine { get; private set; }
/// <summary>
/// Gets the type of the destination.
/// </summary>
/// <value>
/// The type of the destination.
/// </value>
public Type DestinationType { get; private set; }
/// <summary>
/// Gets the property mappers.
/// </summary>
/// <value>
/// The property mappers.
/// </value>
public List<PropertyMapperBase> PropertyMappers { get; private set; }
/// <summary>
/// Gets the type of the source document.
/// </summary>
/// <value>
/// The type of the source document.
/// </value>
public DocumentType SourceDocumentType { get; private set; }
/// <summary>
/// Initializes a new instance of the <see cref="NodeMapper"/> class.
/// </summary>
/// <param name="engine">The engine.</param>
/// <param name="destinationType">Type of the destination.</param>
/// <param name="sourceDocumentType">Type of the source document.</param>
/// <exception cref="System.ArgumentNullException">
/// sourceDocumentType
/// or
/// engine
/// or
/// destinationType
/// </exception>
public NodeMapper(NodeMappingEngine engine, Type destinationType, DocumentType sourceDocumentType)
{
if (sourceDocumentType == null)
{
throw new ArgumentNullException("sourceDocumentType");
}
else if (engine == null)
{
throw new ArgumentNullException("engine");
}
else if (destinationType == null)
{
throw new ArgumentNullException("destinationType");
}
SourceDocumentType = sourceDocumentType;
Engine = engine;
DestinationType = destinationType;
PropertyMappers = new List<PropertyMapperBase>();
// See if base properties have been mapped already.
var baseNodeMapper = Engine.GetBaseNodeMapperForType(destinationType);
if (baseNodeMapper != null)
{
// Use the property mappings of the closest parent
// (ideally they will already have the mappings of all their ancestors)
PropertyMappers.AddRange(baseNodeMapper.PropertyMappers);
}
// Map properties
foreach (var destinationProperty in destinationType.GetProperties())
{
if (PropertyMappers.Any(mapper => mapper.DestinationInfo.Name == destinationProperty.Name) || !destinationProperty.CanWrite)
{
// A mapping already exists for this property on a base type or its a ReadOnly property.
continue;
}
PropertyMapperBase propertyMapper = null;
var sourcePropertyAlias = GetPropertyAlias(destinationProperty);
// Check if this property is equivalent to a default Node property.
var defaultNodeProperty = DefaultPropertyMapper.GetDefaultMappingForName(destinationProperty);
if (sourcePropertyAlias != null // check corresponding source property alias was found
&& destinationProperty.PropertyType.GetMappedPropertyType() == MappedPropertyType.SystemOrEnum)
{
propertyMapper = new BasicPropertyMapper(
x => x, // direct mapping
destinationProperty.PropertyType, // using the desired type
this,
destinationProperty,
sourcePropertyAlias
);
}
else if (destinationProperty.PropertyType.GetMappedPropertyType() == MappedPropertyType.Model)
{
propertyMapper = new SinglePropertyMapper(
null,
null,
this,
destinationProperty,
sourcePropertyAlias // can be null to map ancestor
);
}
else if (destinationProperty.PropertyType.GetMappedPropertyType() == MappedPropertyType.Collection)
{
propertyMapper = new CollectionPropertyMapper(
null,
null,
this,
destinationProperty,
sourcePropertyAlias // can be null to map descendants
);
}
else if (defaultNodeProperty != null)
{
propertyMapper = new DefaultPropertyMapper(
this,
destinationProperty,
defaultNodeProperty,
null
);
}
if (propertyMapper != null)
{
PropertyMappers.Add(propertyMapper);
}
}
}
/// <summary>
/// Maps the node.
/// </summary>
/// <param name="context">The context.</param>
/// <returns></returns>
/// <exception cref="System.ArgumentNullException">context</exception>
/// <exception cref="uComponents.Mapping.InvalidPathException">
/// </exception>
public object MapNode(NodeMappingContext context)
{
if (context == null)
{
throw new ArgumentNullException("context");
}
object destination = Activator.CreateInstance(DestinationType);
PropertyInfo[] includedPaths = null;
// Check included paths are actually required
if (context.Paths != null)
{
includedPaths = GetImmediateProperties(DestinationType, context.Paths);
foreach (var path in includedPaths)
{
var propertyMapper = PropertyMappers.SingleOrDefault(x => x.DestinationInfo.Name == path.Name);
if (propertyMapper == null)
{
throw new InvalidPathException(
string.Format(
"The property '{0}' on '{1}' is not mapped - check your mappings.",
path.Name,
path.PropertyType.FullName
));
}
else if (!propertyMapper.RequiresInclude)
{
throw new InvalidPathException(
string.Format(
@"The property '{0}' on '{1}' does not
require an explicit include (do not include it as a path, it will be populated automatically).",
path.Name,
path.PropertyType.FullName
));
}
}
}
foreach (var propertyMapper in PropertyMappers)
{
if (context.Paths == null // include all paths
|| !propertyMapper.RequiresInclude // map all automatic paths
|| includedPaths.Any(x => x.Name == propertyMapper.DestinationInfo.Name)) // map explicit paths
{
var destinationValue = propertyMapper.MapProperty(context);
propertyMapper.DestinationInfo.SetValue(destination, destinationValue, null);
}
}
return destination;
}
/// <summary>
/// Gets the properties of <paramref name="type"/> which are defined
/// by <paramref name="paths"/>.
/// </summary>
private static PropertyInfo[] GetImmediateProperties(Type type, string[] paths)
{
if (type == null)
{
throw new ArgumentNullException("type");
}
else if (paths == null)
{
throw new ArgumentNullException("paths");
}
var allProperties = type.GetProperties();
var chosenProperties = new List<PropertyInfo>();
foreach (var path in paths)
{
if (string.IsNullOrEmpty(path))
{
throw new ArgumentException("No path can be empty or null.", "paths");
}
var segment = path.Split('.').First();
if (!chosenProperties.Any(p => p.Name == segment))
{
var property = allProperties.SingleOrDefault(p => p.Name == segment);
if (property != null)
{
chosenProperties.Add(property);
}
else
{
throw new InvalidPathException(type, path, segment);
}
}
}
return chosenProperties.ToArray();
}
/// <summary>
/// Finds the corresponding property alias for a model's property.
/// </summary>
/// <returns><c>null</c> if not found.</returns>
public string GetPropertyAlias(PropertyInfo destinationProperty)
{
return SourceDocumentType.PropertyTypes
.Select(prop => prop.Alias)
.Where(alias => string.Equals(alias, destinationProperty.Name, StringComparison.InvariantCultureIgnoreCase))
.SingleOrDefault();
}
/// <summary>
/// Inserts a property mapper,
/// </summary>
/// <param name="propertyMapper"></param>
public void InsertPropertyMapper(PropertyMapperBase propertyMapper)
{
if (propertyMapper == null)
{
throw new ArgumentNullException("propertyMapper");
}
RemovePropertyMapper(propertyMapper.DestinationInfo);
PropertyMappers.Add(propertyMapper);
if (Engine.CacheProvider != null)
{
Engine.CacheProvider.Clear();
}
}
/// <summary>
/// Removes the property mapper.
/// </summary>
/// <param name="destinationProperty">The destination property.</param>
/// <exception cref="System.ArgumentNullException">destinationProperty</exception>
public void RemovePropertyMapper(PropertyInfo destinationProperty)
{
if (destinationProperty == null)
{
throw new ArgumentNullException("destinationProperty");
}
var existingMapper = PropertyMappers
.SingleOrDefault(x => x.DestinationInfo.Name == destinationProperty.Name);
if (existingMapper != null)
{
PropertyMappers.Remove(existingMapper);
}
if (Engine.CacheProvider != null)
{
Engine.CacheProvider.Clear();
}
}
}
/// <summary>
/// Thrown when a path does not match up with the model graph.
/// </summary>
public class InvalidPathException : Exception
{
/// <summary>
/// Basic constructor for path exceptions
/// </summary>
public InvalidPathException(string message)
: base(message)
{
}
/// <param name="type">The type where the segment was expected to correspond
/// to a relationship.</param>
/// <param name="path">The remaining path.</param>
/// <param name="segment">The segment of the path which could to be matched up
/// to the type.</param>
public InvalidPathException(Type type, string path, string segment)
: base(string.Format(
@"The segment '{0}' of the remaining path '{1}' does not refer to a valid
relationship on '{2}'",
segment,
path,
type))
{
}
}
}