/// <summary> /// Returns the root CoursePlugg. The root CoursePlugg has CoursePluggId = 0. It has all first level CoursePluggs as children. /// Navigate up and down hierarchy using children[] and Mother. /// </summary> /// <param name="cultureCode"></param> /// <param name="list"></param> /// <returns></returns> public CoursePlugg RootCoursePlugg(int courseId, string cultureCode, IEnumerable<CoursePlugg> list = null) { if (list == null) list = GetPluggsAsFlatList(courseId, cultureCode); CoursePlugg root = new CoursePlugg(); root.children = new List<CoursePlugg>(); Dictionary<int, CoursePlugg> dict = new Dictionary<int, CoursePlugg>(); foreach (CoursePlugg s in list) { dict.Add(s.CoursePluggId, s); s.children = new List<CoursePlugg>(); } foreach (CoursePlugg e in list) { if (e.MotherId == 0) { root.children.Add(e); e.Mother = root; } else { dict[e.MotherId].children.Add(e); e.Mother = dict[e.MotherId]; } } return root; }
/// <summary> /// Converts a flat list of all CoursePluggs into a hierarchy. /// Will set Mother as well as Children /// </summary> /// <param name="list"></param> /// <param name="Id"></param> /// <param name="_isChildren"></param> /// <returns></returns> public List<CoursePlugg> FlatToHierarchy(IEnumerable<CoursePlugg> list, int motherId = 0, CoursePlugg mother = null) { return (from i in list where i.MotherId == motherId select new CoursePlugg { CoursePluggId = i.CoursePluggId, CourseId = i.CourseId, PluggId = i.PluggId, CPOrder = i.CPOrder, MotherId = i.MotherId, label = i.label, //Mother = mother, children = FlatToHierarchy(list, i.CoursePluggId, i), CreatedOnDate = i.CreatedOnDate, CreatedByUserId = i.CreatedByUserId }).ToList(); }
/// <summary> /// Retrieve next CoursePlugg in hierarchy (Think of an expanded CoursePlugg tree. Will get the "next row") /// </summary> /// <param name="current"></param> /// <param name="_lastCoursePlugg"></param> /// <param name="previousChildOrder"></param> /// <returns></returns> public CoursePlugg NextCoursePlugg(CoursePlugg cp) { bool SearchChildren = true; while (1 == 1) { if (cp.CoursePluggId == 0) return null; if (SearchChildren && cp.children.Count > 0) return cp.children[0]; if (cp.Mother.children.Count > cp.CPOrder) return cp.Mother.children[cp.CPOrder]; cp = cp.Mother; SearchChildren = false; } }
/// <summary> /// Find a specific CoursePlugg in the hierarchy. Navigate up and down hierarchy using children[] and Mother. /// </summary> /// <param name="cultureCode"></param> /// <param name="coursePluggId"></param> /// <param name="root"></param> /// <returns></returns> public CoursePlugg FindCoursePlugg(string cultureCode, int courseId, int coursePluggId, CoursePlugg root = null) { if (root == null) root = RootCoursePlugg(courseId, cultureCode); foreach (CoursePlugg cp in root.children) { if (cp.CoursePluggId == coursePluggId) return cp; if (cp.children.Count > 0) { CoursePlugg fcp = FindCoursePlugg(cultureCode, courseId, coursePluggId, cp); if (fcp != null) return fcp; } } return null; }