zaaReloaded2/zaaReloaded2/Importer/ZaaImporter/ZaaImporter.cs

145 lines
4.6 KiB
C#
Executable File

/* ZaaImporter.cs
* part of zaaReloaded2
*
* Copyright 2015-2017 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.Diagnostics;
using System.Linq;
using System.Text;
using Bovender.Extensions;
using zaaReloaded2.LabModel;
using zaaReloaded2.Thesaurus;
namespace zaaReloaded2.Importer.ZaaImporter
{
/// <summary>
/// Imports laboratory items by parsing the Lauris data from a
/// physician's letter.
/// </summary>
public class ZaaImporter : IImporter
{
#region IImporter implementation
public Laboratory Laboratory
{
[DebuggerStepThrough]
get
{
if (_laboratory == null)
{
_laboratory = new Laboratory();
}
return _laboratory;
}
[DebuggerStepThrough]
set
{
_laboratory = value;
}
}
public bool Success
{
get
{
return Laboratory.TimePoints.Count > 0;
}
}
/// <summary>
/// Splits the <paramref name="text"/> into individual time points
/// and creates <see cref="LaurisTimePoint"/> objects from them.
/// </summary>
/// <param name="text">ZAA-formatted Lauris output to import.</param>
public void Import(string text)
{
Logger.Info("Import: \"{0}\"", text.TruncateWithEllipsis(120));
string[] paragraphs = Helpers.SplitParagraphs(text);
Logger.Info("Import: {0} paragraph(s)", paragraphs.Length);
LaurisTimePoint timePoint = null;
foreach (string paragraph in paragraphs)
{
Logger.Info("Import: \"{0}\"", paragraph.TruncateWithEllipsis(40));
// If the current paragraph looks like a Lauris time stamp,
// create a new time point.
if (LaurisTimePoint.IsTimeStampLine(paragraph))
{
Logger.Info("Import: Time stamp detected", paragraph);
timePoint = new LaurisTimePoint(paragraph, _parameters, _units);
// Add the time point to the laboratory only if none
// with the same time stamp exists yet.
TimePoint existing = null;
if (Laboratory.TryGetTimePoint(timePoint.TimeStamp, ref existing))
{
timePoint = existing as LaurisTimePoint;
}
else
{
Laboratory.AddTimePoint(timePoint);
}
}
// If the current paragraph looks like a paragraph with
// laboratory items, add it to the current time point;
// if no time point exists yet, create one.
else if (LaurisParagraph.ResemblesLaurisParagraph(paragraph))
{
Logger.Info("Import: Lauris paragraph detected");
if (timePoint == null)
{
timePoint = new LaurisTimePoint(_parameters, _units);
Laboratory.AddTimePoint(timePoint);
}
timePoint.AddParagraph(paragraph);
}
else
{
Logger.Debug("Import: Neither time stamp, nor Lauris paragraph");
}
}
}
#endregion
#region Constructor
public ZaaImporter()
{
_parameters = Parameters.Default;
_units = Units.Default;
}
#endregion
#region Fields
Laboratory _laboratory;
Parameters _parameters;
Units _units;
#endregion
#region Class logger
private static NLog.Logger Logger { get { return _logger.Value; } }
private static readonly Lazy<NLog.Logger> _logger = new Lazy<NLog.Logger>(() => NLog.LogManager.GetCurrentClassLogger());
#endregion
}
}