using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using NHibernate; using NHibernate.Linq; using Sleis.Models; using Sleis.Infrastructure; using Sleis.Utility; using System.Collections; using System.Text; using log4net; using Sleis.Data; using System.Reflection; using System.Threading.Tasks; using System.Linq.Expressions; namespace Sleis.Utility { public static class LookupUtility { private static readonly ILog Log = LogManager.GetLogger(typeof(LookupUtility)); private static readonly TimeSpan CacheDuration; private static readonly IPropertyPlaceholder Properties; static LookupUtility() { CacheDuration = SpringServiceProvider.GetService("lokupCacheDuration"); Properties = SpringServiceProvider.GetService("deploymentVars"); } public static void Init() { IPropertyPlaceholder props = Properties; bool precacheLookups = false; //TODO: Attempting fix here. Seems as if cache scheduler is getting here at times before this class is instantiated. if(props == null) props = SpringServiceProvider.GetService("deploymentVars"); Boolean.TryParse(props.Get("lookup.cache"), out precacheLookups); if (precacheLookups) { Task[] tasks = new Task[23] { Task.Factory.StartNew(() => Get()), Task.Factory.StartNew(() => Get()), Task.Factory.StartNew(() => Get()), Task.Factory.StartNew(() => Get()), Task.Factory.StartNew(() => Get()), Task.Factory.StartNew(() => Get()), Task.Factory.StartNew(() => Get()), Task.Factory.StartNew(() => Get()), Task.Factory.StartNew(() => Get()), Task.Factory.StartNew(() => Get()), Task.Factory.StartNew(() => Get()), Task.Factory.StartNew(() => Get()), Task.Factory.StartNew(() => Get()), Task.Factory.StartNew(() => Get()), Task.Factory.StartNew(() => Get()), Task.Factory.StartNew(() => Get()), Task.Factory.StartNew(() => Get()), Task.Factory.StartNew(() => Get()), Task.Factory.StartNew(() => Get()), Task.Factory.StartNew(() => Get()), Task.Factory.StartNew(() => Get()), Task.Factory.StartNew(() => Get()), Task.Factory.StartNew(() => Get()), }; } } public static string[] Reload() { return CacheUtility.Clear(); } public static List Reload() where T : EpaBaseObject { string typeName = typeof(T).Name; CacheUtility.Remove(typeName); return Get(); } public static List Get() where T : EpaBaseObject { string typeName = typeof(T).Name; List list = CacheUtility.Get>(typeName); if (list == null || list.Count < 1) { using (ISession session = SessionFactoryProvider.SessionFactory.OpenSession()) { try { Log.InfoFormat("{0} was not in cache. Loading...", typeName); list = (from d in session.Query() orderby d.Description select new SimpleLookupItem(d)).ToList(); } catch (QueryException) { Log.InfoFormat("{0} does not contain descrption field. Trying query again and odering by code...", typeName); list = (from d in session.Query() orderby d.Code select new SimpleLookupItem(d)).ToList(); } } CacheUtility.Add(typeName, list, CacheDuration, false); } return list; //(d.Epa == null) ? 0 : d.Epa.Year } public static List Get(Expression> predicate) where T : EpaBaseObject, new() { string typeName = typeof(T).Name; List list = CacheUtility.Get>(typeName); //this is causing issues do to caching as SimpleLookupItem. We can't filter predicate on list since T is EpaBaseObject //would be best to re-do caching strategy with simplelookupItems...for now, will just query DB //if(list==null || list.Count<1) //{ using (ISession session = SessionFactoryProvider.SessionFactory.OpenSession()) { try { Log.InfoFormat("{0} was not in cache. Loading...", typeName); list = session.Query().Where(predicate).OrderBy(d=>d.Description).Select(x=>new SimpleLookupItem(x)).ToList(); } catch (QueryException) { Log.InfoFormat("{0} does not contain descrption field. Trying query again and odering by code...", typeName); list = session.Query().Where(predicate).OrderBy(d => d.Code).Select(x => new SimpleLookupItem(x)).ToList(); } } CacheUtility.Add(typeName, list, CacheDuration, false); //} return list; //(d.Epa == null) ? 0 : d.Epa.Year } public static SimpleLookupItem Get(string code) where T : EpaBaseObject { string typeName = typeof(T).Name; SimpleLookupItem result = new SimpleLookupItem(); List list = CacheUtility.Get>(typeName); if (list != null && list.Count > 0) { result = list.SingleOrDefault(x => x.Code == code); } if (result == null || String.IsNullOrWhiteSpace(result.Code)) { return GetNotFromCache(code); } return result; } //will be removed later public static SimpleLookupItem GetNotFromCache(string code) where T : EpaBaseObject { using(ISession session = SessionFactoryProvider.SessionFactory.OpenSession()) { return (from d in session.Query() where d.Code == code orderby d.Description select new SimpleLookupItem(d)).FirstOrDefault(); } } public static List Get(int year) where T : EpaBaseObject { return Get().Where(x => x.Year == 0 || x.Year >= year).ToList(); } //help create the dropdown list public static List Get(string code, int year) where T : EpaBaseObject { //if master inventory, make sure we default year to today's date. if (year <= 0) { year = DateTime.Now.Year; } List list = Get().Where(x => x.Year == 0 || x.Year >= year).ToList(); if(list.Find(x=>x.Code == code)==null) { SimpleLookupItem item = Get(code); if(item!=null) { list.Add(item); } } return list; } public static List GetLike(string type, string code, int maxMatchSize, int year = 0) { List list = CacheUtility.Get>(type); if (list == null || list.Count < 1) { //a little bit of reflection to get the list loaded and into cache Type lookupUtility = typeof(LookupUtility); MethodInfo mi = lookupUtility.GetMethod("Get", new Type[]{}); // Assign the Lookup type to the type parameter of the Get Method on this class. Type lookupType = Type.GetType(String.Format("Sleis.Models.{0}", type), true, true); MethodInfo getMethod = mi.MakeGenericMethod(lookupType); list = (List)getMethod.Invoke(null, null); } //if master inventory, make sure we default year to today's date. if (year <= 0) { year = DateTime.Now.Year; } if (list != null) { IEnumerable resultList = null; if (year > 0) { //Keep the year as not nullable to not have to check for null resultList = list.Where(x => x.Contains(code) && (x.Year == 0 || x.Year >= year)); } else { resultList = list.Where(x => x.Contains(code)); } list = resultList.Take(maxMatchSize).ToList(); } else { Log.ErrorFormat("Lookup type not mapped: {0}", type); list = new List(); } return list; } public static List GetEmissionCalculationMethods() { string typeName = typeof(EpaEmissionCalculationMethod).Name; return LookupUtility.Get(); /*if (list == null || list.Count < 1) { using (ISession session = SessionFactoryProvider.SessionFactory.OpenSession()) { list = (from d in session.Query() select (d)).ToList(); } CacheUtility.Add(typeName, list, CacheDuration, false); } return list.ToList(); * */ } } }