/// <summary>
        /// Get the specified object
        /// </summary>
        internal override TModel Get(DataContext context, Guid key)
        {
            // We need to join, but to what?
            // True to get the cache item
            var cacheService = new AdoPersistenceCache(context);
            var cacheItem    = cacheService?.GetCacheItem <TModel>(key) as TModel;

            if (cacheItem != null && context.Transaction == null)
            {
                if (cacheItem.LoadState < context.LoadState)
                {
                    cacheItem.LoadAssociations(context);
                    cacheService?.Add(cacheItem);
                }
                return(cacheItem);
            }
            else
            {
                var domainQuery = this.m_settingsProvider.GetQueryBuilder().CreateQuery <TModel>(o => o.Key == key && o.ObsoletionTime == null).Build();
                domainQuery.OrderBy <TRootEntity>(o => o.VersionSequenceId, Core.Model.Map.SortOrderType.OrderByDescending);
                cacheItem = this.ToModelInstance(context.FirstOrDefault <TQueryReturn>(domainQuery), context);
                if (cacheService != null)
                {
                    cacheService.Add(cacheItem);
                }
                return(cacheItem);
            }
        }
        /// <summary>
        /// Perform a version aware get
        /// </summary>
        internal override TModel Get(DataContext context, Guid key)
        {
            // Attempt to get a cahce item
            var cacheService = new AdoPersistenceCache(context);
            var retVal       = cacheService.GetCacheItem <TModel>(key);

            if (retVal != null)
            {
                return(retVal);
            }
            else
            {
                var domainQuery = context.CreateSqlStatement <TDomain>().SelectFrom(typeof(TDomain), typeof(TDomainKey))
                                  .InnerJoin <TDomain, TDomainKey>(o => o.Key, o => o.Key)
                                  .Where <TDomain>(o => o.Key == key)
                                  .OrderBy <TDomain>(o => o.VersionSequenceId, Core.Model.Map.SortOrderType.OrderByDescending);

                // Is the most recent version obsolete? If so, un-obsolete it
                var recentVersion = context.FirstOrDefault <CompositeResult <TDomain, TDomainKey> >(domainQuery);
                if (recentVersion?.Object1.ObsoletionTime != null && !context.IsReadonly)
                {
                    this.m_tracer.TraceWarning("Object {0} # {1} has no active versions - Possible BUG - Restoring previous version", recentVersion.GetType().FullName, recentVersion.Object2?.Key);
                    recentVersion.Object1.ObsoletionTime          = null;
                    recentVersion.Object1.ObsoletionTimeSpecified = true;
                    recentVersion.Object1.ObsoletedByKey          = null;
                    recentVersion.Object1.ObsoletedByKeySpecified = true;
                    recentVersion = context.Update(recentVersion);
                }

                return(this.CacheConvert(recentVersion, context));
            }
        }
Esempio n. 3
0
        /// <summary>
        /// Perform a version aware get
        /// </summary>
        internal override TModel Get(DataContext context, Guid key)
        {
            // Attempt to get a cahce item
            var cacheService = new AdoPersistenceCache(context);
            var retVal       = cacheService.GetCacheItem <TModel>(key);

            if (retVal != null)
            {
                return(retVal);
            }
            else
            {
                var domainQuery = context.CreateSqlStatement <TDomain>().SelectFrom(typeof(TDomain), typeof(TDomainKey))
                                  .InnerJoin <TDomain, TDomainKey>(o => o.Key, o => o.Key)
                                  .Where <TDomain>(o => o.Key == key && o.ObsoletionTime == null)
                                  .OrderBy <TDomain>(o => o.VersionSequenceId, Core.Model.Map.SortOrderType.OrderByDescending);
                return(this.CacheConvert(context.FirstOrDefault <CompositeResult <TDomain, TDomainKey> >(domainQuery), context));
            }
        }
Esempio n. 4
0
        /// <summary>
        /// Tru to load from cache
        /// </summary>
        protected virtual TModel CacheConvert(Object o, DataContext context)
        {
            if (o == null)
            {
                return(null);
            }

            var cacheService = new AdoPersistenceCache(context);

            var idData  = (o as CompositeResult)?.Values.OfType <IDbIdentified>().FirstOrDefault() ?? o as IDbIdentified;
            var objData = (o as CompositeResult)?.Values.OfType <IDbBaseData>().FirstOrDefault() ?? o as IDbBaseData;

            if (objData?.ObsoletionTime != null || idData == null || idData.Key == Guid.Empty)
            {
                return(this.ToModelInstance(o, context));
            }
            else
            {
                var cacheItem = cacheService?.GetCacheItem <TModel>(idData?.Key ?? Guid.Empty);
                if (cacheItem != null)
                {
                    if (cacheItem.LoadState < context.LoadState)
                    {
                        cacheItem.LoadAssociations(context);
                        cacheService?.Add(cacheItem);
                    }
                    return(cacheItem);
                }
                else
                {
                    cacheItem = this.ToModelInstance(o, context);
                    if (context.Transaction == null)
                    {
                        cacheService?.Add(cacheItem);
                    }
                }
                return(cacheItem);
            }
        }
Esempio n. 5
0
        /// <summary>
        /// Override cache conversion
        /// </summary>
        protected override Act CacheConvert(object dataInstance, DataContext context)
        {
            if (dataInstance == null)
            {
                return(null);
            }
            DbActVersion dbActVersion = (dataInstance as CompositeResult)?.Values.OfType <DbActVersion>().FirstOrDefault() ?? dataInstance as DbActVersion ?? context.FirstOrDefault <DbActVersion>(o => o.VersionKey == (dataInstance as DbActSubTable).ParentKey);
            DbAct        dbAct        = (dataInstance as CompositeResult)?.Values.OfType <DbAct>().FirstOrDefault() ?? context.FirstOrDefault <DbAct>(o => o.Key == dbActVersion.Key);
            Act          retVal       = null;
            var          cache        = new AdoPersistenceCache(context);

            if (!dbActVersion.ObsoletionTime.HasValue)
            {
                switch (dbAct.ClassConceptKey.ToString().ToUpper())
                {
                case ActClassKeyStrings.ControlAct:
                    retVal = cache?.GetCacheItem <ControlAct>(dbAct.Key);
                    break;

                case ActClassKeyStrings.SubstanceAdministration:
                    retVal = cache?.GetCacheItem <SubstanceAdministration>(dbAct.Key);
                    break;

                case ActClassKeyStrings.Observation:
                    var dbObs = (dataInstance as CompositeResult)?.Values.OfType <DbObservation>().FirstOrDefault() ?? context.FirstOrDefault <DbObservation>(o => o.ParentKey == dbActVersion.VersionKey);
                    if (dbObs != null)
                    {
                        switch (dbObs.ValueType)
                        {
                        case "ST":
                            retVal = cache?.GetCacheItem <TextObservation>(dbAct.Key);
                            break;

                        case "CD":
                            retVal = cache?.GetCacheItem <CodedObservation>(dbAct.Key);
                            break;

                        case "PQ":
                            retVal = cache?.GetCacheItem <QuantityObservation>(dbAct.Key);
                            break;
                        }
                    }
                    break;

                case ActClassKeyStrings.Encounter:
                    retVal = cache?.GetCacheItem <PatientEncounter>(dbAct.Key);
                    break;

                case ActClassKeyStrings.Condition:
                default:
                    retVal = cache?.GetCacheItem <Act>(dbAct.Key);
                    break;
                }
            }

            // Return cache value
            if (retVal != null)
            {
                return(retVal);
            }
            else
            {
                return(base.CacheConvert(dataInstance, context));
            }
        }
        /// <summary>
        /// Perform the cache convert
        /// </summary>
        internal Entity DoCacheConvert(object dataInstance, DataContext context)
        {
            if (dataInstance == null)
            {
                return(null);
            }
            // Alright first, which type am I mapping to?
            var    dbEntityVersion = (dataInstance as CompositeResult)?.Values.OfType <DbEntityVersion>().FirstOrDefault() ?? dataInstance as DbEntityVersion ?? context.FirstOrDefault <DbEntityVersion>(o => o.VersionKey == (dataInstance as DbEntitySubTable).ParentKey);
            var    dbEntity        = (dataInstance as CompositeResult)?.Values.OfType <DbEntity>().FirstOrDefault() ?? context.FirstOrDefault <DbEntity>(o => o.Key == dbEntityVersion.Key);
            Entity retVal          = null;
            var    cache           = new AdoPersistenceCache(context);

            if (!dbEntityVersion.ObsoletionTime.HasValue)
            {
                switch (dbEntity.ClassConceptKey.ToString().ToUpper())
                {
                case EntityClassKeyStrings.Device:
                    retVal = cache?.GetCacheItem <DeviceEntity>(dbEntity.Key);
                    break;

                case EntityClassKeyStrings.NonLivingSubject:
                    retVal = cache?.GetCacheItem <ApplicationEntity>(dbEntity.Key);
                    break;

                case EntityClassKeyStrings.Person:
                    var ue = (dataInstance as CompositeResult)?.Values.OfType <DbUserEntity>().FirstOrDefault() ?? context.FirstOrDefault <DbUserEntity>(o => o.ParentKey == dbEntityVersion.VersionKey);
                    if (ue != null)
                    {
                        retVal = cache?.GetCacheItem <UserEntity>(dbEntity.Key);
                    }

                    else
                    {
                        retVal = cache?.GetCacheItem <Person>(dbEntity.Key);
                    }
                    break;

                case EntityClassKeyStrings.Patient:
                    retVal = cache?.GetCacheItem <Patient>(dbEntity.Key);
                    break;

                case EntityClassKeyStrings.Provider:
                    retVal = cache?.GetCacheItem <Provider>(dbEntity.Key);

                    break;

                case EntityClassKeyStrings.Place:
                case EntityClassKeyStrings.CityOrTown:
                case EntityClassKeyStrings.Country:
                case EntityClassKeyStrings.CountyOrParish:
                case EntityClassKeyStrings.State:
                case EntityClassKeyStrings.ServiceDeliveryLocation:
                    retVal = cache?.GetCacheItem <Place>(dbEntity.Key);

                    break;

                case EntityClassKeyStrings.Organization:
                    retVal = cache?.GetCacheItem <Organization>(dbEntity.Key);

                    break;

                case EntityClassKeyStrings.Material:
                    retVal = cache?.GetCacheItem <Material>(dbEntity.Key);

                    break;

                case EntityClassKeyStrings.ManufacturedMaterial:
                    retVal = cache?.GetCacheItem <ManufacturedMaterial>(dbEntity.Key);

                    break;

                default:
                    retVal = cache?.GetCacheItem <Entity>(dbEntity.Key);
                    break;
                }
            }

            // Return cache value
            if (retVal != null)
            {
                return(retVal);
            }
            else
            {
                return(base.CacheConvert(dataInstance, context));
            }
        }