using System; using Activizr.Logic.Structure; using Activizr.Database; using Activizr.Basic.Types; using System.Collections.Generic; using Activizr.Basic.Enums; namespace Activizr.Logic.Cache { public class GeographyCache { static private DateTime lastRefresh; static private object loadCacheLock = new object(); static public bool loadCache = true; static private Dictionary> __geographyCache = null; static readonly int cacheLifeSpanMinutes = 2; static GeographyCache () { lastRefresh = DateTime.MinValue; } static private Dictionary> GetHashedGeographies () { lock (loadCacheLock) { if (loadCache || lastRefresh.AddMinutes(cacheLifeSpanMinutes) < DateTime.Now) { __geographyCache = PirateDb.GetDatabase().GetHashedGeographies(); lastRefresh = DateTime.Now; loadCache = false; } return __geographyCache; } } static public Geography FromCache (int geographyId) { lock (loadCacheLock) { return Geography.FromBasic(GeographyCache.GetGeography(geographyId)); } } static public BasicGeography[] GetAllGeographies () { lock (loadCacheLock) { List result = new List(); Dictionary> hashedGeographies = GetHashedGeographies(); foreach (int entry in hashedGeographies.Keys) { result.Add(hashedGeographies[entry][0]); } return result.ToArray(); } } static public BasicGeography[] GetGeographies (int[] ids) { List result = new List(); lock (loadCacheLock) { foreach (int i in ids) { result.Add(GeographyCache.GetGeography(i) ); } return result.ToArray(); } } static public BasicGeography[] GetGeographyChildren (int parentGeographyId) { List result = new List(); lock (loadCacheLock) { // Prime the cache BasicGeography partent = GeographyCache.GetGeography(parentGeographyId); //TODO: Possible to miss a child geography here if it was added since last cache reload. Dictionary> hashedGeographies = GetHashedGeographies(); foreach (BasicGeography b in hashedGeographies[parentGeographyId]) { if (b.Identity != parentGeographyId) result.Add(b); } return result.ToArray(); } } public static int CountGeographyChildren (int parentGeographyId) { lock (loadCacheLock) { // Prime the cache BasicGeography partent = GeographyCache.GetGeography(parentGeographyId); return GetHashedGeographies()[parentGeographyId].Count - 1; } } static public BasicGeography[] GetGeographyLine (int leafGeographyId) { List result = new List(); BasicGeography currentNode = GeographyCache.GetGeography(leafGeographyId); // This iterates until the zero-parentid root node is found while (currentNode.GeographyId != 0) { result.Add(currentNode); if (currentNode.ParentGeographyId != 0) { currentNode = GeographyCache.GetGeography(currentNode.ParentGeographyId); } else { currentNode = new BasicGeography(0, 0, string.Empty); } } result.Reverse(); return result.ToArray(); } static public BasicGeography[] GetGeographyTree () { return GetGeographyTree(1); } public static Dictionary GetGeographyHashtable (int startGeographyId) { BasicGeography[] nodes = GetGeographyTree(startGeographyId); Dictionary result = new Dictionary(); foreach (BasicGeography node in nodes) { result[node.GeographyId] = node; } return result; } static public BasicGeography[] GetGeographyTree (int startGeographyId) { Dictionary> nodes = GetHashedGeographies(); return GetGeographyTree(nodes, startGeographyId, 0); } static private BasicGeography[] GetGeographyTree (Dictionary> geographies, int startNodeId, int generation) { List result = new List(); List thisList = geographies[startNodeId]; foreach (BasicGeography node in thisList) { if (node.GeographyId != startNodeId) { result.Add(new BasicGeography(node.GeographyId, node.ParentGeographyId, node.Name, generation + 1)); // Add recursively BasicGeography[] children = GetGeographyTree(geographies, node.GeographyId, generation + 1); if (children.Length > 0) { foreach (BasicGeography child in children) { result.Add(child); } } } else if (generation == 0) { // The top parent is special and should be added; the others shouldn't result.Add(new BasicGeography(node.GeographyId, node.ParentGeographyId, node.Name, generation)); } } return result.ToArray(); } static public BasicGeography GetGeography (int geographyId) { lock (loadCacheLock) { Dictionary> hashedGeographies = GetHashedGeographies(); if (hashedGeographies.ContainsKey(geographyId)) return hashedGeographies[geographyId][0]; else { loadCache = true; hashedGeographies = GetHashedGeographies(); if (hashedGeographies.ContainsKey(geographyId)) return hashedGeographies[geographyId][0]; else { throw new ArgumentException("No such GeographyId: " + geographyId.ToString()); } } } } static public BasicGeography GetGeographyByName (string geographyName) { string cmpName = geographyName.ToLower().Trim(); Dictionary> hashedGeographies = GetHashedGeographies(); foreach (int entry in hashedGeographies.Keys) { if (hashedGeographies[entry][0].Name.ToLower().Trim().StartsWith(cmpName)) return hashedGeographies[entry][0]; } //TODO: This can miss due to that the geography was added after last cache reload throw new ArgumentException("No such GeographyName: " + geographyName); } static public void SetGeographyName (int geographyId, string name) { PirateDb.GetDatabase().SetGeographyName(geographyId, name); BasicGeography geo = PirateDb.GetDatabase().GetGeography(geographyId); Dictionary> hashedGeographies = GetHashedGeographies(); hashedGeographies[geographyId][0] = geo; } /********************** * Might be handled.... public GeographyLevel[] GetGeographyLevelsAtGeographyId (int geographyId) { return PirateDb.GetDatabase().GetGeographyLevelsAtGeographyId(geographyId); } public int[] GetGeographyIdsFromLevel (int countryId, GeographyLevel level) { return PirateDb.GetDatabase().GetGeographyIdsFromLevel(countryId, level); } ************************/ } }