// TODO: Make cancellation token an input. public async Task <IReadOnlyCollection <ResourceSearchParameterStatus> > GetSearchParameterStatuses() { // If the search parameter table in SQL does not yet contain status columns if (_schemaInformation.Current < SchemaVersionConstants.SearchParameterStatusSchemaVersion) { // Get status information from file. return(await _filebasedSearchParameterStatusDataStore.GetSearchParameterStatuses()); } using (IScoped <SqlConnectionWrapperFactory> scopedSqlConnectionWrapperFactory = _scopedSqlConnectionWrapperFactory()) using (SqlConnectionWrapper sqlConnectionWrapper = await scopedSqlConnectionWrapperFactory.Value.ObtainSqlConnectionWrapperAsync(CancellationToken.None, true)) using (SqlCommandWrapper sqlCommandWrapper = sqlConnectionWrapper.CreateSqlCommand()) { VLatest.GetSearchParamStatuses.PopulateCommand(sqlCommandWrapper); var parameterStatuses = new List <ResourceSearchParameterStatus>(); using (SqlDataReader sqlDataReader = await sqlCommandWrapper.ExecuteReaderAsync(CommandBehavior.SequentialAccess, CancellationToken.None)) { while (await sqlDataReader.ReadAsync()) { (string uri, string stringStatus, DateTimeOffset? lastUpdated, bool?isPartiallySupported) = sqlDataReader.ReadRow( VLatest.SearchParam.Uri, VLatest.SearchParam.Status, VLatest.SearchParam.LastUpdated, VLatest.SearchParam.IsPartiallySupported); if (string.IsNullOrEmpty(stringStatus) || lastUpdated == null || isPartiallySupported == null) { // These columns are nullable because they are added to dbo.SearchParam in a later schema version. // They should be populated as soon as they are added to the table and should never be null. throw new NullReferenceException(Resources.SearchParameterStatusShouldNotBeNull); } var status = Enum.Parse <SearchParameterStatus>(stringStatus, true); var resourceSearchParameterStatus = new ResourceSearchParameterStatus() { Uri = new Uri(uri), Status = status, IsPartiallySupported = (bool)isPartiallySupported, LastUpdated = (DateTimeOffset)lastUpdated, }; if (SqlServerSortingValidator.SupportedParameterUris.Contains(resourceSearchParameterStatus.Uri)) { resourceSearchParameterStatus.SortStatus = SortParameterStatus.Enabled; } else { resourceSearchParameterStatus.SortStatus = SortParameterStatus.Supported; } parameterStatuses.Add(resourceSearchParameterStatus); } } return(parameterStatuses); } }
public static SearchParameterStatusWrapper ToSearchParameterStatusWrapper(this ResourceSearchParameterStatus status) { return(new SearchParameterStatusWrapper { Uri = status.Uri, Status = status.Status, LastUpdated = status.LastUpdated, IsPartiallySupported = status.IsPartiallySupported ? true : (bool?)null, }); }
public async Task GivenAStatusRegistry_WhenUpsertingExistingStatuses_ThenTheExistingStatusesAreUpdated() { IReadOnlyCollection <ResourceSearchParameterStatus> statusesBeforeUpdate = await _fixture.SearchParameterStatusDataStore.GetSearchParameterStatuses(CancellationToken.None); // Get two existing statuses. ResourceSearchParameterStatus expectedStatus1 = statusesBeforeUpdate.First(); ResourceSearchParameterStatus expectedStatus2 = statusesBeforeUpdate.Last(); // Modify them in some way. expectedStatus1.IsPartiallySupported = !expectedStatus1.IsPartiallySupported; expectedStatus2.IsPartiallySupported = !expectedStatus2.IsPartiallySupported; var statusesToUpsert = new List <ResourceSearchParameterStatus> { expectedStatus1, expectedStatus2 }; try { // Upsert the two existing, modified statuses. await _fixture.SearchParameterStatusDataStore.UpsertStatuses(statusesToUpsert, CancellationToken.None); IReadOnlyCollection <ResourceSearchParameterStatus> statusesAfterUpdate = await _fixture.SearchParameterStatusDataStore.GetSearchParameterStatuses(CancellationToken.None); Assert.Equal(statusesBeforeUpdate.Count, statusesAfterUpdate.Count); ResourceSearchParameterStatus actualStatus1 = statusesAfterUpdate.FirstOrDefault(s => s.Uri.Equals(expectedStatus1.Uri)); ResourceSearchParameterStatus actualStatus2 = statusesAfterUpdate.FirstOrDefault(s => s.Uri.Equals(expectedStatus2.Uri)); Assert.NotNull(actualStatus1); Assert.NotNull(actualStatus2); Assert.Equal(expectedStatus1.Status, actualStatus1.Status); Assert.Equal(expectedStatus1.IsPartiallySupported, actualStatus1.IsPartiallySupported); Assert.Equal(expectedStatus2.Status, actualStatus2.Status); Assert.Equal(expectedStatus2.IsPartiallySupported, actualStatus2.IsPartiallySupported); } finally { // Reset changes made. expectedStatus1.IsPartiallySupported = !expectedStatus1.IsPartiallySupported; expectedStatus2.IsPartiallySupported = !expectedStatus2.IsPartiallySupported; statusesToUpsert = new List <ResourceSearchParameterStatus> { expectedStatus1, expectedStatus2 }; await _fixture.SearchParameterStatusDataStore.UpsertStatuses(statusesToUpsert, CancellationToken.None); } }
public async Task GivenAStatusRegistry_WhenUpsertingNewStatuses_ThenTheStatusesAreAdded() { string statusName1 = "http://hl7.org/fhir/SearchParameter/Test-1"; string statusName2 = "http://hl7.org/fhir/SearchParameter/Test-2"; var status1 = new ResourceSearchParameterStatus { Uri = new Uri(statusName1), Status = SearchParameterStatus.Disabled, IsPartiallySupported = false, }; var status2 = new ResourceSearchParameterStatus { Uri = new Uri(statusName2), Status = SearchParameterStatus.Disabled, IsPartiallySupported = false, }; IReadOnlyCollection <ResourceSearchParameterStatus> readonlyStatusesBeforeUpsert = await _fixture.SearchParameterStatusDataStore.GetSearchParameterStatuses(CancellationToken.None); var expectedStatuses = readonlyStatusesBeforeUpsert.ToList(); expectedStatuses.Add(status1); expectedStatuses.Add(status2); var statusesToUpsert = new List <ResourceSearchParameterStatus> { status1, status2 }; try { await _fixture.SearchParameterStatusDataStore.UpsertStatuses(statusesToUpsert, CancellationToken.None); IReadOnlyCollection <ResourceSearchParameterStatus> actualStatuses = await _fixture.SearchParameterStatusDataStore.GetSearchParameterStatuses(CancellationToken.None); ValidateSearchParameterStatuses(expectedStatuses, actualStatuses); } finally { await _testHelper.DeleteSearchParameterStatusAsync(statusName1); await _testHelper.DeleteSearchParameterStatusAsync(statusName2); } }
public async Task <IReadOnlyCollection <ResourceSearchParameterStatus> > GetSearchParameterStatuses(CancellationToken cancellationToken) { // If the search parameter table in SQL does not yet contain status columns if (_schemaInformation.Current < SchemaVersionConstants.SearchParameterStatusSchemaVersion) { // Get status information from file. return(await _filebasedSearchParameterStatusDataStore.GetSearchParameterStatuses(cancellationToken)); } using (IScoped <SqlConnectionWrapperFactory> scopedSqlConnectionWrapperFactory = _scopedSqlConnectionWrapperFactory()) using (SqlConnectionWrapper sqlConnectionWrapper = await scopedSqlConnectionWrapperFactory.Value.ObtainSqlConnectionWrapperAsync(cancellationToken, true)) using (SqlCommandWrapper sqlCommandWrapper = sqlConnectionWrapper.CreateSqlCommand()) { VLatest.GetSearchParamStatuses.PopulateCommand(sqlCommandWrapper); var parameterStatuses = new List <ResourceSearchParameterStatus>(); using (SqlDataReader sqlDataReader = await sqlCommandWrapper.ExecuteReaderAsync(CommandBehavior.SequentialAccess, cancellationToken)) { while (await sqlDataReader.ReadAsync(cancellationToken)) { short id; string uri; string stringStatus; DateTimeOffset?lastUpdated; bool? isPartiallySupported; ResourceSearchParameterStatus resourceSearchParameterStatus; if (_schemaInformation.Current >= SchemaVersionConstants.SearchParameterSynchronizationVersion) { (id, uri, stringStatus, lastUpdated, isPartiallySupported) = sqlDataReader.ReadRow( VLatest.SearchParam.SearchParamId, VLatest.SearchParam.Uri, VLatest.SearchParam.Status, VLatest.SearchParam.LastUpdated, VLatest.SearchParam.IsPartiallySupported); if (string.IsNullOrEmpty(stringStatus) || lastUpdated == null || isPartiallySupported == null) { // These columns are nullable because they are added to dbo.SearchParam in a later schema version. // They should be populated as soon as they are added to the table and should never be null. throw new SearchParameterNotSupportedException(Resources.SearchParameterStatusShouldNotBeNull); } var status = Enum.Parse <SearchParameterStatus>(stringStatus, true); resourceSearchParameterStatus = new SqlServerResourceSearchParameterStatus { Id = id, Uri = new Uri(uri), Status = status, IsPartiallySupported = (bool)isPartiallySupported, LastUpdated = (DateTimeOffset)lastUpdated, }; } else { (uri, stringStatus, lastUpdated, isPartiallySupported) = sqlDataReader.ReadRow( VLatest.SearchParam.Uri, VLatest.SearchParam.Status, VLatest.SearchParam.LastUpdated, VLatest.SearchParam.IsPartiallySupported); if (string.IsNullOrEmpty(stringStatus) || lastUpdated == null || isPartiallySupported == null) { // These columns are nullable because they are added to dbo.SearchParam in a later schema version. // They should be populated as soon as they are added to the table and should never be null. throw new SearchParameterNotSupportedException(Resources.SearchParameterStatusShouldNotBeNull); } var status = Enum.Parse <SearchParameterStatus>(stringStatus, true); resourceSearchParameterStatus = new ResourceSearchParameterStatus { Uri = new Uri(uri), Status = status, IsPartiallySupported = (bool)isPartiallySupported, LastUpdated = (DateTimeOffset)lastUpdated, }; } if (_schemaInformation.Current >= SchemaVersionConstants.AddMinMaxForDateAndStringSearchParamVersion) { // For schema versions starting from AddMinMaxForDateAndStringSearchParamVersion we will check // whether the corresponding type of the search parameter is supported. SearchParameterInfo paramInfo = null; try { paramInfo = _searchParameterDefinitionManager.GetSearchParameter(resourceSearchParameterStatus.Uri.OriginalString); } catch (SearchParameterNotSupportedException) { } if (paramInfo != null && SqlServerSortingValidator.SupportedSortParamTypes.Contains(paramInfo.Type)) { resourceSearchParameterStatus.SortStatus = SortParameterStatus.Enabled; } else { resourceSearchParameterStatus.SortStatus = SortParameterStatus.Disabled; } } else { if (_sortingValidator.SupportedParameterUris.Contains(resourceSearchParameterStatus.Uri)) { resourceSearchParameterStatus.SortStatus = SortParameterStatus.Enabled; } else { resourceSearchParameterStatus.SortStatus = SortParameterStatus.Disabled; } } parameterStatuses.Add(resourceSearchParameterStatus); } } return(parameterStatuses); } }