zaaReloaded2/zaaReloaded2/Importer/BaseImporter.cs

171 lines
5.4 KiB
C#
Executable File

/* ZaaImporter.cs
* part of zaaReloaded2
*
* Copyright 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
{
/// <summary>
/// Base class for certain importers such as ZaaImporter, ClinicImporter.
/// </summary>
public class BaseImporter : 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="TimePoint"/> objects from them.
/// </summary>
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);
TimePoint timePoint = null;
foreach (string paragraph in paragraphs)
{
Logger.Info("Import: \"{0}\"", paragraph.TruncateWithEllipsis(40));
// If the current paragraph looks like a time stamp,
// create a new time point.
if (IsTimeStamp(paragraph))
{
Logger.Info("Import: Time stamp detected", paragraph);
timePoint = CreateTimePoint(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;
}
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 (IsItemsParagraph(paragraph))
{
Logger.Info("Import: Paragraph with lab items detected");
if (timePoint == null)
{
timePoint = CreateTimePoint(paragraph, _parameters, _units);
Laboratory.AddTimePoint(timePoint);
}
else
{
timePoint.Parse(paragraph);
}
}
else
{
Logger.Debug("Import: Neither time stamp, nor Lauris paragraph");
}
}
}
#endregion
#region Constructor
public BaseImporter()
{
_parameters = Parameters.Default;
_units = Units.Default;
}
#endregion
#region Virtual methods
protected virtual TimePoint CreateTimePoint(string paragraph, Parameters parameters, Units units)
{
NotImplementedException e = new NotImplementedException("Cannot create TimePoint in base class - derived class must override CreateTimePoint");
Logger.Fatal(e);
throw e;
}
protected virtual bool IsTimeStamp(string paragraph)
{
NotImplementedException e = new NotImplementedException("Cannot test time stamp in base class - derived class must override IsTimeStamp");
Logger.Fatal(e);
throw e;
}
protected virtual bool IsItemsParagraph(string paragraph)
{
NotImplementedException e = new NotImplementedException("Cannot test items in base class - derived class must override IsItemsParagraph");
Logger.Fatal(e);
throw e;
}
#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
}
}