/* LaurisTimePoint.cs * part of zaaReloaded2 * * Copyright 2015 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.Dictionaries; using zaaReloaded2.LabModel; namespace zaaReloaded2.Importer.ZaaImporter { /// /// Holds all laboratory items for a given time point. /// class LaurisTimePoint : TimePoint { #region Properties /// /// Gets an array of paragraphs in this LaurisText. /// public string[] Paragraphs { get; private set; } /// /// Is true if the LaurisText has time stamp in the first /// paragraph and s in the others. /// public bool IsValidTimePoint { get; private set; } /// /// 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); ParseParagraphs(); } } } #endregion #region Constructors public LaurisTimePoint() { } public LaurisTimePoint(string laurisTest) : this() { _parameterDictionary = null; _unitDictionary = null; LaurisText = laurisTest; } public LaurisTimePoint( string laurisTest, ParameterDictionary parameterDictionary, UnitDictionary unitDictionary) : this() { _parameterDictionary = parameterDictionary; _unitDictionary = unitDictionary; LaurisText = laurisTest; } #endregion #region Private methods /// /// Analyzes each Lauris paragraph in this time point, sets the date /// and time, and collects LabItem data. /// /// True if the LaurisText has time stamp in the first paragraphs /// and contains s in the others. bool ParseParagraphs() { Items = new ItemDictionary(); if (Paragraphs.Length > 0) { if (!ParseTimeStamp()) return false; LaurisParagraph lp; if (IsValidTimePoint) { for (int i = 1; i < Paragraphs.Length; i++) { lp = new LaurisParagraph( Paragraphs[i], _parameterDictionary, _unitDictionary); if (lp.IsLaurisParagraph) { Items.Merge(lp.Items); } } } IsValidTimePoint = Items.Count > 0; } return true; } /// /// Analyzes the date and time information that is expected to be /// in the first paragraph. /// /// True if the LaurisText contains a time stamp in the /// first paragraph. bool ParseTimeStamp() { if (Paragraphs.Length == 0) throw new InvalidOperationException("The time point has no paragraphs."); Match m = _dateStampRegex.Match(Paragraphs[0]); bool success = false; if (m.Success) { DateTime dt; success = DateTime.TryParseExact( m.Groups["datetime"].Value, "dd.MM.yyyy HH:mm", CultureInfo.InvariantCulture, DateTimeStyles.AllowWhiteSpaces, out dt); TimeStamp = dt; } IsValidTimePoint = success; return success; } void AddItems(IItemDictionary items) { } #endregion #region Private fields /// /// A regular expression that matches the time stamp in the first /// paragraph of a LaurisText. /// static readonly Regex _dateStampRegex = new Regex( @"^\s*\[?\s*(?\d\d\.\d\d\.\d\d\d\d\s+\d\d:\d\d)"); ParameterDictionary _parameterDictionary; UnitDictionary _unitDictionary; #endregion } }