Aras.Server.Core.CallContext CCO = ((Aras.Server.Core.IOMConnection)serverConnection).CCO;
Aras.Server.Core.IContextState RequestState = CCO.RequestState;
if (System.Diagnostics.Debugger.Launch()) System.Diagnostics.Debugger.Break();
var innovator = this.getInnovator();
IDataAccessLayer dataAccessLayer = new DataAccessLayer(innovator);
BusinessLogic businessLogic = new BusinessLogic(dataAccessLayer, innovator);
return businessLogic.Ocr(this, CCO);
public class BusinessLogic
private static class Constants
internal static class Actions
internal const string Delete = "delete";
internal static class ItemTypeNames
internal const string LineSequence = "c_line_sequence";
private readonly IDataAccessLayer dataAccessLayer;
private readonly Innovator innovator = null;
public BusinessLogic(IDataAccessLayer dataAccessLayer, Innovator innovator)
this.dataAccessLayer = dataAccessLayer;
this.innovator = innovator;
public string TempFolder { get; set; }
public string PrimaryFileName { get; set; }
public string PrimaryTempFilePath { get; set; }
public string OcrFileName { get; set; }
public string OcrTempFilePath { get; set; }
public Item Ocr(Item document, Aras.Server.Core.CallContext cco)
string aml = RemoveExistingOcrFiles(document);
FileInfo fileInfo = GetFileInfo(document, cco);
string ocrText = OcrPdf(fileInfo.PrimaryTempFilePath);
CreateFile(fileInfo.OcrTempFilePath, ocrText);
var ocrFile = MoveOcrFileToVault(fileInfo);
AddOcrFileToDocument(document, ocrFile);
CleanFiles(fileInfo, aml);
private FileInfo GetFileInfo(Item document, Aras.Server.Core.CallContext cco)
var primaryFile = GetPrirmaryFile(document);
FileInfo fileInfo = new FileInfo
TempFolder = GetTempFolderPath(cco),
PrimaryFileName = primaryFile.getProperty("filename")
fileInfo.PrimaryTempFilePath = $@"{ fileInfo.TempFolder}\{fileInfo.PrimaryFileName}";
fileInfo.OcrFileName = fileInfo.PrimaryFileName.Replace(".pdf", ".txt");
fileInfo.OcrTempFilePath = fileInfo.PrimaryTempFilePath.Replace(".pdf", ".txt");
primaryFile.fetchFileProperty("id", $@"{ fileInfo.TempFolder}\{fileInfo.PrimaryFileName}", FetchFileMode.Normal);
private string RemoveExistingOcrFiles(Item document)
document.fetchRelationships("Document File", "related_id");
var documentFiles = document.getRelationships("Document File");
int count = documentFiles.getItemCount();
StringBuilder sb = new StringBuilder("<AML>");
for (int i = 0; i < count; i++)
var documentFile = documentFiles.getItemByIndex(i);
sb.AppendLine($"<Item type='Document File' typeId='653C8EE5DDDF4932B75E5CDF04F0CD42' id='{documentFile.getID()}' action='delete'/>");
sb.AppendLine($"<Item type='File' id='{documentFile.getProperty("related_id")}' action='delete' />");
return sb.Append("</AML>").ToString();
private void CleanFiles(FileInfo fileInfo, string removeExistingOcrFilesAml)
if (File.Exists(fileInfo.PrimaryTempFilePath))
File.Delete(fileInfo.PrimaryTempFilePath);
if (File.Exists(fileInfo.OcrTempFilePath))
File.Delete(fileInfo.OcrTempFilePath);
dataAccessLayer.ApplyAml(removeExistingOcrFilesAml);
private Item MoveOcrFileToVault(FileInfo fileInfo)
Item ocrFile = innovator.newItem("File", "add");
ocrFile.setProperty("filename", fileInfo.OcrFileName);
ocrFile.attachPhysicalFile(fileInfo.OcrTempFilePath);
ocrFile.setAttribute("version", "0");
ocrFile = ocrFile.apply();
throw new Aras.Server.Core.InnovatorServerException("Failed to attach ocr file.");
private Item AddOcrFileToDocument(Item document, Item ocrFile)
Item documentFile = innovator.newItem("Document File", "add");
documentFile.setProperty("source_id", document.getID());
documentFile.setProperty("related_id", ocrFile.getID());
if (documentFile.isError())
throw new Aras.Server.Core.InnovatorServerException("failed to attach file");
private string OcrPdf(string filePath)
if (File.Exists(filePath))
var Ocr = new IronOcr.IronTesseract();
using (var input = new IronOcr.OcrInput())
var Result = Ocr.Read(input);
private void CreateFile(string filePath, string text)
using (StreamWriter ocrStream = File.CreateText(filePath))
private Item GetPrirmaryFile(Item docuement)
string primaryFileId = docuement.getProperty("primary_file");
Item primaryFile = innovator.newItem("File", "get");
primaryFile.setID(primaryFileId);
primaryFile = primaryFile.apply();
if (primaryFile.isError())
throw new Aras.Server.Core.InnovatorServerException("failed to get primary file");
public static string GetTempFolderPath(Aras.Server.Core.CallContext cco)
XmlDocument configXml = cco.Cache.GetCacheInfo("ApplicationXML");
var operatingParameterTempFolder = configXml.SelectSingleNode("//Innovator/operating_parameter[@key='temp_folder']");
return operatingParameterTempFolder.Attributes["value"].Value;
public interface IDataAccessLayer
Item ApplyAml(string aml);
Item ApplySql(string sql);
public class DataAccessLayer : IDataAccessLayer
private readonly Innovator innovator = null;
public DataAccessLayer(Innovator innovator)
this.innovator = innovator;
public Item ApplyAml(string aml)
return innovator.applyAML(aml);
public Item ApplySql(string sql)
return innovator.applySQL(sql);
public void EndOfMethod()