LoadFilePreviewer
using kCura.EDDS.WebAPI.DocumentManagerBase;
using kCura.WinEDDS.Api;
using kCura.WinEDDS.CodeValidator;
using kCura.WinEDDS.Exceptions;
using Microsoft.VisualBasic.CompilerServices;
using Relativity.DataExchange;
using Relativity.DataExchange.Data;
using Relativity.DataExchange.Io;
using Relativity.DataExchange.Process;
using Relativity.DataExchange.Service;
using Relativity.Logging;
using System;
using System.Collections;
using System.Collections.Specialized;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading;
namespace kCura.WinEDDS
{
public class LoadFilePreviewer : LoadFileBase
{
public enum EventType
{
Begin,
Complete,
Progress
}
public class EventArgs
{
private long _bytesRead;
private long _totalBytes;
private long _stepSize;
private EventType _type;
public long BytesRead => _bytesRead;
public long TotalBytes => _totalBytes;
public long StepSize => _stepSize;
public EventType Type => _type;
public EventArgs(EventType type, long bytes, long total, long step)
{
_bytesRead = bytes;
_totalBytes = total;
_stepSize = step;
_type = type;
}
}
public delegate void OnEventEventHandler (EventArgs e);
private bool _errorsOnly;
[CompilerGenerated]
[AccessedThroughProperty("_processContext")]
private ProcessContext __processContext;
private bool _continue;
private string _nativeFileCheckColumnName;
private int _selectedCaseArtifactID;
public static string extractedTextEncodingFieldName = "Extracted Text Encoding";
private DocumentField[] _relationalDocumentFields;
private NameValueCollection _processedIdentifiers;
[CompilerGenerated]
private OnEventEventHandler OnEventEvent;
private virtual ProcessContext _processContext {
[CompilerGenerated]
get {
return __processContext;
}
[MethodImpl(MethodImplOptions.Synchronized)]
[CompilerGenerated]
set {
EventHandler<CancellationRequestEventArgs> value2 = _processContext_HaltProcessEvent;
ProcessContext _processContext = __processContext;
if (_processContext != null)
_processContext.CancellationRequest -= value2;
__processContext = value;
_processContext = __processContext;
if (_processContext != null)
_processContext.CancellationRequest += value2;
}
}
protected override bool UseTimeZoneOffset => false;
public event OnEventEventHandler OnEvent {
[CompilerGenerated]
add {
OnEventEventHandler onEventEventHandler = OnEventEvent;
OnEventEventHandler onEventEventHandler2;
do {
onEventEventHandler2 = onEventEventHandler;
OnEventEventHandler value2 = (OnEventEventHandler)Delegate.Combine(onEventEventHandler2, value);
onEventEventHandler = Interlocked.CompareExchange(ref OnEventEvent, value2, onEventEventHandler2);
} while ((object)onEventEventHandler != onEventEventHandler2);
}
[CompilerGenerated]
remove {
OnEventEventHandler onEventEventHandler = OnEventEvent;
OnEventEventHandler onEventEventHandler2;
do {
onEventEventHandler2 = onEventEventHandler;
OnEventEventHandler value2 = (OnEventEventHandler)Delegate.Remove(onEventEventHandler2, value);
onEventEventHandler = Interlocked.CompareExchange(ref OnEventEvent, value2, onEventEventHandler2);
} while ((object)onEventEventHandler != onEventEventHandler2);
}
}
public LoadFilePreviewer(LoadFile args, IIoReporter reporter, ILog logger, int timeZoneOffset, bool errorsOnly, bool doRetryLogic, CancellationTokenSource tokenSource, Func<string> correlationIdFunc, ProcessContext context = null)
: base(args, reporter, logger, timeZoneOffset, doRetryLogic, true, tokenSource, correlationIdFunc, null)
{
_continue = true;
_nativeFileCheckColumnName = "";
_processedIdentifiers = new NameValueCollection();
_selectedCaseArtifactID = args.CaseInfo.ArtifactID;
_errorsOnly = errorsOnly;
_processContext = context;
}
private void ProcessStart(long bytes, long total, long step)
{
RaiseOnEvent(EventType.Begin, bytes, total, step);
}
private void ProcessProgress(long bytes, long total, long step)
{
RaiseOnEvent(EventType.Progress, bytes, total, step);
}
private void ProcessComplete(long bytes, long total, long step)
{
RaiseOnEvent(EventType.Complete, bytes, total, step);
}
private void RaiseOnEvent(EventType type, long bytes, long total, long step)
{
OnEventEvent?.Invoke(new EventArgs(type, bytes, total, step));
}
public object ReadFile(string path, int formType)
{
bool flag = false;
_relationalDocumentFields = _fieldQuery.RetrieveAllAsDocumentFieldCollection(_selectedCaseArtifactID, _artifactTypeID).GetFieldsByCategory(global::Relativity.DataExchange.Service.FieldCategory.Relational);
long sizeInBytes = _artifactReader.SizeInBytes;
checked {
long step = (long)Math.Round(unchecked((double)sizeInBytes / 100));
ProcessStart(0, sizeInBytes, step);
ArrayList arrayList = new ArrayList();
_artifactReader.GetColumnNames(_settings);
if (_firstLineContainsColumnNames) {
if (_uploadFiles) {
int num = _filePathColumn.LastIndexOf('(') + 1;
int num2 = _filePathColumn.LastIndexOf(')');
_filePathColumnIndex = int.Parse(_filePathColumn.Substring(num, num2 - num)) - 1;
}
if (_artifactReader.HasMoreRecords)
_artifactReader.AdvanceRecord();
} else if (_uploadFiles) {
_filePathColumnIndex = int.Parse(_filePathColumn.Replace("Column", "").Replace("(", "").Replace(")", "")
.Trim()) - 1;
}
int num3 = 0;
while (_artifactReader.HasMoreRecords) {
if (!_continue)
break;
if (arrayList.Count >= AppSettings.Instance.PreviewThreshold) {
flag = true;
break;
}
try {
ArtifactFieldCollection record = _artifactReader.ReadArtifact();
ArtifactField[] array = CheckLine(record, formType);
if (array != null)
arrayList.Add(array);
} catch (ImporterException ex) {
ProjectData.SetProjectError(ex);
ImporterException value = ex;
arrayList.Add(value);
ProjectData.ClearProjectError();
}
num3++;
if (unchecked(num3 % 100) == 0)
ProcessProgress(_artifactReader.BytesProcessed, sizeInBytes, step);
}
if (flag)
ProcessComplete(-1, sizeInBytes, -1);
else
ProcessComplete(sizeInBytes, sizeInBytes, step);
_artifactReader.Close();
return arrayList;
}
}
private ArtifactField[] CheckLine(ArtifactFieldCollection record, int formType)
{
bool flag = false;
ArrayList arrayList = new ArrayList();
HybridDictionary hybridDictionary = new HybridDictionary();
HybridDictionary hybridDictionary2 = new HybridDictionary();
DocumentField[] relationalDocumentFields = _relationalDocumentFields;
foreach (DocumentField documentField in relationalDocumentFields) {
if (documentField.ImportBehavior.HasValue) {
int? nullable = (int?)documentField.ImportBehavior;
if ((nullable.HasValue ? new bool?(nullable.GetValueOrDefault() == 1) : null).GetValueOrDefault())
hybridDictionary.Add(documentField.FieldID, new ArtifactField(documentField));
}
}
ArtifactField artifactField = (_keyFieldID <= 0) ? record.IdentifierField : record[_keyFieldID];
int extractedTextCodePageId = -1;
IEnumerator enumerator = default(IEnumerator);
try {
enumerator = _fieldMap.GetEnumerator();
while (enumerator.MoveNext()) {
LoadFileFieldMap.LoadFileFieldMapItem loadFileFieldMapItem = (LoadFileFieldMap.LoadFileFieldMapItem)enumerator.Current;
if (loadFileFieldMapItem.NativeFileColumnIndex > -1 && loadFileFieldMapItem.DocumentField != null) {
ArtifactField artifactField2 = record[loadFileFieldMapItem.DocumentField.FieldID];
if (!((_artifactTypeID != 10) & (artifactField2.Type == global::Relativity.DataExchange.Service.FieldType.File))) {
switch (artifactField2.Category) {
case global::Relativity.DataExchange.Service.FieldCategory.Relational:
if (hybridDictionary.Contains(artifactField2.ArtifactID))
hybridDictionary.Remove(artifactField2.ArtifactID);
if (!hybridDictionary2.Contains(artifactField2.ArtifactID))
hybridDictionary2.Add(artifactField2.ArtifactID, artifactField2);
break;
case global::Relativity.DataExchange.Service.FieldCategory.Identifier:
if (_keyFieldID <= 0)
artifactField = artifactField2;
break;
}
flag |= SetFieldValueOrErrorMessage(artifactField2, loadFileFieldMapItem.NativeFileColumnIndex, artifactField.ValueAsString, ref extractedTextCodePageId, loadFileFieldMapItem.DocumentField.ImportBehavior);
arrayList.Add(artifactField2);
}
}
}
} finally {
if (enumerator is IDisposable)
(enumerator as IDisposable).Dispose();
}
int extractedTextCodePageId2;
if (artifactField != null && artifactField.Value != null) {
if (_processedIdentifiers[artifactField.Value.ToString()] == null) {
NameValueCollection processedIdentifiers = _processedIdentifiers;
string name = artifactField.Value.ToString();
extractedTextCodePageId2 = CurrentLineNumber;
processedIdentifiers[name] = extractedTextCodePageId2.ToString();
} else {
artifactField.Value = new ErrorMessage($"""{artifactField.Value.ToString()}""{_processedIdentifiers[artifactField.Value.ToString()]}""");
flag = true;
}
}
if (Operators.CompareString(_settings.OverwriteDestination.ToLower(), ((Enum)1).ToString().ToLower(), false) != 0 && ((artifactField != null) & (_artifactTypeID == 10))) {
IEnumerator enumerator2 = default(IEnumerator);
try {
enumerator2 = hybridDictionary.Values.GetEnumerator();
while (enumerator2.MoveNext()) {
ArtifactField artifactField3 = (ArtifactField)enumerator2.Current;
if (record.IdentifierField != null)
artifactField3.Value = NullableTypesHelper.ToEmptyStringOrValue(GetNullableFixedString(record.IdentifierField.ValueAsString, -1, artifactField3.TextLength, artifactField3.DisplayName));
bool num = flag;
ArtifactField field = artifactField3;
string valueAsString = artifactField.ValueAsString;
extractedTextCodePageId2 = -1;
flag = (num | SetFieldValueOrErrorMessage(field, -1, valueAsString, ref extractedTextCodePageId2, null));
arrayList.Add(artifactField3);
}
} finally {
if (enumerator2 is IDisposable)
(enumerator2 as IDisposable).Dispose();
}
}
if (_uploadFiles) {
if (Operators.CompareString(_nativeFileCheckColumnName, "", false) == 0)
SetNativeFileCheckColumnName(arrayList);
string valueAsString2 = record.FileField.ValueAsString;
string path = null;
if (valueAsString2.Length > 1)
path = ((valueAsString2[0] != '\\' || Operators.CompareString(Conversions.ToString(valueAsString2[1]), "\\", false) == 0) ? valueAsString2 : ("." + valueAsString2));
if (Operators.CompareString(valueAsString2, "", false) == 0)
record.FileField.Value = "No File Specified.";
else if (string.IsNullOrEmpty(GetExistingFilePath(path, true))) {
record.FileField.Value = new ErrorMessage($"""{valueAsString2}""");
flag = true;
} else {
record.FileField.Value = valueAsString2;
}
arrayList.Add(record.FileField);
}
if (_createFolderStructure) {
ArtifactField artifactField4 = record.get_FieldList(global::Relativity.DataExchange.Service.FieldCategory.ParentArtifact)[0];
if (_artifactTypeID != 10) {
if (Operators.CompareString(artifactField4.ValueAsString, string.Empty, false) == 0) {
artifactField4.Value = new ErrorMessage(new ParentObjectReferenceRequiredException(CurrentLineNumber, -1).Message);
flag = true;
} else if (_objectManager.RetrieveArtifactIdOfMappedParentObject(_caseArtifactID, artifactField4.ValueAsString, _artifactTypeID).Tables[0].Rows.Count > 1) {
artifactField4.Value = new ErrorMessage(new DuplicateObjectReferenceException(CurrentLineNumber, -1, "Parent Info").Message);
flag = true;
}
artifactField4.DisplayName = "Parent Object Identifier";
} else
artifactField4.DisplayName = "Parent Folder Identifier";
arrayList.Add(artifactField4);
}
if (_settings.FullTextColumnContainsFileLocation && formType == 1) {
ArtifactField artifactField5 = new ArtifactField(extractedTextEncodingFieldName, -500, global::Relativity.DataExchange.Service.FieldType.Varchar, global::Relativity.DataExchange.Service.FieldCategory.Generic, null, null, null, false);
if (extractedTextCodePageId > 0)
artifactField5.Value = Encoding.GetEncoding(extractedTextCodePageId).EncodingName;
else
artifactField5.Value = string.Empty;
arrayList.Add(artifactField5);
}
if (!_errorsOnly)
return (ArtifactField[])arrayList.ToArray(typeof(ArtifactField));
if (!flag)
return null;
return (ArtifactField[])arrayList.ToArray(typeof(ArtifactField));
}
private void SetNativeFileCheckColumnName(ArrayList fields)
{
string text = "Native File to Upload";
int num = 0;
do {
bool flag = false;
IEnumerator enumerator = default(IEnumerator);
try {
enumerator = fields.GetEnumerator();
while (enumerator.MoveNext()) {
ArtifactField artifactField = (ArtifactField)enumerator.Current;
if (Operators.CompareString(text, artifactField.DisplayName, false) == 0) {
flag = true;
break;
}
}
} finally {
if (enumerator is IDisposable)
(enumerator as IDisposable).Dispose();
}
if (!flag) {
_nativeFileCheckColumnName = text;
return;
}
text += " ";
num = checked(num + 1);
} while (num <= 100);
_nativeFileCheckColumnName = text;
}
private bool SetFieldValueOrErrorMessage(ArtifactField field, int column, string identityValue, ref int extractedTextCodePageId, kCura.EDDS.WebAPI.DocumentManagerBase.ImportBehaviorChoice? importBehavior)
{
try {
SetFieldValue(field, column, true, identityValue, ref extractedTextCodePageId, importBehavior);
return field.Value is Exception;
} catch (ImporterException ex) {
ProjectData.SetProjectError(ex);
ImporterException ex2 = ex;
field.Value = new ErrorMessage(ex2.Message);
bool result = true;
ProjectData.ClearProjectError();
return result;
}
}
private void _processContext_HaltProcessEvent(object sender, CancellationRequestEventArgs e)
{
_continue = false;
}
protected override Base GetSingleCodeValidator()
{
return new SinglePreviewer(_settings.CaseInfo, _codeManager);
}
protected override IArtifactReader GetArtifactReader()
{
return new LoadFileReader(_settings, true, GetCorrelationId, ExecutionSource.Unknown);
}
}
}