diff --git a/Basic/Types/Structure/BasicGeography.cs b/Basic/Types/Structure/BasicGeography.cs index 5d3c8de1..304db1b2 100644 --- a/Basic/Types/Structure/BasicGeography.cs +++ b/Basic/Types/Structure/BasicGeography.cs @@ -7,7 +7,7 @@ namespace Swarmops.Basic.Types.Structure /// A geographical node. /// [Serializable] - public class BasicGeography : IHasIdentity + public class BasicGeography : IHasIdentity, IHasParentIdentity { public BasicGeography (int geographyId, int parentGeographyId, string name) : this (geographyId, parentGeographyId, name, 0) @@ -53,12 +53,10 @@ namespace Swarmops.Basic.Types.Structure /// public int Generation { get; protected set; } - #region IHasIdentity Members + #region Interface Members - public int Identity - { - get { return this.GeographyId; } - } + public int Identity { get { return this.GeographyId; } } + public int ParentIdentity { get { return this.ParentGeographyId; }} #endregion } diff --git a/Common/Generics/Tree.cs b/Common/Generics/Tree.cs index 6138b2f0..81b58aa1 100644 --- a/Common/Generics/Tree.cs +++ b/Common/Generics/Tree.cs @@ -19,9 +19,9 @@ namespace Swarmops.Common.Generics public List> RootNodes { get; private set; } private readonly Dictionary> _nodeLookup; - public TNode this [int identity] + public TreeNode this [int identity] { - get { return this._nodeLookup[identity].Data; } + get { return this._nodeLookup[identity]; } } public static Tree FromCollection (List collection) diff --git a/Logic/Structure/Geographies.cs b/Logic/Structure/Geographies.cs index 137b4946..11aa4507 100644 --- a/Logic/Structure/Geographies.cs +++ b/Logic/Structure/Geographies.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using Swarmops.Basic.Types; using Swarmops.Basic.Types.Structure; using Swarmops.Common.Enums; +using Swarmops.Common.Generics; using Swarmops.Database; using Swarmops.Logic.Cache; using Swarmops.Logic.Support; @@ -267,5 +268,27 @@ namespace Swarmops.Logic.Structure get { return this.order; } } } + + public static Tree Tree + { + get + { + const string cacheKey = "FullGeographyTree"; + + object testObject = GuidCache.Get(cacheKey); + if (testObject == null) + { + Geographies geographies = Geography.Root.GetTree(); + Tree geoTree = Tree.FromCollection(geographies); + + GuidCache.Set(cacheKey, geoTree); + return geoTree; + } + else + { + return (Tree)testObject; + } + } + } } } \ No newline at end of file diff --git a/Logic/Structure/Geography.cs b/Logic/Structure/Geography.cs index 67b40b63..375dd3ee 100644 --- a/Logic/Structure/Geography.cs +++ b/Logic/Structure/Geography.cs @@ -4,6 +4,7 @@ using System.Xml.Serialization; using Swarmops.Basic.Types; using Swarmops.Basic.Types.Structure; using Swarmops.Common.Enums; +using Swarmops.Common.Generics; using Swarmops.Database; using Swarmops.Logic.Cache; using Swarmops.Logic.Interfaces; @@ -178,5 +179,6 @@ namespace Swarmops.Logic.Structure { return SwarmDb.GetDatabaseForReading().GetGeographyDesignationsForGeographyId (Identity); } + } } \ No newline at end of file diff --git a/Site5/Automation/Json-GeographiesTree.aspx.cs b/Site5/Automation/Json-GeographiesTree.aspx.cs index 0e90a37b..b21afb32 100644 --- a/Site5/Automation/Json-GeographiesTree.aspx.cs +++ b/Site5/Automation/Json-GeographiesTree.aspx.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Globalization; using System.Threading; using Resources; +using Swarmops.Common.Generics; using Swarmops.Logic.Structure; namespace Swarmops.Frontend.Automation @@ -15,23 +16,27 @@ namespace Swarmops.Frontend.Automation Response.ContentType = "application/json"; - int rootGeographyId = Geography.RootIdentity; + int parentGeographyId = 0; - // What's our root GeographyId? (probably Geography.RootIdentity...) + bool initialExpand = true; - string rootIdString = Request.QueryString["RootGeographyId"]; - if (!string.IsNullOrEmpty (rootIdString)) + if (Request.QueryString["InitialExpand"] == "false") { - rootGeographyId = Int32.Parse (rootIdString); // will throw if invalid Int32, but who cares + initialExpand = false; + } + + string parentIdString = Request.QueryString["ParentGeographyId"]; + if (!string.IsNullOrEmpty (parentIdString)) + { + parentGeographyId = Int32.Parse (parentIdString); // will throw if invalid Int32, but who cares } // Is this stuff in cache already? - string cacheKey = "Geographies-Json-" + rootGeographyId.ToString (CultureInfo.InvariantCulture) + "-" + + string cacheKey = "Geographies-Json-" + parentGeographyId.ToString (CultureInfo.InvariantCulture) + "-" + Thread.CurrentThread.CurrentCulture.Name; - string accountsJson = - (string) Cache[cacheKey]; + string accountsJson = (string) Cache[cacheKey]; if (accountsJson != null) { @@ -42,51 +47,60 @@ namespace Swarmops.Frontend.Automation // Not in cache. Construct. - Geography rootGeography = Geography.FromIdentity (rootGeographyId); + _geoTree = Geographies.Tree; + Countries countries = Countries.GetAll(); + _countryLookup = new Dictionary(); - // Get geography tree - - Geographies geographies = rootGeography.GetTree(); - - // Build tree (there should be a template for this) - - Dictionary> treeMap = new Dictionary>(); - - foreach (Geography geography in geographies) + foreach (Country country in countries) { - if (!treeMap.ContainsKey (geography.ParentIdentity)) + if (country.GeographyId > Geography.RootIdentity) { - treeMap[geography.ParentIdentity] = new List(); + _countryLookup[country.GeographyId] = country; } - - treeMap[geography.ParentIdentity].Add (geography); } - int renderRootNodeId = rootGeography.ParentGeographyId; - // This works as rootGeography will be the only present child in the collection; other children won't be there + List> rootNodes = _geoTree.RootNodes; - accountsJson = RecurseTreeMap (treeMap, renderRootNodeId, true); + if (parentGeographyId != 0) + { + rootNodes = _geoTree[parentGeographyId].Children; + } - Cache.Insert (cacheKey, accountsJson, null, DateTime.Now.AddMinutes (5), TimeSpan.Zero); - // cache lasts for five minutes, no sliding expiration + accountsJson = RecurseTreeMap (rootNodes, false); + + Cache.Insert (cacheKey, accountsJson, null, DateTime.Now.AddMinutes (15), TimeSpan.Zero); + // cache lasts for fifteen minutes, no sliding expiration Response.Output.WriteLine (accountsJson); Response.End(); } - private string RecurseTreeMap (Dictionary> treeMap, int node, bool expanded) + private Tree _geoTree; + private Dictionary _countryLookup; + + private string RecurseTreeMap (List> geoBranch, bool expanded) { List elements = new List(); - foreach (Geography geography in treeMap[node]) + foreach (TreeNode geographyNode in geoBranch) { + Geography geography = geographyNode.Data; + string element = string.Format ("\"id\":{0},\"text\":\"{1}\"", geography.Identity, JsonSanitize (TestLocalization (geography.Name))); - if (treeMap.ContainsKey (geography.Identity)) + if (_countryLookup.ContainsKey (geography.Identity)) + { + // this is a country node. Populate with stuff to enable lazy-load of the tree. + Country country = _countryLookup[geography.Identity]; + + element += ",\"children1\":\"\",\"state\":\"closed\"," + + string.Format ("\"countryId\":\"{0}\",\"countryNode\":\"{1}\"", country.Code, geography.Identity); + } + else if (geographyNode.Children.Count > 0) { element += ",\"state\":\"" + (expanded ? "open" : "closed") + "\",\"children\":" + - RecurseTreeMap (treeMap, geography.Identity, false); + RecurseTreeMap (geographyNode.Children, false); } elements.Add ("{" + element + "}"); diff --git a/Site5/Automation/Json-PositionTree.aspx.cs b/Site5/Automation/Json-PositionTree.aspx.cs index 8fb3182d..f6359a25 100644 --- a/Site5/Automation/Json-PositionTree.aspx.cs +++ b/Site5/Automation/Json-PositionTree.aspx.cs @@ -21,8 +21,6 @@ namespace Swarmops.Frontend.Automation Tree systemPositions = Positions.ForSystem().Tree; - - Response.Output.WriteLine(RecursePositionTree (systemPositions.RootNodes)); Response.End(); @@ -40,7 +38,7 @@ namespace Swarmops.Frontend.Automation foreach (TreeNode positionNode in positionNodes) { Position position = positionNode.Data; - string localizedPositionName = position.Localized (positionNode.Data.MaxCount > 1); + string localizedPositionName = position.Localized (positionNode.Data.MaxCount != 1); PositionAssignments assignments = position.Assignments; string parentName = string.Empty; diff --git a/Site5/Controls/v5/Base/ComboGeographies.ascx b/Site5/Controls/v5/Base/ComboGeographies.ascx index 72e3111b..5b4dbb78 100644 --- a/Site5/Controls/v5/Base/ComboGeographies.ascx +++ b/Site5/Controls/v5/Base/ComboGeographies.ascx @@ -12,10 +12,18 @@ { Response.Write(this.OnClientLoaded + "(); "); }%> - }<% + }, + loader: function (param, success, error) { + if (!param.id) { + $.getJSON("/Automation/Json-GeographiesTree.aspx?ParentGeographyId=<%=this.ParentGeographyId%>", null, success); + } else { + $.getJSON("/Automation/Json-GeographiesTree.aspx?InitialExpand=false&ParentGeographyId=" + param.id, null, success); + } + } + <% if (!String.IsNullOrEmpty(this.OnClientSelect)) { - Response.Write(", onSelect: function(account) { " + this.OnClientSelect + "(account.id); }"); + Response.Write(", onSelect: function(row) { " + this.OnClientSelect + "(row.id); }"); } %> @@ -38,4 +46,4 @@ }); - \ No newline at end of file + \ No newline at end of file diff --git a/Site5/Controls/v5/Base/ComboGeographies.ascx.cs b/Site5/Controls/v5/Base/ComboGeographies.ascx.cs index f4067335..8eac8f16 100644 --- a/Site5/Controls/v5/Base/ComboGeographies.ascx.cs +++ b/Site5/Controls/v5/Base/ComboGeographies.ascx.cs @@ -9,12 +9,14 @@ namespace Swarmops.Controls.Base public string OnClientLoaded { get; set; } public string OnClientSelect { get; set; } protected int RootGeographyId { get; private set; } + protected int ParentGeographyId { get; private set; } protected string RootGeographyName { get; private set; } public Geography RootGeography { set { + ParentGeographyId = value.ParentIdentity; RootGeographyId = value.Identity; RootGeographyName = value.Name; diff --git a/Swarmops.sln b/Swarmops.sln index 32f4463a..fbfde28a 100644 --- a/Swarmops.sln +++ b/Swarmops.sln @@ -44,7 +44,8 @@ Project("{E24C65DC-7377-472B-9ABA-BC803B73C61A}") = "Site5", "Site5\", "{367F36E Release.AspNetCompiler.ForceOverwrite = "true" Release.AspNetCompiler.FixedNames = "false" Release.AspNetCompiler.Debug = "False" - VWDPort = "1186" + VWDPort = "8081" + VWDDynamicPort = "false" VWDVirtualPath = "/" DefaultWebSiteLanguage = "Visual C#" EndProjectSection