Click or drag to resize

Document Routing Sample

When processing reports with varied formats, it is often necessary to examine the title page or the first few pages of a report and then select an appropriate action.

In this example, we use Opait Report Miner API to identify a global date field called ReportDate on the title page. We check the value of this date and decide on how to further process the report.

C#
/// <summary>
/// A simple logging delegate to display engine messages to console.
/// </summary>
static void Log(string message, IEngine engine = null, IRecord record = null, LoggingType type = LoggingType.Message)
{
    if (message != null)
        Console.Write(message + Environment.NewLine);
}

C#
/// <summary>
/// A sample procedure to process or route a report file.
/// </summary>
/// <param name="model">Report Miner model XML file.</param>
/// <param name="report">Report file to be processed.</param>
/// <param name="plugin">Optional plugin DLL file.</param>
static void ProcessDocument(string model, string report, string plugin)
{
    try
    {
        // Create a new instance of the Report Miner processor.
        using (var processor = new Processor())
        {
            // Optionally attach a logging delegate to capture engine messages.
            processor.LoggingDelegate = Log;

            // Process the global fields. 
            var globals = processor.ParseGlobalFields(model, report, plugin);

            // Examine the global fields and decide whether to continue with the report or do something else.
            // For this example we assume that there is a global field named ReportDate, and that we only
            // want to process reports that are more recent than 1/1/2010.

            // We can find the field by enumerating the 'globals' collection above or 
            // ask the attached IEngine to find it for us.
            var reportDate = processor.Engine.GetGlobalField("ReportDate");

            // Parse the value of the field as date and do the comparison.
            if (reportDate != null  DateTime.TryParse(reportDate.Value, out var dateValue) && DateTime.Compare(dateValue, new DateTime(2010, 1, 1)) < 0)
            {
                Log($"Report '{report}' has date of '{dateValue}' and will be archived.");

                // We let the engine move the document to a relative or absolute location. Alternatively, we could move it 
                // ourselves, but would need to call processor.Dispose() to release the document first.
                var newPath = processor.Engine.MoveDocument(report, "OldReports");

                // Log the new location of the document.
                Log($"Document moved to '{newPath}'");
            }
            else
            {
                // Continue processing with the qualified document.
                var record = processor.ParseRecords();

                // Do something with the collection of parsed records.
            }
        }
    }
    catch (OperationCanceledException)
    {
        Log("Operation cancelled.");
    }
    catch (Exception ex)
    {
        Log("Exception: " + ex.Message);
    }
}