Refactor PDFBurner to use dependency injection
Replaced internal construction of dependencies with injected EGDbContext, ILogger, LicenseManager, AnnotationManager, and WorkerOptions. Removed BaseClass inheritance and internal fields. Updated annotation and PDF burning methods to use injected instances. Switched configuration from PDFBurnerParams to WorkerOptions.PDFBurnerOptions. Improves testability and aligns with DI best practices.
This commit is contained in:
@@ -1,8 +1,5 @@
|
||||
using System.Collections.Immutable;
|
||||
using System.Drawing;
|
||||
using System.IO;
|
||||
using DigitalData.Modules.Base;
|
||||
using DigitalData.Modules.Logging;
|
||||
using EnvelopeGenerator.Domain.Entities;
|
||||
using EnvelopeGenerator.Infrastructure;
|
||||
using EnvelopeGenerator.PdfEditor;
|
||||
@@ -14,33 +11,21 @@ using Newtonsoft.Json;
|
||||
|
||||
namespace EnvelopeGenerator.ServiceHost.Jobs.FinalizeDocument;
|
||||
|
||||
public class PDFBurner : BaseClass
|
||||
//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)
|
||||
{
|
||||
private readonly AnnotationManager _manager;
|
||||
private readonly LicenseManager _licenseManager;
|
||||
private readonly DbContextOptions<EGDbContext> _dbContextOptions;
|
||||
|
||||
private readonly PDFBurnerParams _pdfBurnerParams;
|
||||
|
||||
public PDFBurner(LogConfig logConfig, string gdPictureLicenseKey, PDFBurnerParams pdfBurnerParams, string connectionString) : base(logConfig)
|
||||
{
|
||||
_licenseManager = new LicenseManager();
|
||||
_licenseManager.RegisterKEY(gdPictureLicenseKey);
|
||||
|
||||
_manager = new AnnotationManager();
|
||||
|
||||
_pdfBurnerParams = pdfBurnerParams;
|
||||
_dbContextOptions = new DbContextOptionsBuilder<EGDbContext>()
|
||||
.UseSqlServer(connectionString)
|
||||
.EnableDetailedErrors()
|
||||
.EnableSensitiveDataLogging()
|
||||
.Options;
|
||||
}
|
||||
|
||||
private readonly WorkerOptions.PDFBurnerOptions _options = workerOptions.Value.PdfBurner;
|
||||
|
||||
public byte[] BurnAnnotsToPDF(byte[] sourceBuffer, List<string> instantJsonList, int envelopeId)
|
||||
{
|
||||
using var context = new EGDbContext(_dbContextOptions, Options.Create(new DbTriggerParams()));
|
||||
|
||||
var envelope = context.Envelopes.FirstOrDefault(env => env.Id == envelopeId);
|
||||
if (envelope is null)
|
||||
{
|
||||
@@ -69,7 +54,7 @@ public class PDFBurner : BaseClass
|
||||
sourceBuffer = doc.Background(elements, 1.9500000000000002 * 0.93, 2.52 * 0.67).ExportStream().ToArray();
|
||||
|
||||
using var sourceStream = new MemoryStream(sourceBuffer);
|
||||
var result = _manager.InitFromStream(sourceStream);
|
||||
var result = manager.InitFromStream(sourceStream);
|
||||
if (result != GdPictureStatus.OK)
|
||||
{
|
||||
throw new BurnAnnotationException($"Could not open document for burning: [{result}]");
|
||||
@@ -120,13 +105,13 @@ public class PDFBurner : BaseClass
|
||||
}
|
||||
|
||||
using var newStream = new MemoryStream();
|
||||
result = _manager.SaveDocumentToPDF(newStream);
|
||||
result = manager.SaveDocumentToPDF(newStream);
|
||||
if (result != GdPictureStatus.OK)
|
||||
{
|
||||
throw new BurnAnnotationException($"Could not save document to stream: [{result}]");
|
||||
}
|
||||
|
||||
_manager.Close();
|
||||
manager.Close();
|
||||
|
||||
return newStream.ToArray();
|
||||
}
|
||||
@@ -135,7 +120,7 @@ public class PDFBurner : BaseClass
|
||||
public byte[] BurnInstantJSONAnnotsToPDF(byte[] sourceBuffer, List<string> instantJsonList)
|
||||
{
|
||||
using var sourceStream = new MemoryStream(sourceBuffer);
|
||||
var result = _manager.InitFromStream(sourceStream);
|
||||
var result = manager.InitFromStream(sourceStream);
|
||||
if (result != GdPictureStatus.OK)
|
||||
{
|
||||
throw new BurnAnnotationException($"Could not open document for burning: [{result}]");
|
||||
@@ -149,26 +134,26 @@ public class PDFBurner : BaseClass
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.LogWarning("Error in AddInstantJSONAnnotationToPDF - oJson: ");
|
||||
Logger.LogWarning(json);
|
||||
logger.LogWarning("Error in AddInstantJSONAnnotationToPDF - oJson: ");
|
||||
logger.LogWarning(json);
|
||||
throw new BurnAnnotationException("Adding Annotation failed", ex);
|
||||
}
|
||||
}
|
||||
|
||||
result = _manager.BurnAnnotationsToPage(RemoveInitialAnnots: true, VectorMode: true);
|
||||
result = manager.BurnAnnotationsToPage(RemoveInitialAnnots: true, VectorMode: true);
|
||||
if (result != GdPictureStatus.OK)
|
||||
{
|
||||
throw new BurnAnnotationException($"Could not burn annotations to file: [{result}]");
|
||||
}
|
||||
|
||||
using var newStream = new MemoryStream();
|
||||
result = _manager.SaveDocumentToPDF(newStream);
|
||||
result = manager.SaveDocumentToPDF(newStream);
|
||||
if (result != GdPictureStatus.OK)
|
||||
{
|
||||
throw new BurnAnnotationException($"Could not save document to stream: [{result}]");
|
||||
}
|
||||
|
||||
_manager.Close();
|
||||
manager.Close();
|
||||
|
||||
return newStream.ToArray();
|
||||
}
|
||||
@@ -185,7 +170,7 @@ public class PDFBurner : BaseClass
|
||||
|
||||
foreach (var annotation in annotationData.annotations)
|
||||
{
|
||||
Logger.LogDebug("Adding AnnotationID: " + annotation.id);
|
||||
logger.LogDebug("Adding AnnotationID: " + annotation.id);
|
||||
|
||||
switch (annotation.type)
|
||||
{
|
||||
@@ -197,7 +182,7 @@ public class PDFBurner : BaseClass
|
||||
break;
|
||||
case AnnotationType.Widget:
|
||||
var formFieldValue = annotationData.formFieldValues.FirstOrDefault(fv => fv.name == annotation.id);
|
||||
if (formFieldValue is not null && !_pdfBurnerParams.IgnoredLabels.Contains(formFieldValue.value))
|
||||
if (formFieldValue is not null && !_options.IgnoredLabels.Contains(formFieldValue.value))
|
||||
{
|
||||
AddFormFieldValue(annotation, formFieldValue);
|
||||
}
|
||||
@@ -208,8 +193,8 @@ public class PDFBurner : BaseClass
|
||||
|
||||
private void AddImageAnnotation(double x, double y, double width, double height, int page, string base64)
|
||||
{
|
||||
_manager.SelectPage(page);
|
||||
_manager.AddEmbeddedImageAnnotFromBase64(base64, (float) x, (float) y, (float) width, (float) height);
|
||||
manager.SelectPage(page);
|
||||
manager.AddEmbeddedImageAnnotFromBase64(base64, (float) x, (float) y, (float) width, (float) height);
|
||||
}
|
||||
|
||||
private void AddImageAnnotation(Annotation annotation, Dictionary<string, Attachment> attachments)
|
||||
@@ -223,8 +208,8 @@ public class PDFBurner : BaseClass
|
||||
var width = bounds[2];
|
||||
var height = bounds[3];
|
||||
|
||||
_manager.SelectPage(annotation.pageIndex + 1);
|
||||
_manager.AddEmbeddedImageAnnotFromBase64(attachment.Value.binary, (float) x, (float) y, (float) width, (float) height);
|
||||
manager.SelectPage(annotation.pageIndex + 1);
|
||||
manager.AddEmbeddedImageAnnotFromBase64(attachment.Value.binary, (float) x, (float) y, (float) width, (float) height);
|
||||
}
|
||||
|
||||
private void AddInkAnnotation(int page, string value)
|
||||
@@ -237,12 +222,12 @@ public class PDFBurner : BaseClass
|
||||
|
||||
var segments = ink.lines.points;
|
||||
var color = ColorTranslator.FromHtml(ink.strokeColor);
|
||||
_manager.SelectPage(page);
|
||||
manager.SelectPage(page);
|
||||
|
||||
foreach (var segment in segments)
|
||||
{
|
||||
var points = segment.Select(ToPointF).ToArray();
|
||||
_manager.AddFreeHandAnnot(color, points);
|
||||
manager.AddFreeHandAnnot(color, points);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -250,24 +235,24 @@ public class PDFBurner : BaseClass
|
||||
{
|
||||
var segments = annotation.lines.points;
|
||||
var color = ColorTranslator.FromHtml(annotation.strokeColor);
|
||||
_manager.SelectPage(annotation.pageIndex + 1);
|
||||
manager.SelectPage(annotation.pageIndex + 1);
|
||||
|
||||
foreach (var segment in segments)
|
||||
{
|
||||
var points = segment.Select(ToPointF).ToArray();
|
||||
_manager.AddFreeHandAnnot(color, points);
|
||||
manager.AddFreeHandAnnot(color, points);
|
||||
}
|
||||
}
|
||||
|
||||
private void AddFormFieldValue(double x, double y, double width, double height, int page, string value)
|
||||
{
|
||||
_manager.SelectPage(page);
|
||||
manager.SelectPage(page);
|
||||
|
||||
var annot = _manager.AddTextAnnot((float) x, (float) y, (float) width, (float) height, value);
|
||||
annot.FontName = _pdfBurnerParams.FontName;
|
||||
annot.FontSize = _pdfBurnerParams.FontSize;
|
||||
annot.FontStyle = _pdfBurnerParams.FontStyle;
|
||||
_manager.SaveAnnotationsToPage();
|
||||
var annot = manager.AddTextAnnot((float) x, (float) y, (float) width, (float) height, value);
|
||||
annot.FontName = _options.FontName;
|
||||
annot.FontSize = _options.FontSize;
|
||||
annot.FontStyle = _options.FontStyle;
|
||||
manager.SaveAnnotationsToPage();
|
||||
}
|
||||
|
||||
private void AddFormFieldValue(Annotation annotation, FormFieldValue formFieldValue)
|
||||
@@ -277,16 +262,16 @@ public class PDFBurner : BaseClass
|
||||
var bounds = annotation.bbox.Select(ToInches).ToList();
|
||||
|
||||
var x = bounds[0];
|
||||
var y = bounds[1] + _pdfBurnerParams.YOffset * ffIndex + _pdfBurnerParams.TopMargin;
|
||||
var y = bounds[1] + _options.YOffset * ffIndex + _options.TopMargin;
|
||||
var width = bounds[2];
|
||||
var height = bounds[3];
|
||||
|
||||
_manager.SelectPage(annotation.pageIndex + 1);
|
||||
var annot = _manager.AddTextAnnot((float) x, (float) y, (float) width, (float) height, formFieldValue.value);
|
||||
annot.FontName = _pdfBurnerParams.FontName;
|
||||
annot.FontSize = _pdfBurnerParams.FontSize;
|
||||
annot.FontStyle = _pdfBurnerParams.FontStyle;
|
||||
_manager.SaveAnnotationsToPage();
|
||||
manager.SelectPage(annotation.pageIndex + 1);
|
||||
var annot = manager.AddTextAnnot((float) x, (float) y, (float) width, (float) height, formFieldValue.value);
|
||||
annot.FontName = _options.FontName;
|
||||
annot.FontSize = _options.FontSize;
|
||||
annot.FontStyle = _options.FontStyle;
|
||||
manager.SaveAnnotationsToPage();
|
||||
}
|
||||
|
||||
private static PointF ToPointF(List<float> points)
|
||||
|
||||
Reference in New Issue
Block a user