zaaReloaded2/zaaReloaded2/Controller/Elements/Items.cs

214 lines
6.6 KiB
C#
Executable File

/* Items.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.RegularExpressions;
using System.Diagnostics;
using Microsoft.Office.Interop.Word;
using zaaReloaded2.LabModel;
using zaaReloaded2.Formatter;
namespace zaaReloaded2.Controller.Elements
{
/// <summary>
/// This formatting element is concerned with writing <see cref="LabItem"/>s
/// to a Word document.
/// </summary>
[Serializable]
class Items : FormatElementBase
{
#region ElementBase implementation
public override string Label
{
get { return Line; }
}
public override void Run(zaaReloaded2.Formatter.Formatter formatter)
{
if (_items == null || _items.Count == 0) return;
bool _needComma = false;
// Find out if we have any items that we can write
// to the document
List<ItemFormatter> items = CollectItems(formatter);
// If there are items, write the caption (if any), then the items
if (items.Count > 0)
{
if (!String.IsNullOrEmpty(_caption))
{
formatter.Write(String.Format("{0}: ", _caption));
};
foreach (ItemFormatter i in items)
{
if (_needComma)
{
formatter.Write(", ");
}
else
{
_needComma = true;
}
i.WriteToDocument(formatter);
}
formatter.Write("\r");
}
}
#endregion
#region Properties
/// <summary>
/// Gets or sets a text line that contains a comma-separated list of
/// parsable laboratory item names. The list may optionally be preceded
/// with a caption followed by a colon.
/// </summary>
public string Line
{
[DebuggerStepThrough]
get
{
return _line;
}
set
{
_line = value;
ParseLine();
}
}
#endregion
#region Constructors
public Items() : base() { }
public Items(string line)
: this()
{
Line = line;
}
#endregion
#region Private methods
/// <summary>
/// Parses the Line and splits it into an optional caption and individual
/// items.
/// </summary>
void ParseLine()
{
_items = null;
_caption = null;
Regex r = new Regex(@"((?<caption>[^:]+):\s*)?((?<items>[^,]+),\s*)*(?<items>[^,]+)");
Match m = r.Match(Line);
if (m.Success)
{
if (m.Groups["caption"].Success)
{
_caption = m.Groups["caption"].Value;
}
_items = m.Groups["items"].Captures
.Cast<Capture>()
.Select<Capture, string>(c => c.Value).ToList<string>();
}
}
/// <summary>
/// Searches the working time points of the Formatter object
/// for items defined in the Line.
/// </summary>
List<ItemFormatter> CollectItems(zaaReloaded2.Formatter.Formatter formatter)
{
List<ItemFormatter> items = new List<ItemFormatter>();
foreach (string itemName in _items)
{
Match match = _wildcard.Match(itemName);
if (match.Success)
{
// If there is no capture group "material", the Value
// will be String.Empty.
items.AddRange(CollectByWildcard(formatter, match.Groups["material"].Value));
}
else
{
items.AddRange(CollectByName(formatter, itemName));
}
}
return items;
}
/// <summary>
/// Collects items for output by matching wildcard.
/// </summary>
/// <param name="material">Material (e.g., empty string for blood,
/// or "U-" for spot urine, "SU-" for collected urin, and so on).</param>
List<ItemFormatter> CollectByWildcard(zaaReloaded2.Formatter.Formatter formatter, string material)
{
List<ItemFormatter> items = new List<ItemFormatter>();
foreach (TimePointFormatter tpf in formatter.WorkingTimePoints.Values)
{
List<ItemFormatter> newItems = tpf.ItemFormatters.Values
.Where(i => !i.HasBeenUsed && i.LabItem.QualifiedName.StartsWith(material))
.ToList();
newItems.ForEach(i => i.HasBeenUsed = true);
items.AddRange(newItems);
}
return items;
}
/// <summary>
/// Collects items for output by name.
/// </summary>
/// <param name="name">Item name to look for.</param>
List<ItemFormatter> CollectByName(zaaReloaded2.Formatter.Formatter formatter, string name)
{
List<ItemFormatter> items = new List<ItemFormatter>();
TimePointFormatter tpf = formatter.WorkingTimePoints
.FirstOrDefault(tp => tp.Value.ContainsItem(name))
.Value;
if (tpf != null)
{
// If tpf is not null, this means that it contains an
// item with itemName.
ItemFormatter i = tpf.ItemFormatters[name];
i.HasBeenUsed = true;
items.Add(i);
}
return items;
}
#endregion
#region Fields
string _line;
string _caption;
List<string> _items;
static Regex _wildcard = new Regex(@"(?<material>[^-]+-)?\*");
#endregion
}
}