Working comments with CommentPool.
This commit is contained in:
parent
a89a8103e5
commit
02b4bc07a3
60
Tests/Controller/Comments/CommentPoolTest.cs
Executable file
60
Tests/Controller/Comments/CommentPoolTest.cs
Executable 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);
|
||||
}
|
||||
}
|
||||
}
|
@ -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: <default> µg/l)\"");
|
||||
ItemComment i = ItemComment.FromDefinitionString("TAC \"(Zielbereich: <default> µg/l)\"");
|
||||
i.Value = String.Empty;
|
||||
Assert.AreEqual(String.Empty, i.BuildComment());
|
||||
}
|
@ -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: <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)
|
||||
{
|
||||
return _markupStripper.Replace(s, string.Empty);
|
||||
|
@ -80,7 +80,8 @@
|
||||
</Otherwise>
|
||||
</Choose>
|
||||
<ItemGroup>
|
||||
<Compile Include="Formatter\ItemCommentTest.cs" />
|
||||
<Compile Include="Controller\Comments\CommentPoolTest.cs" />
|
||||
<Compile Include="Controller\Comments\ItemCommentTest.cs" />
|
||||
<Compile Include="SerializationTest.cs" />
|
||||
<Compile Include="Controller\SettingsRepositoryTest.cs" />
|
||||
<Compile Include="Controller\SettingsTest.cs" />
|
||||
|
134
zaaReloaded2/Controller/Comments/CommentPool.cs
Executable file
134
zaaReloaded2/Controller/Comments/CommentPool.cs
Executable 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
|
||||
}
|
||||
}
|
@ -21,7 +21,7 @@ using System.Linq;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace zaaReloaded2.Formatter
|
||||
namespace zaaReloaded2.Controller.Comments
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents an optional comment for a laboratory item.
|
||||
@ -46,33 +46,29 @@ namespace zaaReloaded2.Formatter
|
||||
#region Factory
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new ItemComment object from a definition.
|
||||
/// Creates a new ItemComment object from a definition string.
|
||||
/// </summary>
|
||||
/// <param name="definition">Definition string.</param>
|
||||
/// <returns>ItemComment object, or null if the
|
||||
/// <paramref name="definition"/> string could not be
|
||||
/// parsed.</returns>
|
||||
public static ItemComment FromDefinition(string definition)
|
||||
/// <param name="definitionString">String that complies
|
||||
/// with the definition pattern (item "optional prefix <optional
|
||||
/// default value> optioal suffix").</param>
|
||||
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)
|
||||
return new ItemComment(
|
||||
definitionString,
|
||||
match.Groups["item"].Value.Trim(),
|
||||
match.Groups["prefix"].Value,
|
||||
match.Groups["value"].Value,
|
||||
match.Groups["suffix"].Value);
|
||||
}
|
||||
else
|
||||
{
|
||||
itemComment = new ItemComment(
|
||||
checkHasComment.Groups["item"].Value.Trim(),
|
||||
commentComponents.Groups["prefix"].Value,
|
||||
commentComponents.Groups["value"].Value,
|
||||
commentComponents.Groups["suffix"].Value
|
||||
);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
return itemComment;
|
||||
}
|
||||
|
||||
|
||||
#endregion
|
||||
|
||||
@ -99,6 +95,12 @@ namespace zaaReloaded2.Formatter
|
||||
/// </summary>
|
||||
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
|
||||
|
||||
#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(@"(?<item>[^""]+)""(?<comment>[^""]+)""");
|
||||
static readonly Regex _commentDefinition =
|
||||
new Regex(@"(?<prefix>[^<]+)<(?<value>[^>]*)>(?<suffix>[^""]+)");
|
||||
static readonly Regex _definition =
|
||||
new Regex(@"(?<item>[^""]+)""(?<prefix>[^<]+)?<(?<value>[^>]*)>(?<suffix>[^""]+)?""");
|
||||
|
||||
#endregion
|
||||
}
|
@ -20,7 +20,7 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace zaaReloaded2.Formatter
|
||||
namespace zaaReloaded2.Controller.Comments
|
||||
{
|
||||
/// <summary>
|
||||
/// Event arguments used in item commenting.
|
@ -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
|
||||
|
||||
/// <summary>
|
||||
/// Propagates the FillInComment events of collected items.
|
||||
/// </summary>
|
||||
public event EventHandler<ItemCommentEventArgs> 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<ItemCommentEventArgs> h = FillInComment;
|
||||
if (h != null)
|
||||
{
|
||||
h(this, args);
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Fields
|
||||
|
@ -37,17 +37,7 @@ namespace zaaReloaded2.Formatter
|
||||
/// <summary>
|
||||
/// Gets or sets the Settings that this Formatter works with.
|
||||
/// </summary>
|
||||
public Settings Settings
|
||||
{
|
||||
get { return _settings; }
|
||||
set
|
||||
{
|
||||
_settings = value;
|
||||
// Listen to the FillInComment event of
|
||||
_settings.Elements.OfType<Items>().ToList().ForEach(
|
||||
items => items.FillInComment += items_FillInComment);
|
||||
}
|
||||
}
|
||||
public Settings Settings { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the <see cref="Laboratory"/> that shall be
|
||||
@ -93,17 +83,6 @@ namespace zaaReloaded2.Formatter
|
||||
|
||||
#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
|
||||
|
||||
public Formatter()
|
||||
@ -431,23 +410,6 @@ namespace zaaReloaded2.Formatter
|
||||
|
||||
#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
|
||||
|
||||
/// <summary>
|
||||
@ -459,7 +421,6 @@ namespace zaaReloaded2.Formatter
|
||||
|
||||
#region Fields
|
||||
|
||||
Settings _settings;
|
||||
TimePointFormatterDictionary _timePointFormatters;
|
||||
Laboratory _laboratory;
|
||||
DocumentWriter _primaryBuffer;
|
||||
|
@ -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)
|
||||
{
|
||||
|
@ -192,11 +192,12 @@
|
||||
can be found.
|
||||
-->
|
||||
<ItemGroup>
|
||||
<Compile Include="Controller\Comments\CommentPool.cs" />
|
||||
<Compile Include="Controller\Elements\ControlElementBase.cs" />
|
||||
<Compile Include="Controller\Elements\FormatElementBase.cs" />
|
||||
<Compile Include="Controller\Elements\NextColumn.cs" />
|
||||
<Compile Include="Formatter\ItemComment.cs" />
|
||||
<Compile Include="Formatter\ItemCommentEventArgs.cs" />
|
||||
<Compile Include="Controller\Comments\ItemComment.cs" />
|
||||
<Compile Include="Controller\Comments\ItemCommentEventArgs.cs" />
|
||||
<Compile Include="Controller\Elements\SelectEachDay.cs" />
|
||||
<Compile Include="Controller\Elements\SelectLastDay.cs" />
|
||||
<Compile Include="Controller\Elements\SelectFirstDay.cs" />
|
||||
|
Loading…
Reference in New Issue
Block a user