Compare commits
10 Commits
6a9792bb57
...
feat/servi
| Author | SHA1 | Date | |
|---|---|---|---|
| cb7d154f64 | |||
| a3f404b9ae | |||
| 1f7eb5d4ea | |||
| e1ae3ffccb | |||
| 3e3bfaa904 | |||
| f8422ed94c | |||
| c64c63925e | |||
| 2c8ae23203 | |||
| b4be718994 | |||
| 33bf5b1a51 |
@@ -0,0 +1,92 @@
|
|||||||
|
using DigitalData.Core.Abstraction.Application.Repository;
|
||||||
|
using DigitalData.Core.Exceptions;
|
||||||
|
using EnvelopeGenerator.Domain.Entities;
|
||||||
|
using MediatR;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
|
||||||
|
namespace EnvelopeGenerator.Application.ThirdPartyModules.Queries;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Query to read the license text of a third-party module.
|
||||||
|
/// Either <see cref="Id"/> or <see cref="Name"/> must be provided, but not both.
|
||||||
|
/// </summary>
|
||||||
|
public record ReadThirdPartyModuleLicenseQuery : IRequest<string>
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// The unique identifier of the third-party module (optional).
|
||||||
|
/// </summary>
|
||||||
|
public int? Id { get; init; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The name of the third-party module (optional).
|
||||||
|
/// </summary>
|
||||||
|
public string? Name { get; init; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Whether to filter only active modules. Defaults to <c>true</c>.
|
||||||
|
/// </summary>
|
||||||
|
public bool Active { get; init; } = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Handles <see cref="ReadThirdPartyModuleLicenseQuery"/> and returns the license text of a third-party module.
|
||||||
|
/// </summary>
|
||||||
|
public class ReadThirdPartyModuleLicenseQueryHandler : IRequestHandler<ReadThirdPartyModuleLicenseQuery, string>
|
||||||
|
{
|
||||||
|
private readonly IRepository<ThirdPartyModule> _repository;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Initializes a new instance of the <see cref="ReadThirdPartyModuleLicenseQueryHandler"/> class.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="repository">The repository for accessing third-party modules.</param>
|
||||||
|
public ReadThirdPartyModuleLicenseQueryHandler(IRepository<ThirdPartyModule> repository)
|
||||||
|
{
|
||||||
|
_repository = repository;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Handles the query by filtering on Id or Name and returning the license text.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="request">The query parameters.</param>
|
||||||
|
/// <param name="cancel">A cancellation token.</param>
|
||||||
|
/// <returns>The license text of the matching third-party module.</returns>
|
||||||
|
/// <exception cref="BadRequestException">
|
||||||
|
/// Thrown when neither Id nor Name is provided, or when both are provided.
|
||||||
|
/// </exception>
|
||||||
|
/// <exception cref="InvalidOperationException">
|
||||||
|
/// Thrown when multiple modules match the given criteria, indicating a data integrity issue.
|
||||||
|
/// </exception>
|
||||||
|
/// <exception cref="NotFoundException">
|
||||||
|
/// Thrown when no module matches the given criteria.
|
||||||
|
/// </exception>
|
||||||
|
public async Task<string> Handle(ReadThirdPartyModuleLicenseQuery request, CancellationToken cancel)
|
||||||
|
{
|
||||||
|
if (request.Id is null && request.Name is null)
|
||||||
|
throw new BadRequestException("Either Id or Name must be provided.");
|
||||||
|
|
||||||
|
if (request.Id is not null && request.Name is not null)
|
||||||
|
throw new BadRequestException("Only one of Id or Name must be provided, not both.");
|
||||||
|
|
||||||
|
var query = _repository.Query
|
||||||
|
.Where(m => m.Active == request.Active);
|
||||||
|
|
||||||
|
if (request.Id is int id)
|
||||||
|
query = query.Where(m => m.Id == id);
|
||||||
|
|
||||||
|
if (request.Name is string name)
|
||||||
|
query = query.Where(m => m.Name == name);
|
||||||
|
|
||||||
|
var modules = await query
|
||||||
|
.Take(2)
|
||||||
|
.ToListAsync(cancel);
|
||||||
|
|
||||||
|
if (modules.Count > 1)
|
||||||
|
throw new InvalidOperationException(
|
||||||
|
$"Data integrity violation: multiple third-party modules found for the given criteria (Id={request.Id}, Name={request.Name}, Active={request.Active}).");
|
||||||
|
|
||||||
|
return modules.SingleOrDefault() is ThirdPartyModule module
|
||||||
|
? module.License
|
||||||
|
: throw new NotFoundException(
|
||||||
|
$"Third-party module not found for the given criteria (Id={request.Id}, Name={request.Name}, Active={request.Active}).");
|
||||||
|
}
|
||||||
|
}
|
||||||
61
EnvelopeGenerator.Domain/Entities/ThirdPartyModule.cs
Normal file
61
EnvelopeGenerator.Domain/Entities/ThirdPartyModule.cs
Normal file
@@ -0,0 +1,61 @@
|
|||||||
|
using System.ComponentModel.DataAnnotations;
|
||||||
|
using System.ComponentModel.DataAnnotations.Schema;
|
||||||
|
using EnvelopeGenerator.Domain.Interfaces.Auditing;
|
||||||
|
#if NETFRAMEWORK
|
||||||
|
using System;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
namespace EnvelopeGenerator.Domain.Entities
|
||||||
|
{
|
||||||
|
[Table("TBDD_3RD_PARTY_MODULES", Schema = "dbo")]
|
||||||
|
public class ThirdPartyModule : IHasChangedWhen, IHasChangedWho
|
||||||
|
{
|
||||||
|
[Key]
|
||||||
|
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
|
||||||
|
[Column("GUID")]
|
||||||
|
public int Id { get; set; }
|
||||||
|
|
||||||
|
[Required]
|
||||||
|
[Column("ACTIVE", TypeName = "bit")]
|
||||||
|
public bool Active { get; set; }
|
||||||
|
|
||||||
|
[Required]
|
||||||
|
[Column("NAME", TypeName = "varchar(50)")]
|
||||||
|
public string Name { get; set; }
|
||||||
|
|
||||||
|
[Column("DESCRIPTION", TypeName = "varchar(500)")]
|
||||||
|
public string
|
||||||
|
#if nullable
|
||||||
|
?
|
||||||
|
#endif
|
||||||
|
Description { get; set; }
|
||||||
|
|
||||||
|
[Required]
|
||||||
|
[Column("LICENSE", TypeName = "varchar(max)")]
|
||||||
|
public string License { get; set; }
|
||||||
|
|
||||||
|
[Required]
|
||||||
|
[Column("VERSION", TypeName = "varchar(20)")]
|
||||||
|
public string Version { get; set; }
|
||||||
|
|
||||||
|
[Column("ADDED_WHO", TypeName = "varchar(50)")]
|
||||||
|
public string
|
||||||
|
#if nullable
|
||||||
|
?
|
||||||
|
#endif
|
||||||
|
AddedWho { get; set; }
|
||||||
|
|
||||||
|
[Column("ADDED_WHEN", TypeName = "datetime")]
|
||||||
|
public DateTime? AddedWhen { get; set; }
|
||||||
|
|
||||||
|
[Column("CHANGED_WHO", TypeName = "varchar(50)")]
|
||||||
|
public string
|
||||||
|
#if nullable
|
||||||
|
?
|
||||||
|
#endif
|
||||||
|
ChangedWho { get; set; }
|
||||||
|
|
||||||
|
[Column("CHANGED_WHEN", TypeName = "datetime")]
|
||||||
|
public DateTime? ChangedWhen { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -81,6 +81,8 @@ public abstract class EGDbContextBase : DbContext
|
|||||||
|
|
||||||
public DbSet<EnvelopeReport> EnvelopeReports { get; set; }
|
public DbSet<EnvelopeReport> EnvelopeReports { get; set; }
|
||||||
|
|
||||||
|
public DbSet<ThirdPartyModule> ThirdPartyModules { get; set; }
|
||||||
|
|
||||||
private readonly DbTriggerParams _triggers;
|
private readonly DbTriggerParams _triggers;
|
||||||
|
|
||||||
private readonly ILogger
|
private readonly ILogger
|
||||||
|
|||||||
@@ -26,14 +26,7 @@ public static class DependencyInjection
|
|||||||
|
|
||||||
//TODO: Check lifetime of services. They might be singleton or scoped.
|
//TODO: Check lifetime of services. They might be singleton or scoped.
|
||||||
services.AddTransient<GdViewer>();
|
services.AddTransient<GdViewer>();
|
||||||
// Add LicenseManager
|
services.AddSingleton<LicenseManagerFactory>();
|
||||||
services.AddTransient(provider =>
|
|
||||||
{
|
|
||||||
var options = provider.GetRequiredService<IOptions<WorkerOptions>>().Value;
|
|
||||||
var licenseManager = new LicenseManager();
|
|
||||||
licenseManager.RegisterKEY(options.GdPictureLicenseKey);
|
|
||||||
return licenseManager;
|
|
||||||
});
|
|
||||||
services.AddTransient<AnnotationManager>();
|
services.AddTransient<AnnotationManager>();
|
||||||
|
|
||||||
return services;
|
return services;
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ using EnvelopeGenerator.Domain.Entities;
|
|||||||
using EnvelopeGenerator.Infrastructure;
|
using EnvelopeGenerator.Infrastructure;
|
||||||
using EnvelopeGenerator.PdfEditor;
|
using EnvelopeGenerator.PdfEditor;
|
||||||
using EnvelopeGenerator.ServiceHost.Exceptions;
|
using EnvelopeGenerator.ServiceHost.Exceptions;
|
||||||
|
using EnvelopeGenerator.ServiceHost.Jobs;
|
||||||
using GdPicture14;
|
using GdPicture14;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
using Microsoft.Extensions.Options;
|
using Microsoft.Extensions.Options;
|
||||||
@@ -11,16 +12,7 @@ using Newtonsoft.Json;
|
|||||||
|
|
||||||
namespace EnvelopeGenerator.ServiceHost.Jobs.FinalizeDocument;
|
namespace EnvelopeGenerator.ServiceHost.Jobs.FinalizeDocument;
|
||||||
|
|
||||||
//TODO: check if licence manager is needed as a dependency to
|
public class PDFBurner(IOptions<WorkerOptions> workerOptions, EGDbContext context, ILogger<PDFBurner> logger, LicenseManagerFactory licenseManagerFactory, AnnotationManager manager)
|
||||||
/// <summary>
|
|
||||||
///
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="workerOptions"></param>
|
|
||||||
/// <param name="context2"></param>
|
|
||||||
/// <param name="logger2"></param>
|
|
||||||
/// <param name="licenseManager"></param>
|
|
||||||
/// <param name="annotationManager2"></param>
|
|
||||||
public class PDFBurner(IOptions<WorkerOptions> workerOptions, EGDbContext context, ILogger<PDFBurner> logger, LicenseManager licenseManager, AnnotationManager manager)
|
|
||||||
{
|
{
|
||||||
private readonly WorkerOptions.PDFBurnerOptions _options = workerOptions.Value.PdfBurner;
|
private readonly WorkerOptions.PDFBurnerOptions _options = workerOptions.Value.PdfBurner;
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
using EnvelopeGenerator.ServiceHost.Exceptions;
|
using EnvelopeGenerator.ServiceHost.Exceptions;
|
||||||
|
using EnvelopeGenerator.ServiceHost.Jobs;
|
||||||
using GdPicture14;
|
using GdPicture14;
|
||||||
using Microsoft.Extensions.Options;
|
using Microsoft.Extensions.Options;
|
||||||
|
|
||||||
@@ -7,16 +8,16 @@ namespace EnvelopeGenerator.ServiceHost.Jobs.FinalizeDocument;
|
|||||||
public class PDFMerger
|
public class PDFMerger
|
||||||
{
|
{
|
||||||
private readonly AnnotationManager _manager;
|
private readonly AnnotationManager _manager;
|
||||||
private readonly LicenseManager _licenseManager;
|
private readonly LicenseManagerFactory _licenseManagerFactory;
|
||||||
|
|
||||||
private const bool AllowRasterization = true;
|
private const bool AllowRasterization = true;
|
||||||
private const bool AllowVectorization = true;
|
private const bool AllowVectorization = true;
|
||||||
|
|
||||||
private readonly PdfConversionConformance _pdfaConformanceLevel = PdfConversionConformance.PDF_A_1b;
|
private readonly PdfConversionConformance _pdfaConformanceLevel = PdfConversionConformance.PDF_A_1b;
|
||||||
|
|
||||||
public PDFMerger(LicenseManager licenseManager, AnnotationManager annotationManager)
|
public PDFMerger(LicenseManagerFactory licenseManagerFactory, AnnotationManager annotationManager)
|
||||||
{
|
{
|
||||||
_licenseManager = licenseManager;
|
_licenseManagerFactory = licenseManagerFactory;
|
||||||
_manager = annotationManager;
|
_manager = annotationManager;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -38,7 +38,6 @@ public class FinalizeDocumentJob(IOptions<WorkerOptions> options, ILogger<Finali
|
|||||||
|
|
||||||
public async Task ExecuteAsync(IEnumerable<EnvelopeDto> envelopes, CancellationToken cancel = default)
|
public async Task ExecuteAsync(IEnumerable<EnvelopeDto> envelopes, CancellationToken cancel = default)
|
||||||
{
|
{
|
||||||
var gdPictureKey = _options.GdPictureLicenseKey;
|
|
||||||
tempFiles.Create();
|
tempFiles.Create();
|
||||||
var jobId = typeof(FinalizeDocumentJob).FullName;
|
var jobId = typeof(FinalizeDocumentJob).FullName;
|
||||||
|
|
||||||
|
|||||||
39
EnvelopeGenerator.ServiceHost/Jobs/LicenseManagerFactory.cs
Normal file
39
EnvelopeGenerator.ServiceHost/Jobs/LicenseManagerFactory.cs
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
using EnvelopeGenerator.Application.ThirdPartyModules.Queries;
|
||||||
|
using GdPicture14;
|
||||||
|
using MediatR;
|
||||||
|
using Microsoft.Extensions.Caching.Memory;
|
||||||
|
|
||||||
|
namespace EnvelopeGenerator.ServiceHost.Jobs;
|
||||||
|
|
||||||
|
public class LicenseManagerFactory
|
||||||
|
{
|
||||||
|
private static readonly string _cacheKey = Guid.NewGuid().ToString();
|
||||||
|
private readonly IServiceScopeFactory scopeFactory;
|
||||||
|
private readonly IMemoryCache cache;
|
||||||
|
|
||||||
|
public LicenseManagerFactory(IServiceScopeFactory scopeFactory, IMemoryCache cache)
|
||||||
|
{
|
||||||
|
this.scopeFactory = scopeFactory;
|
||||||
|
this.cache = cache;
|
||||||
|
_ = CreateAsync(); // Preload the license key into the cache
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<LicenseManager> CreateAsync(CancellationToken cancellationToken = default)
|
||||||
|
{
|
||||||
|
var key = await GetLicenseKeyAsync(cancellationToken);
|
||||||
|
var licenseManager = new LicenseManager();
|
||||||
|
licenseManager.RegisterKEY(key);
|
||||||
|
return licenseManager;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<string> GetLicenseKeyAsync(CancellationToken cancellationToken = default)
|
||||||
|
{
|
||||||
|
return await cache.GetOrCreateAsync(_cacheKey, async entry =>
|
||||||
|
{
|
||||||
|
entry.Priority = CacheItemPriority.NeverRemove;
|
||||||
|
using var scope = scopeFactory.CreateScope();
|
||||||
|
var mediator = scope.ServiceProvider.GetRequiredService<IMediator>();
|
||||||
|
return await mediator.Send(new ReadThirdPartyModuleLicenseQuery { Name = "GdPicture", Active =true }, cancellationToken);
|
||||||
|
}) ?? throw new InvalidOperationException("License key could not be retrieved.");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -18,8 +18,6 @@ public class WorkerOptions
|
|||||||
|
|
||||||
public Dictionary<string, State> InitialJobState { get; set; } = [];
|
public Dictionary<string, State> InitialJobState { get; set; } = [];
|
||||||
|
|
||||||
public string GdPictureLicenseKey { get; set; } = null!;
|
|
||||||
|
|
||||||
public PDFBurnerOptions PdfBurner { get; set; } = new();
|
public PDFBurnerOptions PdfBurner { get; set; } = new();
|
||||||
|
|
||||||
public record PDFBurnerOptions
|
public record PDFBurnerOptions
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
"Logging": {
|
"Logging": {
|
||||||
"LogLevel": {
|
"LogLevel": {
|
||||||
"Default": "Information",
|
"Default": "Information",
|
||||||
"Microsoft.AspNetCore": "Warning"
|
"Microsoft.AspNetCore": "Information"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,7 +6,11 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"Worker": {
|
"Worker": {
|
||||||
"DelayMilliseconds": 1000
|
"DelayMilliseconds": 1000,
|
||||||
|
// InitialJobState: State values are 0 = Running, 1 = Stopped. Defaults to 1 (Stopped) if not specified.
|
||||||
|
"InitialJobState": {
|
||||||
|
"FinalizeDocumentJob": 0
|
||||||
|
}
|
||||||
},
|
},
|
||||||
"AllowedHosts": "*",
|
"AllowedHosts": "*",
|
||||||
"SupportedCultures": [ "de-DE", "en-US" ],
|
"SupportedCultures": [ "de-DE", "en-US" ],
|
||||||
|
|||||||
Reference in New Issue
Block a user