using System; using System.Collections.Generic; using System.Linq; namespace umbraco.Linq.Core.Node { /// /// Represents a collection of TDocTypeBase retrieved from the umbraco XML cache which are direct children of a node /// /// The type of the doc type base. public sealed class NodeAssociationTree : AssociationTree where TDocTypeBase : DocTypeBase, new() { private object lockObject = new object(); private IEnumerable _nodes; internal NodeAssociationTree(IEnumerable nodes) { this._nodes = new List(); this._nodes = nodes; } /// /// Initializes a new instance of the class for a particular tree section /// /// The parent node id to start from. /// The NodeDataProvider to link the tree with. public NodeAssociationTree(int parentNodeId, NodeDataProvider provider) { this.Provider = provider; this.ParentNodeId = parentNodeId; } /// /// Gets the enumerator for this Tree collection /// /// public override IEnumerator GetEnumerator() { if (this._nodes == null) //first access, otherwise it'd be cached { LoadNodes(); } return this._nodes.GetEnumerator(); } private void LoadNodes() { var provider = this.Provider as NodeDataProvider; provider.CheckDisposed(); lock (lockObject) { var parents = provider .Xml .Descendants() .Where(x => x.Attribute("id") != null && (int)x.Attribute("id") == this.ParentNodeId); var rawNodes = parents .Single() .Elements() .Where(x => x.Attribute("isDoc") != null) ; this._nodes = provider.DynamicNodeCreation(rawNodes).Cast(); //drop is back to the type which was asked for } } /// /// Indicates that the NodeAssociationTree is ReadOnly /// /// /// true /// public override bool IsReadOnly { get { return true; } } /// /// Gets or sets the DataProvider associated with this Tree /// /// The provider. public override UmbracoDataProvider Provider { get; protected set; } /// /// Reloads the cache. /// public override void ReloadCache() { this.LoadNodes(); } public override void InsertOnSubmit(TDocTypeBase item) { throw new NotImplementedException("The NodeAssociationTree does not support Inserting items"); } public override void InsertAllOnSubmit(IEnumerable items) { throw new NotImplementedException("The NodeAssociationTree does not support Inserting items"); } public override void DeleteOnSubmit(TDocTypeBase itemm) { throw new NotImplementedException("The NodeAssociationTree does not support Deleting items"); } public override void DeleteAllOnSubmit(IEnumerable items) { throw new NotImplementedException("The NodeAssociationTree does not support Deleting items"); } } }