Skip to content

Commit

Permalink
changes for HL7Configuration endpoint
Browse files Browse the repository at this point in the history
Signed-off-by: Lillie Dae <[email protected]>
  • Loading branch information
lillie-dae committed Nov 16, 2023
1 parent e6a1400 commit 57e6382
Show file tree
Hide file tree
Showing 10 changed files with 500 additions and 0 deletions.
88 changes: 88 additions & 0 deletions src/Api/Hl7ApplicationConfigEntity.cs
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);
}
}
}
32 changes: 32 additions & 0 deletions src/Database/Api/Repositories/IHl7ApplicationConfigRepository.cs
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);
}
}
2 changes: 2 additions & 0 deletions src/Database/DatabaseManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ public static IServiceCollection ConfigureDatabase(this IServiceCollection servi
services.AddScoped(typeof(IPayloadRepository), typeof(EntityFramework.Repositories.PayloadRepository));
services.AddScoped(typeof(IDicomAssociationInfoRepository), typeof(EntityFramework.Repositories.DicomAssociationInfoRepository));
services.AddScoped(typeof(IVirtualApplicationEntityRepository), typeof(EntityFramework.Repositories.VirtualApplicationEntityRepository));
services.AddScoped(typeof(IHl7ApplicationConfigRepository), typeof(EntityFramework.Repositories.Hl7ApplicationConfigRepository));

services.ConfigureDatabaseFromPlugIns(DatabaseType.EntityFramework, fileSystem, connectionStringConfigurationSection, pluginsConfigurationSection, loggerFactory);
return services;
Expand All @@ -106,6 +107,7 @@ public static IServiceCollection ConfigureDatabase(this IServiceCollection servi
services.AddScoped(typeof(IPayloadRepository), typeof(MongoDB.Repositories.PayloadRepository));
services.AddScoped(typeof(IDicomAssociationInfoRepository), typeof(MongoDB.Repositories.DicomAssociationInfoRepository));
services.AddScoped(typeof(IVirtualApplicationEntityRepository), typeof(MongoDB.Repositories.VirtualApplicationEntityRepository));
services.AddScoped(typeof(IHl7ApplicationConfigRepository), typeof(MongoDB.Repositories.Hl7ApplicationConfigRepository));

services.ConfigureDatabaseFromPlugIns(DatabaseType.MongoDb, fileSystem, connectionStringConfigurationSection, pluginsConfigurationSection, loggerFactory);

Expand Down
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);
}
}
}
4 changes: 4 additions & 0 deletions src/Database/EntityFramework/InformaticsGatewayContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ public InformaticsGatewayContext(DbContextOptions<InformaticsGatewayContext> opt
public virtual DbSet<DicomAssociationInfo> DicomAssociationHistories { get; set; }
public virtual DbSet<VirtualApplicationEntity> VirtualApplicationEntities { get; set; }

public virtual DbSet<Hl7ApplicationConfigEntity> Hl7ApplicationConfig { get; set; }


protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
Expand All @@ -54,6 +57,7 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
modelBuilder.ApplyConfiguration(new StorageMetadataWrapperEntityConfiguration());
modelBuilder.ApplyConfiguration(new DicomAssociationInfoConfiguration());
modelBuilder.ApplyConfiguration(new VirtualApplicationEntityConfiguration());
modelBuilder.ApplyConfiguration(new Hl7ApplicationConfigConfiguration());
}

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
Expand Down
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

View workflow job for this annotation

GitHub Actions / unit-test

Add the default parameter value defined in the overridden method.

Check warning on line 61 in src/Database/EntityFramework/Repositories/Hl7ApplicationConfigRepository.cs

View workflow job for this annotation

GitHub Actions / unit-test

Add the default parameter value defined in the overridden method.
{
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

View workflow job for this annotation

GitHub Actions / integration-test (DicomWebExport, ef)

Possible null reference argument for parameter 'entity' in 'EntityEntry<Hl7ApplicationConfigEntity> DbSet<Hl7ApplicationConfigEntity>.Remove(Hl7ApplicationConfigEntity entity)'.

Check warning on line 66 in src/Database/EntityFramework/Repositories/Hl7ApplicationConfigRepository.cs

View workflow job for this annotation

GitHub Actions / integration-test (DicomWebExport, ef)

Possible null reference argument for parameter 'entity' in 'EntityEntry<Hl7ApplicationConfigEntity> DbSet<Hl7ApplicationConfigEntity>.Remove(Hl7ApplicationConfigEntity entity)'.

Check warning on line 66 in src/Database/EntityFramework/Repositories/Hl7ApplicationConfigRepository.cs

View workflow job for this annotation

GitHub Actions / integration-test (HealthLevel7, ef)

Possible null reference argument for parameter 'entity' in 'EntityEntry<Hl7ApplicationConfigEntity> DbSet<Hl7ApplicationConfigEntity>.Remove(Hl7ApplicationConfigEntity entity)'.

Check warning on line 66 in src/Database/EntityFramework/Repositories/Hl7ApplicationConfigRepository.cs

View workflow job for this annotation

GitHub Actions / integration-test (HealthLevel7, ef)

Possible null reference argument for parameter 'entity' in 'EntityEntry<Hl7ApplicationConfigEntity> DbSet<Hl7ApplicationConfigEntity>.Remove(Hl7ApplicationConfigEntity entity)'.

Check warning on line 66 in src/Database/EntityFramework/Repositories/Hl7ApplicationConfigRepository.cs

View workflow job for this annotation

GitHub Actions / integration-test (DicomDimseScu, ef)

Possible null reference argument for parameter 'entity' in 'EntityEntry<Hl7ApplicationConfigEntity> DbSet<Hl7ApplicationConfigEntity>.Remove(Hl7ApplicationConfigEntity entity)'.

Check warning on line 66 in src/Database/EntityFramework/Repositories/Hl7ApplicationConfigRepository.cs

View workflow job for this annotation

GitHub Actions / integration-test (DicomDimseScu, ef)

Possible null reference argument for parameter 'entity' in 'EntityEntry<Hl7ApplicationConfigEntity> DbSet<Hl7ApplicationConfigEntity>.Remove(Hl7ApplicationConfigEntity entity)'.

Check warning on line 66 in src/Database/EntityFramework/Repositories/Hl7ApplicationConfigRepository.cs

View workflow job for this annotation

GitHub Actions / integration-test (DicomWebStow, ef)

Possible null reference argument for parameter 'entity' in 'EntityEntry<Hl7ApplicationConfigEntity> DbSet<Hl7ApplicationConfigEntity>.Remove(Hl7ApplicationConfigEntity entity)'.

Check warning on line 66 in src/Database/EntityFramework/Repositories/Hl7ApplicationConfigRepository.cs

View workflow job for this annotation

GitHub Actions / integration-test (DicomWebStow, ef)

Possible null reference argument for parameter 'entity' in 'EntityEntry<Hl7ApplicationConfigEntity> DbSet<Hl7ApplicationConfigEntity>.Remove(Hl7ApplicationConfigEntity entity)'.

Check warning on line 66 in src/Database/EntityFramework/Repositories/Hl7ApplicationConfigRepository.cs

View workflow job for this annotation

GitHub Actions / integration-test (AcrApi, ef)

Possible null reference argument for parameter 'entity' in 'EntityEntry<Hl7ApplicationConfigEntity> DbSet<Hl7ApplicationConfigEntity>.Remove(Hl7ApplicationConfigEntity entity)'.

Check warning on line 66 in src/Database/EntityFramework/Repositories/Hl7ApplicationConfigRepository.cs

View workflow job for this annotation

GitHub Actions / integration-test (AcrApi, ef)

Possible null reference argument for parameter 'entity' in 'EntityEntry<Hl7ApplicationConfigEntity> DbSet<Hl7ApplicationConfigEntity>.Remove(Hl7ApplicationConfigEntity entity)'.

Check warning on line 66 in src/Database/EntityFramework/Repositories/Hl7ApplicationConfigRepository.cs

View workflow job for this annotation

GitHub Actions / integration-test (DicomDimseScp, ef)

Possible null reference argument for parameter 'entity' in 'EntityEntry<Hl7ApplicationConfigEntity> DbSet<Hl7ApplicationConfigEntity>.Remove(Hl7ApplicationConfigEntity entity)'.

Check warning on line 66 in src/Database/EntityFramework/Repositories/Hl7ApplicationConfigRepository.cs

View workflow job for this annotation

GitHub Actions / integration-test (DicomDimseScp, ef)

Possible null reference argument for parameter 'entity' in 'EntityEntry<Hl7ApplicationConfigEntity> DbSet<Hl7ApplicationConfigEntity>.Remove(Hl7ApplicationConfigEntity entity)'.

Check warning on line 66 in src/Database/EntityFramework/Repositories/Hl7ApplicationConfigRepository.cs

View workflow job for this annotation

GitHub Actions / integration-test (Fhir, ef)

Possible null reference argument for parameter 'entity' in 'EntityEntry<Hl7ApplicationConfigEntity> DbSet<Hl7ApplicationConfigEntity>.Remove(Hl7ApplicationConfigEntity entity)'.

Check warning on line 66 in src/Database/EntityFramework/Repositories/Hl7ApplicationConfigRepository.cs

View workflow job for this annotation

GitHub Actions / integration-test (Fhir, ef)

Possible null reference argument for parameter 'entity' in 'EntityEntry<Hl7ApplicationConfigEntity> DbSet<Hl7ApplicationConfigEntity>.Remove(Hl7ApplicationConfigEntity entity)'.

Check warning on line 66 in src/Database/EntityFramework/Repositories/Hl7ApplicationConfigRepository.cs

View workflow job for this annotation

GitHub Actions / integration-test (RemoteAppExecutionPlugIn, ef)

Possible null reference argument for parameter 'entity' in 'EntityEntry<Hl7ApplicationConfigEntity> DbSet<Hl7ApplicationConfigEntity>.Remove(Hl7ApplicationConfigEntity entity)'.

Check warning on line 66 in src/Database/EntityFramework/Repositories/Hl7ApplicationConfigRepository.cs

View workflow job for this annotation

GitHub Actions / integration-test (RemoteAppExecutionPlugIn, ef)

Possible null reference argument for parameter 'entity' in 'EntityEntry<Hl7ApplicationConfigEntity> DbSet<Hl7ApplicationConfigEntity>.Remove(Hl7ApplicationConfigEntity entity)'.

Check warning on line 66 in src/Database/EntityFramework/Repositories/Hl7ApplicationConfigRepository.cs

View workflow job for this annotation

GitHub Actions / integration-test (RemoteAppExecutionPlugIn, mongodb)

Possible null reference argument for parameter 'entity' in 'EntityEntry<Hl7ApplicationConfigEntity> DbSet<Hl7ApplicationConfigEntity>.Remove(Hl7ApplicationConfigEntity entity)'.

Check warning on line 66 in src/Database/EntityFramework/Repositories/Hl7ApplicationConfigRepository.cs

View workflow job for this annotation

GitHub Actions / integration-test (RemoteAppExecutionPlugIn, mongodb)

Possible null reference argument for parameter 'entity' in 'EntityEntry<Hl7ApplicationConfigEntity> DbSet<Hl7ApplicationConfigEntity>.Remove(Hl7ApplicationConfigEntity entity)'.

Check warning on line 66 in src/Database/EntityFramework/Repositories/Hl7ApplicationConfigRepository.cs

View workflow job for this annotation

GitHub Actions / integration-test (DicomWebExport, mongodb)

Possible null reference argument for parameter 'entity' in 'EntityEntry<Hl7ApplicationConfigEntity> DbSet<Hl7ApplicationConfigEntity>.Remove(Hl7ApplicationConfigEntity entity)'.

Check warning on line 66 in src/Database/EntityFramework/Repositories/Hl7ApplicationConfigRepository.cs

View workflow job for this annotation

GitHub Actions / integration-test (DicomWebExport, mongodb)

Possible null reference argument for parameter 'entity' in 'EntityEntry<Hl7ApplicationConfigEntity> DbSet<Hl7ApplicationConfigEntity>.Remove(Hl7ApplicationConfigEntity entity)'.

Check warning on line 66 in src/Database/EntityFramework/Repositories/Hl7ApplicationConfigRepository.cs

View workflow job for this annotation

GitHub Actions / integration-test (AcrApi, mongodb)

Possible null reference argument for parameter 'entity' in 'EntityEntry<Hl7ApplicationConfigEntity> DbSet<Hl7ApplicationConfigEntity>.Remove(Hl7ApplicationConfigEntity entity)'.

Check warning on line 66 in src/Database/EntityFramework/Repositories/Hl7ApplicationConfigRepository.cs

View workflow job for this annotation

GitHub Actions / integration-test (AcrApi, mongodb)

Possible null reference argument for parameter 'entity' in 'EntityEntry<Hl7ApplicationConfigEntity> DbSet<Hl7ApplicationConfigEntity>.Remove(Hl7ApplicationConfigEntity entity)'.

Check warning on line 66 in src/Database/EntityFramework/Repositories/Hl7ApplicationConfigRepository.cs

View workflow job for this annotation

GitHub Actions / integration-test (DicomDimseScu, mongodb)

Possible null reference argument for parameter 'entity' in 'EntityEntry<Hl7ApplicationConfigEntity> DbSet<Hl7ApplicationConfigEntity>.Remove(Hl7ApplicationConfigEntity entity)'.

Check warning on line 66 in src/Database/EntityFramework/Repositories/Hl7ApplicationConfigRepository.cs

View workflow job for this annotation

GitHub Actions / integration-test (DicomDimseScu, mongodb)

Possible null reference argument for parameter 'entity' in 'EntityEntry<Hl7ApplicationConfigEntity> DbSet<Hl7ApplicationConfigEntity>.Remove(Hl7ApplicationConfigEntity entity)'.

Check warning on line 66 in src/Database/EntityFramework/Repositories/Hl7ApplicationConfigRepository.cs

View workflow job for this annotation

GitHub Actions / integration-test (Fhir, mongodb)

Possible null reference argument for parameter 'entity' in 'EntityEntry<Hl7ApplicationConfigEntity> DbSet<Hl7ApplicationConfigEntity>.Remove(Hl7ApplicationConfigEntity entity)'.

Check warning on line 66 in src/Database/EntityFramework/Repositories/Hl7ApplicationConfigRepository.cs

View workflow job for this annotation

GitHub Actions / integration-test (Fhir, mongodb)

Possible null reference argument for parameter 'entity' in 'EntityEntry<Hl7ApplicationConfigEntity> DbSet<Hl7ApplicationConfigEntity>.Remove(Hl7ApplicationConfigEntity entity)'.

Check warning on line 66 in src/Database/EntityFramework/Repositories/Hl7ApplicationConfigRepository.cs

View workflow job for this annotation

GitHub Actions / integration-test (DicomDimseScp, mongodb)

Possible null reference argument for parameter 'entity' in 'EntityEntry<Hl7ApplicationConfigEntity> DbSet<Hl7ApplicationConfigEntity>.Remove(Hl7ApplicationConfigEntity entity)'.

Check warning on line 66 in src/Database/EntityFramework/Repositories/Hl7ApplicationConfigRepository.cs

View workflow job for this annotation

GitHub Actions / integration-test (DicomDimseScp, mongodb)

Possible null reference argument for parameter 'entity' in 'EntityEntry<Hl7ApplicationConfigEntity> DbSet<Hl7ApplicationConfigEntity>.Remove(Hl7ApplicationConfigEntity entity)'.

Check warning on line 66 in src/Database/EntityFramework/Repositories/Hl7ApplicationConfigRepository.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

Possible null reference argument for parameter 'entity' in 'EntityEntry<Hl7ApplicationConfigEntity> DbSet<Hl7ApplicationConfigEntity>.Remove(Hl7ApplicationConfigEntity entity)'.

Check warning on line 66 in src/Database/EntityFramework/Repositories/Hl7ApplicationConfigRepository.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

Possible null reference argument for parameter 'entity' in 'EntityEntry<Hl7ApplicationConfigEntity> DbSet<Hl7ApplicationConfigEntity>.Remove(Hl7ApplicationConfigEntity entity)'.

Check warning on line 66 in src/Database/EntityFramework/Repositories/Hl7ApplicationConfigRepository.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest)

Possible null reference argument for parameter 'entity' in 'EntityEntry<Hl7ApplicationConfigEntity> DbSet<Hl7ApplicationConfigEntity>.Remove(Hl7ApplicationConfigEntity entity)'.

Check warning on line 66 in src/Database/EntityFramework/Repositories/Hl7ApplicationConfigRepository.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest)

Possible null reference argument for parameter 'entity' in 'EntityEntry<Hl7ApplicationConfigEntity> DbSet<Hl7ApplicationConfigEntity>.Remove(Hl7ApplicationConfigEntity entity)'.

Check warning on line 66 in src/Database/EntityFramework/Repositories/Hl7ApplicationConfigRepository.cs

View workflow job for this annotation

GitHub Actions / unit-test

Possible null reference argument for parameter 'entity' in 'EntityEntry<Hl7ApplicationConfigEntity> DbSet<Hl7ApplicationConfigEntity>.Remove(Hl7ApplicationConfigEntity entity)'.

Check warning on line 66 in src/Database/EntityFramework/Repositories/Hl7ApplicationConfigRepository.cs

View workflow job for this annotation

GitHub Actions / unit-test

Possible null reference argument for parameter 'entity' in 'EntityEntry<Hl7ApplicationConfigEntity> DbSet<Hl7ApplicationConfigEntity>.Remove(Hl7ApplicationConfigEntity entity)'.

Check warning on line 66 in src/Database/EntityFramework/Repositories/Hl7ApplicationConfigRepository.cs

View workflow job for this annotation

GitHub Actions / integration-test (HealthLevel7, mongodb)

Possible null reference argument for parameter 'entity' in 'EntityEntry<Hl7ApplicationConfigEntity> DbSet<Hl7ApplicationConfigEntity>.Remove(Hl7ApplicationConfigEntity entity)'.

Check warning on line 66 in src/Database/EntityFramework/Repositories/Hl7ApplicationConfigRepository.cs

View workflow job for this annotation

GitHub Actions / integration-test (HealthLevel7, mongodb)

Possible null reference argument for parameter 'entity' in 'EntityEntry<Hl7ApplicationConfigEntity> DbSet<Hl7ApplicationConfigEntity>.Remove(Hl7ApplicationConfigEntity entity)'.

Check warning on line 66 in src/Database/EntityFramework/Repositories/Hl7ApplicationConfigRepository.cs

View workflow job for this annotation

GitHub Actions / integration-test (DicomWebStow, mongodb)

Possible null reference argument for parameter 'entity' in 'EntityEntry<Hl7ApplicationConfigEntity> DbSet<Hl7ApplicationConfigEntity>.Remove(Hl7ApplicationConfigEntity entity)'.

Check warning on line 66 in src/Database/EntityFramework/Repositories/Hl7ApplicationConfigRepository.cs

View workflow job for this annotation

GitHub Actions / integration-test (DicomWebStow, mongodb)

Possible null reference argument for parameter 'entity' in 'EntityEntry<Hl7ApplicationConfigEntity> DbSet<Hl7ApplicationConfigEntity>.Remove(Hl7ApplicationConfigEntity entity)'.

Check warning on line 66 in src/Database/EntityFramework/Repositories/Hl7ApplicationConfigRepository.cs

View workflow job for this annotation

GitHub Actions / docs

Possible null reference argument for parameter 'entity' in 'EntityEntry<Hl7ApplicationConfigEntity> DbSet<Hl7ApplicationConfigEntity>.Remove(Hl7ApplicationConfigEntity entity)'.

Check warning on line 66 in src/Database/EntityFramework/Repositories/Hl7ApplicationConfigRepository.cs

View workflow job for this annotation

GitHub Actions / docs

Possible null reference argument for parameter 'entity' in 'EntityEntry<Hl7ApplicationConfigEntity> DbSet<Hl7ApplicationConfigEntity>.Remove(Hl7ApplicationConfigEntity entity)'.

Check warning on line 66 in src/Database/EntityFramework/Repositories/Hl7ApplicationConfigRepository.cs

View workflow job for this annotation

GitHub Actions / analyze

Possible null reference argument for parameter 'entity' in 'EntityEntry<Hl7ApplicationConfigEntity> DbSet<Hl7ApplicationConfigEntity>.Remove(Hl7ApplicationConfigEntity entity)'.

Check warning on line 66 in src/Database/EntityFramework/Repositories/Hl7ApplicationConfigRepository.cs

View workflow job for this annotation

GitHub Actions / analyze

Possible null reference argument for parameter 'entity' in 'EntityEntry<Hl7ApplicationConfigEntity> DbSet<Hl7ApplicationConfigEntity>.Remove(Hl7ApplicationConfigEntity entity)'.

Check warning on line 66 in src/Database/EntityFramework/Repositories/Hl7ApplicationConfigRepository.cs

View workflow job for this annotation

GitHub Actions / CodeQL-Analyze

Possible null reference argument for parameter 'entity' in 'EntityEntry<Hl7ApplicationConfigEntity> DbSet<Hl7ApplicationConfigEntity>.Remove(Hl7ApplicationConfigEntity entity)'.

Check warning on line 66 in src/Database/EntityFramework/Repositories/Hl7ApplicationConfigRepository.cs

View workflow job for this annotation

GitHub Actions / CodeQL-Analyze

Possible null reference argument for parameter 'entity' in 'EntityEntry<Hl7ApplicationConfigEntity> DbSet<Hl7ApplicationConfigEntity>.Remove(Hl7ApplicationConfigEntity entity)'.
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;
});
}
}
}
132 changes: 132 additions & 0 deletions src/Database/MongoDB/Repositories/Hl7ApplicationConfigRepository.cs
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);
}
}
}
Loading

0 comments on commit 57e6382

Please sign in to comment.