protected static void DefaultOptions(ref RFCatalogOptions options)
 {
     if (options == null)
     {
         options = new RFCatalogOptions();
     }
 }
        public T LoadDocumentContent <T>(RFCatalogKey key, RFCatalogOptions options = null) where T : class
        {
            var document = (LoadEntry(key, options) as RFDocument);

            if (document != null && document.IsValid)
            {
                return(document.GetContent <T>());
            }
            return(null);
        }
        /*public static RFGraphInstance DeriveFrom(RFInstruction i)
         * {
         *  if (i is RFGraphProcessInstruction)
         *  {
         *      var gi = (i as RFGraphProcessInstruction).Instance;
         *      if (gi != null && !gi.IsNull())
         *      {
         *          return gi;
         *      }
         *  }
         *
         *  if (i is RFParamProcessInstruction)
         *  {
         *      var p = (i as RFParamProcessInstruction).Param;
         *      if (p is RFEngineProcessorGraphInstanceParam)
         *      {
         *          var gi = (p as RFEngineProcessorGraphInstanceParam).Instance;
         *          if (gi != null && !gi.IsNull())
         *          {
         *              return gi;
         *          }
         *      }
         *  }
         *
         *  if (i.Event != null && i.Event is RFCatalogUpdateEvent)
         *  {
         *      var gi = (i.Event as RFCatalogUpdateEvent).Key?.GraphInstance;
         *      if (gi != null && !gi.IsNull())
         *      {
         *          return gi;
         *      }
         *  }
         *
         *  return null;
         * }*/

        public static RFCatalogOptions ImplyOptions(RFGraphIOMapping ioMapping /*, PropertyInfo propertyInfo*/)
        {
            if (ioMapping == null)
            {
                throw new RFSystemException(typeof(RFGraphInstance), "Missing IOMapping");
            }
            var options = new RFCatalogOptions();

            /*var dateBehaviour = ioMapping.DateBehaviour; propertyInfo.GetCustomAttributes(typeof(RFDateBehaviourAttribute), true).FirstOrDefault() as RFDateBehaviourAttribute;
             * if (dateBehaviour != null)
             * {
             *  options.DateBehaviour = dateBehaviour.DateBehaviour;
             * }*/
            options.DateBehaviour = ioMapping.DateBehaviour;
            return(options);
        }
        public RFCatalogEntry LoadEntry(RFCatalogKey key, RFCatalogOptions options = null)
        {
            DefaultOptions(ref options);
            if (key is RFCatalogKey)
            {
                lock (_memoryStore)
                {
                    if (_memoryStore.ContainsKey(key.ToString()))
                    {
                        return(_memoryStore[key.ToString()]);
                    }
                }

                switch (options.DateBehaviour)
                {
                case RFDateBehaviour.NotSet:
                case RFDateBehaviour.Exact:
                {
                    var item = _catalog.LoadItem(key as RFCatalogKey, options.Version, options.IgnoreContent);
                    return((item != null && item.IsValid) ? item : null);
                }

                case RFDateBehaviour.Dateless:
                {
                    var keyToLoad = key.CreateForInstance(new RFGraphInstance
                        {
                            Name      = key.GraphInstance.Name,
                            ValueDate = null
                        });
                    var item = _catalog.LoadItem(keyToLoad, options.Version, options.IgnoreContent);
                    return((item != null && item.IsValid) ? item : null);
                }

                case RFDateBehaviour.Latest:
                case RFDateBehaviour.Previous:
                {
                    if (key.GraphInstance == null || !key.GraphInstance.ValueDate.HasValue)
                    {
                        throw new RFSystemException(this, "Unable to load latest date for key without date {0}", key);
                    }
                    var allKeys        = _catalog.GetKeyInstances(key);
                    var candidateDates = new SortedSet <RFDate>();

                    foreach (var candidateKey in allKeys.Where(k => k.Key.Name == key.GraphInstance.Name))
                    {
                        if (candidateKey.Value.GraphInstance.ValueDate.Value <= key.GraphInstance.ValueDate.Value)
                        {
                            if ((options.DateBehaviour == RFDateBehaviour.Latest) ||
                                (options.DateBehaviour == RFDateBehaviour.Previous && candidateKey.Value.GraphInstance.ValueDate.Value < key.GraphInstance.ValueDate.Value))
                            {
                                candidateDates.Add(candidateKey.Value.GraphInstance.ValueDate.Value);
                            }
                        }
                    }
                    if (candidateDates.Count == 0)
                    {
                        SystemLog.Warning(this, "No latest date instance item found for key {0}", key);
                        return(null);
                    }
                    foreach (var latestDate in candidateDates.OrderByDescending(d => d))
                    {
                        var keyToLoad = key.CreateForInstance(new RFGraphInstance
                            {
                                Name      = key.GraphInstance.Name,
                                ValueDate = latestDate
                            });
                        var item = _catalog.LoadItem(keyToLoad, options.Version, options.IgnoreContent);
                        if (item != null && item.IsValid)
                        {
                            return(item);
                        }
                    }
                    return(null);
                }

                default:
                    throw new RFSystemException(this, "Unsupported date behaviour in LoadEntry: {0}", options.DateBehaviour);
                }
            }
            else
            {
                throw new RFSystemException(this, "Unknown store key type {0}", key.ToString());
            }
        }