private static bool IsServiceUrlPrimary(IDtoRootUrlStore PrimaryRootUrlStore, SearchParameterReferanceValue SearchValue)
 {
     if (!string.IsNullOrWhiteSpace(SearchValue.FhirRequestUri.UriPrimaryServiceRoot.OriginalString))
     {
         if (!SearchValue.FhirRequestUri.UriPrimaryServiceRoot.OriginalString.IsEqualUri(PrimaryRootUrlStore.Url))
         {
             return(false);
         }
     }
     return(true);
 }
Esempio n. 2
0
        public IDtoRootUrlStore GetPrimaryRootUrlStore()
        {
            IDtoRootUrlStore DtoRootUrlStore = null;
            _ServiceBaseUrl  oServiceBaseUrl = GetPrimaryPyro_RootUrlStore();

            if (oServiceBaseUrl != null)
            {
                DtoRootUrlStore     = IDtoRootUrlStoreFactory.CreateDtoRootUrlStore();
                DtoRootUrlStore.Id  = oServiceBaseUrl.Id;
                DtoRootUrlStore.Url = oServiceBaseUrl.Url;
                DtoRootUrlStore.IsServersPrimaryUrlRoot = oServiceBaseUrl.IsServersPrimaryUrlRoot;
            }
            return(DtoRootUrlStore);
        }
        /// <summary>
        /// Gets the ServiceBaseUrl Instance if found or creates a new instance if not found
        /// </summary>
        /// <param name="UrlString"></param>
        /// <returns></returns>
        public IDtoRootUrlStore GetAndOrAddService_RootUrlStore(string ServiceRootUrl)
        {
            IDtoRootUrlStore Pyro_RootUrlStore = this.GetPyro_RootUrlStore(ServiceRootUrl);

            if (Pyro_RootUrlStore == null)
            {
                var Pyro_RootUrlStoreDb = new _ServiceBaseUrl();
                Pyro_RootUrlStoreDb.IsServersPrimaryUrlRoot = false;
                Pyro_RootUrlStoreDb.Url = StringSupport.ToLowerFast(ServiceRootUrl.StripHttp());
                Pyro_RootUrlStoreDb     = IPyroDbContext.Set <_ServiceBaseUrl>().Add(Pyro_RootUrlStoreDb);
                this.Save();
                return(Pyro_RootUrlStoreDb);
            }
            else
            {
                return(Pyro_RootUrlStore);
            }
        }
        public IDtoRootUrlStore SetPrimaryRootUrlStore(string RootUrl)
        {
            IDtoRootUrlStore ExsistingPrimaryRootURL = this.GetPrimaryPyro_RootUrlStore();

            if (ExsistingPrimaryRootURL != null)
            {
                ExsistingPrimaryRootURL.IsServersPrimaryUrlRoot = false;
            }
            IDtoRootUrlStore ExsistingNonPrimaryRootURL = this.GetPyro_RootUrlStore(RootUrl);

            if (ExsistingNonPrimaryRootURL != null)
            {
                ExsistingNonPrimaryRootURL.IsServersPrimaryUrlRoot = true;
            }
            else
            {
                _ServiceBaseUrl Pyro_RootUrlStore = new _ServiceBaseUrl();
                Pyro_RootUrlStore.IsServersPrimaryUrlRoot = true;
                Pyro_RootUrlStore.Url = StringSupport.ToLowerFast(RootUrl.StripHttp());
                IPyroDbContext.Set <_ServiceBaseUrl>().Add(Pyro_RootUrlStore);
            }
            this.Save();
            return(this.GetPrimaryRootUrlStore());
        }
        public static ExpressionStarter <ResCurrentType> Build(ResourceSearchExpressionTrees <ResCurrentType, ResIndexStringType, ResIndexTokenType, ResIndexUriType, ResIndexReferenceType, ResIndexQuantityType, ResIndexDateTimeType> Search, ExpressionStarter <ResCurrentType> NewPredicate, SearchParameterBase SearchItem, IDtoRootUrlStore PrimaryRootUrlStore)
        {
            if (SearchItem is SearchParameterReferance SearchTypeReference)
            {
                //Improved Query when searching for FhirIds for the same Resource type and search parameter yet different FhirIds.
                //It creates a SQL 'IN' cause instead of many 'OR' statements and should be more efficient.
                //Heavily used in chain searching where we traverse many References.
                //The 'Type' modifier is already resolved when the search parameter is parsed, so the SearchValue.FhirRequestUri.ResourseName is the correct Resource name at this stage
                if (SearchTypeReference.ValueList.Count > 1 && SearchTypeReference.ValueList.TrueForAll(x =>
                                                                                                        x.FhirRequestUri.IsRelativeToServer &&
                                                                                                        x.FhirRequestUri.ResourseName == SearchTypeReference.ValueList[0].FhirRequestUri.ResourseName &&
                                                                                                        string.IsNullOrWhiteSpace(x.FhirRequestUri.VersionId)))
                {
                    string[] ReferenceFhirIdArray = SearchTypeReference.ValueList.Select(x => x.FhirRequestUri.ResourceId).ToArray();
                    NewPredicate = NewPredicate.Or(Search.ReferanceCollectionAnyEqualTo_ByKey_Many_FhirIds(SearchTypeReference.Id, PrimaryRootUrlStore.Id, SearchTypeReference.ValueList[0].FhirRequestUri.ResourseName, ReferenceFhirIdArray, SearchTypeReference.ValueList[0].FhirRequestUri.VersionId));
                    return(NewPredicate);
                }

                foreach (var SearchValue in SearchTypeReference.ValueList)
                {
                    if (!SearchTypeReference.Modifier.HasValue)
                    {
                        if (IsServiceUrlPrimary(PrimaryRootUrlStore, SearchValue))
                        {
                            NewPredicate = NewPredicate.Or(Search.ReferanceCollectionAnyEqualTo_ByKey(SearchTypeReference.Id, PrimaryRootUrlStore.Id, SearchValue.FhirRequestUri.ResourseName, SearchValue.FhirRequestUri.ResourceId, SearchValue.FhirRequestUri.VersionId));
                        }
                        else
                        {
                            NewPredicate = NewPredicate.Or(Search.ReferanceCollectionAnyEqualTo_ByUrlString(SearchTypeReference.Id, SearchValue.FhirRequestUri.UriPrimaryServiceRoot.OriginalString.StripHttp().ToLower(), SearchValue.FhirRequestUri.ResourseName, SearchValue.FhirRequestUri.ResourceId, SearchValue.FhirRequestUri.VersionId));
                        }
                    }
                    else
                    {
                        switch (SearchTypeReference.Modifier)
                        {
                        case SearchParameter.SearchModifierCode.Missing:
                            if (SearchValue.IsMissing)
                            {
                                NewPredicate = NewPredicate.Or(Search.ReferanceCollectionIsNull(SearchTypeReference.Id));
                            }
                            else
                            {
                                NewPredicate = NewPredicate.Or(Search.ReferanceCollectionIsNotNull(SearchTypeReference.Id));
                            }
                            break;

                        case SearchParameter.SearchModifierCode.Exact:
                            throw new FormatException($"The search modifier: {SearchTypeReference.Modifier.ToString()} is not supported for search parameter types of Reference.");

                        case SearchParameter.SearchModifierCode.Contains:
                            throw new FormatException($"The search modifier: {SearchTypeReference.Modifier.ToString()} is not supported for search parameter types of Reference.");

                        case SearchParameter.SearchModifierCode.Text:
                            throw new FormatException($"The search modifier: {SearchTypeReference.Modifier.ToString()} is not supported for search parameter types of Reference.");

                        case SearchParameter.SearchModifierCode.Type:
                            //The 'Type' modifier is already resolved when the search parameter is parsed, so the SearchValue.FhirRequestUri.ResourseName is the correct Resource name at this stage
                            if (IsServiceUrlPrimary(PrimaryRootUrlStore, SearchValue))
                            {
                                NewPredicate = NewPredicate.Or(Search.ReferanceCollectionAnyEqualTo_ByKey(SearchTypeReference.Id, PrimaryRootUrlStore.Id, SearchValue.FhirRequestUri.ResourseName, SearchValue.FhirRequestUri.ResourceId, SearchValue.FhirRequestUri.VersionId));
                            }
                            else
                            {
                                NewPredicate = NewPredicate.Or(Search.ReferanceCollectionAnyEqualTo_ByUrlString(SearchTypeReference.Id, SearchValue.FhirRequestUri.UriPrimaryServiceRoot.OriginalString.StripHttp().ToLower(), SearchValue.FhirRequestUri.ResourseName, SearchValue.FhirRequestUri.ResourceId, SearchValue.FhirRequestUri.VersionId));
                            }
                            break;

                        case SearchParameter.SearchModifierCode.Below:
                            throw new FormatException($"The search modifier: {SearchTypeReference.Modifier.ToString()} is not supported for search parameter types of Reference.");

                        case SearchParameter.SearchModifierCode.Above:
                            throw new FormatException($"The search modifier: {SearchTypeReference.Modifier.ToString()} is not supported for search parameter types of Reference.");

                        case SearchParameter.SearchModifierCode.In:
                            throw new FormatException($"The search modifier: {SearchTypeReference.Modifier.ToString()} is not supported for search parameter types of Reference.");

                        case SearchParameter.SearchModifierCode.NotIn:
                            throw new FormatException($"The search modifier: {SearchTypeReference.Modifier.ToString()} is not supported for search parameter types of Reference.");

                        default:
                            throw new System.ComponentModel.InvalidEnumArgumentException(SearchTypeReference.Modifier.ToString(), (int)SearchTypeReference.Modifier, typeof(SearchParameter.SearchModifierCode));
                        }
                    }
                }
            }
            return(NewPredicate);
        }
Esempio n. 6
0
        public IDtoRootUrlStore Validate(string RequestServiceRoot)
        {
            string RequestRoot = RequestServiceRoot.ToLower();
            string ErrorMsg    = "Error message not set in PrimaryServiceRootFactory";

            string           WebConfigServiceBase    = IPrimaryServiceRootCache.GetPrimaryRootUrlFromWebConfig().ToLower();
            IDtoRootUrlStore IDtoPrimaryRootUrlStore = IPrimaryServiceRootCache.GetPrimaryRootUrlStoreFromDatabase();

            if (IDtoPrimaryRootUrlStore != null &&
                RequestRoot.IsEqualUri(IDtoPrimaryRootUrlStore.Url) &&
                RequestRoot.IsEqualUri(WebConfigServiceBase))
            {
                //All checks a good return
                return(IDtoPrimaryRootUrlStore);
            }

            if (IDtoPrimaryRootUrlStore == null &&
                RequestRoot.IsEqualUri(WebConfigServiceBase))
            {
                //Clean install.
                //There is no primary in the database / Cache.
                //If the Web.Config ServiceBaseURL equals the incoming request Service Base URL
                //Therefore set the database's primary service root URL, as this is a clean install.
                ILog.Info($"Clean install detected. As the first request's Service root is equal to the ServiceBaseURL found in the App_Data\\PyroApp.config this will be set in the database for future requests. ServiceBaseURL is : {IPrimaryServiceRootCache.GetPrimaryRootUrlFromWebConfig()} ");
                IDtoRootUrlStore DtoRootUrlStore = IServicePrimaryBaseUrlService.SetPrimaryRootUrlStore(WebConfigServiceBase.StripHttp().ToLower());
                //Clear the cache as we just added the ServiceRoot to the database as the cache will have null cached.
                IPrimaryServiceRootCache.ClearPrimaryRootUrlFromCache();
                IPrimaryServiceRootCache.ClearPrimaryRootUrlStoreFromCache();
                return(DtoRootUrlStore);
            }

            if (IDtoPrimaryRootUrlStore != null &&
                RequestRoot.IsEqualUri(WebConfigServiceBase) &&
                !RequestRoot.IsEqualUri(IDtoPrimaryRootUrlStore.Url))
            {
                //Web.Config Changed
                //The incoming request Service Base URL equals the Web.Config entry yet does not equal the Service Base URL
                //found in the database. We will assume someone knows what they are doing if they are capable of changing the Web.Config setting.
                //So update the database primary Service Base URL to match the Web.Config and request
                ILog.Info($"The incoming request's Service Base URL equals the App_Data\\PyroApp.config file entry 'ServiceBaseURL' yet does not equal the Primary Service Base URL found in the database. The server will assume someone knows what they are doing if they are capable of changing the App_Data\\PyroApp.config setting and will update the database primary Service Base URL to match the App_Data\\PyroApp.config and request. ");
                //Clear the cache as we just added the ServiceRoot to the database as the cache will have null cached.
                IPrimaryServiceRootCache.ClearPrimaryRootUrlFromCache();
                IPrimaryServiceRootCache.ClearPrimaryRootUrlStoreFromCache();
                IDtoRootUrlStore DtoRootUrlStore = IServicePrimaryBaseUrlService.SetPrimaryRootUrlStore(WebConfigServiceBase.StripHttp().ToLower());
                return(DtoRootUrlStore);
            }

            if (IDtoPrimaryRootUrlStore == null &&
                !RequestRoot.IsEqualUri(WebConfigServiceBase))
            {
                //Clean Install yet request does not match Web.Config file
                //There is no primary in the database / Cache and the incoming request Service Base URL
                //does not match that in the Web.Config file.
                //This is a configuration error.
                ErrorMsg = "The ServiceBaseURL configured in the Servers Web.Config does not match the service root URL found in the incoming request.";
                throw new PyroException(System.Net.HttpStatusCode.InternalServerError, Common.Tools.FhirOperationOutcomeSupport.Create(OperationOutcome.IssueSeverity.Fatal, OperationOutcome.IssueType.Exception, ErrorMsg), ErrorMsg);
            }

            if (IDtoPrimaryRootUrlStore != null &&
                (!RequestRoot.IsEqualUri(WebConfigServiceBase) ||
                 !RequestRoot.IsEqualUri(IDtoPrimaryRootUrlStore.Url)))
            {
                //Existing server moved and Web.Config file not updated to match the move.
                //The incoming request Service Base URL does not equal the Web.Config entry or the Database
                //Warn the user about the ramifications of this change if they should seek to make the change to the Web.Config file
                ErrorMsg =
                    $"The incoming Http request had a service root URL of: '{RequestServiceRoot.StripHttp()}'. " +
                    $"The server's database service root URL was '{IDtoPrimaryRootUrlStore.Url}'. " +
                    $"The servers PyroApp.config file service root URL was '{WebConfigServiceBase.StripHttp()}'. " +
                    "All three URLs must match for the server to continue! " +
                    "This is most likely due to the server being move from it's original URL location, or the PyroApp.config value being incorrect. " +
                    "You need to consider carefully the ramifications of or actions. you take next. " +
                    "External systems may have absolute references to FHIR resources in this server " +
                    "and changing the primary service root URL may render these external references invalid. " +
                    "What can you do?. In the servers App_Data\\PyroApp.config appSetttings is a Key names 'ServiceBaseURL'. You can update this to " +
                    $"the new service root URL in use. If you change this to equal '{RequestServiceRoot}' the server " +
                    "will use this going forward as the service root URL for new resource. Yet all absolute references outside the database " +
                    "will become invalid. " +
                    "This change is not to be taken lightly and you should consider the ramifications carefully in the context of the " +
                    "systems that interact with this service. ";
                throw new PyroException(System.Net.HttpStatusCode.InternalServerError, Common.Tools.FhirOperationOutcomeSupport.Create(OperationOutcome.IssueSeverity.Fatal, OperationOutcome.IssueType.Exception, ErrorMsg), ErrorMsg);
            }

            ErrorMsg = "Server Internal Error: Logic to resolve the Primary Service Root has failed. ";
            throw new PyroException(System.Net.HttpStatusCode.InternalServerError, Common.Tools.FhirOperationOutcomeSupport.Create(OperationOutcome.IssueSeverity.Fatal, OperationOutcome.IssueType.Exception, ErrorMsg), ErrorMsg);
        }