diff --git a/Tests/Controller/Comments/CommentPoolTest.cs b/Tests/Controller/Comments/CommentPoolTest.cs new file mode 100755 index 0000000..d993e4f --- /dev/null +++ b/Tests/Controller/Comments/CommentPoolTest.cs @@ -0,0 +1,60 @@ +/* CommentPoolTest.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.Linq; +using System.Text; +using NUnit.Framework; +using zaaReloaded2.Controller.Comments; + +namespace Tests.Controller.Comments +{ + [TestFixture] + class CommentPoolTest + { + [Test] + public void CreateCommentIfDoesNotExist() + { + int n = CommentPool.Default.Count; + ItemComment i = CommentPool.Default.GetCommentFor("item \"<>\""); + Assert.AreEqual(n + 1, CommentPool.Default.Count); + } + + [Test] + public void ReturnExistingComment() + { + ItemComment i = CommentPool.Default.GetCommentFor("item \"<>\""); + int n = CommentPool.Default.Count; + i = CommentPool.Default.GetCommentFor("item \"<>\""); + Assert.AreEqual(n, CommentPool.Default.Count); + } + + [Test] + public void BuildingCommentRaisesEvent() + { + ItemComment i = CommentPool.Default.GetCommentFor("item \"<>\""); + bool eventRaised = false; + CommentPool.Default.FillInComment += (sender, args) => + { + eventRaised = true; + }; + string comment = i.BuildComment(); + Assert.IsTrue(eventRaised); + } + } +} diff --git a/Tests/Formatter/ItemCommentTest.cs b/Tests/Controller/Comments/ItemCommentTest.cs similarity index 80% rename from Tests/Formatter/ItemCommentTest.cs rename to Tests/Controller/Comments/ItemCommentTest.cs index 1223273..bb1aaa2 100755 --- a/Tests/Formatter/ItemCommentTest.cs +++ b/Tests/Controller/Comments/ItemCommentTest.cs @@ -21,8 +21,9 @@ using System.Linq; using System.Text; using NUnit.Framework; using zaaReloaded2.Formatter; +using zaaReloaded2.Controller.Comments; -namespace Tests.Formatter +namespace Tests.Controller.Comments { [TestFixture] class ItemCommentTest @@ -30,7 +31,7 @@ namespace Tests.Formatter [Test] public void FactoryWithGoodDefinition() { - ItemComment i = ItemComment.FromDefinition("TAC \"(Zielbereich: <4-7> µg/l)\""); + ItemComment i = ItemComment.FromDefinitionString("TAC \"(Zielbereich: <4-7> µg/l)\""); Assert.IsNotNull(i); Assert.AreEqual("(Zielbereich: ", i.Prefix); Assert.AreEqual("4-7", i.Value); @@ -42,14 +43,14 @@ namespace Tests.Formatter [Test] public void FactoryWithBadDefinition() { - ItemComment i = ItemComment.FromDefinition("some bogus definition"); + ItemComment i = ItemComment.FromDefinitionString("some bogus definition"); Assert.IsNull(i); } [Test] public void EmptyValueProducesEmptyComment() { - ItemComment i = ItemComment.FromDefinition("TAC \"(Zielbereich: µg/l)\""); + ItemComment i = ItemComment.FromDefinitionString("TAC \"(Zielbereich: µg/l)\""); i.Value = String.Empty; Assert.AreEqual(String.Empty, i.BuildComment()); } diff --git a/Tests/Controller/Elements/ItemsTest.cs b/Tests/Controller/Elements/ItemsTest.cs index 4f757ad..ff473f3 100755 --- a/Tests/Controller/Elements/ItemsTest.cs +++ b/Tests/Controller/Elements/ItemsTest.cs @@ -24,7 +24,9 @@ using Microsoft.Office.Interop.Word; using zaaReloaded2.LabModel; using zaaReloaded2.Formatter; using zaa = zaaReloaded2.Controller.Elements; +using zaaReloaded2.Controller.Comments; using System.Text.RegularExpressions; +using zaaReloaded2.Controller.Comments; namespace Tests.Controller.Elements { @@ -203,6 +205,35 @@ namespace Tests.Controller.Elements Assert.AreEqual(expected, _document.Range().Text); } + [Test] + public void ItemCommentWithHandler() + { + Laboratory lab = new Laboratory(); + TimePoint tp = new TimePoint(); + tp.TimeStamp = new DateTime(2015, 7, 13, 13, 31, 00); + tp.AddItem(new LabItem("Na", "133", "133")); + tp.AddItem(new LabItem("K", "6", "5")); + lab.AddTimePoint(tp); + + _formatter.Laboratory = lab; + _formatter.Settings.Elements.Add( + new zaa.Items("Na \"(Zielbereich: µg/l)\"")); + bool messageSent = false; + CommentPool.Default.FillInComment += (sender, args) => + { + messageSent = true; + args.Comment.Value = "4-7"; + }; + _formatter.Run(); + Assert.IsTrue(messageSent, "FillInComment message was not sent"); + string expected = ( + StripMarkup( + TimePointFormatter.DateAndTimeHeader(new DateTime(2015, 07, 13, 13, 31, 00)) + + "Na 133 (Zielbereich: 4-7 µg/l)\r\r").Replace(Environment.NewLine, "\r") + ); + Assert.AreEqual(expected, _document.Range().Text); + } + static string StripMarkup(string s) { return _markupStripper.Replace(s, string.Empty); diff --git a/Tests/Tests.csproj b/Tests/Tests.csproj index 1119cc5..e6a43dc 100755 --- a/Tests/Tests.csproj +++ b/Tests/Tests.csproj @@ -80,7 +80,8 @@ - + + diff --git a/zaaReloaded2/Controller/Comments/CommentPool.cs b/zaaReloaded2/Controller/Comments/CommentPool.cs new file mode 100755 index 0000000..4c9f9cd --- /dev/null +++ b/zaaReloaded2/Controller/Comments/CommentPool.cs @@ -0,0 +1,134 @@ +/* CommentPool.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.Linq; +using System.Text; + +namespace zaaReloaded2.Controller.Comments +{ + /// + /// A pool of comments. + /// + class CommentPool + { + #region Singleton + + /// + /// Gets the singleton instance of the CommentPool. + /// + public static CommentPool Default + { + get + { + return _instance; + } + } + + private static readonly CommentPool _instance = new CommentPool(); + + #endregion + + #region Properties + + /// + /// Gets the number of ItemComments in the pool. + /// + public int Count { get { return _itemComments.Count; } } + + #endregion + + #region Event + + public event EventHandler FillInComment; + + #endregion + + #region Constructor + + /// + /// Static constructor to support the singleton implementation. + /// + /// + /// See http://csharpindepth.com/Articles/General/Singleton.aspx#cctor + /// + static CommentPool() { } + + private CommentPool() + { + _itemComments = new List(); + } + + #endregion + + #region Methods + + /// + /// Retrieves the ItemComment for a given definitionString; + /// creates a new ItemComment object if necessary. + /// + /// + /// ItemComment derived from the + /// + public ItemComment GetCommentFor(string definitionString) + { + ItemComment itemComment = _itemComments.FirstOrDefault( + i => i.Definition == definitionString); + if (itemComment == null) + { + itemComment = ItemComment.FromDefinitionString(definitionString); + if (itemComment != null) + { + _itemComments.Add(itemComment); + itemComment.FillInComment += itemComment_FillInComment; + } + } + return itemComment; + } + + #endregion + + #region Pribate methods + + protected virtual void itemComment_FillInComment(object sender, ItemCommentEventArgs e) + { + OnFillInComment(e); + } + + #endregion + + #region Protected methods + + protected virtual void OnFillInComment(ItemCommentEventArgs args) + { + EventHandler h = FillInComment; + if (h != null) + { + h(this, args); + } + } + + #endregion + + #region Fields + + List _itemComments; + + #endregion + } +} diff --git a/zaaReloaded2/Formatter/ItemComment.cs b/zaaReloaded2/Controller/Comments/ItemComment.cs similarity index 76% rename from zaaReloaded2/Formatter/ItemComment.cs rename to zaaReloaded2/Controller/Comments/ItemComment.cs index 157eebf..dac8c8a 100755 --- a/zaaReloaded2/Formatter/ItemComment.cs +++ b/zaaReloaded2/Controller/Comments/ItemComment.cs @@ -21,7 +21,7 @@ using System.Linq; using System.Text; using System.Text.RegularExpressions; -namespace zaaReloaded2.Formatter +namespace zaaReloaded2.Controller.Comments { /// /// Represents an optional comment for a laboratory item. @@ -46,34 +46,30 @@ namespace zaaReloaded2.Formatter #region Factory /// - /// Creates a new ItemComment object from a definition. + /// Creates a new ItemComment object from a definition string. /// - /// Definition string. - /// ItemComment object, or null if the - /// string could not be - /// parsed. - public static ItemComment FromDefinition(string definition) + /// String that complies + /// with the definition pattern (item "optional prefix <optional + /// default value> optioal suffix"). + public static ItemComment FromDefinitionString(string definitionString) { - ItemComment itemComment = null; - // TODO: Maybe turn these to regexes into one. - Match checkHasComment = _itemDefinition.Match(definition); - if (checkHasComment.Success) + Match match = _definition.Match(definitionString); + if (match.Success) { - Match commentComponents = - _commentDefinition.Match(checkHasComment.Groups["comment"].Value); - if (commentComponents.Success) - { - itemComment = new ItemComment( - checkHasComment.Groups["item"].Value.Trim(), - commentComponents.Groups["prefix"].Value, - commentComponents.Groups["value"].Value, - commentComponents.Groups["suffix"].Value - ); - } + return new ItemComment( + definitionString, + match.Groups["item"].Value.Trim(), + match.Groups["prefix"].Value, + match.Groups["value"].Value, + match.Groups["suffix"].Value); + } + else + { + return null; } - return itemComment; } + #endregion #region Properties @@ -99,6 +95,12 @@ namespace zaaReloaded2.Formatter /// public string Suffix { get; set; } + /// + /// Gets the original definition string that this ItemComment + /// was created from. + /// + public string Definition { get; private set; } + #endregion #region Event @@ -122,6 +124,7 @@ namespace zaaReloaded2.Formatter } public ItemComment(string item, string prefix, string value, string suffix) + : this() { Item = item; Prefix = prefix; @@ -129,6 +132,12 @@ namespace zaaReloaded2.Formatter Suffix = suffix; } + public ItemComment(string definition, string item, string prefix, string value, string suffix) + : this(item, prefix, value, suffix) + { + Definition = definition; + } + #endregion #region Methods @@ -174,10 +183,8 @@ namespace zaaReloaded2.Formatter #region Fields - static readonly Regex _itemDefinition = - new Regex(@"(?[^""]+)""(?[^""]+)"""); - static readonly Regex _commentDefinition = - new Regex(@"(?[^<]+)<(?[^>]*)>(?[^""]+)"); + static readonly Regex _definition = + new Regex(@"(?[^""]+)""(?[^<]+)?<(?[^>]*)>(?[^""]+)?"""); #endregion } diff --git a/zaaReloaded2/Formatter/ItemCommentEventArgs.cs b/zaaReloaded2/Controller/Comments/ItemCommentEventArgs.cs similarity index 97% rename from zaaReloaded2/Formatter/ItemCommentEventArgs.cs rename to zaaReloaded2/Controller/Comments/ItemCommentEventArgs.cs index 9787ed9..b6deaec 100755 --- a/zaaReloaded2/Formatter/ItemCommentEventArgs.cs +++ b/zaaReloaded2/Controller/Comments/ItemCommentEventArgs.cs @@ -20,7 +20,7 @@ using System.Collections.Generic; using System.Linq; using System.Text; -namespace zaaReloaded2.Formatter +namespace zaaReloaded2.Controller.Comments { /// /// Event arguments used in item commenting. diff --git a/zaaReloaded2/Controller/Elements/Items.cs b/zaaReloaded2/Controller/Elements/Items.cs index 8ebf00d..d8f8b63 100755 --- a/zaaReloaded2/Controller/Elements/Items.cs +++ b/zaaReloaded2/Controller/Elements/Items.cs @@ -24,6 +24,7 @@ using Microsoft.Office.Interop.Word; using zaaReloaded2.LabModel; using zaaReloaded2.Formatter; using System.Runtime.Serialization; +using zaaReloaded2.Controller.Comments; namespace zaaReloaded2.Controller.Elements { @@ -94,15 +95,6 @@ namespace zaaReloaded2.Controller.Elements #endregion - #region Events - - /// - /// Propagates the FillInComment events of collected items. - /// - public event EventHandler FillInComment; - - #endregion - #region Constructors public Items() : base() { } @@ -202,12 +194,10 @@ namespace zaaReloaded2.Controller.Elements { // First, check if the item name contains an optional comment // definition - ItemComment comment = ItemComment.FromDefinition(name); + ItemComment comment = CommentPool.Default.GetCommentFor(name); if (comment != null) { name = comment.Item; - // Enable propagation of FillInComment events - comment.FillInComment += comment_FillInComment; } // Then, see if we have such an item @@ -228,23 +218,6 @@ namespace zaaReloaded2.Controller.Elements return items; } - void comment_FillInComment(object sender, ItemCommentEventArgs e) - { - OnFillInComment(e); - } - - #endregion - - #region Protected methods - - protected virtual void OnFillInComment(ItemCommentEventArgs args) - { - EventHandler h = FillInComment; - if (h != null) - { - h(this, args); - } - } #endregion #region Fields diff --git a/zaaReloaded2/Formatter/Formatter.cs b/zaaReloaded2/Formatter/Formatter.cs index c5fd796..b18010e 100755 --- a/zaaReloaded2/Formatter/Formatter.cs +++ b/zaaReloaded2/Formatter/Formatter.cs @@ -37,17 +37,7 @@ namespace zaaReloaded2.Formatter /// /// Gets or sets the Settings that this Formatter works with. /// - public Settings Settings - { - get { return _settings; } - set - { - _settings = value; - // Listen to the FillInComment event of - _settings.Elements.OfType().ToList().ForEach( - items => items.FillInComment += items_FillInComment); - } - } + public Settings Settings { get; set; } /// /// Gets or sets the that shall be @@ -93,17 +83,6 @@ namespace zaaReloaded2.Formatter #endregion - #region Event - - /// - /// Relays the FillInComment events of any Items elements - /// in the Settings, which in turn relay the FillInComment - /// events of their collected items' ItemComments. - /// - public event EventHandler FillInComment; - - #endregion - #region Constructors public Formatter() @@ -431,23 +410,6 @@ namespace zaaReloaded2.Formatter #endregion - #region Private methods - - /// - /// Relays the FillInComment event of Items elements in the - /// Settings. - /// - void items_FillInComment(object sender, ItemCommentEventArgs e) - { - EventHandler h = FillInComment; - if (h != null) - { - h(this, e); - } - } - - #endregion - #region Protected properties /// @@ -459,7 +421,6 @@ namespace zaaReloaded2.Formatter #region Fields - Settings _settings; TimePointFormatterDictionary _timePointFormatters; Laboratory _laboratory; DocumentWriter _primaryBuffer; diff --git a/zaaReloaded2/Formatter/ItemFormatter.cs b/zaaReloaded2/Formatter/ItemFormatter.cs index fcd5fd1..cc27a14 100755 --- a/zaaReloaded2/Formatter/ItemFormatter.cs +++ b/zaaReloaded2/Formatter/ItemFormatter.cs @@ -21,6 +21,7 @@ using System.Linq; using System.Text; using Microsoft.Office.Interop.Word; using zaaReloaded2.LabModel; +using zaaReloaded2.Controller.Comments; namespace zaaReloaded2.Formatter { @@ -174,15 +175,24 @@ namespace zaaReloaded2.Formatter value = LabItem.Value; } + string comment = String.Empty; + if (Comment != null) + { + comment = Comment.BuildComment(); + if (comment != String.Empty) comment = " " + comment; + } + string name = IncludeMaterial ? LabItem.QualifiedName : LabItem.Name; + string output = String.Format( - "{0} {1}{2}{3}", + "{0} {1}{2}{3}{4}", name, value, unit, - reference + reference, + comment ); if (!LabItem.IsNormal) { diff --git a/zaaReloaded2/zaaReloaded2.csproj b/zaaReloaded2/zaaReloaded2.csproj index bfbc184..07448a3 100755 --- a/zaaReloaded2/zaaReloaded2.csproj +++ b/zaaReloaded2/zaaReloaded2.csproj @@ -192,11 +192,12 @@ can be found. --> + - - + +