Added LazyLoad for the world tree and it works BEAUTIFULLY with the new components!

This commit is contained in:
Rick Falkvinge
2015-02-22 14:44:52 +01:00
parent 054c7ca73f
commit 0d21921f65
9 changed files with 92 additions and 46 deletions
+4 -6
View File
@@ -7,7 +7,7 @@ namespace Swarmops.Basic.Types.Structure
/// A geographical node.
/// </summary>
[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
/// </summary>
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
}
+2 -2
View File
@@ -19,9 +19,9 @@ namespace Swarmops.Common.Generics
public List<TreeNode<TNode>> RootNodes { get; private set; }
private readonly Dictionary<int, TreeNode<TNode>> _nodeLookup;
public TNode this [int identity]
public TreeNode<TNode> this [int identity]
{
get { return this._nodeLookup[identity].Data; }
get { return this._nodeLookup[identity]; }
}
public static Tree<TNode> FromCollection (List<TNode> collection)
+23
View File
@@ -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<Geography> Tree
{
get
{
const string cacheKey = "FullGeographyTree";
object testObject = GuidCache.Get(cacheKey);
if (testObject == null)
{
Geographies geographies = Geography.Root.GetTree();
Tree<Geography> geoTree = Tree<Geography>.FromCollection(geographies);
GuidCache.Set(cacheKey, geoTree);
return geoTree;
}
else
{
return (Tree<Geography>)testObject;
}
}
}
}
}
+2
View File
@@ -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);
}
}
}
+45 -31
View File
@@ -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<int, Country>();
// Get geography tree
Geographies geographies = rootGeography.GetTree();
// Build tree (there should be a template for this)
Dictionary<int, List<Geography>> treeMap = new Dictionary<int, List<Geography>>();
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<Geography>();
_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<TreeNode<Geography>> 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<int, List<Geography>> treeMap, int node, bool expanded)
private Tree<Geography> _geoTree;
private Dictionary<int, Country> _countryLookup;
private string RecurseTreeMap (List<TreeNode<Geography>> geoBranch, bool expanded)
{
List<string> elements = new List<string>();
foreach (Geography geography in treeMap[node])
foreach (TreeNode<Geography> 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 + "}");
+1 -3
View File
@@ -21,8 +21,6 @@ namespace Swarmops.Frontend.Automation
Tree<Position> systemPositions = Positions.ForSystem().Tree;
Response.Output.WriteLine(RecursePositionTree (systemPositions.RootNodes));
Response.End();
@@ -40,7 +38,7 @@ namespace Swarmops.Frontend.Automation
foreach (TreeNode<Position> 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;
+11 -3
View File
@@ -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 @@
});
</script>
<span id="<%=this.ClientID %>_SpanGeographies"><select class="easyui-combotree" url="/Automation/Json-GeographiesTree.aspx?RootGeographyId=<%=this.RootGeographyId %>" name="DropGeographies" id="<%=this.ClientID %>_DropGeographies" animate="true" style="width:300px"></select></span>
<span id="<%=this.ClientID %>_SpanGeographies"><select class="easyui-combotree" name="DropGeographies" id="<%=this.ClientID %>_DropGeographies" animate="true" style="width:300px"></select></span>
@@ -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;
+2 -1
View File
@@ -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