zaaReloaded2/zaaReloaded2/Medication/Importer.cs

113 lines
3.1 KiB
C#
Executable File

using Microsoft.Office.Interop.Word;
/* Importer.cs
* part of zaaReloaded2
*
* Copyright 2015 Daniel Kraus
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace zaaReloaded2.Medication
{
/// <summary>
/// Imports prescriptions from a physician's letter.
/// </summary>
public class Importer
{
#region Static methods
/// <summary>
/// Attempts to automatically detect a block of prescriptions
/// in a document. The document is screened from end to start.
/// The detected block is selected.
/// </summary>
/// <returns>True if a block was detected, false if not.</returns>
public static bool AutoDetect(Document document)
{
Paragraph start = null;
Paragraph end = null;
int i = document.Paragraphs.Count;
while (i > 1)
{
string line = document.Paragraphs[i].Range.Text;
if (Prescription.IsPrescriptionLine(line))
{
end = document.Paragraphs[i];
break;
}
i--;
}
if (end != null)
{
start = end;
while (i > 2)
{
if (!Prescription.IsPrescriptionLine(document.Paragraphs[i - 1].Range.Text))
{
start = document.Paragraphs[i];
break;
}
i--;
}
document.Range(start.Range.Start, end.Range.End).Select();
return true;
}
return false;
}
#endregion
#region Properties
public List<Prescription> Prescriptions { get; protected set; }
#endregion
#region Constructor
public Importer() { }
public Importer(string text)
: this()
{
Import(text);
}
#endregion
#region Private methods
protected virtual void Import(string text)
{
Prescriptions = new List<Prescription>();
string[] lines = Helpers.SplitParagraphs(text);
foreach (string line in lines)
{
if (Prescription.IsPrescriptionLine(line))
{
Prescriptions.AddRange(Prescription.ManyFromLine(line));
}
}
}
#endregion
}
}