/// <summary> /// Attempts to retrieve any stored data from the document cache. /// </summary> /// <param name="type"></param> /// <param name="key"></param> /// <param name="nav"></param> /// <returns></returns> protected virtual bool TryGetCachedData(string type, string key, out XPathDataCacheItem item) { object value; Document doc = this.Document; if (null == doc) { throw new NullReferenceException("This data source is not part of the document heirarchy and cannot use the caching capabilities available"); } IPDFCacheProvider cacheProv = doc.CacheProvider; if (null != cacheProv) { if (cacheProv.TryRetrieveFromCache(type, key, out value)) { if (!(value is XPathDataCacheItem)) { throw new InvalidCastException("The data returned from the cache for this XPath datasource '" + this.ID + "' was not an XPathNavigator."); } item = (XPathDataCacheItem)value; return(true); } } item = null; return(false); }
protected System.Data.DataSet CreateSet() { if (null == this.LoadedData) { this.LoadedData = new XPathDataCacheItem(new System.Data.DataSet(this.ID), null); return(this.LoadedData.DataSet); } else { return(this.LoadedData.DataSet); } }
/// <summary> /// Adds the XPath document to the documents cache provider. /// </summary> /// <param name="type"></param> /// <param name="key"></param> /// <param name="nav"></param> protected virtual void AddToCache(string type, string key, XPathDataCacheItem item) { Document doc = this.Document; if (null == doc) { throw new NullReferenceException("This data source is not part of the document heirarchy and cannot use the caching capabilities available"); } IPDFCacheProvider cacheProv = doc.CacheProvider; if (null != cacheProv) { DateTime expires = DateTime.Now.AddMinutes(this.CacheDuration); cacheProv.AddToCache(type, key, item, expires); } }
/// <summary> /// Overrides the base implementation to load the actual data at the path /// </summary> /// <param name="path"></param> /// <param name="context"></param> /// <param name="root">If true then this is the top level selection of data and should use this sources assigned data. If false then it should use the current data context.</param> /// <returns></returns> protected override object DoSelectData(string path, PDFDataContext context) { //Get sthe source data from the implementation string key = this.GetCacheKey(context); XPathDataCacheItem item = null; if (null != this.LoadedData) { item = this.LoadedData; } else if (this.CacheDuration <= 0 || !this.TryGetCachedData(this.Type.ToString(), key, out item)) { if (context.ShouldLogVerbose) { context.TraceLog.Begin(TraceLevel.Verbose, LOG_CATEGORY, "Directly loading the data for datasource component '" + this.ID + "' with cache key '" + key + "'"); } //Load the actual source data. item = LoadSourceXPathData(context); this.LoadedData = item; //Quick fall-out (don't cache it, as not a normal situation) if (null == item) { context.TraceLog.Add(TraceLevel.Warning, LOG_CATEGORY, "No Data was returned from the Datasource '" + this.ID + "'."); return(null); } //Transform as required XPathNavigator nav = item.Navigator; if (null != this.Transformer) { nav = this.Transformer.TransformData(item.Navigator, this.CacheDuration, this, context); } if (this.CacheDuration > 0) { this.AddToCache(this.Type.ToString(), key, item); if (context.ShouldLogVerbose) { context.TraceLog.End(TraceLevel.Verbose, LOG_CATEGORY, "Completed loading the data for datasource component '" + this.ID + "' and added it to the cache with key '" + key + "'"); } else if (context.ShouldLogMessage) { context.TraceLog.Add(TraceLevel.Message, LOG_CATEGORY, "Completed loading the data for datasource component '" + this.ID + "' and added it to the cache with key '" + key + "'"); } } else if (context.ShouldLogVerbose) { context.TraceLog.End(TraceLevel.Verbose, LOG_CATEGORY, "Completed loading the data for datasource component '" + this.ID + "'. Not cached"); } else if (context.ShouldLogMessage) { context.TraceLog.Add(TraceLevel.Message, LOG_CATEGORY, "Completed loading the data for datasource component '" + this.ID + "'. Not cached"); } } else //cache hit { this.LoadedData = item; if (context.ShouldLogMessage) { context.TraceLog.Add(TraceLevel.Message, LOG_CATEGORY, "Cache hit on datasource '" + this.ID + "' with cache key '" + key + "'"); } } //We have the data //Now we need a resolver for the prefixes and select statements. if (null == this.Resolver) { this.Resolver = this.Document.CreateNamespaceResolver(this.Namespaces, item.Navigator.NameTable); } //Push this onto the context context.NamespaceResolver = this.Resolver; XPathNodeIterator itter; //Finally return the select to return an itterator if (string.IsNullOrEmpty(path)) { itter = item.Navigator.Select("*", context.NamespaceResolver); if (itter.Count == 0) { context.TraceLog.Add(TraceLevel.Warning, LOG_CATEGORY, "ZERO items were retrieved from the Datasource '" + this.ID + "'"); } } else { itter = item.Navigator.Select(path, context.NamespaceResolver); if (itter.Count == 0) { context.TraceLog.Add(TraceLevel.Warning, LOG_CATEGORY, "ZERO items were retrieved from the Datasource '" + this.ID + "' based on the select path '" + path + "'"); } } return(itter); }