From d63ae05693d507e56aca0d39fb9391395bb36200 Mon Sep 17 00:00:00 2001 From: "agrath@gmail.com" Date: Sat, 11 Jun 2011 22:15:01 -0200 Subject: [PATCH] Got rid of DynamicMedia and DynamicMediaList implementations (still there but subclasses of DynamicNode & DynamicNodeList now) to enable navigation on Media Started implementing ExamineBackedMedia instead of using new Media() --- .../RazorDynamicNode/DynamicBackingItem.cs | 300 ++++++++++++++ .../DynamicBackingItemType.cs | 13 + .../RazorDynamicNode/DynamicBase.cs | 379 ++++++++++++++++++ .../RazorDynamicNode/DynamicListBase.cs | 19 + .../RazorDynamicNode/DynamicMedia.cs | 129 +----- .../RazorDynamicNode/DynamicMediaList.cs | 38 +- .../RazorDynamicNode/DynamicNode.cs | 297 +++----------- .../RazorDynamicNode/DynamicNodeList.cs | 9 +- .../RazorDynamicNode/ExamineBackedMedia.cs | 194 +++++++++ .../RazorDynamicNode/PropertyResult.cs | 49 +++ .../umbraco.MacroEngines.csproj | 23 +- 11 files changed, 1050 insertions(+), 400 deletions(-) create mode 100644 umbraco.MacroEngines.Juno/RazorDynamicNode/DynamicBackingItem.cs create mode 100644 umbraco.MacroEngines.Juno/RazorDynamicNode/DynamicBackingItemType.cs create mode 100644 umbraco.MacroEngines.Juno/RazorDynamicNode/DynamicListBase.cs create mode 100644 umbraco.MacroEngines.Juno/RazorDynamicNode/ExamineBackedMedia.cs create mode 100644 umbraco.MacroEngines.Juno/RazorDynamicNode/PropertyResult.cs diff --git a/umbraco.MacroEngines.Juno/RazorDynamicNode/DynamicBackingItem.cs b/umbraco.MacroEngines.Juno/RazorDynamicNode/DynamicBackingItem.cs new file mode 100644 index 0000000000..eb133cdbf9 --- /dev/null +++ b/umbraco.MacroEngines.Juno/RazorDynamicNode/DynamicBackingItem.cs @@ -0,0 +1,300 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using umbraco.interfaces; +using umbraco.cms.businesslogic.media; +using umbraco.cms.businesslogic; +using umbraco.cms.businesslogic.property; +using umbraco.presentation.nodeFactory; +using System.Data; + +namespace umbraco.MacroEngines +{ + public class DynamicBackingItem + { + internal INode content; + internal ExamineBackedMedia media; + public DynamicBackingItemType Type; + + public DynamicBackingItem(INode iNode) + { + this.content = iNode; + this.Type = DynamicBackingItemType.Content; + } + public DynamicBackingItem(ExamineBackedMedia media) + { + this.media = media; + this.Type = DynamicBackingItemType.Media; + } + public DynamicBackingItem(int Id) + { + NodeFactory.Node baseNode = new NodeFactory.Node(Id); + //todo: trace this with media + if (baseNode == null) + { + this.media = ExamineBackedMedia.GetUmbracoMedia(Id); + this.Type = DynamicBackingItemType.Media; + } + else + { + this.content = baseNode; + this.Type = DynamicBackingItemType.Content; + } + } + + public DynamicBackingItem(CMSNode node) + { + this.content = (INode)node; + this.Type = DynamicBackingItemType.Content; + } + + public bool IsNull() + { + return ((Type == DynamicBackingItemType.Content && content == null) || media == null); + } + public List ChildrenAsList + { + get + { + if (IsNull()) return null; + if (Type == DynamicBackingItemType.Content) + { + var children = content.ChildrenAsList; + if (children != null) + { + return children.ConvertAll(c => new DynamicBackingItem(c)); + } + } + else + { + var children = media.ChildrenAsList; + if (children != null) + { + return children.ToList().ConvertAll(m => new DynamicBackingItem(m)); + } + } + return new List(); + } + } + + public IProperty GetProperty(string alias) + { + if (IsNull()) return null; + return Type == DynamicBackingItemType.Content ? new PropertyResult(content.GetProperty(alias)) : new PropertyResult(media.GetProperty(alias)); + } + public IProperty GetProperty(string alias, out bool propertyExists) + { + if (IsNull()) + { + propertyExists = false; + return null; + } + if (Type == DynamicBackingItemType.Content) + { + return content.GetProperty(alias, out propertyExists); + } + else + { + return media.GetProperty(alias, out propertyExists); + } + } + + public IProperty GetProperty(string alias, bool recursive) + { + if (!recursive) return GetProperty(alias); + if (IsNull()) return null; + DynamicBackingItem context = this; + IProperty prop = this.GetProperty(alias); + while (prop == null) + { + context = context.Parent; + prop = context.GetProperty(alias); + if (context == null) break; + } + if (prop != null) + { + return prop; + } + return null; + } + public string GetPropertyValue(string alias) + { + var prop = GetProperty(alias); + if (prop != null) return prop.Value; + return null; + } + public string GetPropertyValue(string alias, bool recursive) + { + var prop = GetProperty(alias, recursive); + if (prop != null) return prop.Value; + return null; + } + public List PropertiesAsList + { + get + { + if (IsNull()) return null; + if (Type == DynamicBackingItemType.Content) + { + return content.PropertiesAsList; + } + else + { + return media.PropertiesAsList; + } + } + } + public DataTable ChildrenAsTable() + { + if (IsNull()) return null; + if (Type == DynamicBackingItemType.Content) + { + return content.ChildrenAsTable(); + } + else + { + //sorry + return null; + } + + } + public DataTable ChildrenAsTable(string nodeTypeAlias) + { + if (IsNull()) return null; + if (Type == DynamicBackingItemType.Content) + { + return content.ChildrenAsTable(nodeTypeAlias); + } + else + { + //sorry + return null; + } + + } + public int Level + { + get { if (IsNull()) return 0; return Type == DynamicBackingItemType.Content ? content.Level : media.Level; } + } + + + public int Id + { + get { if (IsNull()) return 0; return Type == DynamicBackingItemType.Content ? content.Id : media.Id; } + } + + public string NodeTypeAlias + { + get { if (IsNull()) return null; return Type == DynamicBackingItemType.Content ? content.NodeTypeAlias : media.NodeTypeAlias; } + } + + public DynamicBackingItem Parent + { + get + { + if (IsNull()) return null; + return Type == DynamicBackingItemType.Content ? + new DynamicBackingItem(content.Parent) : + new DynamicBackingItem(media.Parent); + } + } + public DateTime CreateDate + { + get { if (IsNull()) return DateTime.MinValue; return Type == DynamicBackingItemType.Content ? content.CreateDate : media.CreateDate; } + } + public DateTime UpdateDate + { + get { if (IsNull()) return DateTime.MinValue; return Type == DynamicBackingItemType.Content ? content.UpdateDate : media.UpdateDate; } + } + + public string WriterName + { + get { if (IsNull()) return null; return Type == DynamicBackingItemType.Content ? content.WriterName : null; } + } + + public string Name + { + get { if (IsNull()) return null; return Type == DynamicBackingItemType.Content ? content.Name : media.Name; } + } + public Guid Version + { + get { if (IsNull()) return Guid.Empty; return Type == DynamicBackingItemType.Content ? content.Version : media.Version; } + } + + public string Url + { + get { if (IsNull()) return null; return Type == DynamicBackingItemType.Content ? content.Url : null; } + } + + public string NiceUrl + { + get { if (IsNull()) return null; return Type == DynamicBackingItemType.Content ? content.NiceUrl : null; } + } + + public string UrlName + { + get { if (IsNull()) return null; return Type == DynamicBackingItemType.Content ? content.UrlName : null; } + } + + public int template + { + get { if (IsNull()) return 0; return Type == DynamicBackingItemType.Content ? content.template : 0; } + } + + public int SortOrder + { + get { if (IsNull()) return 0; return Type == DynamicBackingItemType.Content ? content.SortOrder : media.SortOrder; } + } + + + public string CreatorName + { + get { if (IsNull()) return null; return Type == DynamicBackingItemType.Content ? content.CreatorName : media.CreatorName; } + } + + public int WriterID + { + get { if (IsNull()) return 0; return Type == DynamicBackingItemType.Content ? content.WriterID : 0; } + } + + public int CreatorID + { + get { if (IsNull()) return 0; return Type == DynamicBackingItemType.Content ? content.CreatorID : media.CreatorID; } + } + + public string Path + { + get { if (IsNull()) return null; return Type == DynamicBackingItemType.Content ? content.Path : media.Path; } + } + + public List GetChildrenAsList + { + get + { + if (Type == DynamicBackingItemType.Content) + { + List children = content.ChildrenAsList; + //testing + if (children.Count == 0 && content.Id == 0) + { + return new List(new DynamicBackingItem[] { this }); + } + return children.ConvertAll(n => new DynamicBackingItem(n)); + } + else + { + List children = media.ChildrenAsList; + //testing + if (children.Count == 0 && content.Id == 0) + { + return new List(new DynamicBackingItem[] { this }); + } + return children.ConvertAll(n => new DynamicBackingItem(n)); + } + } + } + + + } +} \ No newline at end of file diff --git a/umbraco.MacroEngines.Juno/RazorDynamicNode/DynamicBackingItemType.cs b/umbraco.MacroEngines.Juno/RazorDynamicNode/DynamicBackingItemType.cs new file mode 100644 index 0000000000..1ae11031c0 --- /dev/null +++ b/umbraco.MacroEngines.Juno/RazorDynamicNode/DynamicBackingItemType.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace umbraco.MacroEngines +{ + public enum DynamicBackingItemType + { + Content, + Media + } +} diff --git a/umbraco.MacroEngines.Juno/RazorDynamicNode/DynamicBase.cs b/umbraco.MacroEngines.Juno/RazorDynamicNode/DynamicBase.cs index d00a54af60..83fc4e6b9b 100644 --- a/umbraco.MacroEngines.Juno/RazorDynamicNode/DynamicBase.cs +++ b/umbraco.MacroEngines.Juno/RazorDynamicNode/DynamicBase.cs @@ -3,10 +3,389 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Dynamic; +using umbraco.interfaces; namespace umbraco.MacroEngines { public class DynamicBase : DynamicObject { + internal DynamicListBase ownerList; + internal DynamicBackingItem backingItem; + + public int Level + { + get { if (backingItem == null) return 0; return backingItem.Level; } + } + + public int Position() + { + return this.Index(); + } + + public int Id + { + get { if (backingItem == null) return 0; return backingItem.Id; } + } + + public int Index() + { + if (this.ownerList == null && this.Parent != null) + { + var list = this.Parent.ChildrenAsList.ConvertAll(n => new DynamicBase(n)); + this.ownerList = new DynamicListBase(list); + } + if (this.ownerList != null) + { + List container = this.ownerList.Items.ToList(); + int currentIndex = container.FindIndex(n => backingItem.Id == this.Id); + if (currentIndex != -1) + { + return currentIndex; + } + else + { + throw new IndexOutOfRangeException(string.Format("Node {0} belongs to a DynamicListBase but could not retrieve the index for it's position in the list", this.Id)); + } + } + else + { + throw new ArgumentNullException(string.Format("Node {0} has been orphaned and doesn't belong to a DynamicListBase", this.Id)); + } + } + public bool IsFirst() + { + return IsHelper(n => this.Index() == 0); + } + public string IsFirst(string valueIfTrue) + { + return IsHelper(n => this.Index() == 0, valueIfTrue); + } + public string IsFirst(string valueIfTrue, string valueIfFalse) + { + return IsHelper(n => this.Index() == 0, valueIfTrue, valueIfFalse); + } + public bool IsLast() + { + if (this.ownerList == null) + { + return false; + } + int count = this.ownerList.Items.Count; + return IsHelper(n => this.Index() == count - 1); + } + public string IsLast(string valueIfTrue) + { + if (this.ownerList == null) + { + return string.Empty; + } + int count = this.ownerList.Items.Count; + return IsHelper(n => this.Index() == count - 1, valueIfTrue); + } + public string IsLast(string valueIfTrue, string valueIfFalse) + { + if (this.ownerList == null) + { + return valueIfFalse; + } + int count = this.ownerList.Items.Count; + return IsHelper(n => this.Index() == count - 1, valueIfTrue, valueIfFalse); + } + public bool IsEven() + { + return IsHelper(n => this.Index() % 2 == 0); + } + public string IsEven(string valueIfTrue) + { + return IsHelper(n => this.Index() % 2 == 0, valueIfTrue); + } + public string IsEven(string valueIfTrue, string valueIfFalse) + { + return IsHelper(n => this.Index() % 2 == 0, valueIfTrue, valueIfFalse); + } + public bool IsOdd() + { + return IsHelper(n => this.Index() % 2 == 1); + } + public string IsOdd(string valueIfTrue) + { + return IsHelper(n => this.Index() % 2 == 1, valueIfTrue); + } + public string IsOdd(string valueIfTrue, string valueIfFalse) + { + return IsHelper(n => this.Index() % 2 == 1, valueIfTrue, valueIfFalse); + } + public bool IsEqual(DynamicBase other) + { + return IsHelper(n => backingItem.Id == other.Id); + } + public string IsEqual(DynamicBase other, string valueIfTrue) + { + return IsHelper(n => backingItem.Id == other.Id, valueIfTrue); + } + public string IsEqual(DynamicBase other, string valueIfTrue, string valueIfFalse) + { + return IsHelper(n => backingItem.Id == other.Id, valueIfTrue, valueIfFalse); + } + public bool IsDescendant(DynamicBase other) + { + var ancestors = this.Ancestors(); + return IsHelper(n => ancestors.Items.Find(ancestor => ancestor.Id == other.Id) != null); + } + public string IsDescendant(DynamicBase other, string valueIfTrue) + { + var ancestors = this.Ancestors(); + return IsHelper(n => ancestors.Items.Find(ancestor => ancestor.Id == other.Id) != null, valueIfTrue); + } + public string IsDescendant(DynamicBase other, string valueIfTrue, string valueIfFalse) + { + var ancestors = this.Ancestors(); + return IsHelper(n => ancestors.Items.Find(ancestor => ancestor.Id == other.Id) != null, valueIfTrue, valueIfFalse); + } + public bool IsDescendantOrSelf(DynamicBase other) + { + var ancestors = this.AncestorsOrSelf(); + return IsHelper(n => ancestors.Items.Find(ancestor => ancestor.Id == other.Id) != null); + } + public string IsDescendantOrSelf(DynamicBase other, string valueIfTrue) + { + var ancestors = this.AncestorsOrSelf(); + return IsHelper(n => ancestors.Items.Find(ancestor => ancestor.Id == other.Id) != null, valueIfTrue); + } + public string IsDescendantOrSelf(DynamicBase other, string valueIfTrue, string valueIfFalse) + { + var ancestors = this.AncestorsOrSelf(); + return IsHelper(n => ancestors.Items.Find(ancestor => ancestor.Id == other.Id) != null, valueIfTrue, valueIfFalse); + } + public bool IsAncestor(DynamicBase other) + { + var descendants = this.Descendants(); + return IsHelper(n => descendants.Items.Find(descendant => descendant.Id == other.Id) != null); + } + public string IsAncestor(DynamicBase other, string valueIfTrue) + { + var descendants = this.Descendants(); + return IsHelper(n => descendants.Items.Find(descendant => descendant.Id == other.Id) != null, valueIfTrue); + } + public string IsAncestor(DynamicBase other, string valueIfTrue, string valueIfFalse) + { + var descendants = this.Descendants(); + return IsHelper(n => descendants.Items.Find(descendant => descendant.Id == other.Id) != null, valueIfTrue, valueIfFalse); + } + public bool IsAncestorOrSelf(DynamicBase other) + { + var descendants = this.DescendantsOrSelf(); + return IsHelper(n => descendants.Items.Find(descendant => descendant.Id == other.Id) != null); + } + public string IsAncestorOrSelf(DynamicBase other, string valueIfTrue) + { + var descendants = this.DescendantsOrSelf(); + return IsHelper(n => descendants.Items.Find(descendant => descendant.Id == other.Id) != null, valueIfTrue); + } + public string IsAncestorOrSelf(DynamicBase other, string valueIfTrue, string valueIfFalse) + { + var descendants = this.DescendantsOrSelf(); + return IsHelper(n => descendants.Items.Find(descendant => descendant.Id == other.Id) != null, valueIfTrue, valueIfFalse); + } + public bool IsHelper(Func test) + { + return test(this); + } + public string IsHelper(Func test, string valueIfTrue) + { + return IsHelper(test, valueIfTrue, string.Empty); + } + public string IsHelper(Func test, string valueIfTrue, string valueIfFalse) + { + return test(this) ? valueIfTrue : valueIfFalse; + } + + public DynamicBase AncestorOrSelf() + { + return AncestorOrSelf(node => node.Level == 1); + } + public DynamicBase AncestorOrSelf(int level) + { + return AncestorOrSelf(node => node.Level == level); + } + public DynamicBase AncestorOrSelf(string nodeTypeAlias) + { + return AncestorOrSelf(node => node.NodeTypeAlias == nodeTypeAlias); + } + public DynamicBase AncestorOrSelf(Func func) + { + var node = this; + while (node != null) + { + if (func(node)) return node; + DynamicBase parent = node.Parent; + if (parent != null) + { + if (this != parent) + { + node = parent; + } + else + { + return node; + } + } + else + { + return null; + } + } + return node; + } + public DynamicListBase AncestorsOrSelf(Func func) + { + List ancestorList = new List(); + var node = this; + ancestorList.Add(node); + while (node != null) + { + if (node.Level == 1) break; + DynamicBase parent = node.Parent; + if (parent != null) + { + if (this != parent) + { + node = parent; + if (func(node)) + { + ancestorList.Add(node); + } + } + else + { + break; + } + } + else + { + break; + } + } + ancestorList.Reverse(); + return new DynamicListBase(ancestorList); + } + public DynamicListBase AncestorsOrSelf() + { + return AncestorsOrSelf(n => true); + } + public DynamicListBase AncestorsOrSelf(string nodeTypeAlias) + { + return AncestorsOrSelf(n => backingItem.NodeTypeAlias == nodeTypeAlias); + } + public DynamicListBase AncestorsOrSelf(int level) + { + return AncestorsOrSelf(n => backingItem.Level <= level); + } + public DynamicListBase Descendants(string nodeTypeAlias) + { + return Descendants(p => p.NodeTypeAlias == nodeTypeAlias); + } + public DynamicListBase Descendants(int level) + { + return Descendants(p => p.Level >= level); + } + public DynamicListBase Descendants() + { + return Descendants(n => true); + } + public DynamicListBase Descendants(Func func) + { + var flattenedNodes = this.backingItem.ChildrenAsList.Map(func, (INode n) => { return backingItem.ChildrenAsList; }); + return new DynamicListBase(flattenedNodes.ToList().ConvertAll(iNode => new DynamicBase(iNode))); + } + public DynamicListBase DescendantsOrSelf(int level) + { + return DescendantsOrSelf(p => p.Level >= level); + } + public DynamicListBase DescendantsOrSelf(string nodeTypeAlias) + { + return DescendantsOrSelf(p => p.NodeTypeAlias == nodeTypeAlias); + } + public DynamicListBase DescendantsOrSelf() + { + return DescendantsOrSelf(p => true); + } + public DynamicListBase DescendantsOrSelf(Func func) + { + if (this.n != null) + { + var thisNode = new List(); + if (func(this.n)) + { + thisNode.Add(this.n); + } + var flattenedNodes = this.backingItem.ChildrenAsList.Map(func, (INode n) => { return backingItem.ChildrenAsList; }); + return new DynamicListBase(thisNode.Concat(flattenedNodes).ToList().ConvertAll(iNode => new DynamicBase(iNode))); + } + return new DynamicListBase(new List()); + } + public DynamicListBase Ancestors(int level) + { + return Ancestors(n => backingItem.Level <= level); + } + public DynamicListBase Ancestors(string nodeTypeAlias) + { + return Ancestors(n => backingItem.NodeTypeAlias == nodeTypeAlias); + } + public DynamicListBase Ancestors() + { + return Ancestors(n => true); + } + public DynamicListBase Ancestors(Func func) + { + List ancestorList = new List(); + var node = this; + while (node != null) + { + if (node.Level == 1) break; + DynamicBase parent = node.Parent; + if (parent != null) + { + if (this != parent) + { + node = parent; + if (func(node)) + { + ancestorList.Add(node); + } + } + else + { + break; + } + } + else + { + break; + } + } + ancestorList.Reverse(); + return new DynamicListBase(ancestorList); + } + public DynamicBase Parent + { + get + { + if (n == null) + { + return null; + } + if (backingItem.Parent != null) + { + return new DynamicBase(backingItem.Parent); + } + if (n != null && backingItem.Id == 0) + { + return this; + } + return null; + } + } + } } diff --git a/umbraco.MacroEngines.Juno/RazorDynamicNode/DynamicListBase.cs b/umbraco.MacroEngines.Juno/RazorDynamicNode/DynamicListBase.cs new file mode 100644 index 0000000000..d12439fc36 --- /dev/null +++ b/umbraco.MacroEngines.Juno/RazorDynamicNode/DynamicListBase.cs @@ -0,0 +1,19 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Collections; +using System.Dynamic; + +namespace umbraco.MacroEngines +{ + public class DynamicListBase : DynamicObject, IEnumerable + { + public List Items { get; set; } + + public IEnumerator GetEnumerator() + { + return Items.GetEnumerator(); + } + } +} diff --git a/umbraco.MacroEngines.Juno/RazorDynamicNode/DynamicMedia.cs b/umbraco.MacroEngines.Juno/RazorDynamicNode/DynamicMedia.cs index 6efee66421..67f007ee48 100644 --- a/umbraco.MacroEngines.Juno/RazorDynamicNode/DynamicMedia.cs +++ b/umbraco.MacroEngines.Juno/RazorDynamicNode/DynamicMedia.cs @@ -2,136 +2,11 @@ using System.Collections.Generic; using System.Linq; using System.Text; -using System.Dynamic; -using umbraco.cms.businesslogic.media; -using umbraco.cms.businesslogic.property; namespace umbraco.MacroEngines { - public class DynamicMedia : DynamicBase + //for backwards compatibility only + public class DynamicMedia : DynamicNode { - private Dictionary _propertyCache; - private Media _media; - internal DynamicMediaList ownerList; - - public DynamicMedia(int mediaId) - { - _media = new Media(mediaId); - _propertyCache = new Dictionary(); - } - public DynamicMedia(Media media) - { - _media = media; - _propertyCache = new Dictionary(); - } - public DynamicMedia(string mediaId) - { - int iMediaId = 0; - if (int.TryParse(mediaId, out iMediaId)) - { - _media = new Media(iMediaId); - _propertyCache = new Dictionary(); - } - } - public DynamicMedia(object mediaId) - { - int iMediaId = 0; - if (int.TryParse(string.Format("{0}", mediaId), out iMediaId)) - { - _media = new Media(iMediaId); - _propertyCache = new Dictionary(); - } - } - public DynamicMedia() - { - - } - - private DynamicMediaList _children; - public DynamicMediaList Children - { - get - { - if (_children == null) - { - List nodes = new List(); - foreach (var mSub in _media.Children) - nodes.Add(new DynamicMedia(mSub)); - _children = new DynamicMediaList(nodes); - - } - - return _children; - } - } - - - public override bool TryGetMember(GetMemberBinder binder, out object result) - { - string name = binder.Name; - if (_propertyCache != null && _propertyCache.ContainsKey(name)) - { - result = _propertyCache[name]; - return true; - } - if (_media != null) - { - if (binder.Name.ToLower() == "name") - { - result = _media.Text; - return true; - } - - Property prop = _media.getProperty(name); - // check for nicer support of Pascal Casing EVEN if alias is camelCasing: - if (prop == null && name.Substring(0, 1).ToUpper() == name.Substring(0, 1)) - { - prop = _media.getProperty(name.Substring(0, 1).ToLower() + name.Substring((1))); - } - if (prop != null) - { - result = prop.Value; - if (_propertyCache != null) - { - _propertyCache.Add(name, string.Format("{0}", prop.Value)); - } - return true; - } - - // failback to default properties - try - { - result = _media.GetType().InvokeMember(binder.Name, - System.Reflection.BindingFlags.GetProperty | - System.Reflection.BindingFlags.Instance | - System.Reflection.BindingFlags.Public | - System.Reflection.BindingFlags.NonPublic, - null, - _media, - null); - return true; - } - catch - { - } - - - //return false because we have a media item now but the property doesn't exist - result = null; - return false; - } - result = null; - //return true because the _media is likely null, meaning we're in test mode - return true; - } - - public bool IsNull() - { - return false; - } - public bool HasValue() - { - return true; - } } } diff --git a/umbraco.MacroEngines.Juno/RazorDynamicNode/DynamicMediaList.cs b/umbraco.MacroEngines.Juno/RazorDynamicNode/DynamicMediaList.cs index d5e086a7fb..45de3f983a 100644 --- a/umbraco.MacroEngines.Juno/RazorDynamicNode/DynamicMediaList.cs +++ b/umbraco.MacroEngines.Juno/RazorDynamicNode/DynamicMediaList.cs @@ -2,45 +2,11 @@ using System.Collections.Generic; using System.Linq; using System.Text; -using System.Dynamic; -using System.Collections; -using umbraco.cms.businesslogic.media; namespace umbraco.MacroEngines { - public class DynamicMediaList : DynamicObject, IEnumerable + //for backwards compatibility only + public class DynamicMediaList : DynamicNodeList { - public IEnumerable Items { get; set; } - - public DynamicMediaList() - { - Items = new List(); - } - public DynamicMediaList(IEnumerable items) - { - List list = items.ToList(); - list.ForEach(node => node.ownerList = this); - Items = list; - } - public DynamicMediaList(IEnumerable items) - { - List list = items.Select(x => new DynamicMedia(x)).ToList(); - list.ForEach(node => node.ownerList = this); - Items = list; - } - - public IEnumerator GetEnumerator() - { - return Items.GetEnumerator(); - } - - public bool IsNull() - { - return false; - } - public bool HasValue() - { - return true; - } } } diff --git a/umbraco.MacroEngines.Juno/RazorDynamicNode/DynamicNode.cs b/umbraco.MacroEngines.Juno/RazorDynamicNode/DynamicNode.cs index f7c59164e5..9b082ea80c 100644 --- a/umbraco.MacroEngines.Juno/RazorDynamicNode/DynamicNode.cs +++ b/umbraco.MacroEngines.Juno/RazorDynamicNode/DynamicNode.cs @@ -14,12 +14,13 @@ using umbraco.DataLayer; using umbraco.cms.businesslogic; using System.Xml; using System.Xml.Linq; +using umbraco.cms.businesslogic.media; namespace umbraco.MacroEngines { - public class DynamicNode : DynamicBase + public class DynamicNode : DynamicObject { #region consts // these are private readonlys as const can't be Guids @@ -28,10 +29,11 @@ namespace umbraco.MacroEngines private readonly Guid DATATYPE_UCOMPONENTS_MNTP_GUID = new Guid("c2d6894b-e788-4425-bcf2-308568e3d38b"); #endregion - internal DynamicNodeList ownerList; + internal readonly DynamicBackingItem n; - internal readonly INode n; - public DynamicNode(INode n) + public DynamicNodeList ownerList; + + public DynamicNode(DynamicBackingItem n) { if (n != null) this.n = n; @@ -40,22 +42,22 @@ namespace umbraco.MacroEngines } public DynamicNode(int NodeId) { - this.n = new NodeFactory.Node(NodeId); + this.n = new DynamicBackingItem(NodeId); } public DynamicNode(string NodeId) { - int iNodeId = 0; - if (int.TryParse(NodeId, out iNodeId)) + int DynamicBackingItemId = 0; + if (int.TryParse(NodeId, out DynamicBackingItemId)) { - this.n = new NodeFactory.Node(iNodeId); + this.n = new DynamicBackingItem(DynamicBackingItemId); } } public DynamicNode(object NodeId) { - int iNodeId = 0; - if (int.TryParse(string.Format("{0}", NodeId), out iNodeId)) + int DynamicBackingItemId = 0; + if (int.TryParse(string.Format("{0}", NodeId), out DynamicBackingItemId)) { - this.n = new NodeFactory.Node(iNodeId); + this.n = new DynamicBackingItem(DynamicBackingItemId); } } public DynamicNode() @@ -124,11 +126,11 @@ namespace umbraco.MacroEngines { get { - List children = n.ChildrenAsList; + List children = n.ChildrenAsList; //testing if (children.Count == 0 && n.Id == 0) { - return new DynamicNodeList(new List { this.n }); + return new DynamicNodeList(new List { this.n }); } return new DynamicNodeList(n.ChildrenAsList); } @@ -309,22 +311,22 @@ namespace umbraco.MacroEngines } if (result != null) { - if (result is IEnumerable) + if (result is IEnumerable) { - result = new DynamicNodeList((IEnumerable)result); + result = new DynamicNodeList((IEnumerable)result); } if (result is IEnumerable) { result = new DynamicNodeList((IEnumerable)result); } - if (result is INode) + if (result is DynamicBackingItem) { - result = new DynamicNode((INode)result); + result = new DynamicNode((DynamicBackingItem)result); } } return result; } - private List GetAncestorOrSelfNodeTypeAlias(INode node) + private List GetAncestorOrSelfNodeTypeAlias(DynamicBackingItem node) { List list = new List(); if (node != null) @@ -542,7 +544,7 @@ namespace umbraco.MacroEngines return true; } - public DynamicMedia Media(string propertyAlias) + public DynamicNode Media(string propertyAlias) { if (n != null) { @@ -552,7 +554,7 @@ namespace umbraco.MacroEngines int mediaNodeId; if (int.TryParse(prop.Value, out mediaNodeId)) { - return new DynamicMedia(mediaNodeId); + return MediaById(mediaNodeId); } } return null; @@ -750,10 +752,10 @@ namespace umbraco.MacroEngines { return Descendants(n => true); } - public DynamicNodeList Descendants(Func func) + public DynamicNodeList Descendants(Func func) { - var flattenedNodes = this.n.ChildrenAsList.Map(func, (INode n) => { return n.ChildrenAsList; }); - return new DynamicNodeList(flattenedNodes.ToList().ConvertAll(iNode => new DynamicNode(iNode))); + var flattenedNodes = this.n.ChildrenAsList.Map(func, (DynamicBackingItem n) => { return n.ChildrenAsList; }); + return new DynamicNodeList(flattenedNodes.ToList().ConvertAll(DynamicBackingItem => new DynamicNode(DynamicBackingItem))); } public DynamicNodeList DescendantsOrSelf(int level) { @@ -767,19 +769,19 @@ namespace umbraco.MacroEngines { return DescendantsOrSelf(p => true); } - public DynamicNodeList DescendantsOrSelf(Func func) + public DynamicNodeList DescendantsOrSelf(Func func) { if (this.n != null) { - var thisNode = new List(); + var thisNode = new List(); if (func(this.n)) { thisNode.Add(this.n); } - var flattenedNodes = this.n.ChildrenAsList.Map(func, (INode n) => { return n.ChildrenAsList; }); - return new DynamicNodeList(thisNode.Concat(flattenedNodes).ToList().ConvertAll(iNode => new DynamicNode(iNode))); + var flattenedNodes = this.n.ChildrenAsList.Map(func, (DynamicBackingItem n) => { return n.ChildrenAsList; }); + return new DynamicNodeList(thisNode.Concat(flattenedNodes).ToList().ConvertAll(DynamicBackingItem => new DynamicNode(DynamicBackingItem))); } - return new DynamicNodeList(new List()); + return new DynamicNodeList(new List()); } public DynamicNodeList Ancestors(int level) { @@ -843,6 +845,7 @@ namespace umbraco.MacroEngines return null; } } + public DynamicNode NodeById(int Id) { return new DynamicNode(Id); @@ -873,40 +876,46 @@ namespace umbraco.MacroEngines { return NodeById(Ids.ToList()); } - public DynamicMedia MediaById(int Id) + public DynamicNode MediaById(int Id) { - return new DynamicMedia(Id); + return new DynamicNode(new DynamicBackingItem(new Media(Id))); } - public DynamicMedia MediaById(string Id) + public DynamicNode MediaById(string Id) { - return new DynamicMedia(Id); + int mediaId = 0; + if (int.TryParse(Id, out mediaId)) + { + return MediaById(mediaId); + } + return null; } - public DynamicMedia MediaById(object Id) + public DynamicNode MediaById(object Id) { - return new DynamicMedia(Id); + int mediaId = 0; + if (int.TryParse(string.Format("{0}", Id), out mediaId)) + { + return MediaById(mediaId); + } + return null; } - public DynamicMediaList MediaById(List Ids) + public DynamicNodeList MediaById(List Ids) { - List nodes = new List(); + List nodes = new List(); foreach (object eachId in Ids) - nodes.Add(new DynamicMedia(eachId)); - return new DynamicMediaList(nodes); + nodes.Add(MediaById(eachId)); + return new DynamicNodeList(nodes); } - public DynamicMediaList MediaById(List Ids) + public DynamicNodeList MediaById(List Ids) { - List nodes = new List(); + List nodes = new List(); foreach (int eachId in Ids) - nodes.Add(new DynamicMedia(eachId)); - return new DynamicMediaList(nodes); + nodes.Add(MediaById(eachId)); + return new DynamicNodeList(nodes); } - public DynamicMediaList MediaById(params object[] Ids) + public DynamicNodeList MediaById(params object[] Ids) { return MediaById(Ids.ToList()); } - public int Id - { - get { if (n == null) return 0; return n.Id; } - } public int template { @@ -979,6 +988,10 @@ namespace umbraco.MacroEngines { get { if (n == null) return DateTime.MinValue; return n.CreateDate; } } + public int Id + { + get { if (n == null) return 0; return n.Id; } + } public DateTime UpdateDate { @@ -1005,7 +1018,7 @@ namespace umbraco.MacroEngines get { if (n == null) return null; return n.PropertiesAsList; } } - public List ChildrenAsList + public List ChildrenAsList { get { if (n == null) return null; return n.ChildrenAsList; } } @@ -1019,7 +1032,7 @@ namespace umbraco.MacroEngines { if (!recursive) return GetProperty(alias); if (n == null) return null; - INode context = this.n; + DynamicBackingItem context = this.n; IProperty prop = n.GetProperty(alias); while (prop == null) { @@ -1058,189 +1071,7 @@ namespace umbraco.MacroEngines return n.ChildrenAsTable(nodeTypeAliasFilter); } - public bool IsNull() - { - return false; - } - public bool HasValue() - { - return true; - } - public int Position() - { - return this.Index(); - } - public int Index() - { - if (this.ownerList == null && this.Parent != null) - { - var list = this.Parent.ChildrenAsList.ConvertAll(n => new DynamicNode(n)); - this.ownerList = new DynamicNodeList(list); - } - if (this.ownerList != null) - { - List container = this.ownerList.Items.ToList(); - int currentIndex = container.FindIndex(n => n.Id == this.Id); - if (currentIndex != -1) - { - return currentIndex; - } - else - { - throw new IndexOutOfRangeException(string.Format("Node {0} belongs to a DynamicNodeList but could not retrieve the index for it's position in the list", this.Id)); - } - } - else - { - throw new ArgumentNullException(string.Format("Node {0} has been orphaned and doesn't belong to a DynamicNodeList", this.Id)); - } - } - public bool IsFirst() - { - return IsHelper(n => n.Index() == 0); - } - public string IsFirst(string valueIfTrue) - { - return IsHelper(n => n.Index() == 0, valueIfTrue); - } - public string IsFirst(string valueIfTrue, string valueIfFalse) - { - return IsHelper(n => n.Index() == 0, valueIfTrue, valueIfFalse); - } - public bool IsLast() - { - if (this.ownerList == null) - { - return false; - } - int count = this.ownerList.Items.Count; - return IsHelper(n => n.Index() == count - 1); - } - public string IsLast(string valueIfTrue) - { - if (this.ownerList == null) - { - return string.Empty; - } - int count = this.ownerList.Items.Count; - return IsHelper(n => n.Index() == count - 1, valueIfTrue); - } - public string IsLast(string valueIfTrue, string valueIfFalse) - { - if (this.ownerList == null) - { - return valueIfFalse; - } - int count = this.ownerList.Items.Count; - return IsHelper(n => n.Index() == count - 1, valueIfTrue, valueIfFalse); - } - public bool IsEven() - { - return IsHelper(n => n.Index() % 2 == 0); - } - public string IsEven(string valueIfTrue) - { - return IsHelper(n => n.Index() % 2 == 0, valueIfTrue); - } - public string IsEven(string valueIfTrue, string valueIfFalse) - { - return IsHelper(n => n.Index() % 2 == 0, valueIfTrue, valueIfFalse); - } - public bool IsOdd() - { - return IsHelper(n => n.Index() % 2 == 1); - } - public string IsOdd(string valueIfTrue) - { - return IsHelper(n => n.Index() % 2 == 1, valueIfTrue); - } - public string IsOdd(string valueIfTrue, string valueIfFalse) - { - return IsHelper(n => n.Index() % 2 == 1, valueIfTrue, valueIfFalse); - } - public bool IsEqual(DynamicNode other) - { - return IsHelper(n => n.Id == other.Id); - } - public string IsEqual(DynamicNode other, string valueIfTrue) - { - return IsHelper(n => n.Id == other.Id, valueIfTrue); - } - public string IsEqual(DynamicNode other, string valueIfTrue, string valueIfFalse) - { - return IsHelper(n => n.Id == other.Id, valueIfTrue, valueIfFalse); - } - public bool IsDescendant(DynamicNode other) - { - var ancestors = this.Ancestors(); - return IsHelper(n => ancestors.Items.Find(ancestor => ancestor.Id == other.Id) != null); - } - public string IsDescendant(DynamicNode other, string valueIfTrue) - { - var ancestors = this.Ancestors(); - return IsHelper(n => ancestors.Items.Find(ancestor => ancestor.Id == other.Id) != null, valueIfTrue); - } - public string IsDescendant(DynamicNode other, string valueIfTrue, string valueIfFalse) - { - var ancestors = this.Ancestors(); - return IsHelper(n => ancestors.Items.Find(ancestor => ancestor.Id == other.Id) != null, valueIfTrue, valueIfFalse); - } - public bool IsDescendantOrSelf(DynamicNode other) - { - var ancestors = this.AncestorsOrSelf(); - return IsHelper(n => ancestors.Items.Find(ancestor => ancestor.Id == other.Id) != null); - } - public string IsDescendantOrSelf(DynamicNode other, string valueIfTrue) - { - var ancestors = this.AncestorsOrSelf(); - return IsHelper(n => ancestors.Items.Find(ancestor => ancestor.Id == other.Id) != null, valueIfTrue); - } - public string IsDescendantOrSelf(DynamicNode other, string valueIfTrue, string valueIfFalse) - { - var ancestors = this.AncestorsOrSelf(); - return IsHelper(n => ancestors.Items.Find(ancestor => ancestor.Id == other.Id) != null, valueIfTrue, valueIfFalse); - } - public bool IsAncestor(DynamicNode other) - { - var descendants = this.Descendants(); - return IsHelper(n => descendants.Items.Find(descendant => descendant.Id == other.Id) != null); - } - public string IsAncestor(DynamicNode other, string valueIfTrue) - { - var descendants = this.Descendants(); - return IsHelper(n => descendants.Items.Find(descendant => descendant.Id == other.Id) != null, valueIfTrue); - } - public string IsAncestor(DynamicNode other, string valueIfTrue, string valueIfFalse) - { - var descendants = this.Descendants(); - return IsHelper(n => descendants.Items.Find(descendant => descendant.Id == other.Id) != null, valueIfTrue, valueIfFalse); - } - public bool IsAncestorOrSelf(DynamicNode other) - { - var descendants = this.DescendantsOrSelf(); - return IsHelper(n => descendants.Items.Find(descendant => descendant.Id == other.Id) != null); - } - public string IsAncestorOrSelf(DynamicNode other, string valueIfTrue) - { - var descendants = this.DescendantsOrSelf(); - return IsHelper(n => descendants.Items.Find(descendant => descendant.Id == other.Id) != null, valueIfTrue); - } - public string IsAncestorOrSelf(DynamicNode other, string valueIfTrue, string valueIfFalse) - { - var descendants = this.DescendantsOrSelf(); - return IsHelper(n => descendants.Items.Find(descendant => descendant.Id == other.Id) != null, valueIfTrue, valueIfFalse); - } - public bool IsHelper(Func test) - { - return test(this); - } - public string IsHelper(Func test, string valueIfTrue) - { - return IsHelper(test, valueIfTrue, string.Empty); - } - public string IsHelper(Func test, string valueIfTrue, string valueIfFalse) - { - return test(this) ? valueIfTrue : valueIfFalse; - } + + } } diff --git a/umbraco.MacroEngines.Juno/RazorDynamicNode/DynamicNodeList.cs b/umbraco.MacroEngines.Juno/RazorDynamicNode/DynamicNodeList.cs index 5bfa9e9da1..d62f762307 100644 --- a/umbraco.MacroEngines.Juno/RazorDynamicNode/DynamicNodeList.cs +++ b/umbraco.MacroEngines.Juno/RazorDynamicNode/DynamicNodeList.cs @@ -14,7 +14,7 @@ namespace umbraco.MacroEngines { public class DynamicNodeList : DynamicObject, IEnumerable { - public List Items { get; set; } + public List Items; public DynamicNodeList() { @@ -26,6 +26,13 @@ namespace umbraco.MacroEngines list.ForEach(node => node.ownerList = this); Items = list; } + public DynamicNodeList(IEnumerable items) + { + List list = items.ToList().ConvertAll(n => new DynamicNode(n)); + list.ForEach(node => node.ownerList = this); + Items = list; + } + public DynamicNodeList(IEnumerable items) { List list = items.Select(x => new DynamicNode(x)).ToList(); diff --git a/umbraco.MacroEngines.Juno/RazorDynamicNode/ExamineBackedMedia.cs b/umbraco.MacroEngines.Juno/RazorDynamicNode/ExamineBackedMedia.cs new file mode 100644 index 0000000000..c53678f3fd --- /dev/null +++ b/umbraco.MacroEngines.Juno/RazorDynamicNode/ExamineBackedMedia.cs @@ -0,0 +1,194 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Xml.XPath; +using Examine; +using UmbracoExamine; +using Lucene.Net.Documents; +using umbraco.interfaces; + + +namespace umbraco.MacroEngines +{ + + public class ExamineBackedMedia : INode + { + public static ExamineBackedMedia GetUmbracoMedia(int id) + { + + //first check in Examine as this is WAY faster + var criteria = ExamineManager.Instance + .SearchProviderCollection["InternalSearcher"] + .CreateSearchCriteria("media"); + var filter = criteria.Id(id); + var results = ExamineManager + .Instance.SearchProviderCollection["InternalSearcher"] + .Search(filter.Compile()); + if (results.Any()) + { + return new ExamineBackedMedia(results.First()); + } + + var media = umbraco.library.GetMedia(id, false); + if (media != null && media.Current != null) + { + media.MoveNext(); + return new ExamineBackedMedia(media.Current); + } + + return null; + } + public ExamineBackedMedia(XPathNavigator xpath) + { + if (xpath == null) throw new ArgumentNullException("xpath"); + Name = xpath.GetAttribute("nodeName", ""); + Values = new Dictionary(); + var result = xpath.SelectChildren(XPathNodeType.Element); + while (result.MoveNext()) + { + if (result.Current != null && !result.Current.HasAttributes) + { + Values.Add(result.Current.Name, result.Current.Value); + } + } + } + + public ExamineBackedMedia(SearchResult result) + { + if (result == null) throw new ArgumentNullException("result"); + Name = result.Fields["nodeName"]; + Values = result.Fields; + + } + + public string Name { get; private set; } + private IDictionary Values { get; set; } + + //make this one lazy + public INode Parent + { + get { throw new NotImplementedException(); } + } + + public int Id + { + get { throw new NotImplementedException(); } + } + + public int template + { + get { throw new NotImplementedException(); } + } + + public int SortOrder + { + get { throw new NotImplementedException(); } + } + + public string Url + { + get { throw new NotImplementedException(); } + } + + public string UrlName + { + get { throw new NotImplementedException(); } + } + + public string NodeTypeAlias + { + get { throw new NotImplementedException(); } + } + + public string WriterName + { + get { throw new NotImplementedException(); } + } + + public string CreatorName + { + get { throw new NotImplementedException(); } + } + + public int WriterID + { + get { throw new NotImplementedException(); } + } + + public int CreatorID + { + get { throw new NotImplementedException(); } + } + + public string Path + { + get { throw new NotImplementedException(); } + } + + public DateTime CreateDate + { + get { throw new NotImplementedException(); } + } + + public DateTime UpdateDate + { + get { throw new NotImplementedException(); } + } + + public Guid Version + { + get { throw new NotImplementedException(); } + } + + public string NiceUrl + { + get { throw new NotImplementedException(); } + } + + public int Level + { + get { throw new NotImplementedException(); } + } + + public List PropertiesAsList + { + get { throw new NotImplementedException(); } + } + //make this one lazy + public List ChildrenAsList + { + get { throw new NotImplementedException(); } + } + + //make this one lazy + public System.Data.DataTable ChildrenAsTable() + { + throw new NotImplementedException(); + } + + //make this one lazy + public System.Data.DataTable ChildrenAsTable(string nodeTypeAliasFilter) + { + throw new NotImplementedException(); + } + + public IProperty GetProperty(string Alias) + { + bool exists = false; + return GetProperty(Alias, out exists); + } + + public IProperty GetProperty(string alias, out bool propertyExists) + { + string value = null; + if (Values.TryGetValue(alias, out value)) + { + propertyExists = true; + return new PropertyResult(alias, value, Guid.Empty); + } + propertyExists = false; + return null; + } + } +} \ No newline at end of file diff --git a/umbraco.MacroEngines.Juno/RazorDynamicNode/PropertyResult.cs b/umbraco.MacroEngines.Juno/RazorDynamicNode/PropertyResult.cs new file mode 100644 index 0000000000..afe5b79548 --- /dev/null +++ b/umbraco.MacroEngines.Juno/RazorDynamicNode/PropertyResult.cs @@ -0,0 +1,49 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using umbraco.interfaces; +using umbraco.cms.businesslogic.property; + +namespace umbraco.MacroEngines +{ + public class PropertyResult : IProperty + { + private string _alias; + private string _value; + private Guid _version; + + public PropertyResult(IProperty source) + { + this._alias = source.Alias; + this._value = source.Value; + this._version = source.Version; + } + public PropertyResult(string alias, string value, Guid version) + { + this._alias = alias; + this._value = value; + this._version = version; + } + public PropertyResult(Property source) + { + this._alias = source.PropertyType.Alias; + this._value = string.Format("{0}", source.Value); + this._version = source.VersionId; + } + public string Alias + { + get { return _alias; } + } + + public string Value + { + get { return _value; } + } + + public Guid Version + { + get { return _version; } + } + } +} diff --git a/umbraco.MacroEngines.Juno/umbraco.MacroEngines.csproj b/umbraco.MacroEngines.Juno/umbraco.MacroEngines.csproj index 554bb86067..03454b3977 100644 --- a/umbraco.MacroEngines.Juno/umbraco.MacroEngines.csproj +++ b/umbraco.MacroEngines.Juno/umbraco.MacroEngines.csproj @@ -39,7 +39,14 @@ 4 + + ..\foreign dlls\Examine.dll + + + ..\foreign dlls\Lucene.Net.dll + + @@ -58,12 +65,23 @@ + + ..\foreign dlls\UmbracoExamine.dll + - - + + + + Code + + + Code + + + @@ -77,7 +95,6 @@ -