zaaReloaded2/zaaReloaded2/Importer/ZaaImporter/LaurisTimePoint.cs

231 lines
6.7 KiB
C#
Executable File

/* 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.Thesaurus;
using zaaReloaded2.LabModel;
namespace zaaReloaded2.Importer.ZaaImporter
{
/// <summary>
/// Holds all laboratory items for a given time point.
/// </summary>
class LaurisTimePoint : TimePoint
{
#region Static methods
/// <summary>
/// Examines a string and returns true if it resembles
/// a time stamp line in the Lauris output.
/// </summary>
/// <param name="line">Line to examine.</param>
/// <returns>True if line resembles a time stamp line
/// in the Lauris output.</returns>
static public bool IsTimeStampLine(string line)
{
return _timeStampRegex.IsMatch(line);
}
/// <summary>
/// Gets a Regex object that matches a Lauris time stamp
/// line.
/// </summary>
static public Regex TimeStampRegex
{
get
{
return _timeStampRegex;
}
}
#endregion
#region Properties
/// <summary>
/// Gets an array of paragraphs in this LaurisText.
/// </summary>
public string[] Paragraphs
{
[DebuggerStepThrough]
get
{
return _paragraphs;
}
set
{
_paragraphs = value;
ParseParagraphs();
}
}
/// <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; private set; }
/// <summary>
/// Gets or sets the original Lauris text for this timepoint.
/// </summary>
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);
}
}
}
#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(
string[] paragraphs,
Parameters parameterDictionary,
Units unitDictionary)
: this()
{
_parameterDictionary = parameterDictionary;
_unitDictionary = unitDictionary;
Paragraphs = paragraphs;
}
public LaurisTimePoint(string[] paragraphs)
: this(paragraphs, null, null)
{
}
#endregion
#region Private methods
/// <summary>
/// Analyzes each Lauris paragraph in this time point, sets the date
/// and time, and collects LabItem data.
/// </summary>
/// <returns>True if the LaurisText has time stamp in the first paragraphs
/// and contains <see cref="LabItem"/>s in the others.</returns>
bool ParseParagraphs()
{
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;
}
/// <summary>
/// Analyzes the date and time information that is expected to be
/// in the first paragraph.
/// </summary>
/// <returns>True if the LaurisText contains a time stamp in the
/// first paragraph.</returns>
bool ParseTimeStamp()
{
if (Paragraphs.Length == 0)
throw new InvalidOperationException("The time point has no paragraphs.");
Match m = _timeStampRegex.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)
{
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(
@"^\s*\(?\s*(?<datetime>\d\d\.\d\d\.\d\d\d\d\s+\d\d:\d\d)");
string[] _paragraphs;
Parameters _parameterDictionary;
Units _unitDictionary;
#endregion
}
}