/* ParameterComment.cs * part of zaaReloaded2 * * Copyright 2015-2017 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 System.Text.RegularExpressions; namespace zaaReloaded2.Controller.Comments { /// /// Represents an optional comment for a laboratory item. /// The zaaReloaded2.Controller.Elements.Items class can /// parse these optional comment strings which may be used /// to prompt users to enter target ranges etc. /// /// /// TAC "(Zielspiegel: <8-10> µg/l)" /// /// /// In the example, the tacrolimus trough level TAC carries a comment about the /// recommended range. The comment must be enclosed in quotes in order for it /// to be recognized. The quotes will be stripped. The angle brackets denote /// a place holder that will be replaced by the comment that is set in the /// RequestParameterComment's event args. The text between the angle brackets /// ("8-10") is an optional default value. One could also just use 'empty' /// brackets ("<>"). /// public class ItemComment { #region Factory /// /// Creates a new ItemComment object from a definition string. /// /// String that complies /// with the definition pattern (item "optional prefix <optional /// default value> optioal suffix"). public static ItemComment FromDefinitionString(string definitionString) { Match match = _definition.Match(definitionString); if (match.Success) { return new ItemComment( definitionString, match.Groups["item"].Value.Trim(), match.Groups["prefix"].Value, match.Groups["value"].Value, match.Groups["suffix"].Value); } else { return null; } } #endregion #region Properties /// /// Gets or sets the item name that this comment is for. /// public string Item { get; set; } /// /// Prefix of this comment, e.g. "(target trough level: " /// public string Prefix { get; set; } /// /// Value of this comment; String.Empty represents a /// cancelled comment. /// public string Value { get; set; } /// /// Suffix of this comment, e.g. " µg/l)" /// public string Suffix { get; set; } /// /// Gets the original definition string that this ItemComment /// was created from. /// public string Definition { get; private set; } /// /// Gets or sets whether this comment has been cancelled.mo /// public bool IsCancelled { get; set; } #endregion #region Event /// /// Event that is raised when the comment value needs to be /// filled in by someone or something. /// public event EventHandler FillInComment; #endregion #region Constructors public ItemComment() { Item = String.Empty; Prefix = String.Empty; Value = String.Empty; Suffix = String.Empty; } public ItemComment(string item, string prefix, string value, string suffix) : this() { Item = item; Prefix = prefix; Value = value; Suffix = suffix; } public ItemComment(string definition, string item, string prefix, string value, string suffix) : this(item, prefix, value, suffix) { Definition = definition; } #endregion #region Methods /// /// Builds the comment string from the prefix, the value, and /// the suffix. Raises the FillInComment event to get the value /// first. If the value is an empty string, the entire comment /// string will be an empty string. /// /// Comment string with filled-in value, or String.Empty /// if the value is an empty string. public string BuildComment() { OnFillInComment(); string value = Value; if (String.IsNullOrEmpty(value)) { // If the comment was not cancelled and an empty // value was deliberately entered, return an // empty string. if (!IsCancelled) { return String.Empty; } // If the comment was cancelled and the default // value is an empty string, insert a prompt // to manually enter the comment. else { value = Properties.Settings.Default.ManualCommentPrompt; } } if (!IsCancelled) { return Prefix + value + Suffix; } else { return Prefix + "" + value + "" + Suffix; } } #endregion #region Private methods /// /// Raises the FillInComment event. /// protected virtual void OnFillInComment() { if (_isFilledIn) return; EventHandler h = FillInComment; if (h != null) { _isFilledIn = true; h(this, new ItemCommentEventArgs(this)); } } #endregion #region Fields bool _isFilledIn; static readonly Regex _definition = new Regex(@"(?[^""]+)""(?[^<]+)?<(?[^>]*)>(?[^""]+)?"""); #endregion } }