public static DeweyOrderSet CreateOrderSet(INakedObjectMemberPeer[] members) { var sortedMembersByGroup = new SortedList<string, List<INakedObjectMemberPeer>>(); var nonAnnotatedGroup = new List<INakedObjectMemberPeer>(); // spin over all the members and put them into a Map of SortedSets // any non-annotated members go into additional nonAnnotatedGroup set. foreach (INakedObjectMemberPeer member in members) { var memberOrder = member.GetFacet<IMemberOrderFacet>(); if (memberOrder != null) { List<INakedObjectMemberPeer> sortedMembersForGroup = GetSortedSet(sortedMembersByGroup, memberOrder.Name); sortedMembersForGroup.Add(member); } else { nonAnnotatedGroup.Add(member); } } nonAnnotatedGroup.Sort(new MemberIdentifierComparator()); foreach (var list in sortedMembersByGroup.Values) { list.Sort(new MemberOrderComparator(true)); } // add the non-annotated group to the first "" group. IList<INakedObjectMemberPeer> defaultSet = GetSortedSet(sortedMembersByGroup, ""); foreach (INakedObjectMemberPeer member in nonAnnotatedGroup) { defaultSet.Add(member); } // create OrderSets, wiring up parents and children. // since sortedMembersByGroup is a SortedMap, the // iteration will be in alphabetical order (ie parent groups before their children). ICollection<string> groupNames = sortedMembersByGroup.Keys; IDictionary<string, DeweyOrderSet> orderSetsByGroup = new SortedList<string, DeweyOrderSet>(); foreach (string groupName in groupNames) { var deweyOrderSet = new DeweyOrderSet(groupName); orderSetsByGroup.Add(groupName, deweyOrderSet); EnsureParentFor(orderSetsByGroup, deweyOrderSet); } // now populate the OrderSets foreach (string groupName in groupNames) { DeweyOrderSet deweyOrderSet = orderSetsByGroup[groupName]; IList<INakedObjectMemberPeer> sortedMembers = sortedMembersByGroup[groupName]; foreach (INakedObjectMemberPeer ordeableElement in sortedMembers) { deweyOrderSet.AddElement(ordeableElement); } deweyOrderSet.CopyOverChildren(); } return orderSetsByGroup[""]; }
/// <summary> /// Recursively creates parents all the way up to root (<c>""</c>), /// along the way associating each child with its parent and adding /// the child as an element of its parent. /// </summary> private static void EnsureParentFor(IDictionary<string, DeweyOrderSet> orderSetsByGroup, DeweyOrderSet deweyOrderSet) { string parentGroup = deweyOrderSet.GroupPath; DeweyOrderSet parentOrderSet = orderSetsByGroup[parentGroup]; if (parentOrderSet == null) { parentOrderSet = new DeweyOrderSet(parentGroup); orderSetsByGroup[parentGroup] = parentOrderSet; if (!parentGroup.Equals("")) { EnsureParentFor(orderSetsByGroup, deweyOrderSet); } } // check in case at root if (deweyOrderSet != parentOrderSet) { deweyOrderSet.Parent = parentOrderSet; parentOrderSet.AddChild(deweyOrderSet); } }