zaaReloaded2/zaaReloaded2/Importer/ZaaImporter/LaurisTimePoint.cs

180 lines
5.4 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.Dictionaries;
using zaaReloaded2.LabModel;
namespace zaaReloaded2.Importer.ZaaImporter
{
/// <summary>
/// Holds all laboratory items for a given time point.
/// </summary>
class LaurisTimePoint : TimePoint
{
#region Properties
/// <summary>
/// Gets an array of paragraphs in this LaurisText.
/// </summary>
public string[] Paragraphs { get; private set; }
/// <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);
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
/// <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()
{
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;
}
/// <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 = _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
/// <summary>
/// A regular expression that matches the time stamp in the first
/// paragraph of a LaurisText.
/// </summary>
static readonly Regex _dateStampRegex = new Regex(
@"^\s*\[?\s*(?<datetime>\d\d\.\d\d\.\d\d\d\d\s+\d\d:\d\d)");
ParameterDictionary _parameterDictionary;
UnitDictionary _unitDictionary;
#endregion
}
}