/* 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 { /// /// Holds all laboratory items for a given time point. /// class ClinicTimePoint : TimePoint { #region Static methods /// /// Examines a string and returns true if it resembles /// a time stamp line in the clinic output. /// /// Line to examine. /// True if line resembles a time stamp line /// in the clinic output. static public bool IsTimeStampLine(string line) { return _timeStampRegex.IsMatch(line); } /// /// Gets a Regex object that matches a clinic time stamp /// line. /// static public Regex TimeStampRegex { get { return _timeStampRegex; } } #endregion #region Properties /// /// Gets an array of lines in this ClinicText. /// public IList Lines { [DebuggerStepThrough] get { if (_lines == null) { _lines = new List(); } return _lines; } set { _lines = value; ParseLines(); } } /// /// Is true if the LaurisText has time stamp in the first /// paragraph and s in the others. /// public bool IsValidTimePoint { get { return Items.Count > 0; } } /// /// Gets or sets the original Lauris text for this timepoint. /// 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 lines, Parameters parameterDictionary, Units unitDictionary) : this(parameterDictionary, unitDictionary) { Lines = lines; } public ClinicTimePoint(IList lines) : this(lines, null, null) { } public ClinicTimePoint( Parameters parameterDictionary, Units unitDictionary) : this() { _parameterDictionary = parameterDictionary; _unitDictionary = unitDictionary; } #endregion #region Public methods /// /// Adds a new line to this time point by parsing /// the line for a laboratory item. /// /// Line to add. public override void Parse(string paragraph) { Lines.Add(paragraph); ParseLine(paragraph); } #endregion #region Private methods /// /// Analyzes each clinic line in this time point, sets the date /// and time, and collects LabItem data. /// 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 /// /// A regular expression that matches the time stamp in the first /// paragraph of a LaurisText. /// static readonly Regex _timeStampRegex = new Regex( @"^(Labor:?)?\s*\(?\s*(?\d\d\.\d\d\.\d\d\d\d\s+\d\d:\d\d)(:00)?\)"); IList _lines; Parameters _parameterDictionary; Units _unitDictionary; #endregion #region Class logger private static NLog.Logger Logger { get { return _logger.Value; } } private static readonly Lazy _logger = new Lazy(() => NLog.LogManager.GetCurrentClassLogger()); #endregion } }