Compare commits

..

10 Commits

Author SHA1 Message Date
cb7d154f64 Refactor LicenseManager registration with factory
Replaces transient LicenseManager registration (with per-injection license key retrieval via MediatR) with a singleton LicenseManagerFactory. This centralizes license management and may improve performance and maintainability.
2026-04-14 21:05:21 +02:00
a3f404b9ae Refactor to use LicenseManagerFactory in PDF jobs
Replaced direct LicenseManager dependencies with LicenseManagerFactory in PDFBurner and PDFMerger classes. This change improves license management flexibility and encapsulation, allowing for better handling of license-related logic.
2026-04-14 21:05:08 +02:00
1f7eb5d4ea Add LicenseManagerFactory for GdPicture license caching
Introduced LicenseManagerFactory to create and cache GdPicture14 LicenseManager instances. The factory retrieves the license key via MediatR, caches it using IMemoryCache with NeverRemove priority, and registers it with LicenseManager. Dependencies are injected, and the license key is preloaded on instantiation.
2026-04-14 21:03:54 +02:00
e1ae3ffccb Increase log verbosity; add InitialJobState config
Changed ASP.NET Core log level to Information in development.
Added Worker.InitialJobState setting to control job startup state,
defaulting FinalizeDocumentJob to Running. Added explanatory
comments for job state values.
2026-04-14 14:47:05 +02:00
3e3bfaa904 Remove GdPictureLicenseKey from WorkerOptions
The GdPictureLicenseKey property was removed from the WorkerOptions class as it is no longer needed. This change helps to simplify the configuration and reduce unused properties.
2026-04-14 13:36:54 +02:00
f8422ed94c Remove unused GdPicture license key retrieval
The ExecuteAsync method in FinalizeDocumentJob.cs no longer retrieves the GdPicture license key, as it is not used in this part of the code. This helps clean up unnecessary code and improves maintainability.
2026-04-14 13:36:28 +02:00
c64c63925e Retrieve GdPicture license key via MediatR from database
Refactored LicenseManager registration to fetch the GdPicture license key from the database using MediatR and ReadThirdPartyModuleLicenseQuery, instead of reading from configuration. Updated using statements accordingly.
2026-04-14 13:25:07 +02:00
2c8ae23203 Add ThirdPartyModule DbSet to EGDbContextBase
Added a DbSet property for ThirdPartyModule entities in EGDbContextBase to enable management and querying of third-party modules via Entity Framework.
2026-04-14 13:21:28 +02:00
b4be718994 Add query/handler to fetch third-party module license text
Introduced ReadThirdPartyModuleLicenseQuery and its handler to retrieve the license text of a third-party module by Id or Name (mutually exclusive), with optional filtering for active modules. Includes validation and exception handling for invalid input, not found, or multiple matches.
2026-04-14 12:39:31 +02:00
33bf5b1a51 Add ThirdPartyModule entity with auditing support
Introduced the ThirdPartyModule entity mapped to the TBDD_3RD_PARTY_MODULES table using Entity Framework. The class includes properties for module details and auditing (added/changed by and when), implements IHasChangedWhen and IHasChangedWho interfaces, and uses conditional compilation for .NET Framework and nullable reference types. Data annotations and schema mapping attributes were added for precise database integration.
2026-04-14 12:15:42 +02:00
11 changed files with 207 additions and 26 deletions

View File

@@ -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}).");
}
}

View 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; }
}
}

View File

@@ -81,6 +81,8 @@ public abstract class EGDbContextBase : DbContext
public DbSet<EnvelopeReport> EnvelopeReports { get; set; }
public DbSet<ThirdPartyModule> ThirdPartyModules { get; set; }
private readonly DbTriggerParams _triggers;
private readonly ILogger

View File

@@ -26,14 +26,7 @@ public static class DependencyInjection
//TODO: Check lifetime of services. They might be singleton or scoped.
services.AddTransient<GdViewer>();
// Add LicenseManager
services.AddTransient(provider =>
{
var options = provider.GetRequiredService<IOptions<WorkerOptions>>().Value;
var licenseManager = new LicenseManager();
licenseManager.RegisterKEY(options.GdPictureLicenseKey);
return licenseManager;
});
services.AddSingleton<LicenseManagerFactory>();
services.AddTransient<AnnotationManager>();
return services;

View File

@@ -4,6 +4,7 @@ using EnvelopeGenerator.Domain.Entities;
using EnvelopeGenerator.Infrastructure;
using EnvelopeGenerator.PdfEditor;
using EnvelopeGenerator.ServiceHost.Exceptions;
using EnvelopeGenerator.ServiceHost.Jobs;
using GdPicture14;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Options;
@@ -11,16 +12,7 @@ using Newtonsoft.Json;
namespace EnvelopeGenerator.ServiceHost.Jobs.FinalizeDocument;
//TODO: check if licence manager is needed as a dependency to
/// <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)
public class PDFBurner(IOptions<WorkerOptions> workerOptions, EGDbContext context, ILogger<PDFBurner> logger, LicenseManagerFactory licenseManagerFactory, AnnotationManager manager)
{
private readonly WorkerOptions.PDFBurnerOptions _options = workerOptions.Value.PdfBurner;

View File

@@ -1,4 +1,5 @@
using EnvelopeGenerator.ServiceHost.Exceptions;
using EnvelopeGenerator.ServiceHost.Jobs;
using GdPicture14;
using Microsoft.Extensions.Options;
@@ -7,16 +8,16 @@ namespace EnvelopeGenerator.ServiceHost.Jobs.FinalizeDocument;
public class PDFMerger
{
private readonly AnnotationManager _manager;
private readonly LicenseManager _licenseManager;
private readonly LicenseManagerFactory _licenseManagerFactory;
private const bool AllowRasterization = true;
private const bool AllowVectorization = true;
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;
}

View File

@@ -38,7 +38,6 @@ public class FinalizeDocumentJob(IOptions<WorkerOptions> options, ILogger<Finali
public async Task ExecuteAsync(IEnumerable<EnvelopeDto> envelopes, CancellationToken cancel = default)
{
var gdPictureKey = _options.GdPictureLicenseKey;
tempFiles.Create();
var jobId = typeof(FinalizeDocumentJob).FullName;

View 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.");
}
}

View File

@@ -18,8 +18,6 @@ public class WorkerOptions
public Dictionary<string, State> InitialJobState { get; set; } = [];
public string GdPictureLicenseKey { get; set; } = null!;
public PDFBurnerOptions PdfBurner { get; set; } = new();
public record PDFBurnerOptions

View File

@@ -2,7 +2,7 @@
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
"Microsoft.AspNetCore": "Information"
}
}
}

View File

@@ -6,7 +6,11 @@
}
},
"Worker": {
"DelayMilliseconds": 1000
"DelayMilliseconds": 1000,
// InitialJobState: State values are 0 = Running, 1 = Stopped. Defaults to 1 (Stopped) if not specified.
"InitialJobState": {
"FinalizeDocumentJob": 0
}
},
"AllowedHosts": "*",
"SupportedCultures": [ "de-DE", "en-US" ],