LongTextPrecedenceBuilder
using Castle.Core;
using kCura.WinEDDS;
using kCura.WinEDDS.Exporters;
using Relativity.DataExchange.Export.VolumeManagerV2.Directories;
using Relativity.DataExchange.Export.VolumeManagerV2.Download;
using Relativity.DataExchange.Export.VolumeManagerV2.Metadata.Text;
using Relativity.DataExchange.Export.VolumeManagerV2.Statistics;
using Relativity.DataExchange.Io;
using Relativity.DataExchange.Logger;
using Relativity.DataExchange.Service;
using Relativity.Logging;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
namespace Relativity.DataExchange.Export.VolumeManagerV2.Repository
{
public class LongTextPrecedenceBuilder : ILongTextBuilder
{
private readonly ExportFile _exportSettings;
private readonly IFilePathProvider _filePathProvider;
private readonly IFieldService _fieldService;
private readonly LongTextHelper _longTextHelper;
private readonly IFileNameProvider _fileNameProvider;
private readonly ILog _logger;
private readonly IExportFileValidator _exportFileValidator;
private readonly IMetadataProcessingStatistics _metadataProcessingStatistics;
public LongTextPrecedenceBuilder(ExportFile exportSettings, LongTextFilePathProvider filePathProvider, IFieldService fieldService, LongTextHelper longTextHelper, IFileNameProvider fileNameProvider, ILog logger, IExportFileValidator exportFileValidator, IMetadataProcessingStatistics metadataProcessingStatistics)
: this(exportSettings, (IFilePathProvider)filePathProvider, fieldService, longTextHelper, fileNameProvider, logger, exportFileValidator, metadataProcessingStatistics)
{
}
[DoNotSelect]
public LongTextPrecedenceBuilder(ExportFile exportSettings, IFilePathProvider filePathProvider, IFieldService fieldService, LongTextHelper longTextHelper, IFileNameProvider fileNameProvider, ILog logger, IExportFileValidator exportFileValidator, IMetadataProcessingStatistics metadataProcessingStatistics)
{
_exportSettings = exportSettings;
_filePathProvider = filePathProvider;
_fieldService = fieldService;
_longTextHelper = longTextHelper;
_fileNameProvider = fileNameProvider;
_logger = logger;
_exportFileValidator = exportFileValidator;
_metadataProcessingStatistics = metadataProcessingStatistics;
}
public IList<LongText> CreateLongText(ObjectExportInfo artifact, CancellationToken cancellationToken)
{
_logger.LogVerbose("Attempting to create LongText for TextPrecedence field.", Array.Empty<object>());
if (cancellationToken.IsCancellationRequested)
return Enumerable.Empty<LongText>().ToList();
kCura.WinEDDS.ViewFieldInfo fieldForLongTextPrecedenceDownload = GetFieldForLongTextPrecedenceDownload(artifact);
_logger.LogVerbose("Text Precedence is stored in field {fieldName}:{fieldId}.", new object[2] {
fieldForLongTextPrecedenceDownload.AvfColumnName.Secure(),
fieldForLongTextPrecedenceDownload.FieldArtifactId
});
if (_longTextHelper.IsTextTooLong(artifact, "Text Precedence"))
return CreateForTooLongText(artifact, fieldForLongTextPrecedenceDownload).InList();
return CreateForLongText(artifact, fieldForLongTextPrecedenceDownload).InList();
}
private LongText CreateForTooLongText(ObjectExportInfo artifact, kCura.WinEDDS.ViewFieldInfo field)
{
string destinationLocation = GetDestinationLocation(artifact);
LongTextExportRequest longTextExportRequest = CreateExportRequest(artifact, field, destinationLocation);
Encoding longTextFieldFileEncoding = _longTextHelper.GetLongTextFieldFileEncoding(field);
if (_exportSettings.ExportFullTextAsFile) {
if (CanExport(destinationLocation)) {
_logger.LogVerbose("LongText file missing - creating ExportRequest to destination file.", Array.Empty<object>());
return LongText.CreateFromMissingFile(artifact.ArtifactID, longTextExportRequest.FieldArtifactId, longTextExportRequest, longTextFieldFileEncoding, _exportSettings.TextFileEncoding, artifact.LongTextLength);
}
_logger.LogVerbose("File {file} exists and won't be overwritten - updating statistics.", new object[1] {
destinationLocation.Secure()
});
_metadataProcessingStatistics.UpdateStatisticsForFile(destinationLocation);
_logger.LogWarning("LongText file already exists and cannot overwrite - creating ExportRequest from existing file. Assuming that file encoding is the same as selected.", Array.Empty<object>());
return LongText.CreateFromExistingFile(artifact.ArtifactID, longTextExportRequest.FieldArtifactId, destinationLocation, _exportSettings.TextFileEncoding, artifact.LongTextLength);
}
_logger.LogVerbose("LongText file missing - creating ExportRequest to temporary file.", Array.Empty<object>());
return LongText.CreateFromMissingValue(artifact.ArtifactID, longTextExportRequest.FieldArtifactId, longTextExportRequest, longTextFieldFileEncoding, artifact.LongTextLength);
}
private LongText CreateForLongText(ObjectExportInfo artifact, kCura.WinEDDS.ViewFieldInfo field)
{
string textFromField = _longTextHelper.GetTextFromField(artifact, "Text Precedence");
if (_exportSettings.ExportFullTextAsFile) {
string destinationLocation = GetDestinationLocation(artifact);
if (CanExport(destinationLocation)) {
_logger.LogVerbose("LongText value exists - writing it to destination file {location}.", new object[1] {
destinationLocation.Secure()
});
using (StreamWriter streamWriter = new StreamWriter(destinationLocation, false, _exportSettings.TextFileEncoding))
streamWriter.Write(textFromField);
} else
_logger.LogVerbose("LongText file already exists and cannot overwrite - using existing file {location}.", new object[1] {
destinationLocation.Secure()
});
_logger.LogVerbose("File {file} exists or has been created from metadata - updating statistics.", new object[1] {
destinationLocation.Secure()
});
_metadataProcessingStatistics.UpdateStatisticsForFile(destinationLocation);
return LongText.CreateFromExistingFile(artifact.ArtifactID, field.FieldArtifactId, destinationLocation, _exportSettings.TextFileEncoding, artifact.LongTextLength);
}
_logger.LogVerbose("LongText value exists - storing it into memory.", Array.Empty<object>());
return LongText.CreateFromExistingValue(artifact.ArtifactID, field.FieldArtifactId, textFromField);
}
private LongTextExportRequest CreateExportRequest(ObjectExportInfo artifact, kCura.WinEDDS.ViewFieldInfo field, string destinationLocation)
{
if (_exportSettings.ArtifactTypeID == 10 && field.Category == FieldCategory.FullText && !(field is CoalescedTextViewField))
return LongTextExportRequest.CreateRequestForFullText(artifact, field.FieldArtifactId, destinationLocation);
kCura.WinEDDS.ViewFieldInfo fieldForLongTextPrecedenceDownload = GetFieldForLongTextPrecedenceDownload(artifact, field);
return LongTextExportRequest.CreateRequestForLongText(artifact, fieldForLongTextPrecedenceDownload.FieldArtifactId, destinationLocation);
}
private kCura.WinEDDS.ViewFieldInfo GetFieldForLongTextPrecedenceDownload(ObjectExportInfo artifact)
{
int fieldArtifactId = (int)artifact.Metadata[_fieldService.GetOrdinalIndex("KCURA FULL TEXT SOURCE")];
return _exportSettings.SelectedTextFields.First((kCura.WinEDDS.ViewFieldInfo x) => x.FieldArtifactId == fieldArtifactId);
}
private kCura.WinEDDS.ViewFieldInfo GetFieldForLongTextPrecedenceDownload(ObjectExportInfo artifact, kCura.WinEDDS.ViewFieldInfo field)
{
if (field == null || field.AvfColumnName == "Text Precedence")
return GetFieldForLongTextPrecedenceDownload(artifact);
return field;
}
private string GetDestinationLocation(ObjectExportInfo artifact)
{
if (_exportSettings.ExportFullTextAsFile) {
string textName = _fileNameProvider.GetTextName(artifact);
return _filePathProvider.GetPathForFile(textName, artifact.ArtifactID);
}
return TempFileBuilder.GetTempFileName("rel-long-text");
}
private bool CanExport(string destinationLocation)
{
string warningUserMessage = "Overwriting text file " + destinationLocation + ".";
return _exportFileValidator.CanExport(destinationLocation, warningUserMessage);
}
}
}