-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
Signed-off-by: Lillie Dae <[email protected]>
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
/* | ||
* Copyright 2022 MONAI Consortium | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.ComponentModel.DataAnnotations; | ||
using System.ComponentModel.DataAnnotations.Schema; | ||
using FellowOakDicom; | ||
using Newtonsoft.Json; | ||
|
||
namespace Monai.Deploy.InformaticsGateway.Api | ||
{ | ||
public class Hl7ApplicationConfigEntity : MongoDBEntityBase | ||
{ | ||
/// <summary> | ||
/// Gets or sets the sending identifier. | ||
/// </summary> | ||
[JsonProperty("sending_identifier")] | ||
public KeyValuePair<string, string> SendingId { get; set; } | ||
|
||
/// <summary> | ||
/// Gets or sets the data link. | ||
/// Value is either PatientId or StudyInstanceUid | ||
/// </summary> | ||
[JsonProperty("data_link")] | ||
public KeyValuePair<string, string> DataLink { get; set; } | ||
|
||
/// <summary> | ||
/// Gets or sets the data mapping. | ||
/// Value is a DICOM Tag | ||
/// </summary> | ||
[JsonProperty("data_mapping")] | ||
public KeyValuePair<string, string> DataMapping { get; set; } | ||
|
||
public IEnumerable<string> Validate() | ||
{ | ||
var errors = new List<string>(); | ||
if (string.IsNullOrWhiteSpace(SendingId.Key)) | ||
errors.Add($"{nameof(SendingId.Key)} is missing."); | ||
if (string.IsNullOrWhiteSpace(SendingId.Value)) | ||
errors.Add($"{nameof(SendingId.Value)} is missing."); | ||
if (string.IsNullOrWhiteSpace(DataLink.Key)) | ||
errors.Add($"{nameof(DataLink.Key)} is missing."); | ||
if (string.IsNullOrWhiteSpace(DataLink.Value)) | ||
errors.Add($"{nameof(DataLink.Value)} is missing."); | ||
if (string.IsNullOrWhiteSpace(DataMapping.Key)) | ||
errors.Add($"{nameof(DataMapping.Key)} is missing."); | ||
if (string.IsNullOrWhiteSpace(DataMapping.Value)) | ||
errors.Add($"{nameof(DataMapping.Value)} is missing."); | ||
|
||
if (DataMapping.Value.Length < 8) | ||
{ | ||
errors.Add($"{nameof(DataMapping.Value)} is not a valid DICOM Tag."); | ||
return errors; | ||
} | ||
|
||
try | ||
{ | ||
DicomTag.Parse(DataMapping.Value); | ||
} | ||
catch (Exception e) | ||
{ | ||
errors.Add($"DataMapping.Value is not a valid DICOM Tag. {e.Message}"); | ||
throw; | ||
} | ||
|
||
return errors; | ||
} | ||
|
||
public override string ToString() | ||
{ | ||
return JsonConvert.SerializeObject(this); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
* Copyright 2023 MONAI Consortium | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
using Monai.Deploy.InformaticsGateway.Api; | ||
|
||
namespace Monai.Deploy.InformaticsGateway.Database.Api.Repositories | ||
{ | ||
public interface IHl7ApplicationConfigRepository | ||
{ | ||
Task<List<Hl7ApplicationConfigEntity>> GetAllAsync(CancellationToken cancellationToken = default); | ||
|
||
Task<Hl7ApplicationConfigEntity?> GetByIdAsync(string id); | ||
|
||
Task<Hl7ApplicationConfigEntity> DeleteAsync(string id, CancellationToken cancellationToken = default); | ||
|
||
Task<Hl7ApplicationConfigEntity> CreateAsync(Hl7ApplicationConfigEntity configEntity, | ||
CancellationToken cancellationToken = default); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.EntityFrameworkCore.Metadata.Builders; | ||
using Monai.Deploy.InformaticsGateway.Api; | ||
|
||
namespace Monai.Deploy.InformaticsGateway.Database.EntityFramework.Configuration | ||
{ | ||
internal class Hl7ApplicationConfigConfiguration : IEntityTypeConfiguration<Hl7ApplicationConfigEntity> | ||
{ | ||
public void Configure(EntityTypeBuilder<Hl7ApplicationConfigEntity> builder) | ||
{ | ||
builder.HasKey(j => j.Id); | ||
builder.Property(j => j.DataLink.Key).IsRequired(); | ||
builder.Property(j => j.DataLink.Value).IsRequired(); | ||
builder.Property(j => j.DataMapping.Key).IsRequired(); | ||
builder.Property(j => j.DataMapping.Value).IsRequired(); | ||
builder.Property(j => j.SendingId.Key).IsRequired(); | ||
builder.Property(j => j.SendingId.Value).IsRequired(); | ||
|
||
builder.Ignore(p => p.Id); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/* | ||
* Copyright 2023 MONAI Consortium | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
using Ardalis.GuardClauses; | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Logging; | ||
using Microsoft.Extensions.Options; | ||
using Monai.Deploy.InformaticsGateway.Api; | ||
using Monai.Deploy.InformaticsGateway.Configuration; | ||
using Monai.Deploy.InformaticsGateway.Database.Api.Logging; | ||
using Monai.Deploy.InformaticsGateway.Database.Api.Repositories; | ||
using Polly; | ||
using Polly.Retry; | ||
|
||
namespace Monai.Deploy.InformaticsGateway.Database.EntityFramework.Repositories | ||
{ | ||
public class Hl7ApplicationConfigRepository : IHl7ApplicationConfigRepository | ||
{ | ||
private readonly ILogger<Hl7ApplicationConfigRepository> _logger; | ||
private readonly InformaticsGatewayContext _informaticsGatewayContext; | ||
private readonly AsyncRetryPolicy _retryPolicy; | ||
private readonly DbSet<Hl7ApplicationConfigEntity> _dataset; | ||
|
||
public Hl7ApplicationConfigRepository(ILogger<Hl7ApplicationConfigRepository> logger, | ||
IOptions<DatabaseOptions> options, IServiceScopeFactory serviceScopeFactory) | ||
{ | ||
Guard.Against.Null(serviceScopeFactory, nameof(serviceScopeFactory)); | ||
Guard.Against.Null(options, nameof(options)); | ||
|
||
_logger = logger ?? throw new ArgumentNullException(nameof(logger)); | ||
|
||
var scope = serviceScopeFactory.CreateScope(); | ||
|
||
_informaticsGatewayContext = scope.ServiceProvider.GetRequiredService<InformaticsGatewayContext>(); | ||
_retryPolicy = Policy.Handle<Exception>().WaitAndRetryAsync( | ||
options.Value.Retries.RetryDelays, | ||
(exception, timespan, count, context) => _logger.DatabaseErrorRetry(timespan, count, exception)); | ||
_dataset = _informaticsGatewayContext.Set<Hl7ApplicationConfigEntity>(); | ||
} | ||
|
||
public Task<List<Hl7ApplicationConfigEntity>> GetAllAsync(CancellationToken cancellationToken = default) => | ||
_retryPolicy.ExecuteAsync(() => { return _dataset.ToListAsync(cancellationToken); }); | ||
|
||
public Task<Hl7ApplicationConfigEntity?> GetByIdAsync(string id) => | ||
_retryPolicy.ExecuteAsync(() => _dataset.FirstOrDefaultAsync(x => x.Id.Equals(id))); | ||
|
||
public Task<Hl7ApplicationConfigEntity> DeleteAsync(string id, CancellationToken cancellationToken) | ||
Check warning on line 61 in src/Database/EntityFramework/Repositories/Hl7ApplicationConfigRepository.cs GitHub Actions / unit-test
|
||
{ | ||
return _retryPolicy.ExecuteAsync(async () => | ||
{ | ||
var entity = await GetByIdAsync(id).ConfigureAwait(false); | ||
var result = _dataset.Remove(entity); | ||
Check warning on line 66 in src/Database/EntityFramework/Repositories/Hl7ApplicationConfigRepository.cs GitHub Actions / integration-test (DicomWebExport, ef)
Check warning on line 66 in src/Database/EntityFramework/Repositories/Hl7ApplicationConfigRepository.cs GitHub Actions / integration-test (DicomWebExport, ef)
Check warning on line 66 in src/Database/EntityFramework/Repositories/Hl7ApplicationConfigRepository.cs GitHub Actions / integration-test (HealthLevel7, ef)
Check warning on line 66 in src/Database/EntityFramework/Repositories/Hl7ApplicationConfigRepository.cs GitHub Actions / integration-test (HealthLevel7, ef)
Check warning on line 66 in src/Database/EntityFramework/Repositories/Hl7ApplicationConfigRepository.cs GitHub Actions / integration-test (DicomDimseScu, ef)
Check warning on line 66 in src/Database/EntityFramework/Repositories/Hl7ApplicationConfigRepository.cs GitHub Actions / integration-test (DicomDimseScu, ef)
Check warning on line 66 in src/Database/EntityFramework/Repositories/Hl7ApplicationConfigRepository.cs GitHub Actions / integration-test (DicomWebStow, ef)
Check warning on line 66 in src/Database/EntityFramework/Repositories/Hl7ApplicationConfigRepository.cs GitHub Actions / integration-test (DicomWebStow, ef)
Check warning on line 66 in src/Database/EntityFramework/Repositories/Hl7ApplicationConfigRepository.cs GitHub Actions / integration-test (AcrApi, ef)
Check warning on line 66 in src/Database/EntityFramework/Repositories/Hl7ApplicationConfigRepository.cs GitHub Actions / integration-test (AcrApi, ef)
Check warning on line 66 in src/Database/EntityFramework/Repositories/Hl7ApplicationConfigRepository.cs GitHub Actions / integration-test (DicomDimseScp, ef)
Check warning on line 66 in src/Database/EntityFramework/Repositories/Hl7ApplicationConfigRepository.cs GitHub Actions / integration-test (DicomDimseScp, ef)
Check warning on line 66 in src/Database/EntityFramework/Repositories/Hl7ApplicationConfigRepository.cs GitHub Actions / integration-test (Fhir, ef)
Check warning on line 66 in src/Database/EntityFramework/Repositories/Hl7ApplicationConfigRepository.cs GitHub Actions / integration-test (Fhir, ef)
Check warning on line 66 in src/Database/EntityFramework/Repositories/Hl7ApplicationConfigRepository.cs GitHub Actions / integration-test (RemoteAppExecutionPlugIn, ef)
Check warning on line 66 in src/Database/EntityFramework/Repositories/Hl7ApplicationConfigRepository.cs GitHub Actions / integration-test (RemoteAppExecutionPlugIn, ef)
Check warning on line 66 in src/Database/EntityFramework/Repositories/Hl7ApplicationConfigRepository.cs GitHub Actions / integration-test (RemoteAppExecutionPlugIn, mongodb)
Check warning on line 66 in src/Database/EntityFramework/Repositories/Hl7ApplicationConfigRepository.cs GitHub Actions / integration-test (RemoteAppExecutionPlugIn, mongodb)
Check warning on line 66 in src/Database/EntityFramework/Repositories/Hl7ApplicationConfigRepository.cs GitHub Actions / integration-test (DicomWebExport, mongodb)
Check warning on line 66 in src/Database/EntityFramework/Repositories/Hl7ApplicationConfigRepository.cs GitHub Actions / integration-test (DicomWebExport, mongodb)
Check warning on line 66 in src/Database/EntityFramework/Repositories/Hl7ApplicationConfigRepository.cs GitHub Actions / integration-test (AcrApi, mongodb)
Check warning on line 66 in src/Database/EntityFramework/Repositories/Hl7ApplicationConfigRepository.cs GitHub Actions / integration-test (AcrApi, mongodb)
Check warning on line 66 in src/Database/EntityFramework/Repositories/Hl7ApplicationConfigRepository.cs GitHub Actions / integration-test (DicomDimseScu, mongodb)
Check warning on line 66 in src/Database/EntityFramework/Repositories/Hl7ApplicationConfigRepository.cs GitHub Actions / integration-test (DicomDimseScu, mongodb)
Check warning on line 66 in src/Database/EntityFramework/Repositories/Hl7ApplicationConfigRepository.cs GitHub Actions / integration-test (Fhir, mongodb)
Check warning on line 66 in src/Database/EntityFramework/Repositories/Hl7ApplicationConfigRepository.cs GitHub Actions / integration-test (Fhir, mongodb)
Check warning on line 66 in src/Database/EntityFramework/Repositories/Hl7ApplicationConfigRepository.cs GitHub Actions / integration-test (DicomDimseScp, mongodb)
Check warning on line 66 in src/Database/EntityFramework/Repositories/Hl7ApplicationConfigRepository.cs GitHub Actions / integration-test (DicomDimseScp, mongodb)
Check warning on line 66 in src/Database/EntityFramework/Repositories/Hl7ApplicationConfigRepository.cs GitHub Actions / build (ubuntu-latest)
Check warning on line 66 in src/Database/EntityFramework/Repositories/Hl7ApplicationConfigRepository.cs GitHub Actions / build (ubuntu-latest)
Check warning on line 66 in src/Database/EntityFramework/Repositories/Hl7ApplicationConfigRepository.cs GitHub Actions / build (windows-latest)
Check warning on line 66 in src/Database/EntityFramework/Repositories/Hl7ApplicationConfigRepository.cs GitHub Actions / build (windows-latest)
Check warning on line 66 in src/Database/EntityFramework/Repositories/Hl7ApplicationConfigRepository.cs GitHub Actions / unit-test
Check warning on line 66 in src/Database/EntityFramework/Repositories/Hl7ApplicationConfigRepository.cs GitHub Actions / unit-test
Check warning on line 66 in src/Database/EntityFramework/Repositories/Hl7ApplicationConfigRepository.cs GitHub Actions / integration-test (HealthLevel7, mongodb)
Check warning on line 66 in src/Database/EntityFramework/Repositories/Hl7ApplicationConfigRepository.cs GitHub Actions / integration-test (HealthLevel7, mongodb)
Check warning on line 66 in src/Database/EntityFramework/Repositories/Hl7ApplicationConfigRepository.cs GitHub Actions / integration-test (DicomWebStow, mongodb)
Check warning on line 66 in src/Database/EntityFramework/Repositories/Hl7ApplicationConfigRepository.cs GitHub Actions / integration-test (DicomWebStow, mongodb)
Check warning on line 66 in src/Database/EntityFramework/Repositories/Hl7ApplicationConfigRepository.cs GitHub Actions / docs
Check warning on line 66 in src/Database/EntityFramework/Repositories/Hl7ApplicationConfigRepository.cs GitHub Actions / docs
Check warning on line 66 in src/Database/EntityFramework/Repositories/Hl7ApplicationConfigRepository.cs GitHub Actions / analyze
Check warning on line 66 in src/Database/EntityFramework/Repositories/Hl7ApplicationConfigRepository.cs GitHub Actions / analyze
Check warning on line 66 in src/Database/EntityFramework/Repositories/Hl7ApplicationConfigRepository.cs GitHub Actions / CodeQL-Analyze
Check warning on line 66 in src/Database/EntityFramework/Repositories/Hl7ApplicationConfigRepository.cs GitHub Actions / CodeQL-Analyze
|
||
await _informaticsGatewayContext.SaveChangesAsync(cancellationToken).ConfigureAwait(false); | ||
return result.Entity; | ||
}); | ||
} | ||
|
||
public Task<Hl7ApplicationConfigEntity> CreateAsync(Hl7ApplicationConfigEntity configEntity, | ||
CancellationToken cancellationToken = default) | ||
{ | ||
return _retryPolicy.ExecuteAsync(async () => | ||
{ | ||
var result = await _dataset.AddAsync(configEntity, cancellationToken).ConfigureAwait(false); | ||
await _informaticsGatewayContext.SaveChangesAsync(cancellationToken).ConfigureAwait(false); | ||
return result.Entity; | ||
}); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
/* | ||
* Copyright 2023 MONAI Consortium | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
using Ardalis.GuardClauses; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Logging; | ||
using Microsoft.Extensions.Options; | ||
using Monai.Deploy.InformaticsGateway.Api; | ||
using Monai.Deploy.InformaticsGateway.Configuration; | ||
using Monai.Deploy.InformaticsGateway.Database.Api; | ||
using Monai.Deploy.InformaticsGateway.Database.Api.Logging; | ||
using Monai.Deploy.InformaticsGateway.Database.Api.Repositories; | ||
using MongoDB.Driver; | ||
using Polly; | ||
using Polly.Retry; | ||
|
||
namespace Monai.Deploy.InformaticsGateway.Database.MongoDB.Repositories | ||
{ | ||
public class Hl7ApplicationConfigRepository : IHl7ApplicationConfigRepository, IDisposable | ||
{ | ||
private readonly ILogger<Hl7ApplicationConfigRepository> _logger; | ||
private readonly IServiceScope _scope; | ||
private readonly AsyncRetryPolicy _retryPolicy; | ||
private readonly IMongoCollection<Hl7ApplicationConfigEntity> _collection; | ||
private bool _disposedValue; | ||
|
||
public Hl7ApplicationConfigRepository(IServiceScopeFactory serviceScopeFactory, | ||
ILogger<Hl7ApplicationConfigRepository> logger, | ||
IOptions<DatabaseOptions> options) | ||
{ | ||
Guard.Against.Null(serviceScopeFactory, nameof(serviceScopeFactory)); | ||
Guard.Against.Null(options, nameof(options)); | ||
|
||
_logger = logger ?? throw new ArgumentNullException(nameof(logger)); | ||
|
||
_scope = serviceScopeFactory.CreateScope(); | ||
_retryPolicy = Policy.Handle<Exception>().WaitAndRetryAsync( | ||
options.Value.Retries.RetryDelays, | ||
(exception, timespan, count, context) => _logger.DatabaseErrorRetry(timespan, count, exception)); | ||
|
||
var mongoDbClient = _scope.ServiceProvider.GetRequiredService<IMongoClient>(); | ||
var mongoDatabase = mongoDbClient.GetDatabase(options.Value.DatabaseName); | ||
_collection = mongoDatabase.GetCollection<Hl7ApplicationConfigEntity>(nameof(Hl7ApplicationConfigEntity)); | ||
CreateIndexes(); | ||
} | ||
|
||
private void CreateIndexes() | ||
{ | ||
var options = new CreateIndexOptions { Unique = true }; | ||
|
||
var indexDefinition = Builders<Hl7ApplicationConfigEntity>.IndexKeys | ||
.Ascending(_ => _.DateTimeCreated); | ||
_collection.Indexes.CreateOne(new CreateIndexModel<Hl7ApplicationConfigEntity>(indexDefinition, options)); | ||
} | ||
|
||
public Task<List<Hl7ApplicationConfigEntity>> GetAllAsync(CancellationToken cancellationToken = default) => | ||
_retryPolicy.ExecuteAsync(() => | ||
_collection.Find(Builders<Hl7ApplicationConfigEntity>.Filter.Empty).ToListAsync(cancellationToken)); | ||
|
||
public Task<Hl7ApplicationConfigEntity?> GetByIdAsync(string id) => | ||
_retryPolicy.ExecuteAsync(() => _collection | ||
.Find(x => x.Id.Equals(id)) | ||
.FirstOrDefaultAsync())!; | ||
|
||
public Task<Hl7ApplicationConfigEntity> DeleteAsync(string id, CancellationToken cancellationToken = default) | ||
{ | ||
return _retryPolicy.ExecuteAsync(async () => | ||
{ | ||
var entity = await GetByIdAsync(id).ConfigureAwait(false); | ||
if (entity is null) | ||
{ | ||
throw new DatabaseException("Failed to delete entity."); | ||
} | ||
|
||
var result = await _collection | ||
.DeleteOneAsync(Builders<Hl7ApplicationConfigEntity>.Filter.Where(p => p.Id.Equals(id)), | ||
cancellationToken: cancellationToken).ConfigureAwait(false); | ||
|
||
if (result.DeletedCount == 0) | ||
{ | ||
throw new DatabaseException("Failed to delete entity"); | ||
} | ||
|
||
return entity; | ||
}); | ||
} | ||
|
||
public Task<Hl7ApplicationConfigEntity> CreateAsync(Hl7ApplicationConfigEntity configEntity, | ||
CancellationToken cancellationToken = default) | ||
{ | ||
return _retryPolicy.ExecuteAsync(async () => | ||
{ | ||
await _collection.InsertOneAsync(configEntity, cancellationToken: cancellationToken) | ||
.ConfigureAwait(false); | ||
return configEntity; | ||
}); | ||
} | ||
|
||
protected virtual void Dispose(bool disposing) | ||
{ | ||
if (!_disposedValue) | ||
{ | ||
if (disposing) | ||
{ | ||
_scope.Dispose(); | ||
} | ||
|
||
_disposedValue = true; | ||
} | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method | ||
Dispose(disposing: true); | ||
GC.SuppressFinalize(this); | ||
} | ||
} | ||
} |