public SearchParameterValidatorTests() { _searchParameterDefinitionManager.TryGetSearchParameter(Arg.Is <Uri>(uri => uri != new Uri("http://duplicate")), out _).Returns(false); _searchParameterDefinitionManager.TryGetSearchParameter(new Uri("http://duplicate"), out _).Returns(true); _searchParameterDefinitionManager.TryGetSearchParameter("Patient", Arg.Is <string>(code => code != "duplicate"), out _).Returns(false); _searchParameterDefinitionManager.TryGetSearchParameter("Patient", "duplicate", out _).Returns(true); _fhirOperationDataStore.CheckActiveReindexJobsAsync(CancellationToken.None).Returns((false, string.Empty)); }
public bool TryGetSearchParameter(string resourceType, string code, out SearchParameterInfo searchParameter) { searchParameter = null; if (_inner.TryGetSearchParameter(resourceType, code, out var parameter) && parameter.IsSupported) { searchParameter = parameter; return(true); } return(false); }
/// <summary> /// This method should be called periodically to get any updates to SearchParameters /// added to the DB by other service instances. /// It should also be called when a user starts a reindex job /// </summary> /// <param name="cancellationToken">Cancellation token</param> /// <returns>A task.</returns> public async Task GetAndApplySearchParameterUpdates(CancellationToken cancellationToken = default) { var updatedSearchParameterStatus = await _searchParameterStatusManager.GetSearchParameterStatusUpdates(cancellationToken); // first process any deletes, then we will do any adds or updates // this way any deleted params which might have the same code or name as a new // parameter will not cause conflicts foreach (var searchParam in updatedSearchParameterStatus.Where(p => p.Status == SearchParameterStatus.Deleted)) { DeleteSearchParameter(searchParam.Uri.OriginalString); } var paramsToAdd = new List <ITypedElement>(); foreach (var searchParam in updatedSearchParameterStatus.Where(p => p.Status != SearchParameterStatus.Deleted)) { var searchParamResource = await GetSearchParameterByUrl(searchParam.Uri.OriginalString, cancellationToken); if (searchParamResource == null) { _logger.LogInformation( "Updated SearchParameter status found for SearchParameter: {0}, but did not find any SearchParameter resources when querying for this url.", searchParam.Uri); continue; } if (_searchParameterDefinitionManager.TryGetSearchParameter(searchParam.Uri.OriginalString, out var existingSearchParam)) { // if the search parameter exists we should delete the old information currently stored DeleteSearchParameter(searchParam.Uri.OriginalString); } paramsToAdd.Add(searchParamResource); } // Now add the new or updated parameters to the SearchParameterDefinitionManager if (paramsToAdd.Any()) { _searchParameterDefinitionManager.AddNewSearchParameters(paramsToAdd); } // Once added to the definition manager we can update their status await _searchParameterStatusManager.ApplySearchParameterStatus( updatedSearchParameterStatus.Where(p => p.Status != SearchParameterStatus.Deleted).ToList(), cancellationToken); }
public async Task ValidateSearchParameterInput(SearchParameter searchParam, string method, CancellationToken cancellationToken) { if (await _authorizationService.CheckAccess(DataActions.Reindex, cancellationToken) != DataActions.Reindex) { throw new UnauthorizedFhirActionException(); } // check if reindex job is running using (IScoped <IFhirOperationDataStore> fhirOperationDataStore = _fhirOperationDataStoreFactory()) { (var activeReindexJobs, var reindexJobId) = await fhirOperationDataStore.Value.CheckActiveReindexJobsAsync(cancellationToken); if (activeReindexJobs) { throw new JobConflictException(string.Format(Resources.ChangesToSearchParametersNotAllowedWhileReindexing, reindexJobId)); } } var validationFailures = new List <ValidationFailure>(); if (string.IsNullOrEmpty(searchParam.Url)) { validationFailures.Add( new ValidationFailure(nameof(Base.TypeName), Resources.SearchParameterDefinitionInvalidMissingUri)); } else { try { // If a search parameter with the same uri exists already if (_searchParameterDefinitionManager.TryGetSearchParameter(new Uri(searchParam.Url), out _)) { // And if this is a request to create a new search parameter if (method.Equals(HttpPostName, StringComparison.OrdinalIgnoreCase)) { // We have a conflict validationFailures.Add( new ValidationFailure( nameof(searchParam.Url), string.Format(Resources.SearchParameterDefinitionDuplicatedEntry, searchParam.Url))); } else if (method.Equals(HttpPutName, StringComparison.OrdinalIgnoreCase)) { CheckForConflictingCodeValue(searchParam, validationFailures); } } else { // Otherwise, no search parameters with a matching uri exist // Ensure this isn't a request to modify an existing parameter if (method.Equals(HttpPutName, StringComparison.OrdinalIgnoreCase) || method.Equals(HttpDeleteName, StringComparison.OrdinalIgnoreCase)) { validationFailures.Add( new ValidationFailure( nameof(searchParam.Url), string.Format(Resources.SearchParameterDefinitionNotFound, searchParam.Url))); } CheckForConflictingCodeValue(searchParam, validationFailures); } } catch (FormatException) { validationFailures.Add( new ValidationFailure( nameof(searchParam.Url), string.Format(Resources.SearchParameterDefinitionInvalidDefinitionUri, searchParam.Url))); } } // validate that the url does not correspond to a search param from the spec // TODO: still need a method to determine spec defined search params // validation of the fhir path // TODO: separate user story for this validation if (validationFailures.Any()) { throw new ResourceNotValidException(validationFailures); } }