/* LaurisTimePoint.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.Globalization; using System.Linq; using System.Text; using System.Text.RegularExpressions; using zaaReloaded2.Thesaurus; using zaaReloaded2.LabModel; namespace zaaReloaded2.Importer.ZaaImporter { /// /// Holds all laboratory items for a given time point. /// class LaurisTimePoint : TimePoint { #region Static methods /// /// Examines a string and returns true if it resembles /// a time stamp line in the Lauris output. /// /// Line to examine. /// True if line resembles a time stamp line /// in the Lauris output. static public bool IsTimeStampLine(string line) { return _timeStampRegex.IsMatch(line); } /// /// Gets a Regex object that matches a Lauris time stamp /// line. /// static public Regex TimeStampRegex { get { return _timeStampRegex; } } #endregion #region Properties /// /// Gets an array of paragraphs in this LaurisText. /// public IList Paragraphs { [DebuggerStepThrough] get { if (_paragraphs == null) { _paragraphs = new List(); } return _paragraphs; } set { _paragraphs = value; ParseParagraphs(); } } /// /// 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 LaurisText { [DebuggerStepThrough] get { return String.Join(Environment.NewLine, Paragraphs); } set { if (!String.IsNullOrEmpty(value)) { Paragraphs = value.Split( new string[] { Environment.NewLine }, StringSplitOptions.None).ToList(); } } } #endregion #region Constructors public LaurisTimePoint() { } public LaurisTimePoint( string laurisText, Parameters parameterDictionary, Units unitDictionary) : this() { _parameterDictionary = parameterDictionary; _unitDictionary = unitDictionary; LaurisText = laurisText; } public LaurisTimePoint(string laurisText) : this(laurisText, null, null) { } public LaurisTimePoint( IList paragraphs, Parameters parameterDictionary, Units unitDictionary) : this(parameterDictionary, unitDictionary) { Paragraphs = paragraphs; } public LaurisTimePoint(IList paragraphs) : this(paragraphs, null, null) { } public LaurisTimePoint( Parameters parameterDictionary, Units unitDictionary) : this() { _parameterDictionary = parameterDictionary; _unitDictionary = unitDictionary; } #endregion #region Public methods /// /// Adds a new paragraph to this time point by parsing /// the paragraph for laboratory items. /// /// Paragraph to add. public void AddParagraph(string paragraph) { Paragraphs.Add(paragraph); ParseParagraph(paragraph); } #endregion #region Private methods /// /// Analyzes each Lauris paragraph in this time point, sets the date /// and time, and collects LabItem data. /// void ParseParagraphs() { if (Paragraphs != null) { foreach (string paragraph in Paragraphs) { ParseParagraph(paragraph); } } } void ParseParagraph(string paragraph) { Match m = _timeStampRegex.Match(paragraph); if (m.Success) { Logger.Info("ParseParagraph: 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; } // Put the remainder of the line back to the // 'paragraph' parameter to deal with exceptional // lines such as // "(17.09.2015-201710:44:00) Cyclosporin-A vor Gabe: 130 µg/l;" // We need to add a dummy caption because this is // the normal format of a paragraph generated from Lauris. if (m.Groups["tail"].Success) { Logger.Info("ParseParagraph: Time stamp has a tail -- putting it back with a dummy caption"); paragraph = "DUMMY CAPTION: " + m.Groups["tail"].Value; } else { paragraph = null; } } if (!String.IsNullOrEmpty(paragraph)) { Logger.Info("ParseParagraph: Not a time stamp"); LaurisParagraph lp = new LaurisParagraph( paragraph, _parameterDictionary, _unitDictionary); if (lp.IsLaurisParagraph) { Logger.Debug("ParseParagraph: Merging Lauris paragraph"); Items.Merge(lp.Items); } } } 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\)\s*(?.+)?)?$"); IList _paragraphs; 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 } }