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

@ -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)
{
formatter.WriteToDocument(Text);
formatter.Write(Text);
}
/// <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.
/// </summary>
[Serializable]
class Items : ElementBase
class Items : FormatElementBase
{
#region ElementBase implementation
@ -55,15 +55,13 @@ namespace zaaReloaded2.Controller.Elements
{
if (!String.IsNullOrEmpty(_caption))
{
formatter.Document.Range().InsertAfter(
String.Format("{0}: ", _caption)
);
formatter.Write(String.Format("{0}: ", _caption));
};
foreach (ItemFormatter i in items)
{
if (_needComma)
{
formatter.Document.Range().InsertAfter(", ");
formatter.Write(", ");
}
else
{
@ -71,6 +69,7 @@ namespace zaaReloaded2.Controller.Elements
}
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
/// object.
/// </summary>
class SelectFirstDay : ElementBase
class SelectFirstDay : ControlElementBase
{
public override string Label
{
@ -35,7 +35,13 @@ namespace zaaReloaded2.Controller.Elements
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
/// object.
/// </summary>
class SelectLastDay : ElementBase
class SelectLastDay : ControlElementBase
{
public override string Label
{
@ -36,7 +36,13 @@ namespace zaaReloaded2.Controller.Elements
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; }
/// <summary>
/// Gets the working Word document.
/// </summary>
public Document Document { get; private set; }
/// <summary>
/// Gets or sets the <see cref="Laboratory"/> that shall be
/// formatted.
@ -76,12 +71,15 @@ namespace zaaReloaded2.Formatter
public Formatter()
{
Settings = new Settings();
_secondaryBuffer = new DocumentWriter();
_primaryBuffer = new DocumentWriter(_secondaryBuffer);
}
public Formatter(Document document)
: this()
{
Document = document;
_secondaryBuffer.Document = document;
}
#endregion
@ -94,12 +92,18 @@ namespace zaaReloaded2.Formatter
/// </summary>
/// <param name="text">Text to write to the current document.
/// </param>
public void WriteToDocument(string text)
public void Write(string text)
{
if (Document != null)
{
Document.Range().InsertAfter(text);
}
_primaryBuffer.Write(text);
}
/// <summary>
/// Writes a paragraph to the document.
/// </summary>
/// <param name="text"></param>
public void WriteParagraph(string text)
{
_primaryBuffer.WriteLine(text);
}
/// <summary>
@ -109,9 +113,47 @@ namespace zaaReloaded2.Formatter
/// current position of the cursor).</param>
public void Run()
{
foreach (ElementBase element in Settings.Elements)
int current = 0;
while (current < Settings.Elements.Count)
{
element.Run(this);
// 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)
);
}
}
@ -119,32 +161,131 @@ namespace zaaReloaded2.Formatter
/// Selects all time points for the first day in the
/// laboratory.
/// </summary>
public void SelectFirstDay()
public void ProcessFirstDay(ControlElementBase controlElement)
{
DateTime first = _timePointFormatters.First().Key;
WorkingTimePoints = _timePointFormatters
.Where(kv => kv.Key.Date == first.Date)
.ToDictionary(kv => kv.Key, kv => kv.Value);
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 SelectLastDay()
public void ProcessLastDay(ControlElementBase controlElement)
{
DateTime last = _timePointFormatters.Last().Key;
WorkingTimePoints = _timePointFormatters
.Where(kv => kv.Key.Date == last.Date)
.ToDictionary(kv => kv.Key, kv => kv.Value);
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);
}
}
}
protected void ProcessAllTimePoints(IList<FormatElementBase> formatElements)
{
for (int i = 0; i < _timePointFormatters.Count; i++)
{
WorkingTimePoints = _timePointFormatters
.Skip(i)
.Take(1)
.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
#region Fields
TimePointFormatterDictionary _timePointFormatters;
Laboratory _laboratory;
DocumentWriter _primaryBuffer;
DocumentWriter _secondaryBuffer;
#endregion
}

View File

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

View File

@ -71,6 +71,39 @@ namespace zaaReloaded2.Formatter
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
}
}
}

View File

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