Esempio n. 1
0
        private async Task <SearchQueryHas?> ProccessHas(HasParameter Has, string RawHasParameter)
        {
            var Result = new SearchQueryHas();

            ResourceType?TargetResourceForSearchQuery = IResourceTypeSupport.GetTypeFromName(Has.TargetResourceForSearchQuery);

            if (TargetResourceForSearchQuery.HasValue && IKnownResource.IsKnownResource(this.FhirVersion, Has.TargetResourceForSearchQuery))
            {
                Result.TargetResourceForSearchQuery = TargetResourceForSearchQuery.Value;
            }
            else
            {
                Outcome !.InvalidSearchQueryList.Add(new InvalidSearchQueryParameter(RawHasParameter, $"The resource type name of: {Has.TargetResourceForSearchQuery} in a {FhirSearchQuery.TermHas} parameter could not be resolved to a resource type supported by this server for FHIR version {this.FhirVersion.GetCode()}."));
                return(null);
            }

            List <Bug.Logic.DomainModel.SearchParameter> SearchParameterList = await ISearchParameterCache.GetForIndexingAsync(this.FhirVersion, Result.TargetResourceForSearchQuery);

            Bug.Logic.DomainModel.SearchParameter BackReferenceSearchParameter = SearchParameterList.SingleOrDefault(x => x.Name == Has.BackReferenceSearchParameterName);
            if (BackReferenceSearchParameter is object && BackReferenceSearchParameter.SearchParamTypeId == SearchParamType.Reference)
            {
                Result.BackReferenceSearchParameter = BackReferenceSearchParameter;
            }
            else
            {
                if (BackReferenceSearchParameter is null)
                {
                    string Message = $"The reference search parameter back to the target resource type of: {Has.BackReferenceSearchParameterName} is not a supported search parameter for the resource type {this.ResourceContext.GetCode()} for FHIR version {this.FhirVersion.GetCode()} within this server.";
                    Outcome !.InvalidSearchQueryList.Add(new InvalidSearchQueryParameter(RawHasParameter, Message));
                    return(null);
                }
            }

            if (Has.ChildHasParameter is object)
            {
                Result.ChildSearchQueryHas = await ProccessHas(Has.ChildHasParameter, RawHasParameter);

                return(Result);
            }
            else
            {
                if (Has.SearchQuery.HasValue)
                {
                    SearchParameterList = await ISearchParameterCache.GetForIndexingAsync(this.FhirVersion, Result.TargetResourceForSearchQuery);

                    Bug.Logic.DomainModel.SearchParameter SearchParameter = SearchParameterList.SingleOrDefault(x => x.Name == Has.SearchQuery.Value.Key);
                    if (SearchParameter is object)
                    {
                        IList <ISearchQueryBase> SearchQueryBaseList = await ISearchQueryFactory.Create(this.ResourceContext, SearchParameter, Has.SearchQuery.Value);

                        if (SearchQueryBaseList.Count == 1)
                        {
                            if (SearchQueryBaseList[0].IsValid)
                            {
                                Result.SearchQuery = SearchQueryBaseList[0];
                                return(Result);
                            }
                            else
                            {
                                string Message = $"Error parsing the search parameter found at the end of a {FhirSearchQuery.TermHas} query. The search parameter name was : {Has.SearchQuery.Value.Key} with the value of {Has.SearchQuery.Value.Value}. " +
                                                 $"Additional information: {SearchQueryBaseList[0].InvalidMessage}";
                                Outcome !.InvalidSearchQueryList.Add(new InvalidSearchQueryParameter(RawHasParameter, Message));
                                return(null);
                            }
                        }
                        else
                        {
                            throw new ApplicationException($"The {FhirSearchQuery.TermHas} parameter seems to end with more then one search parameter, this should not be possible.");
                        }
                    }
                    else
                    {
                        string Message = $"The {FhirSearchQuery.TermHas} query finish with a search parameter: {Has.SearchQuery.Value.Key} for the resource type of: {Result.TargetResourceForSearchQuery.GetCode()}. " +
                                         $"However, the search parameter: {Has.SearchQuery.Value.Key} is not a supported search parameter for this resource type in this server for FHIR version {this.FhirVersion.GetCode()}.";
                        Outcome !.InvalidSearchQueryList.Add(new InvalidSearchQueryParameter(RawHasParameter, Message));
                        return(null);
                    }
                }
                else
                {
                    string Message = $"The {FhirSearchQuery.TermHas} query does not finish with a search parameter and value.";
                    Outcome !.InvalidSearchQueryList.Add(new InvalidSearchQueryParameter(RawHasParameter, Message));
                    return(null);
                }
            }
        }
Esempio n. 2
0
        public async Task <ChainQueryProcessingOutcome> Process(FhirVersion fhirVersion, ResourceType resourceContext, KeyValuePair <string, StringValues> Parameter)
        {
            this.FhirVersion     = fhirVersion;
            this.ResourceContext = resourceContext;

            var Outcome = new ChainQueryProcessingOutcome();

            this.RawParameter = $"{Parameter.Key}={Parameter.Value}";
            string[] ChaimedParameterSplit = Parameter.Key.Split(FhirSearchQuery.TermChainDelimiter);

            for (int i = 0; i < ChaimedParameterSplit.Length; i++)
            {
                //Each segment in the chain is a IsChainedReferance except the last segment in the chain which has the value.
                bool IsChainedReferance = !(i == ChaimedParameterSplit.Length - 1);
                Bug.Logic.DomainModel.SearchParameter?SearchParameter = null;
                string       ParameterNameWithModifier = Parameter.Key.Split(FhirSearchQuery.TermChainDelimiter)[i];
                StringValues ParameterValue            = string.Empty;
                //There is no valid Value for a chained reference parameter unless it is the last in a series
                //of chains, so don't set it unless this is the last parameter in the whole chain.
                if (i == ChaimedParameterSplit.Count() - 1)
                {
                    ParameterValue = Parameter.Value;
                }

                var SingleChainedParameter = new KeyValuePair <string, StringValues>(ParameterNameWithModifier, ParameterValue);

                string ParameterName = string.Empty;
                string ParameterModifierTypedResource = string.Empty;

                //Check for and deal with modifiers e.g 'Patient' in the example: subject:Patient.family=millar
                if (ParameterNameWithModifier.Contains(FhirSearchQuery.TermSearchModifierDelimiter))
                {
                    string[] ParameterModifierSplit = ParameterNameWithModifier.Split(FhirSearchQuery.TermSearchModifierDelimiter);
                    ParameterName = ParameterModifierSplit[0].Trim();

                    if (ParameterModifierSplit.Length > 1)
                    {
                        ResourceType?ModifierResourceType = IResourceTypeSupport.GetTypeFromName(ParameterModifierSplit[1].Trim());
                        if (ModifierResourceType.HasValue && IKnownResource.IsKnownResource(this.FhirVersion, ParameterModifierSplit[1].Trim()))
                        {
                            ParameterModifierTypedResource = ParameterModifierSplit[1].Trim();
                        }
                        else
                        {
                            ErrorInSearchParameterProcessing = true;
                            //If the Parent is ok then we can assume that any error further down the chain is an invalid search term rather than an unsupported term
                            //as it is clear that this is a FHIR search term and not some other search parameter forgen to FHIR
                            if (ParentChainSearchParameter is object)
                            {
                                InvalidSearchQueryParameterList.Add(new InvalidSearchQueryParameter(this.RawParameter, $"The resource type modifier: {ParameterModifierSplit[1].Trim()} within the chained search query of {this.RawParameter} is not a known resource for FHIR version: {this.FhirVersion.GetCode()} within this server"));
                            }
                            else
                            {
                                //Here we are only looking up the ParameterName to check weather this should be an unsupported parameter or an invalid parameter.
                                //If we know the ParameterName then it is invalid whereas if we don't then it is unsupported and both are not known.
                                List <Bug.Logic.DomainModel.SearchParameter> TempSearchParameterList = await ISearchParameterCache.GetForIndexingAsync(this.FhirVersion, this.ResourceContext);

                                var TempSearchParameter = TempSearchParameterList.SingleOrDefault(x => x.Name == ParameterName);
                                if (TempSearchParameter is null)
                                {
                                    string Message = $"Both the search parameter name: {ParameterName} for the resource type: {this.ResourceContext.GetCode()} and its resource type modifier: {ParameterModifierSplit[1].Trim()} within the chained search query of {this.RawParameter} are unsupported within this server for FHIR version: {this.FhirVersion.GetCode()}.";
                                    UnsupportedSearchQueryParameterList.Add(new InvalidSearchQueryParameter(this.RawParameter, Message));
                                }
                                else
                                {
                                    InvalidSearchQueryParameterList.Add(new InvalidSearchQueryParameter(this.RawParameter, $"The resource type modifier: {ParameterModifierSplit[1].Trim()} within the chained search query of {this.RawParameter} is not a known resource type for this server and FHIR version {this.FhirVersion.GetCode()}."));
                                }
                            }
                            break;
                        }
                    }
                }
                else
                {
                    ParameterName = ParameterNameWithModifier;
                }

                SearchParameter = await GetSearchParameter(ParameterName);

                //We have no resolved a SearchParameter so we parse the value if it is the end of the chain, see IsChainedReferance
                //or we are only parsing as a chain segment with no end value
                if (SearchParameter != null)
                {
                    //If this is the last parameter in the chain then treat is as not a chain, otherwise treat as a chain
                    await SetChain(SearchParameter, SingleChainedParameter, IsChainedReferance);
                }
                else
                {
                    ErrorInSearchParameterProcessing = true;
                    if (this.InvalidSearchQueryParameterList.Count == 0 && this.UnsupportedSearchQueryParameterList.Count == 0)
                    {
                        throw new ApplicationException("Internal Server Error: When processing a chain search query we failed to resolve a search parameter for the query string however their are " +
                                                       $"no items found in either the {nameof(InvalidSearchQueryParameterList)} or the {nameof(UnsupportedSearchQueryParameterList)}. This is an error in its self.");
                    }
                    break;
                }
            }

            //End of Chain loop
            if (!ErrorInSearchParameterProcessing)
            {
                if (ParentChainSearchParameter is object)
                {
                    Outcome !.SearchQueryList.Add(ParentChainSearchParameter);
                    return(Outcome);
                }
                else
                {
                    throw new NullReferenceException(nameof(PreviousChainSearchParameter));
                }
            }
            else
            {
                InvalidSearchQueryParameterList.ForEach(x => Outcome !.InvalidSearchQueryList.Add(x));
                UnsupportedSearchQueryParameterList.ForEach(x => Outcome !.UnsupportedSearchQueryList.Add(x));
                return(Outcome);
            }
        }
Esempio n. 3
0
        public async Task <IndexerOutcome> Process(Common.FhirTools.FhirResource fhirResource, Bug.Common.Enums.ResourceType resourceType)
        {
            var IndexerOutcome = new IndexerOutcome();

            List <SearchParameter> SearchParameterList = await ISearchParameterCache.GetForIndexingAsync(fhirResource.FhirVersion, resourceType);


            foreach (SearchParameter SearchParameter in SearchParameterList)
            {
                //Composite searchParameters do not require populating as they are a Composite of other SearchParameter Types
                //searchParameters with an empty or null FHIRPath can not be indexed, this is true for _query and _content which
                //in my view should not be searchParameter, just as _sort or _count are not.
                if (SearchParameter.SearchParamTypeId != Common.Enums.SearchParamType.Composite && !string.IsNullOrWhiteSpace(SearchParameter.FhirPath))
                {
                    IEnumerable <ITypedElement>?ResultList;
                    try
                    {
                        ResultList = ITypedElementSupport.Select(fhirResource, SearchParameter.FhirPath);
                    }
                    catch (Exception Exec)
                    {
                        throw new Bug.Common.Exceptions.FhirFatalException(System.Net.HttpStatusCode.InternalServerError, $"Error indexing the FhirPath select expression for the SearchParameter with the name of : {SearchParameter.Name} for a resource type of : {resourceType.GetCode()} with the SearchParameter database primary key of {SearchParameter.Id.ToString()}.  The FhirPath expression was : {SearchParameter.FhirPath}. See inner exception for more info.", Exec);
                    }

                    //This null check of ResultList is only here due to the exception issue above.
                    if (ResultList is object)
                    {
                        foreach (ITypedElement TypedElement in ResultList)
                        {
                            if (TypedElement != null)
                            {
                                switch (SearchParameter.SearchParamTypeId)
                                {
                                case Common.Enums.SearchParamType.Number:
                                    IndexerOutcome.QuantityIndexList.AddRange(INumberSetterSupport.Set(fhirResource.FhirVersion, TypedElement, resourceType, SearchParameter.Id, SearchParameter.Name));
                                    break;

                                case Common.Enums.SearchParamType.Date:
                                    IndexerOutcome.DateTimeIndexList.AddRange(IDateTimeSetterSupport.Set(fhirResource.FhirVersion, TypedElement, resourceType, SearchParameter.Id, SearchParameter.Name));

                                    break;

                                case Common.Enums.SearchParamType.String:
                                    IndexerOutcome.StringIndexList.AddRange(IStringSetterSupport.Set(fhirResource.FhirVersion, TypedElement, resourceType, SearchParameter.Id, SearchParameter.Name));
                                    break;

                                case Common.Enums.SearchParamType.Token:
                                    IndexerOutcome.TokenIndexList.AddRange(ITokenSetterSupport.Set(fhirResource.FhirVersion, TypedElement, resourceType, SearchParameter.Id, SearchParameter.Name));
                                    break;

                                case Common.Enums.SearchParamType.Reference:
                                    IndexerOutcome.ReferenceIndexList.AddRange(await IReferenceSetterSupport.SetAsync(fhirResource.FhirVersion, TypedElement, resourceType, SearchParameter.Id, SearchParameter.Name));
                                    break;

                                case Common.Enums.SearchParamType.Composite:
                                    //Composite searchParameters do not require populating as they are a Composite of other SearchParameter Types
                                    break;

                                case Common.Enums.SearchParamType.Quantity:
                                    IndexerOutcome.QuantityIndexList.AddRange(IQuantitySetterSupport.Set(fhirResource.FhirVersion, TypedElement, resourceType, SearchParameter.Id, SearchParameter.Name));
                                    break;

                                case Common.Enums.SearchParamType.Uri:
                                    IndexerOutcome.UriIndexList.AddRange(IUriSetterSupport.Set(fhirResource.FhirVersion, TypedElement, resourceType, SearchParameter.Id, SearchParameter.Name));
                                    break;

                                case Common.Enums.SearchParamType.Special:
                                    string SpecialMessage = $"Encountered a search parameter of type: {Common.Enums.SearchParamType.Special.GetCode()} which is not supported by the server. " +
                                                            $"The search parameter had the name of : {SearchParameter.Name} with a SearchParameter database primary key of {SearchParameter.Id.ToString()}. The " +
                                                            $"resource type being processed was of type : {resourceType.GetCode()}";
                                    this.ILogger.LogWarning(SpecialMessage);
                                    break;

                                default:
                                    throw new Bug.Common.Exceptions.FhirFatalException(System.Net.HttpStatusCode.InternalServerError, $"Internal Server Error: Encountered an unknown SearchParamType of type {SearchParameter.SearchParamTypeId.GetCode()}");
                                }
                            }
                        }
                    }
                }
            }
            return(IndexerOutcome);
        }