Implement items wildcards.

This commit is contained in:
Daniel Kraus 2015-07-19 08:28:40 +02:00
parent ae66a9dfd2
commit 659713abe3
2 changed files with 137 additions and 22 deletions

View File

@ -36,6 +36,7 @@ namespace Tests.Controller.Elements
public void SetUp() public void SetUp()
{ {
_formatter = new zaaReloaded2.Formatter.Formatter(new Document()); _formatter = new zaaReloaded2.Formatter.Formatter(new Document());
_formatter.Settings.ReferenceStyle = ReferenceStyle.IfAbnormal;
} }
[TearDown] [TearDown]
@ -55,8 +56,8 @@ namespace Tests.Controller.Elements
// We do not add a 'Cl' item, and it should not appear in output. // We do not add a 'Cl' item, and it should not appear in output.
lab.AddTimePoint(tp); lab.AddTimePoint(tp);
_formatter.Settings.ReferenceStyle = ReferenceStyle.IfAbnormal;
_formatter.Laboratory = lab; _formatter.Laboratory = lab;
_formatter.SelectFirstDay();
_formatter.Settings.Elements.Add(new zaa.Items("Na, K, Cl")); _formatter.Settings.Elements.Add(new zaa.Items("Na, K, Cl"));
_formatter.Run(); _formatter.Run();
Assert.AreEqual("Na 133, K 6 (5)\r", _formatter.Document.Range().Text); Assert.AreEqual("Na 133, K 6 (5)\r", _formatter.Document.Range().Text);
@ -73,8 +74,8 @@ namespace Tests.Controller.Elements
// We do not add a 'Cl' item, and it should not appear in output. // We do not add a 'Cl' item, and it should not appear in output.
lab.AddTimePoint(tp); lab.AddTimePoint(tp);
_formatter.Settings.ReferenceStyle = ReferenceStyle.IfAbnormal;
_formatter.Laboratory = lab; _formatter.Laboratory = lab;
_formatter.SelectFirstDay();
_formatter.Settings.Elements.Add(new zaa.Items("Klinische Chemie: Na, K, Cl")); _formatter.Settings.Elements.Add(new zaa.Items("Klinische Chemie: Na, K, Cl"));
_formatter.Run(); _formatter.Run();
Assert.AreEqual("Klinische Chemie: Na 133, K 6 (5)\r", _formatter.Document.Range().Text); Assert.AreEqual("Klinische Chemie: Na 133, K 6 (5)\r", _formatter.Document.Range().Text);
@ -91,11 +92,73 @@ namespace Tests.Controller.Elements
// We do not add a 'Cl' item, and it should not appear in output. // We do not add a 'Cl' item, and it should not appear in output.
lab.AddTimePoint(tp); lab.AddTimePoint(tp);
_formatter.Settings.ReferenceStyle = ReferenceStyle.IfAbnormal;
_formatter.Laboratory = lab; _formatter.Laboratory = lab;
_formatter.SelectFirstDay();
_formatter.Settings.Elements.Add(new zaa.Items("Klinische Chemie: this, does, not, exist")); _formatter.Settings.Elements.Add(new zaa.Items("Klinische Chemie: this, does, not, exist"));
_formatter.Run(); _formatter.Run();
Assert.AreEqual("\r", _formatter.Document.Range().Text); Assert.AreEqual("\r", _formatter.Document.Range().Text);
} }
[Test]
public void GenericItemsWildcard()
{
Laboratory lab = new Laboratory();
TimePoint tp = new TimePoint();
tp.TimeStamp = new DateTime(2015, 7, 13, 13, 31, 00);
tp.AddItem(new LabItem("Na", "133", ""));
tp.AddItem(new LabItem("K", "6", ""));
tp.AddItem(new LabItem("Cl", "110", ""));
lab.AddTimePoint(tp);
_formatter.Settings.ReferenceStyle = ReferenceStyle.Never;
_formatter.Laboratory = lab;
_formatter.SelectFirstDay();
_formatter.Settings.Elements.Add(new zaa.Items("Klinische Chemie: Na, *"));
_formatter.Run();
Assert.AreEqual("Klinische Chemie: Na 133, Cl 110, K 6\r",
_formatter.Document.Range().Text);
}
[Test]
public void MaterialWildcard()
{
Laboratory lab = new Laboratory();
TimePoint tp = new TimePoint();
tp.TimeStamp = new DateTime(2015, 7, 13, 13, 31, 00);
tp.AddItem(new LabItem("Na", "133", ""));
tp.AddItem(new LabItem("U-Na", "99", ""));
tp.AddItem(new LabItem("Cl", "110", ""));
tp.AddItem(new LabItem("SU-Protein", "2.8", ""));
lab.AddTimePoint(tp);
_formatter.Settings.ReferenceStyle = ReferenceStyle.Never;
_formatter.Laboratory = lab;
_formatter.SelectFirstDay();
_formatter.Settings.Elements.Add(new zaa.Items("Klinische Chemie: Na, SU-*"));
_formatter.Run();
Assert.AreEqual("Klinische Chemie: Na 133, SU-Protein 2,8\r",
_formatter.Document.Range().Text);
}
[Test]
public void MaterialAndGenericWildcard()
{
Laboratory lab = new Laboratory();
TimePoint tp = new TimePoint();
tp.TimeStamp = new DateTime(2015, 7, 13, 13, 31, 00);
tp.AddItem(new LabItem("Na", "133", ""));
tp.AddItem(new LabItem("U-Na", "99", ""));
tp.AddItem(new LabItem("Cl", "110", ""));
tp.AddItem(new LabItem("SU-Protein", "2.8", ""));
lab.AddTimePoint(tp);
_formatter.Settings.ReferenceStyle = ReferenceStyle.Never;
_formatter.Laboratory = lab;
_formatter.SelectFirstDay();
_formatter.Settings.Elements.Add(new zaa.Items("Klinische Chemie: Na, SU-*, *"));
_formatter.Run();
Assert.AreEqual("Klinische Chemie: Na 133, SU-Protein 2,8, Cl 110, U-Na 99\r",
_formatter.Document.Range().Text);
}
} }
} }

View File

@ -18,7 +18,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using System.Diagnostics; using System.Diagnostics;
using Microsoft.Office.Interop.Word; using Microsoft.Office.Interop.Word;
@ -43,26 +42,13 @@ namespace zaaReloaded2.Controller.Elements
public override void Run(zaaReloaded2.Formatter.Formatter formatter) public override void Run(zaaReloaded2.Formatter.Formatter formatter)
{ {
if (_items == null || _items.Count == 0) return;
bool _needComma = false; bool _needComma = false;
// Find out if we have any items that we can write // Find out if we have any items that we can write
// to the document // to the document
List<ItemFormatter> items = new List<ItemFormatter>(); List<ItemFormatter> items = CollectItems(formatter);
if (_items != null && _items.Count > 0)
{
foreach (string itemName in _items)
{
TimePointFormatter tpf = formatter.WorkingTimePoints
.FirstOrDefault(tp => tp.Value.ContainsItem(itemName))
.Value;
if (tpf != null)
{
// If tpf is not null, this means that it contains an
// item with itemName.
items.Add(tpf.ItemFormatters[itemName]);
}
}
}
// If there are items, write the caption (if any), then the items // If there are items, write the caption (if any), then the items
if (items.Count > 0) if (items.Count > 0)
@ -83,7 +69,7 @@ namespace zaaReloaded2.Controller.Elements
{ {
_needComma = true; _needComma = true;
} }
i.WriteToDocument(formatter.Document); i.WriteToDocument(formatter);
} }
} }
} }
@ -135,7 +121,7 @@ namespace zaaReloaded2.Controller.Elements
{ {
_items = null; _items = null;
_caption = null; _caption = null;
Regex r = new Regex(@"((?<caption>[^:]+):\s*)?((?<items>[^,]+),\s*)+"); Regex r = new Regex(@"((?<caption>[^:]+):\s*)?((?<items>[^,]+),\s*)*(?<items>[^,]+)");
Match m = r.Match(Line); Match m = r.Match(Line);
if (m.Success) if (m.Success)
{ {
@ -149,6 +135,71 @@ namespace zaaReloaded2.Controller.Elements
} }
} }
/// <summary>
/// Searches the working time points of the Formatter object
/// for items defined in the Line.
/// </summary>
List<ItemFormatter> CollectItems(zaaReloaded2.Formatter.Formatter formatter)
{
List<ItemFormatter> items = new List<ItemFormatter>();
foreach (string itemName in _items)
{
Match match = _wildcard.Match(itemName);
if (match.Success)
{
// If there is no capture group "material", the Value
// will be String.Empty.
items.AddRange(CollectByWildcard(formatter, match.Groups["material"].Value));
}
else
{
items.AddRange(CollectByName(formatter, itemName));
}
}
return items;
}
/// <summary>
/// Collects items for output by matching wildcard.
/// </summary>
/// <param name="material">Material (e.g., empty string for blood,
/// or "U-" for spot urine, "SU-" for collected urin, and so on).</param>
List<ItemFormatter> CollectByWildcard(zaaReloaded2.Formatter.Formatter formatter, string material)
{
List<ItemFormatter> items = new List<ItemFormatter>();
foreach (TimePointFormatter tpf in formatter.WorkingTimePoints.Values)
{
List<ItemFormatter> newItems = tpf.ItemFormatters.Values
.Where(i => !i.HasBeenUsed && i.LabItem.QualifiedName.StartsWith(material))
.ToList();
newItems.ForEach(i => i.HasBeenUsed = true);
items.AddRange(newItems);
}
return items;
}
/// <summary>
/// Collects items for output by name.
/// </summary>
/// <param name="name">Item name to look for.</param>
List<ItemFormatter> CollectByName(zaaReloaded2.Formatter.Formatter formatter, string name)
{
List<ItemFormatter> items = new List<ItemFormatter>();
TimePointFormatter tpf = formatter.WorkingTimePoints
.FirstOrDefault(tp => tp.Value.ContainsItem(name))
.Value;
if (tpf != null)
{
// If tpf is not null, this means that it contains an
// item with itemName.
ItemFormatter i = tpf.ItemFormatters[name];
i.HasBeenUsed = true;
items.Add(i);
}
return items;
}
#endregion #endregion
#region Fields #region Fields
@ -156,6 +207,7 @@ namespace zaaReloaded2.Controller.Elements
string _line; string _line;
string _caption; string _caption;
List<string> _items; List<string> _items;
static Regex _wildcard = new Regex(@"(?<material>[^-]+-)?\*");
#endregion #endregion
} }