Implement DocumentWriter, control elements, and tests.

This commit is contained in:
Daniel Kraus 2015-07-25 14:33:48 +02:00
parent 659713abe3
commit 9df937138d
22 changed files with 722 additions and 151 deletions

View File

@ -30,19 +30,21 @@ namespace Tests.Controller.Elements
[TestFixture] [TestFixture]
class ItemsTest class ItemsTest
{ {
Document _document;
zaaReloaded2.Formatter.Formatter _formatter; zaaReloaded2.Formatter.Formatter _formatter;
[SetUp] [SetUp]
public void SetUp() public void SetUp()
{ {
_formatter = new zaaReloaded2.Formatter.Formatter(new Document()); _document = new Document();
_formatter = new zaaReloaded2.Formatter.Formatter(_document);
_formatter.Settings.ReferenceStyle = ReferenceStyle.IfAbnormal; _formatter.Settings.ReferenceStyle = ReferenceStyle.IfAbnormal;
} }
[TearDown] [TearDown]
public void TearDown() public void TearDown()
{ {
((_Document)_formatter.Document).Close(WdSaveOptions.wdDoNotSaveChanges); ((_Document)_document).Close(WdSaveOptions.wdDoNotSaveChanges);
} }
[Test] [Test]
@ -57,10 +59,9 @@ namespace Tests.Controller.Elements
lab.AddTimePoint(tp); lab.AddTimePoint(tp);
_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", _document.Range().Text);
} }
[Test] [Test]
@ -75,10 +76,9 @@ namespace Tests.Controller.Elements
lab.AddTimePoint(tp); lab.AddTimePoint(tp);
_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", _document.Range().Text);
} }
[Test] [Test]
@ -93,10 +93,9 @@ namespace Tests.Controller.Elements
lab.AddTimePoint(tp); lab.AddTimePoint(tp);
_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", _document.Range().Text);
} }
[Test] [Test]
@ -112,11 +111,10 @@ namespace Tests.Controller.Elements
_formatter.Settings.ReferenceStyle = ReferenceStyle.Never; _formatter.Settings.ReferenceStyle = ReferenceStyle.Never;
_formatter.Laboratory = lab; _formatter.Laboratory = lab;
_formatter.SelectFirstDay();
_formatter.Settings.Elements.Add(new zaa.Items("Klinische Chemie: Na, *")); _formatter.Settings.Elements.Add(new zaa.Items("Klinische Chemie: Na, *"));
_formatter.Run(); _formatter.Run();
Assert.AreEqual("Klinische Chemie: Na 133, Cl 110, K 6\r", Assert.AreEqual("Klinische Chemie: Na 133, Cl 110, K 6\r",
_formatter.Document.Range().Text); _document.Range().Text);
} }
[Test] [Test]
@ -133,11 +131,10 @@ namespace Tests.Controller.Elements
_formatter.Settings.ReferenceStyle = ReferenceStyle.Never; _formatter.Settings.ReferenceStyle = ReferenceStyle.Never;
_formatter.Laboratory = lab; _formatter.Laboratory = lab;
_formatter.SelectFirstDay();
_formatter.Settings.Elements.Add(new zaa.Items("Klinische Chemie: Na, SU-*")); _formatter.Settings.Elements.Add(new zaa.Items("Klinische Chemie: Na, SU-*"));
_formatter.Run(); _formatter.Run();
Assert.AreEqual("Klinische Chemie: Na 133, SU-Protein 2,8\r", Assert.AreEqual("Klinische Chemie: Na 133, SU-Protein 2,8\r",
_formatter.Document.Range().Text); _document.Range().Text);
} }
[Test] [Test]
@ -154,11 +151,10 @@ namespace Tests.Controller.Elements
_formatter.Settings.ReferenceStyle = ReferenceStyle.Never; _formatter.Settings.ReferenceStyle = ReferenceStyle.Never;
_formatter.Laboratory = lab; _formatter.Laboratory = lab;
_formatter.SelectFirstDay();
_formatter.Settings.Elements.Add(new zaa.Items("Klinische Chemie: Na, SU-*, *")); _formatter.Settings.Elements.Add(new zaa.Items("Klinische Chemie: Na, SU-*, *"));
_formatter.Run(); _formatter.Run();
Assert.AreEqual("Klinische Chemie: Na 133, SU-Protein 2,8, Cl 110, U-Na 99\r", Assert.AreEqual("Klinische Chemie: Na 133, SU-Protein 2,8, Cl 110, U-Na 99\r",
_formatter.Document.Range().Text); _document.Range().Text);
} }
} }
} }

View File

@ -1,45 +0,0 @@
/* SelectFirstDayTest.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 f = zaaReloaded2.Formatter;
using zaaReloaded2.Importer.ZaaImporter;
using zaaReloaded2.Controller.Elements;
namespace Tests.Controller.Elements
{
[TestFixture]
class SelectFirstDayTest
{
[Test]
public void SelectFirstDay()
{
SelectFirstDay element = new SelectFirstDay();
ZaaImporter importer = TestHelpers.ZaaImporterFromResource();
f.Formatter formatter = new f.Formatter();
formatter.Laboratory = importer.Laboratory;
element.Run(formatter);
Assert.AreEqual(5, formatter.WorkingTimePoints.Count);
DateTime firstDay = new DateTime(2015, 07, 04);
Assert.AreEqual(firstDay.Date, formatter.WorkingTimePoints.First().Key.Date);
}
}
}

View File

@ -1,49 +0,0 @@
/* SelectFirstDayTest - Copy.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 f = zaaReloaded2.Formatter;
using zaaReloaded2.Importer.ZaaImporter;
using zaaReloaded2.Controller.Elements;
namespace Tests.Controller.Elements
{
[TestFixture]
class SelectLastDayTest
{
[Test]
public void SelectLastDay()
{
SelectLastDay element = new SelectLastDay();
ZaaImporter importer = TestHelpers.ZaaImporterFromResource();
f.Formatter formatter = new f.Formatter();
formatter.Laboratory = importer.Laboratory;
element.Run(formatter);
// There are two time points of 6-July-2015 in the demo output file,
// but since they have the exact same time stamps, they are merged
// into one!
Assert.AreEqual(1, formatter.WorkingTimePoints.Count);
DateTime firstDay = new DateTime(2015, 07, 06);
Assert.AreEqual(firstDay.Date, formatter.WorkingTimePoints.First().Key.Date);
}
}
}

View File

@ -0,0 +1,99 @@
/* DocumentWriterClass.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.Formatter;
namespace Tests.Formatter
{
[TestFixture]
class DocumentWriterTest
{
DocumentWriter _docWriter;
[SetUp]
public void SetUp()
{
_docWriter = new DocumentWriter();
}
[TearDown]
public void TearDown()
{
}
[Test]
public void AddText()
{
string s = "hello world";
_docWriter.Write(s);
Assert.AreEqual(s, _docWriter.ToString());
}
[Test]
public void AddLine()
{
string s = "hello world";
_docWriter.WriteLine(s);
Assert.AreEqual(s + Environment.NewLine, _docWriter.ToString());
}
[Test]
public void PrependText()
{
string first = "first";
string second = "second";
_docWriter.Write(first);
_docWriter.Prepend(second);
Assert.AreEqual(second + first, _docWriter.ToString());
}
[Test]
public void FlushingThrowsWithoutTarget()
{
_docWriter.Write("asdf");
Assert.Throws<InvalidOperationException>(_docWriter.Flush);
}
[Test]
public void FlushBuffer()
{
DocumentWriter parent = new DocumentWriter();
_docWriter.Parent = parent;
string s = "hello world";
_docWriter.Write(s);
_docWriter.Flush();
Assert.AreEqual(s, parent.ToString());
}
[Test]
public void BufferIsEmptyAfterFlush()
{
DocumentWriter parent = new DocumentWriter();
_docWriter.Parent = parent;
string s = "hello world";
_docWriter.Write(s);
_docWriter.Flush();
Assert.IsFalse(_docWriter.HasBufferedText);
Assert.AreEqual(String.Empty, _docWriter.ToString());
}
}
}

View File

@ -0,0 +1,7 @@

04.07.2015 12:31:00:
Klinische Chemie: Na 144 mM, K 4,3 mM
06.07.2015 10:28:00:
Klinische Chemie: Na 138 mM, K 4,6 mM

View File

@ -0,0 +1,7 @@

04.07.2015:
Klinische Chemie: Na 144 mM, K 4,3 mM
06.07.2015:
Klinische Chemie: Na 138 mM, K 4,6 mM

View File

@ -0,0 +1,4 @@

04.07.2015:
Klinische Chemie: Na 144 mM, K 4,3 mM

View File

@ -0,0 +1,4 @@

06.07.2015:
Klinische Chemie: Na 138 mM, K 4,6 mM

View File

@ -25,23 +25,95 @@ using zaaReloaded2.LabModel;
using zaaReloaded2.Importer.ZaaImporter; using zaaReloaded2.Importer.ZaaImporter;
using zaaReloaded2.Controller.Elements; using zaaReloaded2.Controller.Elements;
using Microsoft.Office.Interop.Word; using Microsoft.Office.Interop.Word;
using System.IO;
namespace Tests.Formatter namespace Tests.Formatter
{ {
[TestFixture] [TestFixture]
class FormatterTest class FormatterTest
{ {
Document _document;
f.Formatter _formatter;
[SetUp]
public void SetUp()
{
_document = new Document();
ZaaImporter importer = TestHelpers.ZaaImporterFromResource();
_formatter = new f.Formatter(_document);
_formatter.Laboratory = importer.Laboratory;
}
[TearDown]
public void TearDown()
{
((_Document)_document).Close(WdSaveOptions.wdDoNotSaveChanges);
}
[Test] [Test]
public void FormatLaboratory() public void FormatLaboratory()
{ {
Document document = new Document(); _formatter.Settings.Elements.Add(new Items("Klinische Chemie: Na, K, Cl"));
ZaaImporter importer = TestHelpers.ZaaImporterFromResource(); _formatter.Run();
f.Formatter formatter = new f.Formatter(document); Assert.AreEqual(
formatter.Laboratory = importer.Laboratory; GetResourceText("Tests.Formatter.FormatterTest-all.txt"),
formatter.Settings.Elements.Add(new Items("Klinische Chemie: Na, K, Cl")); _document.Range().Text);
formatter.Run(); }
Assert.AreEqual("Klinische Chemie: Na 144 mM, K 4,3 mM\r", document.Range().Text);
((_Document)document).Close(WdSaveOptions.wdDoNotSaveChanges); [Test]
public void SelectEachDay()
{
_formatter.Settings.Elements.Add(
new SelectEachDay(
new Items("Klinische Chemie: Na, K, Cl"))
);
_formatter.Run();
Assert.AreEqual(
GetResourceText("Tests.Formatter.FormatterTest-eachday.txt"),
_document.Range().Text);
}
[Test]
public void SelectFirstDay()
{
_formatter.Settings.Elements.Add(
new SelectFirstDay(
new Items("Klinische Chemie: Na, K, Cl"))
);
_formatter.Run();
Assert.AreEqual(
GetResourceText("Tests.Formatter.FormatterTest-firstday.txt"),
_document.Range().Text);
}
[Test]
public void SelectLastDay()
{
_formatter.Settings.Elements.Add(
new SelectLastDay(
new Items("Klinische Chemie: Na, K, Cl"))
);
_formatter.Run();
Assert.AreEqual(
GetResourceText("Tests.Formatter.FormatterTest-lastday.txt"),
_document.Range().Text);
}
string GetResourceText(string resource)
{
try
{
Stream s = this.GetType().Assembly
.GetManifestResourceStream(resource);
StreamReader sr = new StreamReader(s);
// Need to replace \r\n with \r because this is the newline character
// that Word uses.
return sr.ReadToEnd().Replace("\r\n", "\r");
}
catch (Exception e)
{
throw new IOException("Unable to read " + resource, e);
}
} }
} }
} }

View File

@ -66,9 +66,8 @@
</Otherwise> </Otherwise>
</Choose> </Choose>
<ItemGroup> <ItemGroup>
<Compile Include="Controller\Elements\SelectLastDayTest.cs" />
<Compile Include="Controller\Elements\SelectFirstDayTest.cs" />
<Compile Include="Controller\SettingsRepositoryTest.cs" /> <Compile Include="Controller\SettingsRepositoryTest.cs" />
<Compile Include="Formatter\DocumentWriterTest.cs" />
<Compile Include="Thesaurus\ThesaurusTest.cs" /> <Compile Include="Thesaurus\ThesaurusTest.cs" />
<Compile Include="Controller\Elements\ItemsTest.cs" /> <Compile Include="Controller\Elements\ItemsTest.cs" />
<Compile Include="Formatter\FormatterTest.cs" /> <Compile Include="Formatter\FormatterTest.cs" />
@ -98,7 +97,18 @@
<ItemGroup> <ItemGroup>
<EmbeddedResource Include="demo-output.txt" /> <EmbeddedResource Include="demo-output.txt" />
</ItemGroup> </ItemGroup>
<ItemGroup /> <ItemGroup>
<EmbeddedResource Include="Formatter\FormatterTest-eachday.txt" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="Formatter\FormatterTest-firstday.txt" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="Formatter\FormatterTest-lastday.txt" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="Formatter\FormatterTest-all.txt" />
</ItemGroup>
<Choose> <Choose>
<When Condition="'$(VisualStudioVersion)' == '10.0' And '$(IsCodedUITest)' == 'True'"> <When Condition="'$(VisualStudioVersion)' == '10.0' And '$(IsCodedUITest)' == 'True'">
<ItemGroup> <ItemGroup>

View File

@ -0,0 +1,45 @@
/* ControlElementBase.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.Collections.Generic;
namespace zaaReloaded2.Controller.Elements
{
/// <summary>
/// Base class for control elements that control e.g. the working
/// set of time points in a Formatter object.
/// </summary>
public abstract class ControlElementBase : ElementBase
{
/// <summary>
/// Gets a list of child elements, all of which must be derived
/// from FormatElementBase.
/// </summary>
public IList<FormatElementBase> FormatElements { get; private set; }
public ControlElementBase() { }
public ControlElementBase(IList<FormatElementBase> formatElements)
{
FormatElements = formatElements;
}
public ControlElementBase(FormatElementBase formatElement)
: this(new List<FormatElementBase>() { formatElement })
{ }
}
}

View File

@ -34,7 +34,7 @@ namespace zaaReloaded2.Controller.Elements
public override void Run(Formatter.Formatter formatter) public override void Run(Formatter.Formatter formatter)
{ {
formatter.WriteToDocument(Text); formatter.Write(Text);
} }
/// <summary> /// <summary>

View File

@ -0,0 +1,27 @@
/* FormatElementBase.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.
*/
namespace zaaReloaded2.Controller.Elements
{
/// <summary>
/// Base class for elements that perform actual formatting.
/// </summary>
public abstract class FormatElementBase : ElementBase
{
}
}

View File

@ -31,7 +31,7 @@ namespace zaaReloaded2.Controller.Elements
/// to a Word document. /// to a Word document.
/// </summary> /// </summary>
[Serializable] [Serializable]
class Items : ElementBase class Items : FormatElementBase
{ {
#region ElementBase implementation #region ElementBase implementation
@ -55,15 +55,13 @@ namespace zaaReloaded2.Controller.Elements
{ {
if (!String.IsNullOrEmpty(_caption)) if (!String.IsNullOrEmpty(_caption))
{ {
formatter.Document.Range().InsertAfter( formatter.Write(String.Format("{0}: ", _caption));
String.Format("{0}: ", _caption)
);
}; };
foreach (ItemFormatter i in items) foreach (ItemFormatter i in items)
{ {
if (_needComma) if (_needComma)
{ {
formatter.Document.Range().InsertAfter(", "); formatter.Write(", ");
} }
else else
{ {
@ -71,6 +69,7 @@ namespace zaaReloaded2.Controller.Elements
} }
i.WriteToDocument(formatter); i.WriteToDocument(formatter);
} }
formatter.Write("\r");
} }
} }

View File

@ -0,0 +1,43 @@
/* SelectEachDay.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.Elements
{
class SelectEachDay : ControlElementBase
{
public override string Label
{
get { return "Jeden Tag auswählen"; }
}
public override void Run(Formatter.Formatter formatter)
{
formatter.ProcessEachDay(this);
}
public SelectEachDay() : base() { }
public SelectEachDay(FormatElementBase formatElement)
: base(formatElement)
{ }
}
}

View File

@ -26,7 +26,7 @@ namespace zaaReloaded2.Controller.Elements
/// Selects the time points of the first day in a given Formatter /// Selects the time points of the first day in a given Formatter
/// object. /// object.
/// </summary> /// </summary>
class SelectFirstDay : ElementBase class SelectFirstDay : ControlElementBase
{ {
public override string Label public override string Label
{ {
@ -35,7 +35,13 @@ namespace zaaReloaded2.Controller.Elements
public override void Run(Formatter.Formatter formatter) public override void Run(Formatter.Formatter formatter)
{ {
formatter.SelectFirstDay(); formatter.ProcessFirstDay(this);
} }
public SelectFirstDay() : base() { }
public SelectFirstDay(FormatElementBase formatElement)
: base(formatElement)
{ }
} }
} }

View File

@ -27,7 +27,7 @@ namespace zaaReloaded2.Controller.Elements
/// Selects the time points of the last day in a given Formatter /// Selects the time points of the last day in a given Formatter
/// object. /// object.
/// </summary> /// </summary>
class SelectLastDay : ElementBase class SelectLastDay : ControlElementBase
{ {
public override string Label public override string Label
{ {
@ -36,7 +36,13 @@ namespace zaaReloaded2.Controller.Elements
public override void Run(Formatter.Formatter formatter) public override void Run(Formatter.Formatter formatter)
{ {
formatter.SelectLastDay(); formatter.ProcessLastDay(this);
} }
public SelectLastDay() : base() { }
public SelectLastDay(FormatElementBase formatElement)
: base(formatElement)
{ }
} }
} }

View File

@ -0,0 +1,162 @@
/* DocumentWriter.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 Microsoft.Office.Interop.Word;
namespace zaaReloaded2.Formatter
{
/// <summary>
/// Helper class that serves to write text to a Word document or
/// to a linked DocumentWriter. Provides a buffer that can be
/// appended or prepended to, which facilitates conditional output
/// depending on whether there is text in the buffer or not.
/// </summary>
/// <remarks>
/// Linking several DocumentWriters permits a cascading work flow
/// with several buffers.
/// </remarks>
class DocumentWriter
{
#region Properties
/// <summary>
/// Gets the Document associated with this DocumentWriter,
/// or null if there is no associated Document.
/// </summary>
public Document Document { get; set; }
/// <summary>
/// Gets the parent DocumentWriter (if any).
/// </summary>
public DocumentWriter Parent { get; set; }
/// <summary>
/// Returns true if there is text in the buffer.
/// </summary>
public bool HasBufferedText { get { return _buffer.Length > 0; } }
#endregion
#region Constructors
public DocumentWriter()
{
_buffer = new StringBuilder();
}
/// <summary>
/// Creates a new DocumentWriter instance that is associated
/// with a Word Document.
/// </summary>
/// <param name="document"></param>
public DocumentWriter(Document document)
: this()
{
Document = document;
}
/// <summary>
/// Creates a new DocumentWriter instance that is associated
/// with a parent DocumentWriter.
/// </summary>
/// <param name="parent"></param>
public DocumentWriter(DocumentWriter parent)
: this()
{
Parent = parent;
}
#endregion
#region Overrides
public override string ToString()
{
return _buffer.ToString();
}
#endregion
#region Public methods
/// <summary>
/// Flushes the buffer to the associated Word document and/or the
/// parent DocumentWriter.
/// </summary>
public void Flush()
{
if (!HasBufferedText) return;
if (Document == null && Parent == null)
{
throw new InvalidOperationException(
"No document and no parent buffer to flush into.");
}
if (Document != null)
{
Document.Range().Text = _buffer.ToString();
}
if (Parent != null)
{
Parent.Write(_buffer.ToString());
}
_buffer.Clear();
}
/// <summary>
/// Writes text to the buffer.
/// </summary>
/// <param name="text">Text to write to the buffer.</param>
public void Write(string text)
{
_buffer.Append(text);
}
/// <summary>
/// Appends a line of text to the buffer.
/// </summary>
/// <param name="text">Text to append.</param>
public void WriteLine(string text)
{
_buffer.AppendLine(text);
}
/// <summary>
/// Inserts text at the start of the buffer.
/// </summary>
/// <param name="text">Text to insert at the start of the
/// buffer.</param>
public void Prepend(string text)
{
_buffer.Insert(0, text);
}
#endregion
#region Fields
StringBuilder _buffer;
#endregion
}
}

View File

@ -36,11 +36,6 @@ namespace zaaReloaded2.Formatter
public Settings Settings { get; set; } public Settings Settings { get; set; }
/// <summary>
/// Gets the working Word document.
/// </summary>
public Document Document { get; private set; }
/// <summary> /// <summary>
/// Gets or sets the <see cref="Laboratory"/> that shall be /// Gets or sets the <see cref="Laboratory"/> that shall be
/// formatted. /// formatted.
@ -76,12 +71,15 @@ namespace zaaReloaded2.Formatter
public Formatter() public Formatter()
{ {
Settings = new Settings(); Settings = new Settings();
_secondaryBuffer = new DocumentWriter();
_primaryBuffer = new DocumentWriter(_secondaryBuffer);
} }
public Formatter(Document document) public Formatter(Document document)
: this() : this()
{ {
Document = document; Document = document;
_secondaryBuffer.Document = document;
} }
#endregion #endregion
@ -94,12 +92,18 @@ namespace zaaReloaded2.Formatter
/// </summary> /// </summary>
/// <param name="text">Text to write to the current document. /// <param name="text">Text to write to the current document.
/// </param> /// </param>
public void WriteToDocument(string text) public void Write(string text)
{ {
if (Document != null) _primaryBuffer.Write(text);
{
Document.Range().InsertAfter(text);
} }
/// <summary>
/// Writes a paragraph to the document.
/// </summary>
/// <param name="text"></param>
public void WriteParagraph(string text)
{
_primaryBuffer.WriteLine(text);
} }
/// <summary> /// <summary>
@ -109,35 +113,170 @@ namespace zaaReloaded2.Formatter
/// current position of the cursor).</param> /// current position of the cursor).</param>
public void Run() public void Run()
{ {
foreach (ElementBase element in Settings.Elements) int current = 0;
while (current < Settings.Elements.Count)
{
// If there are FormatElements in the first level of the
// elements list, collect all consecutive ones and process
// them for each individual time point.
if (Settings.Elements[current] is FormatElementBase)
{
int notAFormatElement = CollectFormatElements(current);
IList<FormatElementBase> list = Settings.Elements
.Skip(current)
.Take(notAFormatElement - current)
.Cast<FormatElementBase>().ToList();
ProcessAllTimePoints(list);
current = notAFormatElement;
}
else
{
// The current element is not derived from FormatElementBase;
// so go ahead and 'run' it.
Settings.Elements[current].Run(this);
current++;
}
}
_secondaryBuffer.Flush();
}
/// <summary>
/// Selects one time point per day in the laboratory.
/// </summary>
public void ProcessEachDay(ControlElementBase controlElement)
{
IEnumerable<DateTime> days = _timePointFormatters.Keys.Select(k => k.Date).Distinct();
foreach (DateTime day in days)
{
ProcessDay(
controlElement,
_timePointFormatters
.Where(kv => kv.Key.Date == day.Date)
.ToDictionary(kv => kv.Key, kv => kv.Value)
);
}
}
/// <summary>
/// Selects all time points for the first day in the
/// laboratory.
/// </summary>
public void ProcessFirstDay(ControlElementBase controlElement)
{
DateTime first = _timePointFormatters.First().Key;
ProcessDay(
controlElement,
_timePointFormatters
.Where(kv => kv.Key.Date == first.Date)
.ToDictionary(kv => kv.Key, kv => kv.Value)
);
}
/// <summary>
/// Selects all time points for the first day in the
/// laboratory.
/// </summary>
public void ProcessLastDay(ControlElementBase controlElement)
{
DateTime last = _timePointFormatters.Last().Key;
ProcessDay(
controlElement,
_timePointFormatters
.Where(kv => kv.Key.Date == last.Date)
.ToDictionary(kv => kv.Key, kv => kv.Value)
);
}
/// <summary>
/// Processes the FormatElementBase children of <paramref name="controlElement"/>
/// for each individual time point.
/// </summary>
/// <param name="controlElement">ControlElementBase descendant whose
/// FormatElementBase children to process.</param>
public void ProcessAllTimePoints(ControlElementBase controlElement)
{
ProcessAllTimePoints(controlElement.FormatElements);
}
#endregion
#region Protected methods
/// <summary>
/// Collects all consecutive FormatElements from Settings.Elements.
/// </summary>
/// <returns>Index of the first element that is not a FormatElement.
/// </returns>
protected int CollectFormatElements(int startIndex)
{
int i = startIndex;
while (i < Settings.Elements.Count)
{
if (!(Settings.Elements[i] is FormatElementBase))
{
break;
}
i++;
}
return i;
}
protected void ProcessElements(IList<FormatElementBase> formatElements)
{
if (formatElements != null)
{
foreach (ElementBase element in formatElements)
{ {
element.Run(this); element.Run(this);
} }
} }
/// <summary>
/// Selects all time points for the first day in the
/// laboratory.
/// </summary>
public void SelectFirstDay()
{
DateTime first = _timePointFormatters.First().Key;
WorkingTimePoints = _timePointFormatters
.Where(kv => kv.Key.Date == first.Date)
.ToDictionary(kv => kv.Key, kv => kv.Value);
} }
/// <summary> protected void ProcessAllTimePoints(IList<FormatElementBase> formatElements)
/// Selects all time points for the first day in the {
/// laboratory. for (int i = 0; i < _timePointFormatters.Count; i++)
/// </summary>
public void SelectLastDay()
{ {
DateTime last = _timePointFormatters.Last().Key;
WorkingTimePoints = _timePointFormatters WorkingTimePoints = _timePointFormatters
.Where(kv => kv.Key.Date == last.Date) .Skip(i)
.Take(1)
.ToDictionary(kv => kv.Key, kv => kv.Value); .ToDictionary(kv => kv.Key, kv => kv.Value);
ProcessElements(formatElements);
if (_primaryBuffer.HasBufferedText)
{
_primaryBuffer.Prepend(
WorkingTimePoints.First().Value.GetDateAndTimeHeader()
);
} }
_primaryBuffer.Flush();
}
}
protected void ProcessDay(
ControlElementBase controlElement,
Dictionary<DateTime, TimePointFormatter> workingTimePoints)
{
if (workingTimePoints == null)
throw new ArgumentNullException("workingTimePoints");
WorkingTimePoints = workingTimePoints;
ProcessElements(controlElement.FormatElements);
if (_primaryBuffer.HasBufferedText)
{
_primaryBuffer.Prepend(
WorkingTimePoints.First().Value.GetDateHeader());
}
_primaryBuffer.Flush();
}
#endregion
#region Protected properties
/// <summary>
/// Gets the working Word document.
/// </summary>
protected Document Document { get; set; }
#endregion #endregion
@ -145,6 +284,8 @@ namespace zaaReloaded2.Formatter
TimePointFormatterDictionary _timePointFormatters; TimePointFormatterDictionary _timePointFormatters;
Laboratory _laboratory; Laboratory _laboratory;
DocumentWriter _primaryBuffer;
DocumentWriter _secondaryBuffer;
#endregion #endregion
} }

View File

@ -134,7 +134,7 @@ namespace zaaReloaded2.Formatter
} }
// Insert the formatted text into the document. // Insert the formatted text into the document.
formatter.WriteToDocument( formatter.Write(
String.Format( String.Format(
"{0} {1}{2}{3}", "{0} {1}{2}{3}",
LabItem.QualifiedName, LabItem.QualifiedName,

View File

@ -71,6 +71,39 @@ namespace zaaReloaded2.Formatter
return ItemFormatters.ContainsKey(itemName); return ItemFormatters.ContainsKey(itemName);
} }
/// <summary>
/// Creates a header text line with the time point's date.
/// </summary>
/// <returns></returns>
public string GetDateHeader()
{
return FormatHeader(TimeStamp.ToShortDateString());
}
/// <summary>
/// Creates a header text line with the time point's date
/// and time.
/// </summary>
/// <returns></returns>
public string GetDateAndTimeHeader()
{
return FormatHeader(TimeStamp.ToString());
}
#endregion
#region Private methods
string FormatHeader(string text)
{
return String.Format("{0}{1}:{2}",
Environment.NewLine,
text,
Environment.NewLine
);
}
#endregion #endregion
} }
} }

View File

@ -163,11 +163,15 @@
can be found. can be found.
--> -->
<ItemGroup> <ItemGroup>
<Compile Include="Controller\Elements\ControlElementBase.cs" />
<Compile Include="Controller\Elements\FormatElementBase.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" />
<Compile Include="Controller\Elements\CustomText.cs" /> <Compile Include="Controller\Elements\CustomText.cs" />
<Compile Include="Controller\Settings.cs" /> <Compile Include="Controller\Settings.cs" />
<Compile Include="Controller\SettingsRepository.cs" /> <Compile Include="Controller\SettingsRepository.cs" />
<Compile Include="Formatter\DocumentWriter.cs" />
<Compile Include="Thesaurus\ThesaurusBase.cs" /> <Compile Include="Thesaurus\ThesaurusBase.cs" />
<Compile Include="Formatter\IItemFormatterDictionary.cs" /> <Compile Include="Formatter\IItemFormatterDictionary.cs" />
<Compile Include="Formatter\ItemFormatter.cs" /> <Compile Include="Formatter\ItemFormatter.cs" />