zaaReloaded2/zaaReloaded2/Importer/ClinicImporter/ClinicLine.cs

139 lines
4.3 KiB
C#
Executable File

/* ClinicLine.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.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using zaaReloaded2.Thesaurus;
using zaaReloaded2.LabModel;
using zaaReloaded2.Importer.ZaaImporter;
using Bovender.Extensions;
namespace zaaReloaded2.Importer.ClinicImporter
{
/// <summary>
/// Parses a line in a lab section produced by the outpatients clinic system,
/// and creates a list of <see cref="zaaReloaded2.Importer.BaseImporter.LabItem"/>s.
/// </summary>
public class ClinicLine
{
#region Static methods
/// <summary>
/// Investigates a paragraph and determines whether it looks
/// like a clinic laboratory items line.
/// </summary>
public static bool ResemblesClinicLine(string line)
{
return _expectedFormat.IsMatch(line);
}
#endregion
#region Public properties
public LaurisItem Item { get; private set; }
/// <summary>
/// Gets the original line that this object was constructed from.
/// </summary>
public string OriginalLine{ get; private set; }
/// <summary>
/// Is true if the <see cref="OriginalLine"/> matches the expected
/// format and contains <see cref="LabItem"/>s.
/// </summary>
public bool IsClinicLine{ get; private set; }
#endregion
#region Constructor
public ClinicLine(string line)
{
OriginalLine = line;
Parse();
}
/// <summary>
/// Constructs a <see cref="ClinicLine"/> object from a given line,
/// using a <paramref name="parameterDictionary"/> and a
/// <paramref name="unitDictionary"/> to translate the individual
/// items' properties.
/// </summary>
/// <param name="line">line to parse.</param>
/// <param name="parameterDictionary">ParameterDictionary that contains
/// canonical names and material types.</param>
/// <param name="unitDictionary">Unit dictionary that contains canonical
/// unit names.</param>
public ClinicLine(string line,
Thesaurus.Parameters parameterDictionary,
Thesaurus.Units unitDictionary)
{
OriginalLine = line;
_parameterDictionary = parameterDictionary;
_unitDictionary = unitDictionary;
Parse();
}
#endregion
#region Private methods
/// <summary>
/// Attempts to parse a line.
/// </summary>
void Parse()
{
Logger.Info("Parse: \"{0}\"", OriginalLine.TruncateWithEllipsis(40).Replace("\t", " "));
Match m = _expectedFormat.Match(OriginalLine);
if (m.Success)
{
Logger.Info("Parse: Matches clinic line format");
Item = new LaurisItem(m.Groups["item"].Value, _parameterDictionary, _unitDictionary);
IsClinicLine = true;
}
else
{
Logger.Info("Parse: Does not match clinic line format");
IsClinicLine = false;
}
}
#endregion
#region Fields
static readonly Regex _expectedFormat = new Regex(@"\t(?<item>[^:]+:(\t[^\t]+){3})");
Thesaurus.Parameters _parameterDictionary;
Thesaurus.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
}
}