using System; using Swarmops.Logic.Structure; using System.Collections.Generic; using Swarmops.Basic.Enums; using Swarmops.Basic.Types; using Swarmops.Database; namespace Swarmops.Logic.Cache { public class MailTemplateCache { static private DateTime lastAccess = DateTime.MinValue; static private object loadCacheLock = new object(); static public bool loadCache = true; static private Dictionary> __MailTemplateCache = new Dictionary>(); static readonly int cacheLifeSpanMinutes = 1; static MailTemplateCache () { lastAccess = DateTime.MinValue; } static private List GetCachedTemplates (string templateName) { lock (loadCacheLock) { if (loadCache || lastAccess.AddMinutes(cacheLifeSpanMinutes) < DateTime.Now) { __MailTemplateCache = new Dictionary>(); loadCache = false; } if (!__MailTemplateCache.ContainsKey(templateName)) { BasicMailTemplate[] basicTemplates = SwarmDb.GetDatabaseForReading().GetMailTemplatesByName(templateName); List tmplList = new List(basicTemplates); __MailTemplateCache[templateName] = tmplList; } lastAccess = DateTime.Now; return __MailTemplateCache[templateName]; } } static public BasicMailTemplate GetBestMatch (string templateName, string language, string country, Organization org) { List tmplList = GetCachedTemplates(templateName); Organizations orgLine = ( org != null) ? org.GetLine() : new Organizations(); int[] lineIDs = orgLine.Identities; List idlist = new List(lineIDs); BasicMailTemplate templateDefault = null; BasicMailTemplate countryDefault = null; BasicMailTemplate bestSofar = null; int bestIndex = -1; foreach (BasicMailTemplate bmt in tmplList) { int thisIndex = idlist.IndexOf(bmt.OrganizationId); if (thisIndex > bestIndex) { bestIndex = thisIndex; bestSofar = bmt; } else if (bmt.CountryCode.ToUpper() == country && bmt.OrganizationId < 1) { countryDefault = bmt; } else if (bmt.CountryCode == "" && bmt.OrganizationId < 1) { templateDefault = bmt; } } if (bestSofar != null) return bestSofar; else if (countryDefault != null) return countryDefault; else if (templateDefault != null) return templateDefault; else return null; } } }