Implement items wildcards.
This commit is contained in:
		@@ -18,7 +18,6 @@
 | 
			
		||||
using System;
 | 
			
		||||
using System.Collections.Generic;
 | 
			
		||||
using System.Linq;
 | 
			
		||||
using System.Text;
 | 
			
		||||
using System.Text.RegularExpressions;
 | 
			
		||||
using System.Diagnostics;
 | 
			
		||||
using Microsoft.Office.Interop.Word;
 | 
			
		||||
@@ -43,26 +42,13 @@ namespace zaaReloaded2.Controller.Elements
 | 
			
		||||
 | 
			
		||||
        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 = new List<ItemFormatter>();
 | 
			
		||||
            if (_items != null && _items.Count > 0)
 | 
			
		||||
            {
 | 
			
		||||
                foreach (string itemName in _items)
 | 
			
		||||
                {
 | 
			
		||||
                    TimePointFormatter tpf = formatter.WorkingTimePoints
 | 
			
		||||
                        .FirstOrDefault(tp => tp.Value.ContainsItem(itemName))
 | 
			
		||||
                        .Value;
 | 
			
		||||
                    if (tpf != null)
 | 
			
		||||
                    {
 | 
			
		||||
                        // If tpf is not null, this means that it contains an
 | 
			
		||||
                        // item with itemName.
 | 
			
		||||
                        items.Add(tpf.ItemFormatters[itemName]);
 | 
			
		||||
                    }
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
            List<ItemFormatter> items = CollectItems(formatter);
 | 
			
		||||
 | 
			
		||||
            // If there are items, write the caption (if any), then the items
 | 
			
		||||
            if (items.Count > 0)
 | 
			
		||||
@@ -83,7 +69,7 @@ namespace zaaReloaded2.Controller.Elements
 | 
			
		||||
                    {
 | 
			
		||||
                        _needComma = true;
 | 
			
		||||
                    }
 | 
			
		||||
                    i.WriteToDocument(formatter.Document);
 | 
			
		||||
                    i.WriteToDocument(formatter);
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
@@ -135,7 +121,7 @@ namespace zaaReloaded2.Controller.Elements
 | 
			
		||||
        {
 | 
			
		||||
            _items = null;
 | 
			
		||||
            _caption = null;
 | 
			
		||||
            Regex r = new Regex(@"((?<caption>[^:]+):\s*)?((?<items>[^,]+),\s*)+");
 | 
			
		||||
            Regex r = new Regex(@"((?<caption>[^:]+):\s*)?((?<items>[^,]+),\s*)*(?<items>[^,]+)");
 | 
			
		||||
            Match m = r.Match(Line);
 | 
			
		||||
            if (m.Success)
 | 
			
		||||
            {
 | 
			
		||||
@@ -149,6 +135,71 @@ namespace zaaReloaded2.Controller.Elements
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        /// <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
 | 
			
		||||
@@ -156,6 +207,7 @@ namespace zaaReloaded2.Controller.Elements
 | 
			
		||||
        string _line;
 | 
			
		||||
        string _caption;
 | 
			
		||||
        List<string> _items;
 | 
			
		||||
        static Regex _wildcard = new Regex(@"(?<material>[^-]+-)?\*");
 | 
			
		||||
 | 
			
		||||
        #endregion
 | 
			
		||||
    }
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user