/// <summary> /// Calculates the hierarchical order of divisions. /// </summary> /// <param name="divisions">Divisions that must be properly sorted before the call.</param> protected static void CalculateOrder(IList <DivisionObrnadzorViewModel> divisions) { // TODO: Get hierarchical data from DB without recalculating it? const string separator = "."; var orderCounter = 1; var orderStack = new List <int> (); var returnStack = new Stack <DivisionObrnadzorViewModel> (); DivisionObrnadzorViewModel prevDivision = null; foreach (var division in divisions) { if (prevDivision != null) { if (division.ParentDivisionID == prevDivision.ParentDivisionID) { // moving on same level orderCounter++; } else if (division.ParentDivisionID == prevDivision.DivisionID) { // moving down orderStack.Add(orderCounter); returnStack.Push(prevDivision); orderCounter = 1; } else { // moving up while (returnStack.Count > 0 && orderStack.Count > 0) { orderCounter = orderStack [orderStack.Count - 1]; orderStack.RemoveAt(orderStack.Count - 1); if (division.ParentDivisionID == returnStack.Pop().ParentDivisionID) { break; } } orderCounter++; } } // format order value if (orderStack.Count == 0) { division.Order = orderCounter + separator; division.Level = 0; } else { division.Order = TextUtils.FormatList(separator, orderStack) + separator + orderCounter + separator; } prevDivision = division; } }
/// <summary> /// Handles Load event for a control /// </summary> /// <param name="e">Event args.</param> protected override void OnLoad(EventArgs e) { base.OnLoad(e); try { if (Settings.Mode == DivisionDirectoryMode.Search) { if (!IsPostBack) { if (!string.IsNullOrWhiteSpace(SearchText) || !Null.IsNull(SearchDivision)) { // restore current search textSearch.Text = SearchText; if (Null.IsNull(SearchDivision)) { // select first node if (treeDivisions.Nodes.Count > 0) { treeDivisions.Nodes [0].Selected = true; } } else { treeDivisions.SelectAndExpandByValue(SearchDivision.ToString()); } // perform search if (SearchParamsOK(SearchText, SearchDivision, false)) { DoSearch(SearchText, SearchDivision); } } } } else if (Settings.Mode == DivisionDirectoryMode.ObrnadzorDivisions) { var divisions = GetDivisions(); if (!divisions.IsNullOrEmpty()) { gridObrnadzorDivisions.DataSource = DivisionObrnadzorViewModel.Create(divisions, ViewModelContext); gridObrnadzorDivisions.DataBind(); } } else if (Settings.Mode == DivisionDirectoryMode.ObrnadzorGoverningDivisions) { var divisions = GetDivisions(); if (!divisions.IsNullOrEmpty()) { gridObrnadzorGoverningDivisions.DataSource = DivisionObrnadzorViewModel.Create(divisions, ViewModelContext); gridObrnadzorGoverningDivisions.DataBind(); } } } catch (Exception ex) { Exceptions.ProcessModuleLoadException(this, ex); } }