zaaReloaded2/zaaReloaded2/Importer/ClinicImporter/ClinicTimePoint.cs

259 lines
7.1 KiB
C#
Executable File

/* ClinicTimePoint.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.Globalization;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using zaaReloaded2.Thesaurus;
using zaaReloaded2.LabModel;
namespace zaaReloaded2.Importer.ClinicImporter
{
/// <summary>
/// Holds all laboratory items for a given time point.
/// </summary>
class ClinicTimePoint : TimePoint
{
#region Static methods
/// <summary>
/// Examines a string and returns true if it resembles
/// a time stamp line in the clinic output.
/// </summary>
/// <param name="line">Line to examine.</param>
/// <returns>True if line resembles a time stamp line
/// in the clinic output.</returns>
static public bool IsTimeStampLine(string line)
{
return _timeStampRegex.IsMatch(line);
}
/// <summary>
/// Gets a Regex object that matches a clinic time stamp
/// line.
/// </summary>
static public Regex TimeStampRegex
{
get
{
return _timeStampRegex;
}
}
#endregion
#region Properties
/// <summary>
/// Gets an array of lines in this ClinicText.
/// </summary>
public IList<String> Lines
{
[DebuggerStepThrough]
get
{
if (_lines == null)
{
_lines = new List<String>();
}
return _lines;
}
set
{
_lines = value;
ParseLines();
}
}
/// <summary>
/// Is true if the LaurisText has time stamp in the first
/// paragraph and <see cref="LabItem"/>s in the others.
/// </summary>
public bool IsValidTimePoint
{
get
{
return Items.Count > 0;
}
}
/// <summary>
/// Gets or sets the original Lauris text for this timepoint.
/// </summary>
public string ClinicText
{
[DebuggerStepThrough]
get
{
return String.Join(Environment.NewLine, Lines);
}
set
{
if (!String.IsNullOrEmpty(value))
{
Lines = value.Split(
new string[] { Environment.NewLine },
StringSplitOptions.None).ToList();
}
}
}
#endregion
#region Constructors
public ClinicTimePoint() { }
public ClinicTimePoint(
string clinicText,
Parameters parameterDictionary,
Units unitDictionary)
: this()
{
_parameterDictionary = parameterDictionary;
_unitDictionary = unitDictionary;
ClinicText = clinicText;
}
public ClinicTimePoint(string clinicText)
: this(clinicText, null, null)
{ }
public ClinicTimePoint(
IList<String> lines,
Parameters parameterDictionary,
Units unitDictionary)
: this(parameterDictionary, unitDictionary)
{
Lines = lines;
}
public ClinicTimePoint(IList<String> lines)
: this(lines, null, null)
{
}
public ClinicTimePoint(
Parameters parameterDictionary,
Units unitDictionary)
: this()
{
_parameterDictionary = parameterDictionary;
_unitDictionary = unitDictionary;
}
#endregion
#region Public methods
/// <summary>
/// Adds a new line to this time point by parsing
/// the line for a laboratory item.
/// </summary>
/// <param name="paragraph">Line to add.</param>
public override void Parse(string paragraph)
{
Lines.Add(paragraph);
ParseLine(paragraph);
}
#endregion
#region Private methods
/// <summary>
/// Analyzes each clinic line in this time point, sets the date
/// and time, and collects LabItem data.
/// </summary>
void ParseLines()
{
if (Lines != null)
{
foreach (string paragraph in Lines)
{
ParseLine(paragraph);
}
}
}
void ParseLine(string line)
{
Match m = _timeStampRegex.Match(line);
if (m.Success)
{
Logger.Info("ParseLine: Matches time stamp");
DateTime dt;
if (DateTime.TryParseExact(
m.Groups["datetime"].Value,
"dd.MM.yyyy HH:mm",
CultureInfo.InvariantCulture,
DateTimeStyles.AllowWhiteSpaces,
out dt))
{
TimeStamp = dt;
}
}
else
{
Logger.Info("ParseLine: Not a time stamp");
ClinicLine clinicLine = new ClinicLine(
line,
_parameterDictionary,
_unitDictionary);
if (clinicLine.IsClinicLine)
{
Logger.Debug("ParseParagraph: Merging Lauris paragraph");
Items[clinicLine.Item.QualifiedName] = clinicLine.Item;
}
}
}
void AddItems(IItemDictionary items)
{
Items.Merge(items);
}
#endregion
#region Private fields
/// <summary>
/// A regular expression that matches the time stamp in the first
/// paragraph of a LaurisText.
/// </summary>
static readonly Regex _timeStampRegex = new Regex(
@"^(Labor:?)?\s*\(?\s*(?<datetime>\d\d\.\d\d\.\d\d\d\d\s+\d\d:\d\d)(:00)?\)");
IList<String> _lines;
Parameters _parameterDictionary;
Units _unitDictionary;
#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
}
}