Repository navigation
Expand file tree
/
Copy pathNodeMappingContext.cs
More file actions
142 lines (119 loc) · 3.83 KB
/
Copy pathNodeMappingContext.cs
File metadata and controls
142 lines (119 loc) · 3.83 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using umbraco.NodeFactory;
namespace uComponents.Mapping
{
/// <summary>
/// Provides a context for a stack of mapping operations. Lifetime should complete
/// once the bottom mapping operation is completed.
/// </summary>
public class NodeMappingContext
{
/// <summary>
/// Initializes a new instance of the <see cref="NodeMappingContext"/> class.
/// </summary>
/// <param name="id">The unique identifier.</param>
/// <param name="paths">The paths.</param>
/// <param name="parent">The parent.</param>
public NodeMappingContext(int id, string[] paths, NodeMappingContext parent)
{
Id = id;
Paths = paths;
ParentContext = parent;
_nodeCache = new List<Node>();
}
/// <summary>
/// Initializes a new instance of the <see cref="NodeMappingContext"/> class.
/// </summary>
/// <param name="node">The node.</param>
/// <param name="paths">The paths.</param>
/// <param name="parent">The parent.</param>
public NodeMappingContext(Node node, string[] paths, NodeMappingContext parent)
: this(node.Id, paths, parent)
{
_nodeCache.Add(node);
}
/// <summary>
/// The ID of the node being mapped.
/// </summary>
public int Id { get; set; }
/// <summary>
/// The paths to be mapped.
/// </summary>
public string[] Paths { get; set; }
/// <summary>
/// The context which spawned this context
/// </summary>
public NodeMappingContext ParentContext { get; set; }
/// <summary>
/// Gets the node being mapped.
/// </summary>
public Node GetNode()
{
var foundNode = GetNodeFromContextCache(Id);
if (foundNode == null)
{
foundNode = new Node(Id);
if (string.IsNullOrEmpty(foundNode.Name))
{
return null;
}
}
AddNodeToContextCache(foundNode);
return foundNode;
}
#region Context cache
/// <summary>
/// Stores the nodes which have been cached during this operation.
/// </summary>
private readonly List<Node> _nodeCache;
/// <summary>
/// Looks for a <c>Node</c> in the context tree.
/// </summary>
/// <param name="id">The ID of the node.</param>
/// <returns>The node, or null if not found.</returns>
public Node GetNodeFromContextCache(int id)
{
var currentContext = this;
while (currentContext.ParentContext != null)
{
var foundNode = currentContext._nodeCache.FirstOrDefault(n => n.Id == id);
if (foundNode != null)
{
return foundNode;
}
currentContext = currentContext.ParentContext;
}
return null;
}
/// <summary>
/// Adds a node to the context cache.
/// </summary>
public void AddNodeToContextCache(Node node)
{
if (node == null
|| string.IsNullOrEmpty(node.Name))
{
return;
}
_nodeCache.Add(node);
}
/// <summary>
/// Adds a collection of nodes to the context cache.
/// </summary>
public void AddNodesToContextCache(IEnumerable<Node> nodes)
{
if (nodes == null)
{
throw new ArgumentNullException("nodes");
}
foreach (var node in nodes)
{
AddNodeToContextCache(node);
}
}
#endregion
}
}