diff --git a/Tests/Controller/Elements/ItemsTest.cs b/Tests/Controller/Elements/ItemsTest.cs index 5375933..a57c665 100755 --- a/Tests/Controller/Elements/ItemsTest.cs +++ b/Tests/Controller/Elements/ItemsTest.cs @@ -36,6 +36,7 @@ namespace Tests.Controller.Elements public void SetUp() { _formatter = new zaaReloaded2.Formatter.Formatter(new Document()); + _formatter.Settings.ReferenceStyle = ReferenceStyle.IfAbnormal; } [TearDown] @@ -55,8 +56,8 @@ namespace Tests.Controller.Elements // We do not add a 'Cl' item, and it should not appear in output. lab.AddTimePoint(tp); - _formatter.Settings.ReferenceStyle = ReferenceStyle.IfAbnormal; _formatter.Laboratory = lab; + _formatter.SelectFirstDay(); _formatter.Settings.Elements.Add(new zaa.Items("Na, K, Cl")); _formatter.Run(); 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. lab.AddTimePoint(tp); - _formatter.Settings.ReferenceStyle = ReferenceStyle.IfAbnormal; _formatter.Laboratory = lab; + _formatter.SelectFirstDay(); _formatter.Settings.Elements.Add(new zaa.Items("Klinische Chemie: Na, K, Cl")); _formatter.Run(); 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. lab.AddTimePoint(tp); - _formatter.Settings.ReferenceStyle = ReferenceStyle.IfAbnormal; _formatter.Laboratory = lab; + _formatter.SelectFirstDay(); _formatter.Settings.Elements.Add(new zaa.Items("Klinische Chemie: this, does, not, exist")); _formatter.Run(); 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); + } } } diff --git a/zaaReloaded2/Controller/Elements/Items.cs b/zaaReloaded2/Controller/Elements/Items.cs index 1b6702e..c58cadf 100755 --- a/zaaReloaded2/Controller/Elements/Items.cs +++ b/zaaReloaded2/Controller/Elements/Items.cs @@ -18,7 +18,6 @@ using System; using System.Collections.Generic; using System.Linq; -using System.Text; using System.Text.RegularExpressions; using System.Diagnostics; using Microsoft.Office.Interop.Word; @@ -43,26 +42,13 @@ namespace zaaReloaded2.Controller.Elements public override void Run(zaaReloaded2.Formatter.Formatter formatter) { + if (_items == null || _items.Count == 0) return; + bool _needComma = false; // Find out if we have any items that we can write // to the document - List items = new List(); - 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]); - } - } - } + List items = CollectItems(formatter); // If there are items, write the caption (if any), then the items if (items.Count > 0) @@ -83,7 +69,7 @@ namespace zaaReloaded2.Controller.Elements { _needComma = true; } - i.WriteToDocument(formatter.Document); + i.WriteToDocument(formatter); } } } @@ -135,7 +121,7 @@ namespace zaaReloaded2.Controller.Elements { _items = null; _caption = null; - Regex r = new Regex(@"((?[^:]+):\s*)?((?[^,]+),\s*)+"); + Regex r = new Regex(@"((?[^:]+):\s*)?((?[^,]+),\s*)*(?[^,]+)"); Match m = r.Match(Line); if (m.Success) { @@ -149,6 +135,71 @@ namespace zaaReloaded2.Controller.Elements } } + /// + /// Searches the working time points of the Formatter object + /// for items defined in the Line. + /// + List CollectItems(zaaReloaded2.Formatter.Formatter formatter) + { + List items = new List(); + 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; + } + + /// + /// Collects items for output by matching wildcard. + /// + /// Material (e.g., empty string for blood, + /// or "U-" for spot urine, "SU-" for collected urin, and so on). + List CollectByWildcard(zaaReloaded2.Formatter.Formatter formatter, string material) + { + List items = new List(); + foreach (TimePointFormatter tpf in formatter.WorkingTimePoints.Values) + { + List 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; + } + + /// + /// Collects items for output by name. + /// + /// Item name to look for. + List CollectByName(zaaReloaded2.Formatter.Formatter formatter, string name) + { + List items = new List(); + 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 #region Fields @@ -156,6 +207,7 @@ namespace zaaReloaded2.Controller.Elements string _line; string _caption; List _items; + static Regex _wildcard = new Regex(@"(?[^-]+-)?\*"); #endregion }