Working comments with CommentPool.

This commit is contained in:
Daniel Kraus 2015-08-30 07:48:32 +02:00
parent a89a8103e5
commit 02b4bc07a3
11 changed files with 285 additions and 106 deletions

View File

@ -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);
}
}
}

View File

@ -21,8 +21,9 @@ using System.Linq;
using System.Text; using System.Text;
using NUnit.Framework; using NUnit.Framework;
using zaaReloaded2.Formatter; using zaaReloaded2.Formatter;
using zaaReloaded2.Controller.Comments;
namespace Tests.Formatter namespace Tests.Controller.Comments
{ {
[TestFixture] [TestFixture]
class ItemCommentTest class ItemCommentTest
@ -30,7 +31,7 @@ namespace Tests.Formatter
[Test] [Test]
public void FactoryWithGoodDefinition() 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.IsNotNull(i);
Assert.AreEqual("(Zielbereich: ", i.Prefix); Assert.AreEqual("(Zielbereich: ", i.Prefix);
Assert.AreEqual("4-7", i.Value); Assert.AreEqual("4-7", i.Value);
@ -42,14 +43,14 @@ namespace Tests.Formatter
[Test] [Test]
public void FactoryWithBadDefinition() public void FactoryWithBadDefinition()
{ {
ItemComment i = ItemComment.FromDefinition("some bogus definition"); ItemComment i = ItemComment.FromDefinitionString("some bogus definition");
Assert.IsNull(i); Assert.IsNull(i);
} }
[Test] [Test]
public void EmptyValueProducesEmptyComment() public void EmptyValueProducesEmptyComment()
{ {
ItemComment i = ItemComment.FromDefinition("TAC \"(Zielbereich: <default> µg/l)\""); ItemComment i = ItemComment.FromDefinitionString("TAC \"(Zielbereich: <default> µg/l)\"");
i.Value = String.Empty; i.Value = String.Empty;
Assert.AreEqual(String.Empty, i.BuildComment()); Assert.AreEqual(String.Empty, i.BuildComment());
} }

View File

@ -24,7 +24,9 @@ using Microsoft.Office.Interop.Word;
using zaaReloaded2.LabModel; using zaaReloaded2.LabModel;
using zaaReloaded2.Formatter; using zaaReloaded2.Formatter;
using zaa = zaaReloaded2.Controller.Elements; using zaa = zaaReloaded2.Controller.Elements;
using zaaReloaded2.Controller.Comments;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using zaaReloaded2.Controller.Comments;
namespace Tests.Controller.Elements namespace Tests.Controller.Elements
{ {
@ -203,6 +205,35 @@ namespace Tests.Controller.Elements
Assert.AreEqual(expected, _document.Range().Text); 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: <default> µ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) static string StripMarkup(string s)
{ {
return _markupStripper.Replace(s, string.Empty); return _markupStripper.Replace(s, string.Empty);

View File

@ -80,7 +80,8 @@
</Otherwise> </Otherwise>
</Choose> </Choose>
<ItemGroup> <ItemGroup>
<Compile Include="Formatter\ItemCommentTest.cs" /> <Compile Include="Controller\Comments\CommentPoolTest.cs" />
<Compile Include="Controller\Comments\ItemCommentTest.cs" />
<Compile Include="SerializationTest.cs" /> <Compile Include="SerializationTest.cs" />
<Compile Include="Controller\SettingsRepositoryTest.cs" /> <Compile Include="Controller\SettingsRepositoryTest.cs" />
<Compile Include="Controller\SettingsTest.cs" /> <Compile Include="Controller\SettingsTest.cs" />

View File

@ -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
{
/// <summary>
/// A pool of comments.
/// </summary>
class CommentPool
{
#region Singleton
/// <summary>
/// Gets the singleton instance of the CommentPool.
/// </summary>
public static CommentPool Default
{
get
{
return _instance;
}
}
private static readonly CommentPool _instance = new CommentPool();
#endregion
#region Properties
/// <summary>
/// Gets the number of ItemComments in the pool.
/// </summary>
public int Count { get { return _itemComments.Count; } }
#endregion
#region Event
public event EventHandler<ItemCommentEventArgs> FillInComment;
#endregion
#region Constructor
/// <summary>
/// Static constructor to support the singleton implementation.
/// </summary>
/// <remarks>
/// See http://csharpindepth.com/Articles/General/Singleton.aspx#cctor
/// </remarks>
static CommentPool() { }
private CommentPool()
{
_itemComments = new List<ItemComment>();
}
#endregion
#region Methods
/// <summary>
/// Retrieves the ItemComment for a given definitionString;
/// creates a new ItemComment object if necessary.
/// </summary>
/// <param name="definitionString"></param>
/// <returns>ItemComment derived from the
/// <paramref name="definitionString"/></returns>
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<ItemCommentEventArgs> h = FillInComment;
if (h != null)
{
h(this, args);
}
}
#endregion
#region Fields
List<ItemComment> _itemComments;
#endregion
}
}

View File

@ -21,7 +21,7 @@ using System.Linq;
using System.Text; using System.Text;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
namespace zaaReloaded2.Formatter namespace zaaReloaded2.Controller.Comments
{ {
/// <summary> /// <summary>
/// Represents an optional comment for a laboratory item. /// Represents an optional comment for a laboratory item.
@ -46,34 +46,30 @@ namespace zaaReloaded2.Formatter
#region Factory #region Factory
/// <summary> /// <summary>
/// Creates a new ItemComment object from a definition. /// Creates a new ItemComment object from a definition string.
/// </summary> /// </summary>
/// <param name="definition">Definition string.</param> /// <param name="definitionString">String that complies
/// <returns>ItemComment object, or null if the /// with the definition pattern (item "optional prefix &lt;optional
/// <paramref name="definition"/> string could not be /// default value&gt; optioal suffix").</param>
/// parsed.</returns> public static ItemComment FromDefinitionString(string definitionString)
public static ItemComment FromDefinition(string definition)
{ {
ItemComment itemComment = null; Match match = _definition.Match(definitionString);
// TODO: Maybe turn these to regexes into one. if (match.Success)
Match checkHasComment = _itemDefinition.Match(definition);
if (checkHasComment.Success)
{ {
Match commentComponents = return new ItemComment(
_commentDefinition.Match(checkHasComment.Groups["comment"].Value); definitionString,
if (commentComponents.Success) match.Groups["item"].Value.Trim(),
{ match.Groups["prefix"].Value,
itemComment = new ItemComment( match.Groups["value"].Value,
checkHasComment.Groups["item"].Value.Trim(), match.Groups["suffix"].Value);
commentComponents.Groups["prefix"].Value, }
commentComponents.Groups["value"].Value, else
commentComponents.Groups["suffix"].Value {
); return null;
}
} }
return itemComment;
} }
#endregion #endregion
#region Properties #region Properties
@ -99,6 +95,12 @@ namespace zaaReloaded2.Formatter
/// </summary> /// </summary>
public string Suffix { get; set; } public string Suffix { get; set; }
/// <summary>
/// Gets the original definition string that this ItemComment
/// was created from.
/// </summary>
public string Definition { get; private set; }
#endregion #endregion
#region Event #region Event
@ -122,6 +124,7 @@ namespace zaaReloaded2.Formatter
} }
public ItemComment(string item, string prefix, string value, string suffix) public ItemComment(string item, string prefix, string value, string suffix)
: this()
{ {
Item = item; Item = item;
Prefix = prefix; Prefix = prefix;
@ -129,6 +132,12 @@ namespace zaaReloaded2.Formatter
Suffix = suffix; Suffix = suffix;
} }
public ItemComment(string definition, string item, string prefix, string value, string suffix)
: this(item, prefix, value, suffix)
{
Definition = definition;
}
#endregion #endregion
#region Methods #region Methods
@ -174,10 +183,8 @@ namespace zaaReloaded2.Formatter
#region Fields #region Fields
static readonly Regex _itemDefinition = static readonly Regex _definition =
new Regex(@"(?<item>[^""]+)""(?<comment>[^""]+)"""); new Regex(@"(?<item>[^""]+)""(?<prefix>[^<]+)?<(?<value>[^>]*)>(?<suffix>[^""]+)?""");
static readonly Regex _commentDefinition =
new Regex(@"(?<prefix>[^<]+)<(?<value>[^>]*)>(?<suffix>[^""]+)");
#endregion #endregion
} }

View File

@ -20,7 +20,7 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
namespace zaaReloaded2.Formatter namespace zaaReloaded2.Controller.Comments
{ {
/// <summary> /// <summary>
/// Event arguments used in item commenting. /// Event arguments used in item commenting.

View File

@ -24,6 +24,7 @@ using Microsoft.Office.Interop.Word;
using zaaReloaded2.LabModel; using zaaReloaded2.LabModel;
using zaaReloaded2.Formatter; using zaaReloaded2.Formatter;
using System.Runtime.Serialization; using System.Runtime.Serialization;
using zaaReloaded2.Controller.Comments;
namespace zaaReloaded2.Controller.Elements namespace zaaReloaded2.Controller.Elements
{ {
@ -94,15 +95,6 @@ namespace zaaReloaded2.Controller.Elements
#endregion #endregion
#region Events
/// <summary>
/// Propagates the FillInComment events of collected items.
/// </summary>
public event EventHandler<ItemCommentEventArgs> FillInComment;
#endregion
#region Constructors #region Constructors
public Items() : base() { } public Items() : base() { }
@ -202,12 +194,10 @@ namespace zaaReloaded2.Controller.Elements
{ {
// First, check if the item name contains an optional comment // First, check if the item name contains an optional comment
// definition // definition
ItemComment comment = ItemComment.FromDefinition(name); ItemComment comment = CommentPool.Default.GetCommentFor(name);
if (comment != null) if (comment != null)
{ {
name = comment.Item; name = comment.Item;
// Enable propagation of FillInComment events
comment.FillInComment += comment_FillInComment;
} }
// Then, see if we have such an item // Then, see if we have such an item
@ -228,23 +218,6 @@ namespace zaaReloaded2.Controller.Elements
return items; return items;
} }
void comment_FillInComment(object sender, ItemCommentEventArgs e)
{
OnFillInComment(e);
}
#endregion
#region Protected methods
protected virtual void OnFillInComment(ItemCommentEventArgs args)
{
EventHandler<ItemCommentEventArgs> h = FillInComment;
if (h != null)
{
h(this, args);
}
}
#endregion #endregion
#region Fields #region Fields

View File

@ -37,17 +37,7 @@ namespace zaaReloaded2.Formatter
/// <summary> /// <summary>
/// Gets or sets the Settings that this Formatter works with. /// Gets or sets the Settings that this Formatter works with.
/// </summary> /// </summary>
public Settings Settings public Settings Settings { get; set; }
{
get { return _settings; }
set
{
_settings = value;
// Listen to the FillInComment event of
_settings.Elements.OfType<Items>().ToList().ForEach(
items => items.FillInComment += items_FillInComment);
}
}
/// <summary> /// <summary>
/// Gets or sets the <see cref="Laboratory"/> that shall be /// Gets or sets the <see cref="Laboratory"/> that shall be
@ -93,17 +83,6 @@ namespace zaaReloaded2.Formatter
#endregion #endregion
#region Event
/// <summary>
/// Relays the FillInComment events of any Items elements
/// in the Settings, which in turn relay the FillInComment
/// events of their collected items' ItemComments.
/// </summary>
public event EventHandler<ItemCommentEventArgs> FillInComment;
#endregion
#region Constructors #region Constructors
public Formatter() public Formatter()
@ -431,23 +410,6 @@ namespace zaaReloaded2.Formatter
#endregion #endregion
#region Private methods
/// <summary>
/// Relays the FillInComment event of Items elements in the
/// Settings.
/// </summary>
void items_FillInComment(object sender, ItemCommentEventArgs e)
{
EventHandler<ItemCommentEventArgs> h = FillInComment;
if (h != null)
{
h(this, e);
}
}
#endregion
#region Protected properties #region Protected properties
/// <summary> /// <summary>
@ -459,7 +421,6 @@ namespace zaaReloaded2.Formatter
#region Fields #region Fields
Settings _settings;
TimePointFormatterDictionary _timePointFormatters; TimePointFormatterDictionary _timePointFormatters;
Laboratory _laboratory; Laboratory _laboratory;
DocumentWriter _primaryBuffer; DocumentWriter _primaryBuffer;

View File

@ -21,6 +21,7 @@ using System.Linq;
using System.Text; using System.Text;
using Microsoft.Office.Interop.Word; using Microsoft.Office.Interop.Word;
using zaaReloaded2.LabModel; using zaaReloaded2.LabModel;
using zaaReloaded2.Controller.Comments;
namespace zaaReloaded2.Formatter namespace zaaReloaded2.Formatter
{ {
@ -174,15 +175,24 @@ namespace zaaReloaded2.Formatter
value = LabItem.Value; 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 name = IncludeMaterial ? LabItem.QualifiedName : LabItem.Name;
string output = string output =
String.Format( String.Format(
"{0} {1}{2}{3}", "{0} {1}{2}{3}{4}",
name, name,
value, value,
unit, unit,
reference reference,
comment
); );
if (!LabItem.IsNormal) if (!LabItem.IsNormal)
{ {

View File

@ -192,11 +192,12 @@
can be found. can be found.
--> -->
<ItemGroup> <ItemGroup>
<Compile Include="Controller\Comments\CommentPool.cs" />
<Compile Include="Controller\Elements\ControlElementBase.cs" /> <Compile Include="Controller\Elements\ControlElementBase.cs" />
<Compile Include="Controller\Elements\FormatElementBase.cs" /> <Compile Include="Controller\Elements\FormatElementBase.cs" />
<Compile Include="Controller\Elements\NextColumn.cs" /> <Compile Include="Controller\Elements\NextColumn.cs" />
<Compile Include="Formatter\ItemComment.cs" /> <Compile Include="Controller\Comments\ItemComment.cs" />
<Compile Include="Formatter\ItemCommentEventArgs.cs" /> <Compile Include="Controller\Comments\ItemCommentEventArgs.cs" />
<Compile Include="Controller\Elements\SelectEachDay.cs" /> <Compile Include="Controller\Elements\SelectEachDay.cs" />
<Compile Include="Controller\Elements\SelectLastDay.cs" /> <Compile Include="Controller\Elements\SelectLastDay.cs" />
<Compile Include="Controller\Elements\SelectFirstDay.cs" /> <Compile Include="Controller\Elements\SelectFirstDay.cs" />