This repository was archived by the owner on Feb 26, 2022. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathNodeQuery.cs
More file actions
378 lines (308 loc) · 11.2 KB
/
Copy pathNodeQuery.cs
File metadata and controls
378 lines (308 loc) · 11.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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Expressions;
using umbraco.NodeFactory;
using umbraco;
using System.Collections;
using uComponents.Mapping.Property;
namespace uComponents.Mapping
{
/// <summary>
/// Represents a query for mapped Umbraco nodes. Enumerating the
/// query gets mapped instances of every node which can be mapped to
/// <typeparamref name="TDestination"/>.
/// </summary>
/// <typeparam name="TDestination">
/// The type which queried nodes will be mapped to.
/// </typeparam>
internal class NodeQuery<TDestination> : INodeQuery<TDestination>
where TDestination : class, new()
{
// Cache keys
private const string _explicitCacheFormat = "Explicit_{0}";
private const string _allCacheFormat = "All_{0}";
// The paths included in the query
private readonly List<string> _paths;
private readonly Dictionary<string, Func<object, bool>> _propertyFilters;
private bool _isExplicit = false;
// The engine which will execute the query
private readonly NodeMappingEngine _engine;
public NodeQuery(NodeMappingEngine engine)
{
var destinationType = typeof(TDestination);
if (engine == null)
{
throw new ArgumentNullException("engine");
}
else if (!engine.NodeMappers.ContainsKey(destinationType))
{
throw new MapNotFoundException(destinationType);
}
_engine = engine;
_paths = new List<string>();
_propertyFilters = new Dictionary<string, Func<object, bool>>();
}
public INodeMappingEngine Engine
{
get
{
return _engine;
}
}
#region Include
public INodeQuery<TDestination> Include(string path)
{
if (string.IsNullOrEmpty(path))
{
throw new ArgumentException("The path cannot be null or empty", "path");
}
if (!_paths.Contains(path))
{
_paths.Add(path);
}
return this;
}
public INodeQuery<TDestination> Include(Expression<Func<TDestination, object>> path)
{
if (path == null)
{
throw new ArgumentNullException("path");
}
string parsedPath;
if (!path.Body.TryParsePath(out parsedPath)
|| parsedPath == null)
{
throw new ArgumentException(
string.Format("Path could not be parsed (got this far: '{0}')", parsedPath),
"path"
);
}
return Include(parsedPath);
}
public INodeQuery<TDestination> IncludeMany(string[] paths)
{
if (paths == null)
{
throw new ArgumentNullException("paths");
}
foreach (var path in paths)
{
this.Include(path);
}
return this;
}
public INodeQuery<TDestination> IncludeMany(Expression<Func<TDestination, object>>[] paths)
{
if (paths == null)
{
throw new ArgumentNullException("paths");
}
foreach (var path in paths)
{
this.Include(path);
}
return this;
}
#endregion
#region Filtering
public INodeQuery<TDestination> Explicit()
{
_isExplicit = true;
return this;
}
public INodeQuery<TDestination> WhereProperty<TProperty>(
Expression<Func<TDestination, TProperty>> property,
Func<TProperty, bool> predicate
)
{
if (property == null)
{
throw new ArgumentNullException("property");
}
else if (predicate == null)
{
throw new ArgumentNullException("predicate");
}
var destinationType = typeof(TDestination);
var destinationInfo = property.GetPropertyInfo();
if (!_engine.NodeMappers[destinationType].PropertyMappers
.Any(x => x.DestinationInfo.Name == destinationInfo.Name))
{
throw new PropertyNotMappedException(destinationType, destinationInfo.Name);
}
_propertyFilters.Add(destinationInfo.Name, x => predicate((TProperty)x));
return this;
}
/// <summary>
/// Filters a collection of mapping contexts based on the predicates in <see cref="_propertyFilters"/>.
/// </summary>
/// <param name="contexts">The mapping contexts to filter</param>
/// <returns>The filtered subset of <paramref name="contexts"/>.</returns>
private IEnumerable<NodeMappingContext> FilterSet(IEnumerable<NodeMappingContext> contexts)
{
if (contexts == null)
{
throw new ArgumentNullException();
}
var filteredContexts = new List<NodeMappingContext>(contexts);
foreach (var filter in _propertyFilters)
{
var propertyMapper = _engine.NodeMappers[typeof(TDestination)]
.PropertyMappers
.Single(x => x.DestinationInfo.Name == filter.Key);
filteredContexts.RemoveAll(context =>
{
var property = propertyMapper.MapProperty(context);
return !filter.Value(property);
});
}
return filteredContexts;
}
#endregion
#region Execute
public TDestination Map(Node node)
{
if (node == null
|| string.IsNullOrEmpty(node.Name))
{
return null;
}
var context = new NodeMappingContext(node, _paths.ToArray(), null);
return (TDestination)_engine.Map(
context,
typeof(TDestination)
);
}
[Obsolete("Use Find() instead")]
public TDestination Single(int id)
{
return Find(id);
}
public TDestination Find(int id)
{
var context = new NodeMappingContext(id, _paths.ToArray(), null);
return (TDestination)_engine.Map(
context,
typeof(TDestination)
);
}
public TDestination Current()
{
return Map(Node.GetCurrent());
}
/// <summary>
/// Maps a collection of contexts to <typeparamref name="TDestination"/>.
/// </summary>
private IEnumerable<TDestination> Many(IEnumerable<NodeMappingContext> contexts)
{
if (contexts == null)
{
throw new ArgumentNullException();
}
var filteredContexts = FilterSet(contexts);
return filteredContexts.Select(c =>
{
return (TDestination)_engine.Map(c, typeof(TDestination));
});
}
public IEnumerable<TDestination> Many(IEnumerable<int> ids)
{
if (ids == null)
{
throw new ArgumentNullException();
}
var paths = _paths.ToArray();
var contexts = ids.Select(id => new NodeMappingContext(id, paths, null));
return Many(contexts);
}
public IEnumerable<TDestination> Many(IEnumerable<Node> nodes)
{
if (nodes == null)
{
throw new ArgumentNullException("nodes");
}
var paths = _paths.ToArray();
var contexts = nodes.Select(n => new NodeMappingContext(n, paths, null));
return Many(contexts);
}
public IEnumerable<TProperty> SelectProperty<TProperty>(
Expression<Func<TDestination, TProperty>> property
)
{
if (property == null)
{
throw new ArgumentNullException("property");
}
var sourceSet = EvaluateSourceSet();
var propertyInfo = property.GetPropertyInfo();
var propertyMapper = _engine.NodeMappers[typeof(TDestination)]
.PropertyMappers
.SingleOrDefault(x => x.DestinationInfo.Name == propertyInfo.Name);
if (propertyMapper == null)
{
throw new PropertyNotMappedException(typeof(TDestination), propertyInfo.Name);
}
return sourceSet.Select(c => (TProperty)propertyMapper.MapProperty(c));
}
#endregion
#region Enumeration
/// <summary>
/// Gets an enumerable representing the current source set of the query.
/// </summary>
private IEnumerable<NodeMappingContext> EvaluateSourceSet()
{
var destinationType = typeof(TDestination);
var paths = _paths.ToArray();
IEnumerable<NodeMappingContext> sourceSet;
if (!_engine.NodeMappers.ContainsKey(destinationType))
{
throw new MapNotFoundException(destinationType);
}
string cacheKey = string.Format(
_isExplicit ? _explicitCacheFormat : _allCacheFormat,
destinationType.FullName
);
if (_engine.CacheProvider != null
&& _engine.CacheProvider.ContainsKey(cacheKey))
{
var ids = _engine.CacheProvider.Get(cacheKey) as int[];
sourceSet = ids.Select(id => new NodeMappingContext(id, paths, null));
}
else
{
// Check whether to include derived maps
var sourceNodeTypeAliases = _isExplicit
? new[] { _engine.NodeMappers[destinationType].SourceDocumentType.Alias }
: _engine.GetCompatibleNodeTypeAliases(destinationType);
var nodes = sourceNodeTypeAliases.SelectMany(alias => uQuery.GetNodesByType(alias));
if (_engine.CacheProvider != null)
{
// Cache the node IDs
_engine.CacheProvider.Insert(cacheKey, nodes.Select(n => n.Id).ToArray());
}
sourceSet = nodes.Select(n => new NodeMappingContext(n, paths, null));
}
return FilterSet(sourceSet);
}
/// <summary>
/// Gets and enumerator of mapped instances of every node which
/// can be mapped to <typeparamref name="TDestination"/>.
/// </summary>
public IEnumerator<TDestination> GetEnumerator()
{
var sourceSet = EvaluateSourceSet();
var destinationSet = sourceSet.Select(c =>
{
return (TDestination)_engine.Map(c, typeof(TDestination));
});
return destinationSet.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
#endregion
}
}