LongTextRepository
using Relativity.DataExchange.Export.VolumeManagerV2.Download;
using Relativity.DataExchange.Export.VolumeManagerV2.Metadata.Text;
using Relativity.DataExchange.Io;
using Relativity.DataExchange.Logger;
using Relativity.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
namespace Relativity.DataExchange.Export.VolumeManagerV2.Repository
{
public class LongTextRepository : IClearable, ILongTextRepository
{
private List<LongText> _longTexts;
private Dictionary<int, List<LongText>> _longTextsByArtifactIdDictionary;
private List<LongTextExportRequest> _exportRequests;
private readonly IFile _fileWrapper;
private readonly ILog _logger;
private readonly object _syncLock = new object();
public LongTextRepository(IFile fileWrapper, ILog logger)
{
_fileWrapper = fileWrapper;
_logger = logger;
InitializeCollections();
}
public void Add(IList<LongText> longTexts)
{
lock (_syncLock) {
_longTexts.AddRange(longTexts);
IndexLongTexts(longTexts);
}
}
public string GetTextFileLocation(int artifactId, int fieldArtifactId)
{
return GetLongText(artifactId, fieldArtifactId).Location;
}
public LongText GetLongText(int artifactId, int fieldArtifactId)
{
lock (_syncLock) {
return GetArtifactLongTexts(artifactId).First((LongText x) => x.FieldArtifactId == fieldArtifactId);
}
}
public IList<LongText> GetLongTexts()
{
lock (_syncLock) {
return _longTexts;
}
}
public IEnumerable<LongTextExportRequest> GetExportRequests()
{
lock (_syncLock) {
return _exportRequests;
}
}
public bool AnyRequestForLocation(string destinationLocation)
{
if (string.IsNullOrWhiteSpace(destinationLocation))
return false;
lock (_syncLock) {
return GetExportRequests().Any((LongTextExportRequest x) => string.Compare(x.DestinationLocation, destinationLocation, StringComparison.OrdinalIgnoreCase) == 0);
}
}
public LongText GetByLineNumber(int lineNumber)
{
lock (_syncLock) {
return _longTexts.FirstOrDefault(delegate(LongText x) {
if (x.ExportRequest != null)
return x.ExportRequest.Order == lineNumber;
return false;
});
}
}
public IEnumerable<LongText> GetArtifactLongTexts(int artifactId)
{
lock (_syncLock) {
object result;
if (!_longTextsByArtifactIdDictionary.TryGetValue(artifactId, out List<LongText> value))
result = Enumerable.Empty<LongText>();
else {
IEnumerable<LongText> enumerable = value;
result = enumerable;
}
return (IEnumerable<LongText>)result;
}
}
public void Clear()
{
lock (_syncLock) {
foreach (LongText longText in _longTexts) {
if (longText.RequireDeletion) {
_logger.LogInformation("Removing long text temp file {file}.", new object[1] {
longText.Location.Secure()
});
try {
_fileWrapper.Delete(longText.Location);
} catch (Exception) {
_logger.LogError("Failed to delete temp file {file} with LongText.", new object[1] {
longText.Location.Secure()
});
}
}
}
InitializeCollections();
}
}
private void IndexLongTexts(IList<LongText> longTexts)
{
foreach (LongText longText in longTexts) {
IndexLongText(longText);
}
}
private void IndexLongText(LongText longText)
{
GetOrCreateLongTextListInDictionary(longText).Add(longText);
LongTextExportRequest exportRequest = longText.ExportRequest;
if (exportRequest != null)
_exportRequests.Add(exportRequest);
}
private List<LongText> GetOrCreateLongTextListInDictionary(LongText longText)
{
int artifactId = longText.ArtifactId;
if (!_longTextsByArtifactIdDictionary.TryGetValue(artifactId, out List<LongText> value)) {
value = new List<LongText>();
_longTextsByArtifactIdDictionary[artifactId] = value;
}
return value;
}
private void InitializeCollections()
{
_longTexts = new List<LongText>();
_longTextsByArtifactIdDictionary = new Dictionary<int, List<LongText>>();
_exportRequests = new List<LongTextExportRequest>();
}
}
}